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 | // |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 5 | // This file was developed by Reid Spencer and is distributed under the |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source 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" |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 16 | #include <map> |
| 17 | |
| 18 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
| 19 | static std::map<std::string, void *> g_symbols; |
| 20 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 21 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 22 | void *symbolValue) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 23 | g_symbols[symbolName] = symbolValue; |
| 24 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 25 | |
| 26 | // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL |
| 27 | // license and special exception would cause all of LLVM to be placed under |
| 28 | // the LGPL. This is because the exception applies only when libtool is |
| 29 | // used, and obviously libtool is not used with Visual Studio. An entirely |
| 30 | // separate implementation is provided in win32/DynamicLibrary.cpp. |
| 31 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 32 | #ifdef LLVM_ON_WIN32 |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 33 | |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 34 | #include "Win32/DynamicLibrary.inc" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 35 | |
| 36 | #else |
| 37 | |
Reid Spencer | 29ae177 | 2004-11-29 12:39:10 +0000 | [diff] [blame] | 38 | #include "ltdl.h" |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 39 | #include <cassert> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 40 | using namespace llvm; |
| 41 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 45 | //=== independent code. |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 48 | static bool did_initialize_ltdl = false; |
| 49 | |
| 50 | static inline void check_ltdl_initialization() { |
| 51 | if (!did_initialize_ltdl) { |
| 52 | if (0 != lt_dlinit()) |
| 53 | throw std::string(lt_dlerror()); |
| 54 | did_initialize_ltdl = true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static std::vector<lt_dlhandle> OpenedHandles; |
| 59 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 60 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 61 | check_ltdl_initialization(); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 62 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 63 | lt_dlhandle a_handle = lt_dlopen(0); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 65 | if (a_handle == 0) |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 66 | throw std::string("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 | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 72 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 73 | check_ltdl_initialization(); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 74 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 75 | lt_dlhandle a_handle = lt_dlopen(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 76 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 77 | if (a_handle == 0) |
| 78 | a_handle = lt_dlopenext(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 80 | if (a_handle == 0) |
| 81 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 82 | |
| 83 | handle = a_handle; |
| 84 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 88 | lt_dlhandle a_handle = (lt_dlhandle) handle; |
| 89 | if (a_handle) { |
| 90 | lt_dlclose(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 91 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 92 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 93 | E = OpenedHandles.end(); I != E; ++I) { |
| 94 | if (*I == a_handle) { |
| 95 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 96 | OpenedHandles.erase(I); |
Chris Lattner | 2b80e8d | 2006-05-12 18:13:11 +0000 | [diff] [blame] | 97 | return; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 103 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 104 | std::string *ErrMsg) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 105 | check_ltdl_initialization(); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 106 | lt_dlhandle a_handle = lt_dlopen(Filename); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 107 | |
| 108 | if (a_handle == 0) |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 109 | a_handle = lt_dlopenext(Filename); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 110 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 111 | if (a_handle == 0) { |
| 112 | if (ErrMsg) |
| 113 | *ErrMsg = std::string("Can't open :") + |
| 114 | (Filename ? Filename : "<current process>") + ": " + lt_dlerror(); |
| 115 | return true; |
| 116 | } |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 117 | |
| 118 | lt_dlmakeresident(a_handle); |
| 119 | |
| 120 | OpenedHandles.push_back(a_handle); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame^] | 121 | return false; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 125 | check_ltdl_initialization(); |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 126 | |
| 127 | // First check symbols added via AddSymbol(). |
| 128 | std::map<std::string, void *>::iterator I = g_symbols.find(symbolName); |
| 129 | if (I != g_symbols.end()) |
| 130 | return I->second; |
| 131 | |
| 132 | // Now search the libraries. |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 133 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 134 | E = OpenedHandles.end(); I != E; ++I) { |
| 135 | lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 136 | if (ptr) |
| 137 | return ptr; |
| 138 | } |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 139 | |
| 140 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 141 | // important symbols are marked 'private external' which doesn't allow |
| 142 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 143 | // there is only a small handful of them. |
| 144 | #ifdef __APPLE__ |
| 145 | { |
Chris Lattner | 368cb8e | 2004-12-04 04:17:20 +0000 | [diff] [blame] | 146 | #define EXPLICIT_SYMBOL(SYM) \ |
| 147 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
| 148 | EXPLICIT_SYMBOL(__ashldi3); |
| 149 | EXPLICIT_SYMBOL(__ashrdi3); |
| 150 | EXPLICIT_SYMBOL(__cmpdi2); |
| 151 | EXPLICIT_SYMBOL(__divdi3); |
| 152 | EXPLICIT_SYMBOL(__eprintf); |
| 153 | EXPLICIT_SYMBOL(__fixdfdi); |
| 154 | EXPLICIT_SYMBOL(__fixsfdi); |
| 155 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 156 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 157 | EXPLICIT_SYMBOL(__floatdidf); |
| 158 | EXPLICIT_SYMBOL(__floatdisf); |
| 159 | EXPLICIT_SYMBOL(__lshrdi3); |
| 160 | EXPLICIT_SYMBOL(__moddi3); |
| 161 | EXPLICIT_SYMBOL(__udivdi3); |
| 162 | EXPLICIT_SYMBOL(__umoddi3); |
| 163 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 167 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 171 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 172 | return lt_dlsym((lt_dlhandle) handle, symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 175 | #endif // LLVM_ON_WIN32 |