really fix PR1581, thanks to Daniel Dunbar for pointing
[oota-llvm.git] / lib / System / DynamicLibrary.cpp
1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This header file implements the operating system DynamicLibrary concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Config/config.h"
16 #include <map>
17
18 // Collection of symbol name/value pairs to be searched prior to any libraries.
19 static std::map<std::string, void *> g_symbols;
20
21 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
22                                           void *symbolValue) {
23   g_symbols[symbolName] = symbolValue;
24 }
25
26 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
27 // license and special exception would cause all of LLVM to be placed under
28 // the LGPL.  This is because the exception applies only when libtool is
29 // used, and obviously libtool is not used with Visual Studio.  An entirely
30 // separate implementation is provided in win32/DynamicLibrary.cpp.
31
32 #ifdef LLVM_ON_WIN32
33
34 #include "Win32/DynamicLibrary.inc"
35
36 #else
37
38 #include "ltdl.h"
39 #include <cassert>
40 using namespace llvm;
41 using namespace llvm::sys;
42
43 //===----------------------------------------------------------------------===//
44 //=== WARNING: Implementation here must contain only TRULY operating system
45 //===          independent code.
46 //===----------------------------------------------------------------------===//
47
48 static inline void check_ltdl_initialization() {
49   static bool did_initialize_ltdl = false;
50   if (!did_initialize_ltdl) {
51     int Err = lt_dlinit();
52     Err = Err; // Silence warning.
53     assert(0 == Err && "Can't init the ltdl library");
54     did_initialize_ltdl = true;
55   }
56 }
57
58 static std::vector<lt_dlhandle> OpenedHandles;
59
60 DynamicLibrary::DynamicLibrary() : handle(0) {
61   check_ltdl_initialization();
62
63   lt_dlhandle a_handle = lt_dlopen(0);
64
65   assert(a_handle && "Can't open program as dynamic library");
66
67   handle = a_handle;
68   OpenedHandles.push_back(a_handle);
69 }
70
71 /*
72 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
73   check_ltdl_initialization();
74
75   lt_dlhandle a_handle = lt_dlopen(filename);
76
77   if (a_handle == 0)
78     a_handle = lt_dlopenext(filename);
79
80   if (a_handle == 0)
81     throw std::string("Can't open :") + filename + ": " + lt_dlerror();
82
83   handle = a_handle;
84   OpenedHandles.push_back(a_handle);
85 }
86 */
87
88 DynamicLibrary::~DynamicLibrary() {
89   lt_dlhandle a_handle = (lt_dlhandle) handle;
90   if (a_handle) {
91     lt_dlclose(a_handle);
92
93     for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
94          E = OpenedHandles.end(); I != E; ++I) {
95       if (*I == a_handle) {
96         // Note: don't use the swap/pop_back trick here. Order is important.
97         OpenedHandles.erase(I);
98         return;
99       }
100     }
101   }
102 }
103
104 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
105                                             std::string *ErrMsg) {
106   check_ltdl_initialization();
107   lt_dlhandle a_handle = lt_dlopen(Filename);
108
109   if (a_handle == 0)
110     a_handle = lt_dlopenext(Filename);
111
112   if (a_handle == 0) {
113     if (ErrMsg)
114       *ErrMsg = std::string("Can't open :") +
115           (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
116     return true;
117   }
118
119   lt_dlmakeresident(a_handle);
120
121   OpenedHandles.push_back(a_handle);
122   return false;
123 }
124
125 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
126   check_ltdl_initialization();
127
128   // First check symbols added via AddSymbol().
129   std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
130   if (I != g_symbols.end())
131     return I->second;
132
133   // Now search the libraries.
134   for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
135        E = OpenedHandles.end(); I != E; ++I) {
136     lt_ptr ptr = lt_dlsym(*I, symbolName);
137     if (ptr)
138       return ptr;
139   }
140
141   // If this is darwin, it has some funky issues, try to solve them here.  Some
142   // important symbols are marked 'private external' which doesn't allow
143   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
144   // there is only a small handful of them.
145
146 #ifdef __APPLE__
147 #define EXPLICIT_SYMBOL(SYM) \
148    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
149   {
150     EXPLICIT_SYMBOL(__ashldi3);
151     EXPLICIT_SYMBOL(__ashrdi3);
152     EXPLICIT_SYMBOL(__cmpdi2);
153     EXPLICIT_SYMBOL(__divdi3);
154     EXPLICIT_SYMBOL(__eprintf);
155     EXPLICIT_SYMBOL(__fixdfdi);
156     EXPLICIT_SYMBOL(__fixsfdi);
157     EXPLICIT_SYMBOL(__fixunsdfdi);
158     EXPLICIT_SYMBOL(__fixunssfdi);
159     EXPLICIT_SYMBOL(__floatdidf);
160     EXPLICIT_SYMBOL(__floatdisf);
161     EXPLICIT_SYMBOL(__lshrdi3);
162     EXPLICIT_SYMBOL(__moddi3);
163     EXPLICIT_SYMBOL(__udivdi3);
164     EXPLICIT_SYMBOL(__umoddi3);
165   }
166 #undef EXPLICIT_SYMBOL
167 #endif
168
169 // This macro returns the address of a well-known, explicit symbol
170 #define EXPLICIT_SYMBOL(SYM) \
171    if (!strcmp(symbolName, #SYM)) return &SYM
172
173 // On linux we have a weird situation. The stderr/out/in symbols are both
174 // macros and global variables because of standards requirements. So, we 
175 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
176 #if defined(__linux__)
177   {
178     EXPLICIT_SYMBOL(stderr);
179     EXPLICIT_SYMBOL(stdout);
180     EXPLICIT_SYMBOL(stdin);
181   }
182 #else
183   // For everything else, we want to check to make sure the symbol isn't defined
184   // as a macro before using EXPLICIT_SYMBOL.
185   {
186 #ifndef stdin
187     EXPLICIT_SYMBOL(stdin);
188 #endif
189 #ifndef stdout
190     EXPLICIT_SYMBOL(stdout);
191 #endif
192 #ifndef stderr
193     EXPLICIT_SYMBOL(stderr);
194 #endif
195   }
196 #endif
197 #undef EXPLICIT_SYMBOL
198
199   return 0;
200 }
201
202 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
203   assert(handle != 0 && "Invalid DynamicLibrary handle");
204   return lt_dlsym((lt_dlhandle) handle, symbolName);
205 }
206
207 #endif // LLVM_ON_WIN32
208
209 DEFINING_FILE_FOR(SystemDynamicLibrary)