08b7a88cbb3d5f572cd4b12c6464536e198f2bf4
[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     assert(0 == lt_dlinit() || "Can't init the ltdl library");
52     did_initialize_ltdl = true;
53   }
54 }
55
56 static std::vector<lt_dlhandle> OpenedHandles;
57
58 DynamicLibrary::DynamicLibrary() : handle(0) {
59   check_ltdl_initialization();
60
61   lt_dlhandle a_handle = lt_dlopen(0);
62
63   assert(a_handle == 0 || "Can't open program as dynamic library");
64
65   handle = a_handle;
66   OpenedHandles.push_back(a_handle);
67 }
68
69 /*
70 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
71   check_ltdl_initialization();
72
73   lt_dlhandle a_handle = lt_dlopen(filename);
74
75   if (a_handle == 0)
76     a_handle = lt_dlopenext(filename);
77
78   if (a_handle == 0)
79     throw std::string("Can't open :") + filename + ": " + lt_dlerror();
80
81   handle = a_handle;
82   OpenedHandles.push_back(a_handle);
83 }
84 */
85
86 DynamicLibrary::~DynamicLibrary() {
87   lt_dlhandle a_handle = (lt_dlhandle) handle;
88   if (a_handle) {
89     lt_dlclose(a_handle);
90
91     for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
92          E = OpenedHandles.end(); I != E; ++I) {
93       if (*I == a_handle) {
94         // Note: don't use the swap/pop_back trick here. Order is important.
95         OpenedHandles.erase(I);
96         return;
97       }
98     }
99   }
100 }
101
102 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
103                                             std::string *ErrMsg) {
104   check_ltdl_initialization();
105   lt_dlhandle a_handle = lt_dlopen(Filename);
106
107   if (a_handle == 0)
108     a_handle = lt_dlopenext(Filename);
109
110   if (a_handle == 0) {
111     if (ErrMsg)
112       *ErrMsg = std::string("Can't open :") +
113           (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
114     return true;
115   }
116
117   lt_dlmakeresident(a_handle);
118
119   OpenedHandles.push_back(a_handle);
120   return false;
121 }
122
123 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
124   check_ltdl_initialization();
125
126   // First check symbols added via AddSymbol().
127   std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
128   if (I != g_symbols.end())
129     return I->second;
130
131   // Now search the libraries.
132   for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
133        E = OpenedHandles.end(); I != E; ++I) {
134     lt_ptr ptr = lt_dlsym(*I, symbolName);
135     if (ptr)
136       return ptr;
137   }
138
139   // If this is darwin, it has some funky issues, try to solve them here.  Some
140   // important symbols are marked 'private external' which doesn't allow
141   // SearchForAddressOfSymbol to find them.  As such, we special case them here,
142   // there is only a small handful of them.
143 #ifdef __APPLE__
144   {
145 #define EXPLICIT_SYMBOL(SYM) \
146    extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
147     EXPLICIT_SYMBOL(__ashldi3);
148     EXPLICIT_SYMBOL(__ashrdi3);
149     EXPLICIT_SYMBOL(__cmpdi2);
150     EXPLICIT_SYMBOL(__divdi3);
151     EXPLICIT_SYMBOL(__eprintf);
152     EXPLICIT_SYMBOL(__fixdfdi);
153     EXPLICIT_SYMBOL(__fixsfdi);
154     EXPLICIT_SYMBOL(__fixunsdfdi);
155     EXPLICIT_SYMBOL(__fixunssfdi);
156     EXPLICIT_SYMBOL(__floatdidf);
157     EXPLICIT_SYMBOL(__floatdisf);
158     EXPLICIT_SYMBOL(__lshrdi3);
159     EXPLICIT_SYMBOL(__moddi3);
160     EXPLICIT_SYMBOL(__udivdi3);
161     EXPLICIT_SYMBOL(__umoddi3);
162 #undef EXPLICIT_SYMBOL
163   }
164 #endif
165
166   return 0;
167 }
168
169 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
170   assert(handle != 0 && "Invalid DynamicLibrary handle");
171   return lt_dlsym((lt_dlhandle) handle, symbolName);
172 }
173
174 #endif // LLVM_ON_WIN32
175
176 DEFINING_FILE_FOR(SystemDynamicLibrary)