LLVM API Documentation
00001 //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header file implements the operating system DynamicLibrary concept. 00011 // 00012 // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is 00013 // not thread safe! 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "llvm/System/DynamicLibrary.h" 00018 #include "llvm/Support/ManagedStatic.h" 00019 #include "llvm/Config/config.h" 00020 #include <cstdio> 00021 #include <cstring> 00022 #include <map> 00023 #include <vector> 00024 00025 // Collection of symbol name/value pairs to be searched prior to any libraries. 00026 static std::map<std::string, void*> *ExplicitSymbols = 0; 00027 00028 void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, 00029 void *symbolValue) { 00030 if (ExplicitSymbols == 0) 00031 ExplicitSymbols = new std::map<std::string, void*>(); 00032 (*ExplicitSymbols)[symbolName] = symbolValue; 00033 } 00034 00035 #ifdef LLVM_ON_WIN32 00036 00037 #include "Win32/DynamicLibrary.inc" 00038 00039 #else 00040 00041 #include <dlfcn.h> 00042 using namespace llvm; 00043 using namespace llvm::sys; 00044 00045 //===----------------------------------------------------------------------===// 00046 //=== WARNING: Implementation here must contain only TRULY operating system 00047 //=== independent code. 00048 //===----------------------------------------------------------------------===// 00049 00050 static std::vector<void *> *OpenedHandles = 0; 00051 00052 00053 bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, 00054 std::string *ErrMsg) { 00055 void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL); 00056 if (H == 0) { 00057 if (ErrMsg) *ErrMsg = dlerror(); 00058 return true; 00059 } 00060 if (OpenedHandles == 0) 00061 OpenedHandles = new std::vector<void *>(); 00062 OpenedHandles->push_back(H); 00063 return false; 00064 } 00065 00066 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { 00067 // First check symbols added via AddSymbol(). 00068 if (ExplicitSymbols) { 00069 std::map<std::string, void *>::iterator I = 00070 ExplicitSymbols->find(symbolName); 00071 std::map<std::string, void *>::iterator E = ExplicitSymbols->end(); 00072 00073 if (I != E) 00074 return I->second; 00075 } 00076 00077 // Now search the libraries. 00078 if (OpenedHandles) { 00079 for (std::vector<void *>::iterator I = OpenedHandles->begin(), 00080 E = OpenedHandles->end(); I != E; ++I) { 00081 //lt_ptr ptr = lt_dlsym(*I, symbolName); 00082 void *ptr = dlsym(*I, symbolName); 00083 if (ptr) { 00084 return ptr; 00085 } 00086 } 00087 } 00088 00089 #define EXPLICIT_SYMBOL(SYM) \ 00090 extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM 00091 00092 // If this is darwin, it has some funky issues, try to solve them here. Some 00093 // important symbols are marked 'private external' which doesn't allow 00094 // SearchForAddressOfSymbol to find them. As such, we special case them here, 00095 // there is only a small handful of them. 00096 00097 #ifdef __APPLE__ 00098 { 00099 EXPLICIT_SYMBOL(__ashldi3); 00100 EXPLICIT_SYMBOL(__ashrdi3); 00101 EXPLICIT_SYMBOL(__cmpdi2); 00102 EXPLICIT_SYMBOL(__divdi3); 00103 EXPLICIT_SYMBOL(__eprintf); 00104 EXPLICIT_SYMBOL(__fixdfdi); 00105 EXPLICIT_SYMBOL(__fixsfdi); 00106 EXPLICIT_SYMBOL(__fixunsdfdi); 00107 EXPLICIT_SYMBOL(__fixunssfdi); 00108 EXPLICIT_SYMBOL(__floatdidf); 00109 EXPLICIT_SYMBOL(__floatdisf); 00110 EXPLICIT_SYMBOL(__lshrdi3); 00111 EXPLICIT_SYMBOL(__moddi3); 00112 EXPLICIT_SYMBOL(__udivdi3); 00113 EXPLICIT_SYMBOL(__umoddi3); 00114 } 00115 #endif 00116 00117 #ifdef __CYGWIN__ 00118 { 00119 EXPLICIT_SYMBOL(_alloca); 00120 EXPLICIT_SYMBOL(__main); 00121 } 00122 #endif 00123 00124 #undef EXPLICIT_SYMBOL 00125 00126 // This macro returns the address of a well-known, explicit symbol 00127 #define EXPLICIT_SYMBOL(SYM) \ 00128 if (!strcmp(symbolName, #SYM)) return &SYM 00129 00130 // On linux we have a weird situation. The stderr/out/in symbols are both 00131 // macros and global variables because of standards requirements. So, we 00132 // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. 00133 #if defined(__linux__) 00134 { 00135 EXPLICIT_SYMBOL(stderr); 00136 EXPLICIT_SYMBOL(stdout); 00137 EXPLICIT_SYMBOL(stdin); 00138 } 00139 #else 00140 // For everything else, we want to check to make sure the symbol isn't defined 00141 // as a macro before using EXPLICIT_SYMBOL. 00142 { 00143 #ifndef stdin 00144 EXPLICIT_SYMBOL(stdin); 00145 #endif 00146 #ifndef stdout 00147 EXPLICIT_SYMBOL(stdout); 00148 #endif 00149 #ifndef stderr 00150 EXPLICIT_SYMBOL(stderr); 00151 #endif 00152 } 00153 #endif 00154 #undef EXPLICIT_SYMBOL 00155 00156 return 0; 00157 } 00158 00159 #endif // LLVM_ON_WIN32