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 Err
= Err
; // Silence warning.
53 assert(0 == Err
&& "Can't init the ltdl library");
54 did_initialize_ltdl
= true;
58 static std::vector
<lt_dlhandle
> OpenedHandles
;
60 DynamicLibrary::DynamicLibrary() : handle(0) {
61 check_ltdl_initialization();
63 lt_dlhandle a_handle
= lt_dlopen(0);
65 assert(a_handle
== 0 || "Can't open program as dynamic library");
68 OpenedHandles
.push_back(a_handle
);
72 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
73 check_ltdl_initialization();
75 lt_dlhandle a_handle = lt_dlopen(filename);
78 a_handle = lt_dlopenext(filename);
81 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
84 OpenedHandles.push_back(a_handle);
88 DynamicLibrary::~DynamicLibrary() {
89 lt_dlhandle a_handle
= (lt_dlhandle
) handle
;
93 for (std::vector
<lt_dlhandle
>::iterator I
= OpenedHandles
.begin(),
94 E
= OpenedHandles
.end(); I
!= E
; ++I
) {
96 // Note: don't use the swap/pop_back trick here. Order is important.
97 OpenedHandles
.erase(I
);
104 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename
,
105 std::string
*ErrMsg
) {
106 check_ltdl_initialization();
107 lt_dlhandle a_handle
= lt_dlopen(Filename
);
110 a_handle
= lt_dlopenext(Filename
);
114 *ErrMsg
= std::string("Can't open :") +
115 (Filename
? Filename
: "<current process>") + ": " + lt_dlerror();
119 lt_dlmakeresident(a_handle
);
121 OpenedHandles
.push_back(a_handle
);
125 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName
) {
126 check_ltdl_initialization();
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())
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
);
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.
147 #define EXPLICIT_SYMBOL(SYM) \
148 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
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
);
166 #undef EXPLICIT_SYMBOL
169 // This macro returns the address of a well-known, explicit symbol
170 #define EXPLICIT_SYMBOL(SYM) \
171 if (!strcmp(symbolName, #SYM)) return &SYM
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__)
178 EXPLICIT_SYMBOL(stderr
);
179 EXPLICIT_SYMBOL(stdout
);
180 EXPLICIT_SYMBOL(stdin
);
183 // For everything else, we want to check to make sure the symbol isn't defined
184 // as a macro before using EXPLICIT_SYMBOL.
187 EXPLICIT_SYMBOL(stdin
);
190 EXPLICIT_SYMBOL(stdout
);
193 EXPLICIT_SYMBOL(stderr
);
197 #undef EXPLICIT_SYMBOL
202 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName
) {
203 assert(handle
!= 0 && "Invalid DynamicLibrary handle");
204 return lt_dlsym((lt_dlhandle
) handle
, symbolName
);
207 #endif // LLVM_ON_WIN32
209 DEFINING_FILE_FOR(SystemDynamicLibrary
)