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"
16 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
17 // license and special exception would cause all of LLVM to be placed under
18 // the LGPL. This is because the exception applies only when libtool is
19 // used, and obviously libtool is not used with Visual Studio. An entirely
20 // separate implementation is provided in win32/DynamicLibrary.cpp.
24 #include "win32/DynamicLibrary.cpp"
31 using namespace llvm::sys;
33 //===----------------------------------------------------------------------===//
34 //=== WARNING: Implementation here must contain only TRULY operating system
35 //=== independent code.
36 //===----------------------------------------------------------------------===//
38 static bool did_initialize_ltdl = false;
40 static inline void check_ltdl_initialization() {
41 if (!did_initialize_ltdl) {
43 throw std::string(lt_dlerror());
44 did_initialize_ltdl = true;
48 static std::vector<lt_dlhandle> OpenedHandles;
50 DynamicLibrary::DynamicLibrary() : handle(0) {
51 check_ltdl_initialization();
53 lt_dlhandle a_handle = lt_dlopen(0);
56 throw std::string("Can't open program as dynamic library");
59 OpenedHandles.push_back(a_handle);
62 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
63 check_ltdl_initialization();
65 lt_dlhandle a_handle = lt_dlopen(filename);
68 a_handle = lt_dlopenext(filename);
71 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
74 OpenedHandles.push_back(a_handle);
77 DynamicLibrary::~DynamicLibrary() {
78 lt_dlhandle a_handle = (lt_dlhandle) handle;
82 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
83 E = OpenedHandles.end(); I != E; ++I) {
85 // Note: don't use the swap/pop_back trick here. Order is important.
86 OpenedHandles.erase(I);
92 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
93 check_ltdl_initialization();
94 lt_dlhandle a_handle = lt_dlopen(filename);
97 a_handle = lt_dlopenext(filename);
100 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
102 lt_dlmakeresident(a_handle);
104 OpenedHandles.push_back(a_handle);
107 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
108 check_ltdl_initialization();
109 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
110 E = OpenedHandles.end(); I != E; ++I) {
111 lt_ptr ptr = lt_dlsym(*I, symbolName);
116 // If this is darwin, it has some funky issues, try to solve them here. Some
117 // important symbols are marked 'private external' which doesn't allow
118 // SearchForAddressOfSymbol to find them. As such, we special case them here,
119 // there is only a small handful of them.
122 #define EXPLICIT_SYMBOL(SYM) \
123 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
124 EXPLICIT_SYMBOL(__ashldi3);
125 EXPLICIT_SYMBOL(__ashrdi3);
126 EXPLICIT_SYMBOL(__cmpdi2);
127 EXPLICIT_SYMBOL(__divdi3);
128 EXPLICIT_SYMBOL(__eprintf);
129 EXPLICIT_SYMBOL(__fixdfdi);
130 EXPLICIT_SYMBOL(__fixsfdi);
131 EXPLICIT_SYMBOL(__fixunsdfdi);
132 EXPLICIT_SYMBOL(__fixunssfdi);
133 EXPLICIT_SYMBOL(__floatdidf);
134 EXPLICIT_SYMBOL(__floatdisf);
135 EXPLICIT_SYMBOL(__lshrdi3);
136 EXPLICIT_SYMBOL(__moddi3);
137 EXPLICIT_SYMBOL(__udivdi3);
138 EXPLICIT_SYMBOL(__umoddi3);
139 #undef EXPLICIT_SYMBOL
146 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
147 assert(handle != 0 && "Invalid DynamicLibrary handle");
148 return lt_dlsym((lt_dlhandle) handle, symbolName);