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 | // |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 12 | // FIXME: This file leaks ExplicitSymbols and OpenedHandles! |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 13 | // |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/DenseSet.h" |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DynamicLibrary.h" |
| 19 | #include "llvm/Support/Mutex.h" |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 20 | #include "llvm/Config/config.h" |
Duncan Sands | 4520dd2 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Duncan Sands | f52e32a | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 22 | #include <cstring> |
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. |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 25 | static llvm::StringMap<void *> *ExplicitSymbols = 0; |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 26 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | struct ExplicitSymbolsDeleter { |
Torok Edwin | 72ddf7b | 2009-08-31 16:12:29 +0000 | [diff] [blame] | 30 | ~ExplicitSymbolsDeleter() { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 31 | delete ExplicitSymbols; |
Torok Edwin | 72ddf7b | 2009-08-31 16:12:29 +0000 | [diff] [blame] | 32 | } |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | } |
| 36 | |
| 37 | static ExplicitSymbolsDeleter Dummy; |
Torok Edwin | 72ddf7b | 2009-08-31 16:12:29 +0000 | [diff] [blame] | 38 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 39 | |
| 40 | static llvm::sys::SmartMutex<true>& getMutex() { |
| 41 | static llvm::sys::SmartMutex<true> HandlesMutex; |
| 42 | return HandlesMutex; |
| 43 | } |
| 44 | |
| 45 | void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName, |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 46 | void *symbolValue) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 47 | SmartScopedLock<true> lock(getMutex()); |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 48 | if (ExplicitSymbols == 0) |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 49 | ExplicitSymbols = new llvm::StringMap<void*>(); |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 50 | (*ExplicitSymbols)[symbolName] = symbolValue; |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 51 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 52 | |
Jordy Rose | 008a5f5 | 2011-08-17 18:38:42 +0000 | [diff] [blame] | 53 | char llvm::sys::DynamicLibrary::Invalid = 0; |
| 54 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 55 | #ifdef LLVM_ON_WIN32 |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 56 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 57 | #include "Windows/DynamicLibrary.inc" |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 58 | |
| 59 | #else |
| 60 | |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 61 | #if HAVE_DLFCN_H |
Devang Patel | e45252e | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 62 | #include <dlfcn.h> |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 63 | using namespace llvm; |
| 64 | using namespace llvm::sys; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 65 | |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | //=== WARNING: Implementation here must contain only TRULY operating system |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 68 | //=== independent code. |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 71 | static DenseSet<void *> *OpenedHandles = 0; |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 72 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 73 | DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, |
| 74 | std::string *errMsg) { |
Jordy Rose | eeb37f1 | 2011-08-22 19:01:52 +0000 | [diff] [blame] | 75 | SmartScopedLock<true> lock(getMutex()); |
| 76 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 77 | void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL); |
| 78 | if (handle == 0) { |
| 79 | if (errMsg) *errMsg = dlerror(); |
| 80 | return DynamicLibrary(); |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 81 | } |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 6ccd0da | 2010-08-17 15:42:43 +0000 | [diff] [blame] | 83 | #ifdef __CYGWIN__ |
| 84 | // Cygwin searches symbols only in the main |
| 85 | // with the handle of dlopen(NULL, RTLD_GLOBAL). |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 86 | if (filename == NULL) |
| 87 | handle = RTLD_DEFAULT; |
Chris Lattner | 6ccd0da | 2010-08-17 15:42:43 +0000 | [diff] [blame] | 88 | #endif |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 89 | |
Chris Lattner | a9b7d60 | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 90 | if (OpenedHandles == 0) |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 91 | OpenedHandles = new DenseSet<void *>(); |
| 92 | |
| 93 | // If we've already loaded this library, dlclose() the handle in order to |
| 94 | // keep the internal refcount at +1. |
| 95 | if (!OpenedHandles->insert(handle).second) |
| 96 | dlclose(handle); |
| 97 | |
| 98 | return DynamicLibrary(handle); |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 99 | } |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 100 | |
| 101 | void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { |
| 102 | if (!isValid()) |
| 103 | return NULL; |
| 104 | return dlsym(Data, symbolName); |
| 105 | } |
| 106 | |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 107 | #else |
| 108 | |
| 109 | using namespace llvm; |
| 110 | using namespace llvm::sys; |
| 111 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 112 | DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, |
| 113 | std::string *errMsg) { |
| 114 | if (errMsg) *errMsg = "dlopen() not supported on this platform"; |
| 115 | return DynamicLibrary(); |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 116 | } |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 117 | |
| 118 | void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 122 | #endif |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 123 | |
Jeffrey Yasskin | 6220754 | 2010-03-11 06:14:32 +0000 | [diff] [blame] | 124 | namespace llvm { |
| 125 | void *SearchForAddressOfSpecialSymbol(const char* symbolName); |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 128 | void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) { |
| 129 | SmartScopedLock<true> Lock(getMutex()); |
| 130 | |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 131 | // First check symbols added via AddSymbol(). |
| 132 | if (ExplicitSymbols) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 133 | StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName); |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 134 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 135 | if (i != ExplicitSymbols->end()) |
| 136 | return i->second; |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 139 | #if HAVE_DLFCN_H |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 140 | // Now search the libraries. |
| 141 | if (OpenedHandles) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 142 | for (DenseSet<void *>::iterator I = OpenedHandles->begin(), |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 143 | E = OpenedHandles->end(); I != E; ++I) { |
| 144 | //lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 145 | void *ptr = dlsym(*I, symbolName); |
| 146 | if (ptr) { |
| 147 | return ptr; |
| 148 | } |
| 149 | } |
| 150 | } |
Chris Lattner | 21aa347 | 2010-04-09 20:45:04 +0000 | [diff] [blame] | 151 | #endif |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 152 | |
Jeffrey Yasskin | 6220754 | 2010-03-11 06:14:32 +0000 | [diff] [blame] | 153 | if (void *Result = llvm::SearchForAddressOfSpecialSymbol(symbolName)) |
Douglas Gregor | 37b7bae | 2009-12-23 19:12:50 +0000 | [diff] [blame] | 154 | return Result; |
Anton Korobeynikov | 96ea209 | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 155 | |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 156 | // This macro returns the address of a well-known, explicit symbol |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 157 | #define EXPLICIT_SYMBOL(SYM) \ |
| 158 | if (!strcmp(symbolName, #SYM)) return &SYM |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 159 | |
| 160 | // On linux we have a weird situation. The stderr/out/in symbols are both |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 161 | // macros and global variables because of standards requirements. So, we |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 162 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
Evgeniy Stepanov | 68d92bd | 2012-09-04 09:14:45 +0000 | [diff] [blame] | 163 | #if defined(__linux__) and !defined(__ANDROID__) |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 164 | { |
| 165 | EXPLICIT_SYMBOL(stderr); |
| 166 | EXPLICIT_SYMBOL(stdout); |
| 167 | EXPLICIT_SYMBOL(stdin); |
| 168 | } |
| 169 | #else |
| 170 | // For everything else, we want to check to make sure the symbol isn't defined |
| 171 | // as a macro before using EXPLICIT_SYMBOL. |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 172 | { |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 173 | #ifndef stdin |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 174 | EXPLICIT_SYMBOL(stdin); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 175 | #endif |
| 176 | #ifndef stdout |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 177 | EXPLICIT_SYMBOL(stdout); |
Reid Spencer | 81e3954 | 2007-01-19 21:30:39 +0000 | [diff] [blame] | 178 | #endif |
| 179 | #ifndef stderr |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 180 | EXPLICIT_SYMBOL(stderr); |
Reid Spencer | 65de742 | 2007-01-11 00:35:10 +0000 | [diff] [blame] | 181 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 182 | } |
Reid Spencer | 02f20d3 | 2007-01-19 21:41:04 +0000 | [diff] [blame] | 183 | #endif |
Reid Spencer | 11f457a | 2007-01-10 19:50:43 +0000 | [diff] [blame] | 184 | #undef EXPLICIT_SYMBOL |
Chris Lattner | 28dabf7 | 2004-12-03 23:02:42 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 19cd4a9 | 2004-11-29 13:33:28 +0000 | [diff] [blame] | 186 | return 0; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jeff Cohen | a4c9751 | 2004-12-24 16:26:47 +0000 | [diff] [blame] | 189 | #endif // LLVM_ON_WIN32 |