Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 1 | //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system DynamicLibrary concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/DynamicLibrary.h" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame^] | 15 | |
| 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. |
| 21 | |
| 22 | #ifdef _WIN32 |
| 23 | |
| 24 | #include "win32/DynamicLibrary.cpp" |
| 25 | |
| 26 | #else |
| 27 | |
Reid Spencer | 29ae177 | 2004-11-29 12:39:10 +0000 | [diff] [blame] | 28 | #include "ltdl.h" |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 29 | #include <cassert> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 35 | //=== independent code. |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 38 | static bool did_initialize_ltdl = false; |
| 39 | |
| 40 | static inline void check_ltdl_initialization() { |
| 41 | if (!did_initialize_ltdl) { |
| 42 | if (0 != lt_dlinit()) |
| 43 | throw std::string(lt_dlerror()); |
| 44 | did_initialize_ltdl = true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static std::vector<lt_dlhandle> OpenedHandles; |
| 49 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 50 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 51 | check_ltdl_initialization(); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 52 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 53 | lt_dlhandle a_handle = lt_dlopen(0); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 55 | if (a_handle == 0) |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 56 | throw std::string("Can't open program as dynamic library"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 57 | |
| 58 | handle = a_handle; |
| 59 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 62 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 63 | check_ltdl_initialization(); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 64 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 65 | lt_dlhandle a_handle = lt_dlopen(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 66 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 67 | if (a_handle == 0) |
| 68 | a_handle = lt_dlopenext(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 69 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 70 | if (a_handle == 0) |
| 71 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 72 | |
| 73 | handle = a_handle; |
| 74 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 78 | lt_dlhandle a_handle = (lt_dlhandle) handle; |
| 79 | if (a_handle) { |
| 80 | lt_dlclose(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 81 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 82 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 83 | E = OpenedHandles.end(); I != E; ++I) { |
| 84 | if (*I == a_handle) { |
| 85 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 86 | OpenedHandles.erase(I); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void DynamicLibrary::LoadLibraryPermanently(const char* filename) { |
| 93 | check_ltdl_initialization(); |
| 94 | lt_dlhandle a_handle = lt_dlopen(filename); |
| 95 | |
| 96 | if (a_handle == 0) |
| 97 | a_handle = lt_dlopenext(filename); |
| 98 | |
| 99 | if (a_handle == 0) |
| 100 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 101 | |
| 102 | lt_dlmakeresident(a_handle); |
| 103 | |
| 104 | OpenedHandles.push_back(a_handle); |
| 105 | } |
| 106 | |
| 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); |
| 112 | if (ptr) |
| 113 | return ptr; |
| 114 | } |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 115 | |
| 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. |
| 120 | #ifdef __APPLE__ |
| 121 | { |
Chris Lattner | 368cb8e | 2004-12-04 04:17:20 +0000 | [diff] [blame] | 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 |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 140 | } |
| 141 | #endif |
| 142 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 143 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 147 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 148 | return lt_dlsym((lt_dlhandle) handle, symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame^] | 151 | #endif // _WIN32 |