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, void *symbolValue) {
22 g_symbols[symbolName] = symbolValue;
25 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
26 // license and special exception would cause all of LLVM to be placed under
27 // the LGPL. This is because the exception applies only when libtool is
28 // used, and obviously libtool is not used with Visual Studio. An entirely
29 // separate implementation is provided in win32/DynamicLibrary.cpp.
33 #include "Win32/DynamicLibrary.inc"
40 using namespace llvm::sys;
42 //===----------------------------------------------------------------------===//
43 //=== WARNING: Implementation here must contain only TRULY operating system
44 //=== independent code.
45 //===----------------------------------------------------------------------===//
47 static bool did_initialize_ltdl = false;
49 static inline void check_ltdl_initialization() {
50 if (!did_initialize_ltdl) {
52 throw std::string(lt_dlerror());
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);
65 throw std::string("Can't open program as dynamic library");
68 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);
86 DynamicLibrary::~DynamicLibrary() {
87 lt_dlhandle a_handle = (lt_dlhandle) handle;
91 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
92 E = OpenedHandles.end(); I != E; ++I) {
94 // Note: don't use the swap/pop_back trick here. Order is important.
95 OpenedHandles.erase(I);
101 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
102 check_ltdl_initialization();
103 lt_dlhandle a_handle = lt_dlopen(filename);
106 a_handle = lt_dlopenext(filename);
109 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
111 lt_dlmakeresident(a_handle);
113 OpenedHandles.push_back(a_handle);
116 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
117 check_ltdl_initialization();
119 // First check symbols added via AddSymbol().
120 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
121 if (I != g_symbols.end())
124 // Now search the libraries.
125 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
126 E = OpenedHandles.end(); I != E; ++I) {
127 lt_ptr ptr = lt_dlsym(*I, symbolName);
132 // If this is darwin, it has some funky issues, try to solve them here. Some
133 // important symbols are marked 'private external' which doesn't allow
134 // SearchForAddressOfSymbol to find them. As such, we special case them here,
135 // there is only a small handful of them.
138 #define EXPLICIT_SYMBOL(SYM) \
139 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
140 EXPLICIT_SYMBOL(__ashldi3);
141 EXPLICIT_SYMBOL(__ashrdi3);
142 EXPLICIT_SYMBOL(__cmpdi2);
143 EXPLICIT_SYMBOL(__divdi3);
144 EXPLICIT_SYMBOL(__eprintf);
145 EXPLICIT_SYMBOL(__fixdfdi);
146 EXPLICIT_SYMBOL(__fixsfdi);
147 EXPLICIT_SYMBOL(__fixunsdfdi);
148 EXPLICIT_SYMBOL(__fixunssfdi);
149 EXPLICIT_SYMBOL(__floatdidf);
150 EXPLICIT_SYMBOL(__floatdisf);
151 EXPLICIT_SYMBOL(__lshrdi3);
152 EXPLICIT_SYMBOL(__moddi3);
153 EXPLICIT_SYMBOL(__udivdi3);
154 EXPLICIT_SYMBOL(__umoddi3);
155 #undef EXPLICIT_SYMBOL
162 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
163 assert(handle != 0 && "Invalid DynamicLibrary handle");
164 return lt_dlsym((lt_dlhandle) handle, symbolName);
167 #endif // LLVM_ON_WIN32