1 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header file implements the operating system DynamicLibrary concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/System/DynamicLibrary.h"
15 #include "llvm/Config/config.h"
18 // Collection of symbol name/value pairs to be searched prior to any libraries.
19 static std::map<std::string, void *> g_symbols;
21 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName,
23 g_symbols[symbolName] = symbolValue;
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.
34 #include "Win32/DynamicLibrary.inc"
41 using namespace llvm::sys;
43 //===----------------------------------------------------------------------===//
44 //=== WARNING: Implementation here must contain only TRULY operating system
45 //=== independent code.
46 //===----------------------------------------------------------------------===//
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 assert(0 == Err && "Can't init the ltdl library");
53 did_initialize_ltdl = true;
57 static std::vector<lt_dlhandle> OpenedHandles;
59 DynamicLibrary::DynamicLibrary() : handle(0) {
60 check_ltdl_initialization();
62 lt_dlhandle a_handle = lt_dlopen(0);
64 assert(a_handle == 0 || "Can't open program as dynamic library");
67 OpenedHandles.push_back(a_handle);
71 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
72 check_ltdl_initialization();
74 lt_dlhandle a_handle = lt_dlopen(filename);
77 a_handle = lt_dlopenext(filename);
80 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
83 OpenedHandles.push_back(a_handle);
87 DynamicLibrary::~DynamicLibrary() {
88 lt_dlhandle a_handle = (lt_dlhandle) handle;
92 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
93 E = OpenedHandles.end(); I != E; ++I) {
95 // Note: don't use the swap/pop_back trick here. Order is important.
96 OpenedHandles.erase(I);
103 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename,
104 std::string *ErrMsg) {
105 check_ltdl_initialization();
106 lt_dlhandle a_handle = lt_dlopen(Filename);
109 a_handle = lt_dlopenext(Filename);
113 *ErrMsg = std::string("Can't open :") +
114 (Filename ? Filename : "<current process>") + ": " + lt_dlerror();
118 lt_dlmakeresident(a_handle);
120 OpenedHandles.push_back(a_handle);
124 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
125 check_ltdl_initialization();
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())
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);
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.
146 #define EXPLICIT_SYMBOL(SYM) \
147 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
149 EXPLICIT_SYMBOL(__ashldi3);
150 EXPLICIT_SYMBOL(__ashrdi3);
151 EXPLICIT_SYMBOL(__cmpdi2);
152 EXPLICIT_SYMBOL(__divdi3);
153 EXPLICIT_SYMBOL(__eprintf);
154 EXPLICIT_SYMBOL(__fixdfdi);
155 EXPLICIT_SYMBOL(__fixsfdi);
156 EXPLICIT_SYMBOL(__fixunsdfdi);
157 EXPLICIT_SYMBOL(__fixunssfdi);
158 EXPLICIT_SYMBOL(__floatdidf);
159 EXPLICIT_SYMBOL(__floatdisf);
160 EXPLICIT_SYMBOL(__lshrdi3);
161 EXPLICIT_SYMBOL(__moddi3);
162 EXPLICIT_SYMBOL(__udivdi3);
163 EXPLICIT_SYMBOL(__umoddi3);
165 #undef EXPLICIT_SYMBOL
167 #define EXPLICIT_SYMBOL(SYM) \
168 if (!strcmp(symbolName, #SYM)) return &SYM
169 // Try a few well known symbols just to give lli a shot at working.
170 // Note that on some systems stdin, etc. are macros so we have to
171 // avoid attempting to take the address of a macro :)
174 EXPLICIT_SYMBOL(stdin);
175 EXPLICIT_SYMBOL(stdout);
176 EXPLICIT_SYMBOL(stderr);
179 #undef EXPLICIT_SYMBOL
184 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
185 assert(handle != 0 && "Invalid DynamicLibrary handle");
186 return lt_dlsym((lt_dlhandle) handle, symbolName);
189 #endif // LLVM_ON_WIN32
191 DEFINING_FILE_FOR(SystemDynamicLibrary)