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 | // |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 5 | // This file was developed by Jeff Cohen and is distributed under the |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 40 | extern "C" { |
| 41 | static BOOL CALLBACK ELM_Callback(PSTR ModuleName, |
| 42 | ULONG ModuleBase, |
| 43 | ULONG ModuleSize, |
| 44 | PVOID UserContext) |
| 45 | { |
| 46 | // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded |
| 47 | // into the process. |
| 48 | if (stricmp(ModuleName, "msvci70") != 0 && |
| 49 | stricmp(ModuleName, "msvcirt") != 0 && |
| 50 | stricmp(ModuleName, "msvcp50") != 0 && |
| 51 | stricmp(ModuleName, "msvcp60") != 0 && |
| 52 | stricmp(ModuleName, "msvcp70") != 0 && |
| 53 | stricmp(ModuleName, "msvcr70") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame^] | 54 | #ifndef __MINGW32__ |
| 55 | // Mingw32 uses msvcrt.dll by default. Don't ignore it. |
| 56 | // Otherwise, user should be aware, what he's doing :) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 57 | stricmp(ModuleName, "msvcrt") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame^] | 58 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 59 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 60 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 61 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 62 | } |
| 63 | return TRUE; |
| 64 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 67 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 68 | handle = GetModuleHandle(NULL); |
| 69 | OpenedHandles.push_back((HMODULE)handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 72 | DynamicLibrary::~DynamicLibrary() { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 73 | if (handle == 0) |
| 74 | return; |
| 75 | |
| 76 | // GetModuleHandle() does not increment the ref count, so we must not free |
| 77 | // the handle to the executable. |
| 78 | if (handle != GetModuleHandle(NULL)) |
| 79 | FreeLibrary((HMODULE)handle); |
| 80 | handle = 0; |
| 81 | |
| 82 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 83 | E = OpenedHandles.end(); I != E; ++I) { |
| 84 | if (*I == handle) { |
| 85 | // Note: don't use the swap/pop_back trick here. Order is important. |
| 86 | OpenedHandles.erase(I); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame^] | 91 | #ifdef __MINGW32__ |
| 92 | #define EXPLICIT_SYMBOL(SYM) \ |
| 93 | if (!strcmp(symbolName, #SYM)) return (void*)&SYM |
| 94 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 95 | if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO |
| 96 | #define EXPLICIT_SYMBOL_DEF(SYM) \ |
| 97 | extern "C" { extern void *SYM; } |
| 98 | |
| 99 | EXPLICIT_SYMBOL_DEF(_alloca); |
| 100 | #endif |
| 101 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 102 | bool DynamicLibrary::LoadLibraryPermanently(const char *filename, |
| 103 | std::string *ErrMsg) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 104 | if (filename) { |
| 105 | HMODULE a_handle = LoadLibrary(filename); |
| 106 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 107 | if (a_handle == 0) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 108 | return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : "); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 109 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 110 | OpenedHandles.push_back(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 111 | } else { |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 112 | // When no file is specified, enumerate all DLLs and EXEs in the |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 113 | // process. |
| 114 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 115 | } |
| 116 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 117 | // 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] | 118 | // it is loaded permanently. |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 119 | return false; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 123 | // First check symbols added via AddSymbol(). |
| 124 | std::map<std::string, void *>::iterator I = g_symbols.find(symbolName); |
| 125 | if (I != g_symbols.end()) |
| 126 | return I->second; |
| 127 | |
| 128 | // Now search the libraries. |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 129 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 130 | E = OpenedHandles.end(); I != E; ++I) { |
| 131 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 132 | if (ptr) |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 133 | return (void *) ptr; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame^] | 136 | #ifdef __MINGW32__ |
| 137 | { |
| 138 | EXPLICIT_SYMBOL(_alloca); |
| 139 | EXPLICIT_SYMBOL2(alloca, _alloca); |
| 140 | #undef EXPLICIT_SYMBOL |
| 141 | #undef EXPLICIT_SYMBOL2 |
| 142 | #undef EXPLICIT_SYMBOL_DEF |
| 143 | } |
| 144 | #endif |
| 145 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 146 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 150 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 151 | return (void *) GetProcAddress((HMODULE)handle, symbolName); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } |
| 155 | |