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" |
Reid Spencer | 29ae177 | 2004-11-29 12:39:10 +0000 | [diff] [blame] | 15 | #include "ltdl.h" |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 16 | #include <cassert> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 22 | //=== independent code. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 25 | static bool did_initialize_ltdl = false; |
| 26 | |
| 27 | static inline void check_ltdl_initialization() { |
| 28 | if (!did_initialize_ltdl) { |
| 29 | if (0 != lt_dlinit()) |
| 30 | throw std::string(lt_dlerror()); |
| 31 | did_initialize_ltdl = true; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | static std::vector<lt_dlhandle> OpenedHandles; |
| 36 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 37 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 38 | check_ltdl_initialization(); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 40 | lt_dlhandle a_handle = lt_dlopen(0); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 41 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 42 | if (a_handle == 0) |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 43 | throw std::string("Can't open program as dynamic library"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 44 | |
| 45 | handle = a_handle; |
| 46 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 49 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 50 | check_ltdl_initialization(); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 51 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 52 | lt_dlhandle a_handle = lt_dlopen(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 54 | if (a_handle == 0) |
| 55 | a_handle = lt_dlopenext(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 56 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 57 | if (a_handle == 0) |
| 58 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 59 | |
| 60 | handle = a_handle; |
| 61 | OpenedHandles.push_back(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 65 | lt_dlhandle a_handle = (lt_dlhandle) handle; |
| 66 | if (a_handle) { |
| 67 | lt_dlclose(a_handle); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 69 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 70 | E = OpenedHandles.end(); I != E; ++I) { |
| 71 | if (*I == a_handle) { |
| 72 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 73 | OpenedHandles.erase(I); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void DynamicLibrary::LoadLibraryPermanently(const char* filename) { |
| 80 | check_ltdl_initialization(); |
| 81 | lt_dlhandle a_handle = lt_dlopen(filename); |
| 82 | |
| 83 | if (a_handle == 0) |
| 84 | a_handle = lt_dlopenext(filename); |
| 85 | |
| 86 | if (a_handle == 0) |
| 87 | throw std::string("Can't open :") + filename + ": " + lt_dlerror(); |
| 88 | |
| 89 | lt_dlmakeresident(a_handle); |
| 90 | |
| 91 | OpenedHandles.push_back(a_handle); |
| 92 | } |
| 93 | |
| 94 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 95 | check_ltdl_initialization(); |
| 96 | for (std::vector<lt_dlhandle>::iterator I = OpenedHandles.begin(), |
| 97 | E = OpenedHandles.end(); I != E; ++I) { |
| 98 | lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 99 | if (ptr) |
| 100 | return ptr; |
| 101 | } |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 102 | |
| 103 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 104 | // important symbols are marked 'private external' which doesn't allow |
| 105 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 106 | // there is only a small handful of them. |
| 107 | #ifdef __APPLE__ |
| 108 | { |
Chris Lattner | 368cb8e | 2004-12-04 04:17:20 +0000 | [diff] [blame] | 109 | #define EXPLICIT_SYMBOL(SYM) \ |
| 110 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
| 111 | EXPLICIT_SYMBOL(__ashldi3); |
| 112 | EXPLICIT_SYMBOL(__ashrdi3); |
| 113 | EXPLICIT_SYMBOL(__cmpdi2); |
| 114 | EXPLICIT_SYMBOL(__divdi3); |
| 115 | EXPLICIT_SYMBOL(__eprintf); |
| 116 | EXPLICIT_SYMBOL(__fixdfdi); |
| 117 | EXPLICIT_SYMBOL(__fixsfdi); |
| 118 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 119 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 120 | EXPLICIT_SYMBOL(__floatdidf); |
| 121 | EXPLICIT_SYMBOL(__floatdisf); |
| 122 | EXPLICIT_SYMBOL(__lshrdi3); |
| 123 | EXPLICIT_SYMBOL(__moddi3); |
| 124 | EXPLICIT_SYMBOL(__udivdi3); |
| 125 | EXPLICIT_SYMBOL(__umoddi3); |
| 126 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 127 | } |
| 128 | #endif |
| 129 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 130 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 134 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 135 | return lt_dlsym((lt_dlhandle) handle, symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |