Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 1 | //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// |
| 2 | // |
| 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. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 10 | // This file provides the Win32 specific implementation of DynamicLibrary. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Win32.h" |
Chris Lattner | 8e5a084 | 2009-07-08 00:29:32 +0000 | [diff] [blame] | 15 | #include "llvm/System/RWMutex.h" |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 16 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 17 | #ifdef __MINGW32__ |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 18 | #include <imagehlp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 19 | #else |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 20 | #include <dbghelp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 21 | #endif |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 22 | |
Stefanus Du Toit | 5a4e11d | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 23 | #ifdef _MSC_VER |
| 24 | #include <ntverp.h> |
| 25 | #endif |
| 26 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 27 | #ifdef __MINGW32__ |
| 28 | #if (HAVE_LIBIMAGEHLP != 1) |
| 29 | #error "libimagehlp.a should be present" |
| 30 | #endif |
| 31 | #else |
| 32 | #pragma comment(lib, "dbghelp.lib") |
| 33 | #endif |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | using namespace sys; |
| 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 40 | //=== and must not be UNIX code. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 43 | static std::vector<HMODULE> OpenedHandles; |
| 44 | |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 45 | #ifdef _WIN64 |
| 46 | typedef DWORD64 ModuleBaseType; |
| 47 | #else |
| 48 | typedef ULONG ModuleBaseType; |
| 49 | #endif |
| 50 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 51 | extern "C" { |
Stefanus Du Toit | 5a4e11d | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 52 | // Use old callback if: |
| 53 | // - Not using Visual Studio |
| 54 | // - Visual Studio 2005 or earlier but only if we are not using the Windows SDK |
| 55 | // or Windows SDK version is older than 6.0 |
| 56 | // Use new callback if: |
| 57 | // - Newer Visual Studio (comes with newer SDK). |
| 58 | // - Visual Studio 2005 with Windows SDK 6.0+ |
| 59 | #if !defined(_MSC_VER) || _MSC_VER < 1500 && (!defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 6000) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 60 | static BOOL CALLBACK ELM_Callback(PSTR ModuleName, |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 61 | ModuleBaseType ModuleBase, |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 62 | ULONG ModuleSize, |
| 63 | PVOID UserContext) |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 64 | #else |
| 65 | static BOOL CALLBACK ELM_Callback(PCSTR ModuleName, |
| 66 | ModuleBaseType ModuleBase, |
| 67 | ULONG ModuleSize, |
| 68 | PVOID UserContext) |
| 69 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 70 | { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 71 | llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock); |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 72 | // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded |
| 73 | // into the process. |
| 74 | if (stricmp(ModuleName, "msvci70") != 0 && |
| 75 | stricmp(ModuleName, "msvcirt") != 0 && |
| 76 | stricmp(ModuleName, "msvcp50") != 0 && |
| 77 | stricmp(ModuleName, "msvcp60") != 0 && |
| 78 | stricmp(ModuleName, "msvcp70") != 0 && |
| 79 | stricmp(ModuleName, "msvcr70") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 80 | #ifndef __MINGW32__ |
| 81 | // Mingw32 uses msvcrt.dll by default. Don't ignore it. |
| 82 | // Otherwise, user should be aware, what he's doing :) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 83 | stricmp(ModuleName, "msvcrt") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 84 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 85 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 86 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 87 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 88 | } |
| 89 | return TRUE; |
| 90 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 93 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 94 | SmartScopedWriter<true> Writer(&SymbolsLock); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 95 | handle = GetModuleHandle(NULL); |
| 96 | OpenedHandles.push_back((HMODULE)handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 99 | DynamicLibrary::~DynamicLibrary() { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 100 | llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 101 | if (handle == 0) |
| 102 | return; |
| 103 | |
| 104 | // GetModuleHandle() does not increment the ref count, so we must not free |
| 105 | // the handle to the executable. |
| 106 | if (handle != GetModuleHandle(NULL)) |
| 107 | FreeLibrary((HMODULE)handle); |
| 108 | handle = 0; |
| 109 | |
| 110 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 111 | E = OpenedHandles.end(); I != E; ++I) { |
| 112 | if (*I == handle) { |
| 113 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 114 | OpenedHandles.erase(I); |
| 115 | } |
| 116 | } |
| 117 | } |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 118 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 119 | bool DynamicLibrary::LoadLibraryPermanently(const char *filename, |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 120 | std::string *ErrMsg) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 121 | if (filename) { |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 122 | llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 123 | HMODULE a_handle = LoadLibrary(filename); |
| 124 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 125 | if (a_handle == 0) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 126 | return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : "); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 127 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 128 | OpenedHandles.push_back(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 129 | } else { |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 130 | // When no file is specified, enumerate all DLLs and EXEs in the |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 131 | // process. |
| 132 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 133 | } |
| 134 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 135 | // Because we don't remember the handle, we will never free it; hence, |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 136 | // it is loaded permanently. |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 137 | return false; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 140 | // Stack probing routines are in the support library (e.g. libgcc), but we don't |
| 141 | // have dynamic linking on windows. Provide a hook. |
| 142 | #if defined(__MINGW32__) || defined (_MSC_VER) |
| 143 | #define EXPLICIT_SYMBOL(SYM) \ |
| 144 | if (!strcmp(symbolName, #SYM)) return (void*)&SYM |
| 145 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 146 | if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO |
| 147 | #define EXPLICIT_SYMBOL_DEF(SYM) \ |
| 148 | extern "C" { extern void *SYM; } |
| 149 | |
| 150 | #if defined(__MINGW32__) |
| 151 | EXPLICIT_SYMBOL_DEF(_alloca); |
| 152 | EXPLICIT_SYMBOL_DEF(__main); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 153 | EXPLICIT_SYMBOL_DEF(__ashldi3); |
| 154 | EXPLICIT_SYMBOL_DEF(__ashrdi3); |
| 155 | EXPLICIT_SYMBOL_DEF(__cmpdi2); |
| 156 | EXPLICIT_SYMBOL_DEF(__divdi3); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 157 | EXPLICIT_SYMBOL_DEF(__fixdfdi); |
| 158 | EXPLICIT_SYMBOL_DEF(__fixsfdi); |
| 159 | EXPLICIT_SYMBOL_DEF(__fixunsdfdi); |
| 160 | EXPLICIT_SYMBOL_DEF(__fixunssfdi); |
| 161 | EXPLICIT_SYMBOL_DEF(__floatdidf); |
| 162 | EXPLICIT_SYMBOL_DEF(__floatdisf); |
| 163 | EXPLICIT_SYMBOL_DEF(__lshrdi3); |
| 164 | EXPLICIT_SYMBOL_DEF(__moddi3); |
| 165 | EXPLICIT_SYMBOL_DEF(__udivdi3); |
| 166 | EXPLICIT_SYMBOL_DEF(__umoddi3); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 167 | #elif defined(_MSC_VER) |
| 168 | EXPLICIT_SYMBOL_DEF(_alloca_probe); |
| 169 | #endif |
| 170 | #endif |
| 171 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 172 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 173 | // First check symbols added via AddSymbol(). |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 174 | SymbolsLock.reader_acquire(); |
| 175 | std::map<std::string, void *>::iterator I = symbols.find(symbolName); |
| 176 | std::map<std::string, void *>::iterator E = symbols.end(); |
| 177 | SymbolsLock.reader_release(); |
| 178 | if (I != E) |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 179 | return I->second; |
| 180 | |
| 181 | // Now search the libraries. |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 182 | SymbolsLock.writer_acquire(); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 183 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 184 | E = OpenedHandles.end(); I != E; ++I) { |
| 185 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 186 | if (ptr) { |
| 187 | SymbolsLock.writer_release(); |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 188 | return (void *) ptr; |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 189 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 190 | } |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 191 | SymbolsLock.writer_release(); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 192 | |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 193 | #if defined(__MINGW32__) |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 194 | { |
| 195 | EXPLICIT_SYMBOL(_alloca); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 196 | EXPLICIT_SYMBOL(__main); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 197 | EXPLICIT_SYMBOL(__ashldi3); |
| 198 | EXPLICIT_SYMBOL(__ashrdi3); |
| 199 | EXPLICIT_SYMBOL(__cmpdi2); |
| 200 | EXPLICIT_SYMBOL(__divdi3); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 201 | EXPLICIT_SYMBOL(__fixdfdi); |
| 202 | EXPLICIT_SYMBOL(__fixsfdi); |
| 203 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 204 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 205 | EXPLICIT_SYMBOL(__floatdidf); |
| 206 | EXPLICIT_SYMBOL(__floatdisf); |
| 207 | EXPLICIT_SYMBOL(__lshrdi3); |
| 208 | EXPLICIT_SYMBOL(__moddi3); |
| 209 | EXPLICIT_SYMBOL(__udivdi3); |
| 210 | EXPLICIT_SYMBOL(__umoddi3); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 211 | |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 212 | EXPLICIT_SYMBOL2(alloca, _alloca); |
| 213 | #undef EXPLICIT_SYMBOL |
| 214 | #undef EXPLICIT_SYMBOL2 |
| 215 | #undef EXPLICIT_SYMBOL_DEF |
| 216 | } |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 217 | #elif defined(_MSC_VER) |
| 218 | { |
| 219 | EXPLICIT_SYMBOL2(alloca, _alloca_probe); |
| 220 | EXPLICIT_SYMBOL2(_alloca, _alloca_probe); |
| 221 | #undef EXPLICIT_SYMBOL |
| 222 | #undef EXPLICIT_SYMBOL2 |
| 223 | #undef EXPLICIT_SYMBOL_DEF |
| 224 | } |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 225 | #endif |
| 226 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 227 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 230 | } |
| 231 | |