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 | // |
Chris Lattner | 71ac96d | 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 | // |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/System/DynamicLibrary.h" |
| 18 | #include "llvm/Config/config.h" |
Duncan Sands | 05f6837 | 2008-10-08 07:23:46 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Duncan Sands | fca2014 | 2008-01-09 19:42:09 +0000 | [diff] [blame] | 20 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include <map> |
Chris Lattner | 0d4e129 | 2009-07-07 18:01:58 +0000 | [diff] [blame] | 22 | #include <vector> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | |
| 24 | // Collection of symbol name/value pairs to be searched prior to any libraries. |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 25 | static std::map<std::string, void*> *ExplicitSymbols = 0; |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 26 | |
Edwin Török | 6d5f43b | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | void llvm::sys::DynamicLibrary::AddSymbol(const char* symbolName, |
| 35 | void *symbolValue) { |
Chris Lattner | 71ac96d | 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; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 41 | #ifdef LLVM_ON_WIN32 |
| 42 | |
| 43 | #include "Win32/DynamicLibrary.inc" |
| 44 | |
| 45 | #else |
| 46 | |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 47 | #include <dlfcn.h> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | using namespace llvm::sys; |
| 50 | |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 53 | //=== independent code. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 56 | static std::vector<void *> *OpenedHandles = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | |
| 59 | bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, |
| 60 | std::string *ErrMsg) { |
Chris Lattner | 859cf63 | 2008-07-10 00:52:20 +0000 | [diff] [blame] | 61 | void *H = dlopen(Filename, RTLD_LAZY|RTLD_GLOBAL); |
Devang Patel | aa4ea5a | 2008-02-13 17:11:39 +0000 | [diff] [blame] | 62 | if (H == 0) { |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 63 | if (ErrMsg) *ErrMsg = dlerror(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 66 | if (OpenedHandles == 0) |
| 67 | OpenedHandles = new std::vector<void *>(); |
| 68 | OpenedHandles->push_back(H); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Douglas Gregor | 16cd8cd | 2009-12-23 18:56:27 +0000 | [diff] [blame^] | 72 | #define EXPLICIT_SYMBOL(SYM) \ |
| 73 | extern "C" void *SYM; |
| 74 | #include "DynamicLibrarySymbolDefs.def" |
| 75 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | // First check symbols added via AddSymbol(). |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 78 | if (ExplicitSymbols) { |
| 79 | std::map<std::string, void *>::iterator I = |
| 80 | ExplicitSymbols->find(symbolName); |
| 81 | std::map<std::string, void *>::iterator E = ExplicitSymbols->end(); |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 83 | if (I != E) |
| 84 | return I->second; |
| 85 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | |
| 87 | // Now search the libraries. |
Chris Lattner | 71ac96d | 2009-07-07 18:17:07 +0000 | [diff] [blame] | 88 | if (OpenedHandles) { |
| 89 | for (std::vector<void *>::iterator I = OpenedHandles->begin(), |
| 90 | E = OpenedHandles->end(); I != E; ++I) { |
| 91 | //lt_ptr ptr = lt_dlsym(*I, symbolName); |
| 92 | void *ptr = dlsym(*I, symbolName); |
| 93 | if (ptr) { |
| 94 | return ptr; |
| 95 | } |
Owen Anderson | 132a2f2 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 96 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 99 | #define EXPLICIT_SYMBOL(SYM) \ |
Douglas Gregor | 16cd8cd | 2009-12-23 18:56:27 +0000 | [diff] [blame^] | 100 | if (!strcmp(symbolName, #SYM)) return &SYM; |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 101 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | // If this is darwin, it has some funky issues, try to solve them here. Some |
| 103 | // important symbols are marked 'private external' which doesn't allow |
| 104 | // SearchForAddressOfSymbol to find them. As such, we special case them here, |
| 105 | // there is only a small handful of them. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | { |
Douglas Gregor | 16cd8cd | 2009-12-23 18:56:27 +0000 | [diff] [blame^] | 107 | #include "DynamicLibrarySymbolDefs.def" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 108 | } |
Anton Korobeynikov | 942cbab | 2007-12-03 05:30:41 +0000 | [diff] [blame] | 109 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | // This macro returns the address of a well-known, explicit symbol |
| 111 | #define EXPLICIT_SYMBOL(SYM) \ |
| 112 | if (!strcmp(symbolName, #SYM)) return &SYM |
| 113 | |
| 114 | // On linux we have a weird situation. The stderr/out/in symbols are both |
| 115 | // macros and global variables because of standards requirements. So, we |
| 116 | // boldly use the EXPLICIT_SYMBOL macro without checking for a #define first. |
| 117 | #if defined(__linux__) |
| 118 | { |
| 119 | EXPLICIT_SYMBOL(stderr); |
| 120 | EXPLICIT_SYMBOL(stdout); |
| 121 | EXPLICIT_SYMBOL(stdin); |
| 122 | } |
| 123 | #else |
| 124 | // For everything else, we want to check to make sure the symbol isn't defined |
| 125 | // as a macro before using EXPLICIT_SYMBOL. |
| 126 | { |
| 127 | #ifndef stdin |
| 128 | EXPLICIT_SYMBOL(stdin); |
| 129 | #endif |
| 130 | #ifndef stdout |
| 131 | EXPLICIT_SYMBOL(stdout); |
| 132 | #endif |
| 133 | #ifndef stderr |
| 134 | EXPLICIT_SYMBOL(stderr); |
| 135 | #endif |
| 136 | } |
| 137 | #endif |
| 138 | #undef EXPLICIT_SYMBOL |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | #endif // LLVM_ON_WIN32 |