Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 1 | //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system DynamicLibrary concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/DynamicLibrary.h" |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame^] | 16 | #include <cstring> |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 17 | #include <map> |
| 18 | |
| 19 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
| 20 | static std::map<std::string, void *> g_symbols; |
| 21 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 22 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 23 | void *symbolValue) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 24 | g_symbols[symbolName] = symbolValue; |
| 25 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 26 | |
| 27 | // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL |
| 28 | // license and special exception would cause all of LLVM to be placed under |
| 29 | // the LGPL. This is because the exception applies only when libtool is |
| 30 | // used, and obviously libtool is not used with Visual Studio. An entirely |
| 31 | // separate implementation is provided in win32/DynamicLibrary.cpp. |
| 32 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 33 | #ifdef LLVM_ON_WIN32 |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 34 | |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 35 | #include "Win32/DynamicLibrary.inc" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 36 | |
| 37 | #else |
| 38 | |
Reid Spencer | 29ae177 | 2004-11-29 12:39:10 +0000 | [diff] [blame] | 39 | #include "ltdl.h" |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 40 | #include <cassert> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 46 | //=== independent code. |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 49 | static inline void check_ltdl_initialization() { |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 50 | static bool did_initialize_ltdl = false; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 51 | if (!did_initialize_ltdl) { |
Chris Lattner | 9b0d6f4 | 2006-08-30 20:37:06 +0000 | [diff] [blame] | 52 | int Err = lt_dlinit(); |
Chris Lattner | acf8145 | 2007-02-01 04:57:00 +0000 | [diff] [blame] | 53 | Err = Err; // Silence warning. |
Chris Lattner | 9b0d6f4 | 2006-08-30 20:37:06 +0000 | [diff] [blame] | 54 | assert(0 == Err && "Can't init the ltdl library"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 55 | did_initialize_ltdl = true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static std::vector<lt_dlhandle> OpenedHandles; |
| 60 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 61 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 62 | check_ltdl_initialization(); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 63 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 64 | lt_dlhandle a_handle = lt_dlopen(0); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 6745458 | 2007-09-28 20:50:50 +0000 | [diff] [blame] | 66 | assert(a_handle && "Can't open program as dynamic library"); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 67 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 68 | handle = a_handle; |
| 69 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 72 | /* |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 73 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 74 | check_ltdl_initialization(); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 75 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 76 | lt_dlhandle a_handle = lt_dlopen(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 77 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 78 | if (a_handle == 0) |
| 79 | a_handle = lt_dlopenext(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 80 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 81 | if (a_handle == 0) |
| 82 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 83 | |
| 84 | handle = a_handle; |
| 85 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 86 | } |
Reid Spencer | 99655e1 | 2006-08-25 19:54:53 +0000 | [diff] [blame] | 87 | */ |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 88 | |
| 89 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 90 | lt_dlhandle a_handle = (lt_dlhandle) handle; |
| 91 | if (a_handle) { |
| 92 | lt_dlclose(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 93 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 94 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 95 | E = OpenedHandles.end(); I != E; ++I) { |
| 96 | if (*I == a_handle) { |
| 97 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 98 | OpenedHandles.erase(I); |
Chris Lattner | 2b80e8d | 2006-05-12 18:13:11 +0000 | [diff] [blame] | 99 | return; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 105 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 106 | std::string *ErrMsg) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 107 | check_ltdl_initialization(); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 108 | lt_dlhandle a_handle = lt_dlopen(Filename); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 109 | |
| 110 | if (a_handle == 0) |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 111 | a_handle = lt_dlopenext(Filename); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 112 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 113 | if (a_handle == 0) { |
| 114 | if (ErrMsg) |
| 115 | *ErrMsg = std::string("Can't open :") + |
| 116 | (Filename ? Filename : "<current process>") + ": " + lt_dlerror(); |
| 117 | return true; |
| 118 | } |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 119 | |
| 120 | lt_dlmakeresident(a_handle); |
| 121 | |
| 122 | OpenedHandles.push_back(a_handle); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 123 | return false; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 127 | check_ltdl_initialization(); |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 128 | |
| 129 | // First check symbols added via AddSymbol(). |
| 130 | std::map<std::string, void *>::iterator I = g_symbols.find(symbolName); |
| 131 | if (I != g_symbols.end()) |
| 132 | return I->second; |
| 133 | |
| 134 | // Now search the libraries. |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 135 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 136 | E = OpenedHandles.end(); I != E; ++I) { |
| 137 | lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 138 | if (ptr) |
| 139 | return ptr; |
| 140 | } |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 141 | |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 142 | #define EXPLICIT_SYMBOL(SYM) \ |
| 143 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
| 144 | |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 145 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 146 | // important symbols are marked 'private external' which doesn't allow |
| 147 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 148 | // there is only a small handful of them. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 150 | #ifdef __APPLE__ |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 151 | { |
Chris Lattner | 368cb8e | 2004-12-04 04:17:20 +0000 | [diff] [blame] | 152 | EXPLICIT_SYMBOL(__ashldi3); |
| 153 | EXPLICIT_SYMBOL(__ashrdi3); |
| 154 | EXPLICIT_SYMBOL(__cmpdi2); |
| 155 | EXPLICIT_SYMBOL(__divdi3); |
| 156 | EXPLICIT_SYMBOL(__eprintf); |
| 157 | EXPLICIT_SYMBOL(__fixdfdi); |
| 158 | EXPLICIT_SYMBOL(__fixsfdi); |
| 159 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 160 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 161 | EXPLICIT_SYMBOL(__floatdidf); |
| 162 | EXPLICIT_SYMBOL(__floatdisf); |
| 163 | EXPLICIT_SYMBOL(__lshrdi3); |
| 164 | EXPLICIT_SYMBOL(__moddi3); |
| 165 | EXPLICIT_SYMBOL(__udivdi3); |
| 166 | EXPLICIT_SYMBOL(__umoddi3); |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 167 | } |
| 168 | #endif |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 169 | |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 170 | #ifdef __CYGWIN__ |
| 171 | { |
| 172 | EXPLICIT_SYMBOL(_alloca); |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | #undef EXPLICIT_SYMBOL |
| 177 | |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 178 | // This macro returns the address of a well-known, explicit symbol |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 179 | #define EXPLICIT_SYMBOL(SYM) \ |
| 180 | if (!strcmp(symbolName, #SYM)) return &SYM |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 181 | |
| 182 | // On linux we have a weird situation. The stderr/out/in symbols are both |
| 183 | // macros and global variables because of standards requirements. So, we |
| 184 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
| 185 | #if defined(__linux__) |
| 186 | { |
| 187 | EXPLICIT_SYMBOL(stderr); |
| 188 | EXPLICIT_SYMBOL(stdout); |
| 189 | EXPLICIT_SYMBOL(stdin); |
| 190 | } |
| 191 | #else |
| 192 | // For everything else, we want to check to make sure the symbol isn't defined |
| 193 | // as a macro before using EXPLICIT_SYMBOL. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 194 | { |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 195 | #ifndef stdin |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 196 | EXPLICIT_SYMBOL(stdin); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 197 | #endif |
| 198 | #ifndef stdout |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 199 | EXPLICIT_SYMBOL(stdout); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 200 | #endif |
| 201 | #ifndef stderr |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 202 | EXPLICIT_SYMBOL(stderr); |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 203 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 204 | } |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 205 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 206 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 207 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 208 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 212 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 213 | return lt_dlsym((lt_dlhandle) handle, symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 216 | #endif // LLVM_ON_WIN32 |
Reid Spencer | 23dd332 | 2006-07-26 16:55:39 +0000 | [diff] [blame] | 217 | |
| 218 | DEFINING_FILE_FOR(SystemDynamicLibrary) |