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