Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- DynamicLibrary.cpp - Runtime link/load libraries --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file implements the operating system DynamicLibrary concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/System/DynamicLibrary.h" |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include "llvm/System/RWMutex.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Duncan Sands | 05f6837 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 18 | #include <cstdio> |
Duncan Sands | fca2014 | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 19 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | |
| 22 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 23 | static std::map<std::string, void*> symbols; |
| 24 | static llvm::sys::SmartRWMutex<true> SymbolsLock; |
| 25 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 27 | void *symbolValue) { |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 28 | llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock); |
| 29 | symbols[symbolName] = symbolValue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | #ifdef LLVM_ON_WIN32 |
| 33 | |
| 34 | #include "Win32/DynamicLibrary.inc" |
| 35 | |
| 36 | #else |
| 37 | |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 38 | #include <dlfcn.h> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | #include <cassert> |
| 40 | using namespace llvm; |
| 41 | using namespace llvm::sys; |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 45 | //=== independent code. |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 48 | static std::vector<void *> OpenedHandles; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | |
Devang Patel | 79df1c6 | 2008-03-13 16:55:34 +0000 | [diff] [blame] | 50 | DynamicLibrary::DynamicLibrary() {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | |
| 52 | DynamicLibrary::~DynamicLibrary() { |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 53 | SmartScopedWriter<true> Writer(&SymbolsLock); |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 54 | while(!OpenedHandles.empty()) { |
Chris Lattner | fce334d | 2009-07-07 17:50:11 +0000 | [diff] [blame] | 55 | void *H = OpenedHandles.back(); |
| 56 | OpenedHandles.pop_back(); |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 57 | dlclose(H); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 62 | std::string *ErrMsg) { |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 63 | SmartScopedWriter<true> Writer(&SymbolsLock); |
Chris Lattner | 859cf63 | 2008-07-10 00:52:20 +0000 | [diff] [blame] | 64 | void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL); |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 65 | if (H == 0) { |
Chris Lattner | acc8193 | 2008-03-12 00:50:01 +0000 | [diff] [blame] | 66 | if (ErrMsg) |
| 67 | *ErrMsg = dlerror(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | return true; |
| 69 | } |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 70 | OpenedHandles.push_back(H); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | |
| 74 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | // First check symbols added via AddSymbol(). |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 76 | SymbolsLock.reader_acquire(); |
| 77 | std::map<std::string, void *>::iterator I = symbols.find(symbolName); |
| 78 | std::map<std::string, void *>::iterator E = symbols.end(); |
| 79 | SymbolsLock.reader_release(); |
| 80 | |
| 81 | if (I != E) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | return I->second; |
| 83 | |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 84 | SymbolsLock.writer_acquire(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | // Now search the libraries. |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 86 | for (std::vector<void *>::iterator I = OpenedHandles.begin(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | E = OpenedHandles.end(); I != E; ++I) { |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 88 | //lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 89 | void *ptr = dlsym(*I, symbolName); |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 90 | if (ptr) { |
| 91 | SymbolsLock.writer_release(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 92 | return ptr; |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 93 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 94 | } |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 95 | SymbolsLock.writer_release(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 97 | #define EXPLICIT_SYMBOL(SYM) \ |
| 98 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
| 99 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 101 | // important symbols are marked 'private external' which doesn't allow |
| 102 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 103 | // there is only a small handful of them. |
| 104 | |
| 105 | #ifdef __APPLE__ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | { |
| 107 | EXPLICIT_SYMBOL(__ashldi3); |
| 108 | EXPLICIT_SYMBOL(__ashrdi3); |
| 109 | EXPLICIT_SYMBOL(__cmpdi2); |
| 110 | EXPLICIT_SYMBOL(__divdi3); |
| 111 | EXPLICIT_SYMBOL(__eprintf); |
| 112 | EXPLICIT_SYMBOL(__fixdfdi); |
| 113 | EXPLICIT_SYMBOL(__fixsfdi); |
| 114 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 115 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 116 | EXPLICIT_SYMBOL(__floatdidf); |
| 117 | EXPLICIT_SYMBOL(__floatdisf); |
| 118 | EXPLICIT_SYMBOL(__lshrdi3); |
| 119 | EXPLICIT_SYMBOL(__moddi3); |
| 120 | EXPLICIT_SYMBOL(__udivdi3); |
| 121 | EXPLICIT_SYMBOL(__umoddi3); |
| 122 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 125 | #ifdef __CYGWIN__ |
| 126 | { |
| 127 | EXPLICIT_SYMBOL(_alloca); |
Anton Korobeynikov | 71da038 | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 128 | EXPLICIT_SYMBOL(__main); |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 129 | } |
| 130 | #endif |
| 131 | |
| 132 | #undef EXPLICIT_SYMBOL |
| 133 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 134 | // This macro returns the address of a well-known, explicit symbol |
| 135 | #define EXPLICIT_SYMBOL(SYM) \ |
| 136 | if (!strcmp(symbolName, #SYM)) return &SYM |
| 137 | |
| 138 | // On linux we have a weird situation. The stderr/out/in symbols are both |
| 139 | // macros and global variables because of standards requirements. So, we |
| 140 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
| 141 | #if defined(__linux__) |
| 142 | { |
| 143 | EXPLICIT_SYMBOL(stderr); |
| 144 | EXPLICIT_SYMBOL(stdout); |
| 145 | EXPLICIT_SYMBOL(stdin); |
| 146 | } |
| 147 | #else |
| 148 | // For everything else, we want to check to make sure the symbol isn't defined |
| 149 | // as a macro before using EXPLICIT_SYMBOL. |
| 150 | { |
| 151 | #ifndef stdin |
| 152 | EXPLICIT_SYMBOL(stdin); |
| 153 | #endif |
| 154 | #ifndef stdout |
| 155 | EXPLICIT_SYMBOL(stdout); |
| 156 | #endif |
| 157 | #ifndef stderr |
| 158 | EXPLICIT_SYMBOL(stderr); |
| 159 | #endif |
| 160 | } |
| 161 | #endif |
| 162 | #undef EXPLICIT_SYMBOL |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | #endif // LLVM_ON_WIN32 |