Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 1 | //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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. |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jeff Cohen | f365c33 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 10 | // This file provides the Win32 specific implementation of DynamicLibrary. |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 14 | #include "WindowsSupport.h" |
Jeff Cohen | f365c33 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 15 | |
Jeff Cohen | 07e22ba | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 16 | #ifdef __MINGW32__ |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 17 | #include <imagehlp.h> |
Jeff Cohen | f365c33 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 18 | #else |
Reid Spencer | 187b4ad | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 19 | #include <dbghelp.h> |
Jeff Cohen | f365c33 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 20 | #endif |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 21 | |
Stefanus Du Toit | d2b7be6 | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 22 | #ifdef _MSC_VER |
| 23 | #include <ntverp.h> |
| 24 | #endif |
| 25 | |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | using namespace sys; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 30 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 31 | //=== and must not be UNIX code. |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Leny Kholodov | bebb27b | 2015-07-02 14:34:57 +0000 | [diff] [blame] | 34 | typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID); |
| 35 | static fpEnumerateLoadedModules fEnumerateLoadedModules; |
Vassil Vassilev | 8bdc36e | 2017-03-02 14:30:05 +0000 | [diff] [blame] | 36 | static llvm::ManagedStatic<DenseSet<HMODULE> > OpenedHandles; |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 37 | |
Leny Kholodov | bebb27b | 2015-07-02 14:34:57 +0000 | [diff] [blame] | 38 | static bool loadDebugHelp(void) { |
| 39 | HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll"); |
| 40 | if (hLib) { |
| 41 | fEnumerateLoadedModules = (fpEnumerateLoadedModules) |
| 42 | ::GetProcAddress(hLib, "EnumerateLoadedModules64"); |
| 43 | } |
| 44 | return fEnumerateLoadedModules != 0; |
| 45 | } |
| 46 | |
NAKAMURA Takumi | bbae11b | 2014-09-23 01:09:46 +0000 | [diff] [blame] | 47 | static BOOL CALLBACK |
NAKAMURA Takumi | 2de1b32 | 2016-03-07 00:13:09 +0000 | [diff] [blame] | 48 | ELM_Callback(PCSTR ModuleName, DWORD64 ModuleBase, |
Yaron Keren | fb06908 | 2014-09-22 21:40:15 +0000 | [diff] [blame] | 49 | ULONG ModuleSize, PVOID UserContext) { |
| 50 | OpenedHandles->insert((HMODULE)ModuleBase); |
| 51 | return TRUE; |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 54 | DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, |
| 55 | std::string *errMsg) { |
Filip Pizlo | 57093e8 | 2013-09-18 16:40:14 +0000 | [diff] [blame] | 56 | SmartScopedLock<true> lock(*SymbolsMutex); |
Jordy Rose | 5765f4c | 2011-08-22 19:01:52 +0000 | [diff] [blame] | 57 | |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 58 | if (!filename) { |
| 59 | // When no file is specified, enumerate all DLLs and EXEs in the process. |
Davide Italiano | 0540267 | 2015-12-01 05:33:24 +0000 | [diff] [blame] | 60 | if (!fEnumerateLoadedModules) { |
| 61 | if (!loadDebugHelp()) { |
| 62 | assert(false && "These APIs should always be available"); |
| 63 | return DynamicLibrary(); |
| 64 | } |
| 65 | } |
Leny Kholodov | bebb27b | 2015-07-02 14:34:57 +0000 | [diff] [blame] | 66 | |
| 67 | fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 68 | // Dummy library that represents "search all handles". |
| 69 | // This is mostly to ensure that the return value still shows up as "valid". |
| 70 | return DynamicLibrary(&OpenedHandles); |
| 71 | } |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 72 | |
| 73 | SmallVector<wchar_t, MAX_PATH> filenameUnicode; |
Rafael Espindola | adccf86 | 2014-06-12 21:53:57 +0000 | [diff] [blame] | 74 | if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) { |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 75 | SetLastError(ec.value()); |
Paul Robinson | af19bc3 | 2015-11-23 17:34:20 +0000 | [diff] [blame] | 76 | MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16"); |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 77 | return DynamicLibrary(); |
| 78 | } |
NAKAMURA Takumi | 4fb6748 | 2017-02-28 10:15:25 +0000 | [diff] [blame] | 79 | |
David Majnemer | 61eae2e | 2013-10-07 01:00:07 +0000 | [diff] [blame] | 80 | HMODULE a_handle = LoadLibraryW(filenameUnicode.data()); |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 81 | |
| 82 | if (a_handle == 0) { |
Paul Robinson | af19bc3 | 2015-11-23 17:34:20 +0000 | [diff] [blame] | 83 | MakeErrMsg(errMsg, std::string(filename) + ": Can't open"); |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 84 | return DynamicLibrary(); |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 87 | // If we've already loaded this library, FreeLibrary() the handle in order to |
| 88 | // keep the internal refcount at +1. |
NAKAMURA Takumi | 3d369cb | 2017-02-28 10:15:18 +0000 | [diff] [blame] | 89 | if (!OpenedHandles->insert(a_handle).second) |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 90 | FreeLibrary(a_handle); |
| 91 | |
NAKAMURA Takumi | 3d369cb | 2017-02-28 10:15:18 +0000 | [diff] [blame] | 92 | return DynamicLibrary(a_handle); |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Vassil Vassilev | 7f1c255 | 2017-03-02 17:56:45 +0000 | [diff] [blame] | 95 | DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle, |
| 96 | std::string *errMsg) { |
| 97 | SmartScopedLock<true> lock(*SymbolsMutex); |
| 98 | // If we've already loaded this library, tell the caller. |
Vassil Vassilev | 2e7f560 | 2017-03-02 18:12:59 +0000 | [diff] [blame] | 99 | if (!OpenedHandles->insert((HMODULE)handle).second) { |
Vassil Vassilev | 7f1c255 | 2017-03-02 17:56:45 +0000 | [diff] [blame] | 100 | MakeErrMsg(errMsg, "Library already loaded"); |
| 101 | return DynamicLibrary(); |
| 102 | } |
| 103 | |
| 104 | return DynamicLibrary(handle); |
| 105 | } |
| 106 | |
Anton Korobeynikov | 6636210 | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 107 | // Stack probing routines are in the support library (e.g. libgcc), but we don't |
| 108 | // have dynamic linking on windows. Provide a hook. |
NAKAMURA Takumi | 03a541f | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 109 | #define EXPLICIT_SYMBOL(SYM) \ |
| 110 | extern "C" { extern void *SYM; } |
| 111 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO) |
Anton Korobeynikov | 6636210 | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 112 | |
Reid Kleckner | 9aeb047 | 2014-11-13 23:32:52 +0000 | [diff] [blame] | 113 | #ifdef _M_IX86 |
| 114 | // Win32 on x86 implements certain single-precision math functions as macros. |
| 115 | // These functions are not exported by the DLL, but will still be needed |
| 116 | // for symbol-resolution by the JIT loader. Therefore, this Support libray |
| 117 | // provides helper functions with the same implementation. |
| 118 | |
| 119 | #define INLINE_DEF_SYMBOL1(TYP, SYM) \ |
| 120 | extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); } |
| 121 | #define INLINE_DEF_SYMBOL2(TYP, SYM) \ |
| 122 | extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); } |
| 123 | #endif |
| 124 | |
NAKAMURA Takumi | 03a541f | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 125 | #include "explicit_symbols.inc" |
| 126 | |
| 127 | #undef EXPLICIT_SYMBOL |
| 128 | #undef EXPLICIT_SYMBOL2 |
Reid Kleckner | 9aeb047 | 2014-11-13 23:32:52 +0000 | [diff] [blame] | 129 | #undef INLINE_DEF_SYMBOL1 |
| 130 | #undef INLINE_DEF_SYMBOL2 |
Anton Korobeynikov | 6636210 | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 131 | |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 132 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Filip Pizlo | 57093e8 | 2013-09-18 16:40:14 +0000 | [diff] [blame] | 133 | SmartScopedLock<true> Lock(*SymbolsMutex); |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 134 | |
Jeff Cohen | baeb39c | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 135 | // First check symbols added via AddSymbol(). |
Filip Pizlo | 57093e8 | 2013-09-18 16:40:14 +0000 | [diff] [blame] | 136 | if (ExplicitSymbols.isConstructed()) { |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 137 | StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName); |
| 138 | |
| 139 | if (i != ExplicitSymbols->end()) |
| 140 | return i->second; |
Chris Lattner | e4807c5 | 2009-07-08 00:52:12 +0000 | [diff] [blame] | 141 | } |
Jeff Cohen | baeb39c | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 142 | |
| 143 | // Now search the libraries. |
Vassil Vassilev | 8bdc36e | 2017-03-02 14:30:05 +0000 | [diff] [blame] | 144 | if (OpenedHandles.isConstructed()) { |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 145 | for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(), |
| 146 | E = OpenedHandles->end(); I != E; ++I) { |
| 147 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 148 | if (ptr) { |
| 149 | return (void *)(intptr_t)ptr; |
| 150 | } |
Owen Anderson | 021c3b0 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 151 | } |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Reid Kleckner | 9aeb047 | 2014-11-13 23:32:52 +0000 | [diff] [blame] | 154 | #define EXPLICIT_SYMBOL(SYM) \ |
| 155 | if (!strcmp(symbolName, #SYM)) \ |
| 156 | return (void *)&SYM; |
| 157 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 158 | if (!strcmp(symbolName, #SYMFROM)) \ |
| 159 | return (void *)&SYMTO; |
| 160 | |
| 161 | #ifdef _M_IX86 |
| 162 | #define INLINE_DEF_SYMBOL1(TYP, SYM) \ |
| 163 | if (!strcmp(symbolName, #SYM)) \ |
| 164 | return (void *)&inline_##SYM; |
| 165 | #define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM) |
| 166 | #endif |
Anton Korobeynikov | 6636210 | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 167 | |
Anton Korobeynikov | ec9038bc | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 168 | { |
Reid Kleckner | 9aeb047 | 2014-11-13 23:32:52 +0000 | [diff] [blame] | 169 | #include "explicit_symbols.inc" |
Anton Korobeynikov | 105682d | 2010-01-14 20:19:51 +0000 | [diff] [blame] | 170 | } |
NAKAMURA Takumi | 03a541f | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 171 | |
Reid Kleckner | 9aeb047 | 2014-11-13 23:32:52 +0000 | [diff] [blame] | 172 | #undef EXPLICIT_SYMBOL |
| 173 | #undef EXPLICIT_SYMBOL2 |
| 174 | #undef INLINE_DEF_SYMBOL1 |
| 175 | #undef INLINE_DEF_SYMBOL2 |
Anton Korobeynikov | eef04ba | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 176 | |
Jeff Cohen | 039b4ab | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 177 | return 0; |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Jordy Rose | a19917d | 2011-08-17 00:29:32 +0000 | [diff] [blame] | 180 | void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { |
| 181 | if (!isValid()) |
| 182 | return NULL; |
| 183 | if (Data == &OpenedHandles) |
| 184 | return SearchForAddressOfSymbol(symbolName); |
| 185 | return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName); |
| 186 | } |
| 187 | |
Reid Spencer | 4befbf3 | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 188 | } |