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