Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 1 | //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +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. |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 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 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "Windows.h" |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 15 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 16 | #ifdef __MINGW32__ |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 17 | #include <imagehlp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 18 | #else |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 19 | #include <dbghelp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 20 | #endif |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 21 | |
Stefanus Du Toit | 5a4e11d | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 22 | #ifdef _MSC_VER |
| 23 | #include <ntverp.h> |
| 24 | #endif |
| 25 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 26 | #ifdef __MINGW32__ |
| 27 | #if (HAVE_LIBIMAGEHLP != 1) |
| 28 | #error "libimagehlp.a should be present" |
| 29 | #endif |
| 30 | #else |
| 31 | #pragma comment(lib, "dbghelp.lib") |
| 32 | #endif |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
| 35 | using namespace sys; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 38 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 39 | //=== and must not be UNIX code. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 42 | static DenseSet<HMODULE> *OpenedHandles; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 43 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 44 | extern "C" { |
NAKAMURA Takumi | ad1b453 | 2011-02-09 04:18:12 +0000 | [diff] [blame] | 45 | |
NAKAMURA Takumi | 6073a05 | 2011-05-01 13:29:49 +0000 | [diff] [blame] | 46 | static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, |
| 47 | ULONG_PTR ModuleBase, |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 48 | ULONG ModuleSize, |
| 49 | PVOID UserContext) |
| 50 | { |
| 51 | // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded |
| 52 | // into the process. |
| 53 | if (stricmp(ModuleName, "msvci70") != 0 && |
| 54 | stricmp(ModuleName, "msvcirt") != 0 && |
| 55 | stricmp(ModuleName, "msvcp50") != 0 && |
| 56 | stricmp(ModuleName, "msvcp60") != 0 && |
| 57 | stricmp(ModuleName, "msvcp70") != 0 && |
| 58 | stricmp(ModuleName, "msvcr70") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 59 | #ifndef __MINGW32__ |
| 60 | // Mingw32 uses msvcrt.dll by default. Don't ignore it. |
| 61 | // Otherwise, user should be aware, what he's doing :) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 62 | stricmp(ModuleName, "msvcrt") != 0 && |
Anton Korobeynikov | f4b6d88 | 2010-01-14 20:19:51 +0000 | [diff] [blame] | 63 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 64 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 65 | stricmp(ModuleName, "msvcrt40") != 0) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 66 | OpenedHandles->push_back((HMODULE)ModuleBase); |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 67 | } |
| 68 | return TRUE; |
| 69 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 72 | DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, |
| 73 | std::string *errMsg) { |
| 74 | if (!filename) { |
| 75 | // When no file is specified, enumerate all DLLs and EXEs in the process. |
| 76 | SmartScopedLock<true> lock(getMutex()); |
| 77 | if (OpenedHandles == 0) |
| 78 | OpenedHandles = new DenseSet<HMODULE>(); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 79 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 80 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 81 | // Dummy library that represents "search all handles". |
| 82 | // This is mostly to ensure that the return value still shows up as "valid". |
| 83 | return DynamicLibrary(&OpenedHandles); |
| 84 | } |
| 85 | |
| 86 | HMODULE a_handle = LoadLibrary(filename); |
| 87 | |
| 88 | if (a_handle == 0) { |
| 89 | MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : "); |
| 90 | return DynamicLibrary(); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 93 | SmartScopedLock<true> lock(getMutex()); |
| 94 | if (OpenedHandles == 0) |
| 95 | OpenedHandles = new DenseSet<HMODULE>(); |
| 96 | |
| 97 | // If we've already loaded this library, FreeLibrary() the handle in order to |
| 98 | // keep the internal refcount at +1. |
| 99 | if (!OpenedHandles->insert(a_handle).second) |
| 100 | FreeLibrary(a_handle); |
| 101 | |
| 102 | return DynamicLibrary(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 105 | // Stack probing routines are in the support library (e.g. libgcc), but we don't |
| 106 | // have dynamic linking on windows. Provide a hook. |
NAKAMURA Takumi | 1f6832a | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 107 | #define EXPLICIT_SYMBOL(SYM) \ |
| 108 | extern "C" { extern void *SYM; } |
| 109 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO) |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 110 | |
NAKAMURA Takumi | 1f6832a | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 111 | #include "explicit_symbols.inc" |
| 112 | |
| 113 | #undef EXPLICIT_SYMBOL |
| 114 | #undef EXPLICIT_SYMBOL2 |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 115 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 116 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 117 | SmartScopedLock<true> Lock(getMutex()); |
| 118 | |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 119 | // First check symbols added via AddSymbol(). |
Chris Lattner | 01d9a1b | 2009-07-08 00:52:12 +0000 | [diff] [blame] | 120 | if (ExplicitSymbols) { |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 121 | StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName); |
| 122 | |
| 123 | if (i != ExplicitSymbols->end()) |
| 124 | return i->second; |
Chris Lattner | 01d9a1b | 2009-07-08 00:52:12 +0000 | [diff] [blame] | 125 | } |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 126 | |
| 127 | // Now search the libraries. |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 128 | if (OpenedHandles) { |
| 129 | for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(), |
| 130 | E = OpenedHandles->end(); I != E; ++I) { |
| 131 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 132 | if (ptr) { |
| 133 | return (void *)(intptr_t)ptr; |
| 134 | } |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 135 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 136 | } |
| 137 | |
NAKAMURA Takumi | 1f6832a | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 138 | #define EXPLICIT_SYMBOL(SYM) \ |
| 139 | if (!strcmp(symbolName, #SYM)) return (void*)&SYM; |
| 140 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 141 | if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO; |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 142 | |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 143 | { |
NAKAMURA Takumi | 1f6832a | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 144 | #include "explicit_symbols.inc" |
Anton Korobeynikov | f4b6d88 | 2010-01-14 20:19:51 +0000 | [diff] [blame] | 145 | } |
NAKAMURA Takumi | 1f6832a | 2011-02-05 15:11:53 +0000 | [diff] [blame] | 146 | |
| 147 | #undef EXPLICIT_SYMBOL |
| 148 | #undef EXPLICIT_SYMBOL2 |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 149 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 150 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Jordy Rose | 0bce85f | 2011-08-17 00:29:32 +0000 | [diff] [blame^] | 153 | |
| 154 | void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { |
| 155 | if (!isValid()) |
| 156 | return NULL; |
| 157 | if (Data == &OpenedHandles) |
| 158 | return SearchForAddressOfSymbol(symbolName); |
| 159 | return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName); |
| 160 | } |
| 161 | |
| 162 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 163 | } |