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" |
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 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 22 | #ifdef __MINGW32__ |
| 23 | #if (HAVE_LIBIMAGEHLP != 1) |
| 24 | #error "libimagehlp.a should be present" |
| 25 | #endif |
| 26 | #else |
| 27 | #pragma comment(lib, "dbghelp.lib") |
| 28 | #endif |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
| 31 | using namespace sys; |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 35 | //=== and must not be UNIX code. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 38 | static std::vector<HMODULE> OpenedHandles; |
| 39 | |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 40 | #ifdef _WIN64 |
| 41 | typedef DWORD64 ModuleBaseType; |
| 42 | #else |
| 43 | typedef ULONG ModuleBaseType; |
| 44 | #endif |
| 45 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 46 | extern "C" { |
| 47 | static BOOL CALLBACK ELM_Callback(PSTR ModuleName, |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 48 | ModuleBaseType ModuleBase, |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 49 | ULONG ModuleSize, |
| 50 | PVOID UserContext) |
| 51 | { |
| 52 | // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded |
| 53 | // into the process. |
| 54 | if (stricmp(ModuleName, "msvci70") != 0 && |
| 55 | stricmp(ModuleName, "msvcirt") != 0 && |
| 56 | stricmp(ModuleName, "msvcp50") != 0 && |
| 57 | stricmp(ModuleName, "msvcp60") != 0 && |
| 58 | stricmp(ModuleName, "msvcp70") != 0 && |
| 59 | stricmp(ModuleName, "msvcr70") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 60 | #ifndef __MINGW32__ |
| 61 | // Mingw32 uses msvcrt.dll by default. Don't ignore it. |
| 62 | // Otherwise, user should be aware, what he's doing :) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 63 | stricmp(ModuleName, "msvcrt") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 64 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 65 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 66 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 67 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 68 | } |
| 69 | return TRUE; |
| 70 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 73 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 74 | handle = GetModuleHandle(NULL); |
| 75 | OpenedHandles.push_back((HMODULE)handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 78 | DynamicLibrary::~DynamicLibrary() { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 79 | if (handle == 0) |
| 80 | return; |
| 81 | |
| 82 | // GetModuleHandle() does not increment the ref count, so we must not free |
| 83 | // the handle to the executable. |
| 84 | if (handle != GetModuleHandle(NULL)) |
| 85 | FreeLibrary((HMODULE)handle); |
| 86 | handle = 0; |
| 87 | |
| 88 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 89 | E = OpenedHandles.end(); I != E; ++I) { |
| 90 | if (*I == handle) { |
| 91 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 92 | OpenedHandles.erase(I); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 97 | // Stack probing routines are in the support library (e.g. libgcc), but we don't |
| 98 | // have dynamic linking on windows. Provide a hook. |
| 99 | #if defined(__MINGW32__) || defined (_MSC_VER) |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 100 | #define EXPLICIT_SYMBOL(SYM) \ |
| 101 | if (!strcmp(symbolName, #SYM)) return (void*)&SYM |
| 102 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 103 | if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO |
| 104 | #define EXPLICIT_SYMBOL_DEF(SYM) \ |
| 105 | extern "C" { extern void *SYM; } |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 106 | |
| 107 | #if defined(__MINGW32__) |
| 108 | EXPLICIT_SYMBOL_DEF(_alloca); |
| 109 | #elif defined(_MSC_VER) |
| 110 | EXPLICIT_SYMBOL_DEF(_alloca_probe); |
| 111 | #endif |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 112 | #endif |
| 113 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 114 | bool DynamicLibrary::LoadLibraryPermanently(const char *filename, |
| 115 | std::string *ErrMsg) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 116 | if (filename) { |
| 117 | HMODULE a_handle = LoadLibrary(filename); |
| 118 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 119 | if (a_handle == 0) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 120 | return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : "); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 121 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 122 | OpenedHandles.push_back(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 123 | } else { |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 124 | // When no file is specified, enumerate all DLLs and EXEs in the |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 125 | // process. |
| 126 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 127 | } |
| 128 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 129 | // 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] | 130 | // it is loaded permanently. |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 131 | return false; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 135 | // First check symbols added via AddSymbol(). |
| 136 | std::map<std::string, void *>::iterator I = g_symbols.find(symbolName); |
| 137 | if (I != g_symbols.end()) |
| 138 | return I->second; |
| 139 | |
| 140 | // Now search the libraries. |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 141 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 142 | E = OpenedHandles.end(); I != E; ++I) { |
| 143 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 144 | if (ptr) |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 145 | return (void *) ptr; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 148 | #if defined(__MINGW32__) |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 149 | { |
| 150 | EXPLICIT_SYMBOL(_alloca); |
| 151 | EXPLICIT_SYMBOL2(alloca, _alloca); |
| 152 | #undef EXPLICIT_SYMBOL |
| 153 | #undef EXPLICIT_SYMBOL2 |
| 154 | #undef EXPLICIT_SYMBOL_DEF |
| 155 | } |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 156 | #elif defined(_MSC_VER) |
| 157 | { |
| 158 | EXPLICIT_SYMBOL2(alloca, _alloca_probe); |
| 159 | EXPLICIT_SYMBOL2(_alloca, _alloca_probe); |
| 160 | #undef EXPLICIT_SYMBOL |
| 161 | #undef EXPLICIT_SYMBOL2 |
| 162 | #undef EXPLICIT_SYMBOL_DEF |
| 163 | } |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 166 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 170 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 171 | return (void *) GetProcAddress((HMODULE)handle, symbolName); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } |
| 175 | |