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"
17 // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL
18 // license and special exception would cause all of LLVM to be placed under
19 // the LGPL. This is because the exception applies only when libtool is
20 // used, and obviously libtool is not used with Visual Studio. An entirely
21 // separate implementation is provided in win32/DynamicLibrary.cpp.
25 #include "Win32/DynamicLibrary.inc"
32 using namespace llvm::sys;
34 //===----------------------------------------------------------------------===//
35 //=== WARNING: Implementation here must contain only TRULY operating system
36 //=== independent code.
37 //===----------------------------------------------------------------------===//
39 static bool did_initialize_ltdl = false;
41 static inline void check_ltdl_initialization() {
42 if (!did_initialize_ltdl) {
44 throw std::string(lt_dlerror());
45 did_initialize_ltdl = true;
49 static std::vector<lt_dlhandle> OpenedHandles;
51 DynamicLibrary::DynamicLibrary() : handle(0) {
52 check_ltdl_initialization();
54 lt_dlhandle a_handle = lt_dlopen(0);
57 throw std::string("Can't open program as dynamic library");
60 OpenedHandles.push_back(a_handle);
63 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
64 check_ltdl_initialization();
66 lt_dlhandle a_handle = lt_dlopen(filename);
69 a_handle = lt_dlopenext(filename);
72 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
75 OpenedHandles.push_back(a_handle);
78 DynamicLibrary::~DynamicLibrary() {
79 lt_dlhandle a_handle = (lt_dlhandle) handle;
83 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
84 E = OpenedHandles.end(); I != E; ++I) {
86 // Note: don't use the swap/pop_back trick here. Order is important.
87 OpenedHandles.erase(I);
93 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
94 check_ltdl_initialization();
95 lt_dlhandle a_handle = lt_dlopen(filename);
98 a_handle = lt_dlopenext(filename);
101 throw std::string("Can't open :") + filename + ": " + lt_dlerror();
103 lt_dlmakeresident(a_handle);
105 OpenedHandles.push_back(a_handle);
108 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
109 check_ltdl_initialization();
110 for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(),
111 E = OpenedHandles.end(); I != E; ++I) {
112 lt_ptr ptr = lt_dlsym(*I, symbolName);
117 // If this is darwin, it has some funky issues, try to solve them here. Some
118 // important symbols are marked 'private external' which doesn't allow
119 // SearchForAddressOfSymbol to find them. As such, we special case them here,
120 // there is only a small handful of them.
123 #define EXPLICIT_SYMBOL(SYM) \
124 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM
125 EXPLICIT_SYMBOL(__ashldi3);
126 EXPLICIT_SYMBOL(__ashrdi3);
127 EXPLICIT_SYMBOL(__cmpdi2);
128 EXPLICIT_SYMBOL(__divdi3);
129 EXPLICIT_SYMBOL(__eprintf);
130 EXPLICIT_SYMBOL(__fixdfdi);
131 EXPLICIT_SYMBOL(__fixsfdi);
132 EXPLICIT_SYMBOL(__fixunsdfdi);
133 EXPLICIT_SYMBOL(__fixunssfdi);
134 EXPLICIT_SYMBOL(__floatdidf);
135 EXPLICIT_SYMBOL(__floatdisf);
136 EXPLICIT_SYMBOL(__lshrdi3);
137 EXPLICIT_SYMBOL(__moddi3);
138 EXPLICIT_SYMBOL(__udivdi3);
139 EXPLICIT_SYMBOL(__umoddi3);
140 #undef EXPLICIT_SYMBOL
147 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
148 assert(handle != 0 && "Invalid DynamicLibrary handle");
149 return lt_dlsym((lt_dlhandle) handle, symbolName);
152 #endif // LLVM_ON_WIN32