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 | // |
Chris Lattner | 4ee451d | 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. |
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" |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include "llvm/System/RWMutex.h" |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 17 | #include "llvm/Config/config.h" |
Duncan Sands | 4520dd2 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 18 | #include <cstdio> |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 19 | #include <cstring> |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | |
| 22 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
Owen Anderson | f37feb9 | 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 | |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 26 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 27 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 28 | void *symbolValue) { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 29 | llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock); |
| 30 | symbols[symbolName] = symbolValue; |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 31 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 32 | |
| 33 | // It is not possible to use ltdl.c on VC++ builds as the terms of its LGPL |
| 34 | // license and special exception would cause all of LLVM to be placed under |
| 35 | // the LGPL. This is because the exception applies only when libtool is |
| 36 | // used, and obviously libtool is not used with Visual Studio. An entirely |
| 37 | // separate implementation is provided in win32/DynamicLibrary.cpp. |
| 38 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 39 | #ifdef LLVM_ON_WIN32 |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 40 | |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 41 | #include "Win32/DynamicLibrary.inc" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 42 | |
| 43 | #else |
| 44 | |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 45 | //#include "ltdl.h" |
| 46 | #include <dlfcn.h> |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 47 | #include <cassert> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 53 | //=== independent code. |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 56 | //static std::vector<lt_dlhandle> OpenedHandles; |
| 57 | static std::vector<void *> OpenedHandles; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 58 | |
Devang Patel | a133417 | 2008-03-13 16:55:34 +0000 | [diff] [blame] | 59 | DynamicLibrary::DynamicLibrary() {} |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 60 | |
| 61 | DynamicLibrary::~DynamicLibrary() { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 62 | SmartScopedWriter<true> Writer(&SymbolsLock); |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 63 | while(!OpenedHandles.empty()) { |
| 64 | void *H = OpenedHandles.back(); OpenedHandles.pop_back(); |
| 65 | dlclose(H); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 69 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 70 | std::string *ErrMsg) { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 71 | SmartScopedWriter<true> Writer(&SymbolsLock); |
Chris Lattner | ae55e04 | 2008-07-10 00:52:20 +0000 | [diff] [blame] | 72 | void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL); |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 73 | if (H == 0) { |
Chris Lattner | d5f1627 | 2008-03-12 00:50:01 +0000 | [diff] [blame] | 74 | if (ErrMsg) |
| 75 | *ErrMsg = dlerror(); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 76 | return true; |
| 77 | } |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 78 | OpenedHandles.push_back(H); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 79 | return false; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 83 | // check_ltdl_initialization(); |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 84 | |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 85 | // First check symbols added via AddSymbol(). |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 86 | SymbolsLock.reader_acquire(); |
| 87 | std::map<std::string, void *>::iterator I = symbols.find(symbolName); |
| 88 | std::map<std::string, void *>::iterator E = symbols.end(); |
| 89 | SymbolsLock.reader_release(); |
| 90 | |
| 91 | if (I != E) |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 92 | return I->second; |
| 93 | |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 94 | SymbolsLock.writer_acquire(); |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 95 | // Now search the libraries. |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 96 | for (std::vector<void *>::iterator I = OpenedHandles.begin(), |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 97 | E = OpenedHandles.end(); I != E; ++I) { |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 98 | //lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 99 | void *ptr = dlsym(*I, symbolName); |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 100 | if (ptr) { |
| 101 | SymbolsLock.writer_release(); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 102 | return ptr; |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 103 | } |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 104 | } |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame^] | 105 | SymbolsLock.writer_release(); |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 106 | |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 107 | #define EXPLICIT_SYMBOL(SYM) \ |
| 108 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
| 109 | |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 110 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 111 | // important symbols are marked 'private external' which doesn't allow |
| 112 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 113 | // there is only a small handful of them. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 115 | #ifdef __APPLE__ |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 116 | { |
Chris Lattner | 368cb8e | 2004-12-04 04:17:20 +0000 | [diff] [blame] | 117 | EXPLICIT_SYMBOL(__ashldi3); |
| 118 | EXPLICIT_SYMBOL(__ashrdi3); |
| 119 | EXPLICIT_SYMBOL(__cmpdi2); |
| 120 | EXPLICIT_SYMBOL(__divdi3); |
| 121 | EXPLICIT_SYMBOL(__eprintf); |
| 122 | EXPLICIT_SYMBOL(__fixdfdi); |
| 123 | EXPLICIT_SYMBOL(__fixsfdi); |
| 124 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 125 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 126 | EXPLICIT_SYMBOL(__floatdidf); |
| 127 | EXPLICIT_SYMBOL(__floatdisf); |
| 128 | EXPLICIT_SYMBOL(__lshrdi3); |
| 129 | EXPLICIT_SYMBOL(__moddi3); |
| 130 | EXPLICIT_SYMBOL(__udivdi3); |
| 131 | EXPLICIT_SYMBOL(__umoddi3); |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 132 | } |
| 133 | #endif |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 134 | |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 135 | #ifdef __CYGWIN__ |
| 136 | { |
| 137 | EXPLICIT_SYMBOL(_alloca); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 138 | EXPLICIT_SYMBOL(__main); |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 139 | } |
| 140 | #endif |
| 141 | |
| 142 | #undef EXPLICIT_SYMBOL |
| 143 | |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 144 | // This macro returns the address of a well-known, explicit symbol |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 145 | #define EXPLICIT_SYMBOL(SYM) \ |
| 146 | if (!strcmp(symbolName, #SYM)) return &SYM |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 147 | |
| 148 | // On linux we have a weird situation. The stderr/out/in symbols are both |
| 149 | // macros and global variables because of standards requirements. So, we |
| 150 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
| 151 | #if defined(__linux__) |
| 152 | { |
| 153 | EXPLICIT_SYMBOL(stderr); |
| 154 | EXPLICIT_SYMBOL(stdout); |
| 155 | EXPLICIT_SYMBOL(stdin); |
| 156 | } |
| 157 | #else |
| 158 | // For everything else, we want to check to make sure the symbol isn't defined |
| 159 | // as a macro before using EXPLICIT_SYMBOL. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 160 | { |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 161 | #ifndef stdin |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 162 | EXPLICIT_SYMBOL(stdin); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 163 | #endif |
| 164 | #ifndef stdout |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 165 | EXPLICIT_SYMBOL(stdout); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 166 | #endif |
| 167 | #ifndef stderr |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 168 | EXPLICIT_SYMBOL(stderr); |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 169 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 170 | } |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 171 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 172 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 173 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 174 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 177 | #endif // LLVM_ON_WIN32 |