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