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