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 | // |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 12 | // FIXME: This file leaks the ExplicitSymbols and OpenedHandles vector, and is |
| 13 | // not thread safe! |
| 14 | // |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/System/DynamicLibrary.h" |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" |
Duncan Sands | 4520dd2 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 20 | #include <cstring> |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 21 | #include <map> |
Chris Lattner | 72501b4 | 2009-07-07 18:01:58 +0000 | [diff] [blame] | 22 | #include <vector> |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 23 | |
| 24 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 25 | static std::map<std::string, void*> *ExplicitSymbols = 0; |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 26 | |
Torok Edwin | 72ddf7b | 2009-08-31 16:12:29 +0000 | [diff] [blame] | 27 | static struct ExplicitSymbolsDeleter { |
| 28 | ~ExplicitSymbolsDeleter() { |
| 29 | if (ExplicitSymbols) |
| 30 | delete ExplicitSymbols; |
| 31 | } |
| 32 | } Dummy; |
| 33 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 34 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 35 | void *symbolValue) { |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 36 | if (ExplicitSymbols == 0) |
| 37 | ExplicitSymbols = new std::map<std::string, void*>(); |
| 38 | (*ExplicitSymbols)[symbolName] = symbolValue; |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 39 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 40 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 41 | #ifdef LLVM_ON_WIN32 |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 42 | |
Reid Spencer | bccc8ab | 2005-01-09 23:29:00 +0000 | [diff] [blame] | 43 | #include "Win32/DynamicLibrary.inc" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 44 | |
| 45 | #else |
| 46 | |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 47 | #include <dlfcn.h> |
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 | |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 56 | static std::vector<void *> *OpenedHandles = 0; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 58 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 59 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 60 | std::string *ErrMsg) { |
Chris Lattner | ae55e04 | 2008-07-10 00:52:20 +0000 | [diff] [blame] | 61 | void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL); |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 62 | if (H == 0) { |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 63 | if (ErrMsg) *ErrMsg = dlerror(); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 66 | if (OpenedHandles == 0) |
| 67 | OpenedHandles = new std::vector<void *>(); |
| 68 | OpenedHandles->push_back(H); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 69 | return false; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame^] | 72 | static void *SearchForAddressOfSpecialSymbol(const char* symbolName) { |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 73 | #define EXPLICIT_SYMBOL(SYM) \ |
Douglas Gregor | 0b64472 | 2009-12-23 19:04:10 +0000 | [diff] [blame] | 74 | extern void *SYM; if (!strcmp(symbolName, #SYM)) return &SYM |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 76 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 77 | // important symbols are marked 'private external' which doesn't allow |
| 78 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 79 | // there is only a small handful of them. |
Douglas Gregor | 0b64472 | 2009-12-23 19:04:10 +0000 | [diff] [blame] | 80 | |
| 81 | #ifdef __APPLE__ |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 82 | { |
Douglas Gregor | 0b64472 | 2009-12-23 19:04:10 +0000 | [diff] [blame] | 83 | EXPLICIT_SYMBOL(__ashldi3); |
| 84 | EXPLICIT_SYMBOL(__ashrdi3); |
| 85 | EXPLICIT_SYMBOL(__cmpdi2); |
| 86 | EXPLICIT_SYMBOL(__divdi3); |
| 87 | EXPLICIT_SYMBOL(__eprintf); |
| 88 | EXPLICIT_SYMBOL(__fixdfdi); |
| 89 | EXPLICIT_SYMBOL(__fixsfdi); |
| 90 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 91 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 92 | EXPLICIT_SYMBOL(__floatdidf); |
| 93 | EXPLICIT_SYMBOL(__floatdisf); |
| 94 | EXPLICIT_SYMBOL(__lshrdi3); |
| 95 | EXPLICIT_SYMBOL(__moddi3); |
| 96 | EXPLICIT_SYMBOL(__udivdi3); |
| 97 | EXPLICIT_SYMBOL(__umoddi3); |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 98 | } |
Douglas Gregor | 0b64472 | 2009-12-23 19:04:10 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | #ifdef __CYGWIN__ |
| 102 | { |
| 103 | EXPLICIT_SYMBOL(_alloca); |
| 104 | EXPLICIT_SYMBOL(__main); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #undef EXPLICIT_SYMBOL |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame^] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 113 | // First check symbols added via AddSymbol(). |
| 114 | if (ExplicitSymbols) { |
| 115 | std::map<std::string, void *>::iterator I = |
| 116 | ExplicitSymbols->find(symbolName); |
| 117 | std::map<std::string, void *>::iterator E = ExplicitSymbols->end(); |
| 118 | |
| 119 | if (I != E) |
| 120 | return I->second; |
| 121 | } |
| 122 | |
| 123 | // Now search the libraries. |
| 124 | if (OpenedHandles) { |
| 125 | for (std::vector<void *>::iterator I = OpenedHandles->begin(), |
| 126 | E = OpenedHandles->end(); I != E; ++I) { |
| 127 | //lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 128 | void *ptr = dlsym(*I, symbolName); |
| 129 | if (ptr) { |
| 130 | return ptr; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (void *Result = SearchForAddressOfSpecialSymbol(symbolName)) |
| 136 | return Result; |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 137 | |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 138 | // This macro returns the address of a well-known, explicit symbol |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 139 | #define EXPLICIT_SYMBOL(SYM) \ |
| 140 | if (!strcmp(symbolName, #SYM)) return &SYM |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 141 | |
| 142 | // On linux we have a weird situation. The stderr/out/in symbols are both |
| 143 | // macros and global variables because of standards requirements. So, we |
| 144 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
| 145 | #if defined(__linux__) |
| 146 | { |
| 147 | EXPLICIT_SYMBOL(stderr); |
| 148 | EXPLICIT_SYMBOL(stdout); |
| 149 | EXPLICIT_SYMBOL(stdin); |
| 150 | } |
| 151 | #else |
| 152 | // For everything else, we want to check to make sure the symbol isn't defined |
| 153 | // as a macro before using EXPLICIT_SYMBOL. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 154 | { |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 155 | #ifndef stdin |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 156 | EXPLICIT_SYMBOL(stdin); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 157 | #endif |
| 158 | #ifndef stdout |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 159 | EXPLICIT_SYMBOL(stdout); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 160 | #endif |
| 161 | #ifndef stderr |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 162 | EXPLICIT_SYMBOL(stderr); |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 163 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 164 | } |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 165 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 166 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 167 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 168 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 171 | #endif // LLVM_ON_WIN32 |