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 && |
| 54 | stricmp(ModuleName, "msvcrt") != 0 && |
| 55 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 56 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 57 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 58 | } |
| 59 | return TRUE; |
| 60 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 63 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 64 | handle = GetModuleHandle(NULL); |
| 65 | OpenedHandles.push_back((HMODULE)handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 69 | HMODULE a_handle = LoadLibrary(filename); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 70 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 71 | if (a_handle == 0) |
| 72 | ThrowError(std::string(filename) + ": Can't open : "); |
| 73 | |
| 74 | handle = a_handle; |
| 75 | OpenedHandles.push_back(a_handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 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 | |
| 97 | void DynamicLibrary::LoadLibraryPermanently(const char* filename) { |
| 98 | if (filename) { |
| 99 | HMODULE a_handle = LoadLibrary(filename); |
| 100 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 101 | if (a_handle == 0) |
| 102 | ThrowError(std::string(filename) + ": Can't open : "); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 103 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 104 | OpenedHandles.push_back(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 105 | } else { |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 106 | // When no file is specified, enumerate all DLLs and EXEs in the |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 107 | // process. |
| 108 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 109 | } |
| 110 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 111 | // 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] | 112 | // it is loaded permanently. |
| 113 | } |
| 114 | |
| 115 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 116 | // First check symbols added via AddSymbol(). |
| 117 | std::map<std::string, void *>::iterator I = g_symbols.find(symbolName); |
| 118 | if (I != g_symbols.end()) |
| 119 | return I->second; |
| 120 | |
| 121 | // Now search the libraries. |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 122 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 123 | E = OpenedHandles.end(); I != E; ++I) { |
| 124 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 125 | if (ptr) |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 126 | return (void *) ptr; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 133 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 134 | return (void *) GetProcAddress((HMODULE)handle, symbolName); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |
| 138 | |