blob: 6efab948fa76f150b070da1ec75d4802edd43149 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header file implements the operating system DynamicLibrary concept.
11//
Chris Lattner71ac96d2009-07-07 18:17:07 +000012// FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is
13// not thread safe!
14//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015//===----------------------------------------------------------------------===//
16
17#include "llvm/System/DynamicLibrary.h"
Owen Anderson132a2f22009-06-25 18:12:44 +000018#include "llvm/Support/ManagedStatic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Config/config.h"
Duncan Sands05f68372008-10-08 07:23:46 +000020#include <cstdio>
Duncan Sandsfca20142008-01-09 19:42:09 +000021#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <map>
Chris Lattner0d4e1292009-07-07 18:01:58 +000023#include <vector>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024
25// Collection of symbol name/value pairs to be searched prior to any libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000026static std::map<std::string, void*> *ExplicitSymbols = 0;
Owen Anderson132a2f22009-06-25 18:12:44 +000027
Edwin Török6d5f43b2009-08-31 16:12:29 +000028static struct ExplicitSymbolsDeleter {
29 ~ExplicitSymbolsDeleter() {
30 if (ExplicitSymbols)
31 delete ExplicitSymbols;
32 }
33} Dummy;
34
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
36 void *symbolValue) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000037 if (ExplicitSymbols == 0)
38 ExplicitSymbols = new std::map<std::string, void*>();
39 (*ExplicitSymbols)[symbolName] = symbolValue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040}
41
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#ifdef LLVM_ON_WIN32
43
44#include "Win32/DynamicLibrary.inc"
45
46#else
47
Devang Patelaa4ea5a2008-02-13 17:11:39 +000048#include <dlfcn.h>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049using namespace llvm;
50using namespace llvm::sys;
51
52//===----------------------------------------------------------------------===//
53//=== WARNING: Implementation here must contain only TRULY operating system
54//=== independent code.
55//===----------------------------------------------------------------------===//
56
Chris Lattner71ac96d2009-07-07 18:17:07 +000057static std::vector<void *> *OpenedHandles = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
61 std::string *ErrMsg) {
Chris Lattner859cf632008-07-10 00:52:20 +000062 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL);
Devang Patelaa4ea5a2008-02-13 17:11:39 +000063 if (H == 0) {
Chris Lattner71ac96d2009-07-07 18:17:07 +000064 if (ErrMsg) *ErrMsg = dlerror();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 return true;
66 }
Chris Lattner71ac96d2009-07-07 18:17:07 +000067 if (OpenedHandles == 0)
68 OpenedHandles = new std::vector<void *>();
69 OpenedHandles->push_back(H);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 return false;
71}
72
73void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 // First check symbols added via AddSymbol().
Chris Lattner71ac96d2009-07-07 18:17:07 +000075 if (ExplicitSymbols) {
76 std::map<std::string, void *>::iterator I =
77 ExplicitSymbols->find(symbolName);
78 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
Owen Anderson132a2f22009-06-25 18:12:44 +000079
Chris Lattner71ac96d2009-07-07 18:17:07 +000080 if (I != E)
81 return I->second;
82 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
84 // Now search the libraries.
Chris Lattner71ac96d2009-07-07 18:17:07 +000085 if (OpenedHandles) {
86 for (std::vector<void *>::iterator I = OpenedHandles->begin(),
87 E = OpenedHandles->end(); I != E; ++I) {
88 //lt_ptr ptr = lt_dlsym(*I, symbolName);
89 void *ptr = dlsym(*I, symbolName);
90 if (ptr) {
91 return ptr;
92 }
Owen Anderson132a2f22009-06-25 18:12:44 +000093 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 }
95
Anton Korobeynikov942cbab2007-12-03 05:30:41 +000096#define EXPLICIT_SYMBOL(SYM) \
97 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 // If this is darwin, it has some funky issues, try to solve them here. Some
100 // important symbols are marked 'private external' which doesn't allow
101 // SearchForAddressOfSymbol to find them. As such, we special case them here,
102 // there is only a small handful of them.
103
104#ifdef __APPLE__
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 {
106 EXPLICIT_SYMBOL(__ashldi3);
107 EXPLICIT_SYMBOL(__ashrdi3);
108 EXPLICIT_SYMBOL(__cmpdi2);
109 EXPLICIT_SYMBOL(__divdi3);
110 EXPLICIT_SYMBOL(__eprintf);
111 EXPLICIT_SYMBOL(__fixdfdi);
112 EXPLICIT_SYMBOL(__fixsfdi);
113 EXPLICIT_SYMBOL(__fixunsdfdi);
114 EXPLICIT_SYMBOL(__fixunssfdi);
115 EXPLICIT_SYMBOL(__floatdidf);
116 EXPLICIT_SYMBOL(__floatdisf);
117 EXPLICIT_SYMBOL(__lshrdi3);
118 EXPLICIT_SYMBOL(__moddi3);
119 EXPLICIT_SYMBOL(__udivdi3);
120 EXPLICIT_SYMBOL(__umoddi3);
121 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122#endif
123
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000124#ifdef __CYGWIN__
125 {
126 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000127 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov942cbab2007-12-03 05:30:41 +0000128 }
129#endif
130
131#undef EXPLICIT_SYMBOL
132
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133// This macro returns the address of a well-known, explicit symbol
134#define EXPLICIT_SYMBOL(SYM) \
135 if (!strcmp(symbolName, #SYM)) return &SYM
136
137// On linux we have a weird situation. The stderr/out/in symbols are both
138// macros and global variables because of standards requirements. So, we
139// boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
140#if defined(__linux__)
141 {
142 EXPLICIT_SYMBOL(stderr);
143 EXPLICIT_SYMBOL(stdout);
144 EXPLICIT_SYMBOL(stdin);
145 }
146#else
147 // For everything else, we want to check to make sure the symbol isn't defined
148 // as a macro before using EXPLICIT_SYMBOL.
149 {
150#ifndef stdin
151 EXPLICIT_SYMBOL(stdin);
152#endif
153#ifndef stdout
154 EXPLICIT_SYMBOL(stdout);
155#endif
156#ifndef stderr
157 EXPLICIT_SYMBOL(stderr);
158#endif
159 }
160#endif
161#undef EXPLICIT_SYMBOL
162
163 return 0;
164}
165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166#endif // LLVM_ON_WIN32