1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system DynamicLibrary concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Config/config.h"
19 // Collection of symbol name/value pairs to be searched prior to any libraries.
20 static std::map<std::string, void *> g_symbols;
22 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
24 g_symbols[symbolName] = symbolValue;
27 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
28 // license and special exception would cause all of LLVM to be placed under
29 // the LGPL. This is because the exception applies only when libtool is
30 // used, and obviously libtool is not used with Visual Studio. An entirely
31 // separate implementation is provided in win32/DynamicLibrary.cpp.
35 #include "Win32/DynamicLibrary.inc"
42 using namespace llvm::sys;
44 //===----------------------------------------------------------------------===//
45 //=== WARNING: Implementation here must contain only TRULY operating system
46 //=== independent code.
47 //===----------------------------------------------------------------------===//
49 static inline void check_ltdl_initialization() {
50 static bool did_initialize_ltdl = false;
51 if (!did_initialize_ltdl) {
52 int Err = lt_dlinit();
53 Err = Err; // Silence warning.
54 assert(0 == Err && "Can't init the ltdl library");
55 did_initialize_ltdl = true;
59 static std::vector<lt_dlhandle> OpenedHandles;
61 DynamicLibrary::DynamicLibrary() : handle(0) {
62 check_ltdl_initialization();
64 lt_dlhandle a_handle = lt_dlopen(0);
66 assert(a_handle && "Can't open program as dynamic library");
69 OpenedHandles.push_back(a_handle);
73 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
74 check_ltdl_initialization();
76 lt_dlhandle a_handle = lt_dlopen(filename);
79 a_handle = lt_dlopenext(filename);
82 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
85 OpenedHandles.push_back(a_handle);
89 DynamicLibrary::~DynamicLibrary() {
90 lt_dlhandle a_handle = (lt_dlhandle) handle;
94 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
95 E = OpenedHandles.end(); I != E; ++I) {
97 // Note: don't use the swap/pop_back trick here. Order is important.
98 OpenedHandles.erase(I);
105 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
106 std::string *ErrMsg) {
107 check_ltdl_initialization();
108 lt_dlhandle a_handle = lt_dlopen(Filename);
111 a_handle = lt_dlopenext(Filename);
115 *ErrMsg = std::string("Can't open :") +
116 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
120 lt_dlmakeresident(a_handle);
122 OpenedHandles.push_back(a_handle);
126 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
127 check_ltdl_initialization();
129 // First check symbols added via AddSymbol().
130 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
131 if (I != g_symbols.end())
134 // Now search the libraries.
135 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
136 E = OpenedHandles.end(); I != E; ++I) {
137 lt_ptr ptr = lt_dlsym(*I, symbolName);
142 #define EXPLICIT_SYMBOL(SYM) \
143 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
145 // If this is darwin, it has some funky issues, try to solve them here. Some
146 // important symbols are marked 'private external' which doesn't allow
147 // SearchForAddressOfSymbol to find them. As such, we special case them here,
148 // there is only a small handful of them.
152 EXPLICIT_SYMBOL(__ashldi3);
153 EXPLICIT_SYMBOL(__ashrdi3);
154 EXPLICIT_SYMBOL(__cmpdi2);
155 EXPLICIT_SYMBOL(__divdi3);
156 EXPLICIT_SYMBOL(__eprintf);
157 EXPLICIT_SYMBOL(__fixdfdi);
158 EXPLICIT_SYMBOL(__fixsfdi);
159 EXPLICIT_SYMBOL(__fixunsdfdi);
160 EXPLICIT_SYMBOL(__fixunssfdi);
161 EXPLICIT_SYMBOL(__floatdidf);
162 EXPLICIT_SYMBOL(__floatdisf);
163 EXPLICIT_SYMBOL(__lshrdi3);
164 EXPLICIT_SYMBOL(__moddi3);
165 EXPLICIT_SYMBOL(__udivdi3);
166 EXPLICIT_SYMBOL(__umoddi3);
172 EXPLICIT_SYMBOL(_alloca);
176 #undef EXPLICIT_SYMBOL
178 // This macro returns the address of a well-known, explicit symbol
179 #define EXPLICIT_SYMBOL(SYM) \
180 if (!strcmp(symbolName, #SYM)) return &SYM
182 // On linux we have a weird situation. The stderr/out/in symbols are both
183 // macros and global variables because of standards requirements. So, we
184 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first.
185 #if defined(__linux__)
187 EXPLICIT_SYMBOL(stderr);
188 EXPLICIT_SYMBOL(stdout);
189 EXPLICIT_SYMBOL(stdin);
192 // For everything else, we want to check to make sure the symbol isn't defined
193 // as a macro before using EXPLICIT_SYMBOL.
196 EXPLICIT_SYMBOL(stdin);
199 EXPLICIT_SYMBOL(stdout);
202 EXPLICIT_SYMBOL(stderr);
206 #undef EXPLICIT_SYMBOL
211 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
212 assert(handle != 0 && "Invalid DynamicLibrary handle");
213 return lt_dlsym((lt_dlhandle) handle, symbolName);
216 #endif // LLVM_ON_WIN32
218 DEFINING_FILE_FOR(SystemDynamicLibrary)