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__ |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 17 | #include <imagehlp.h> |
| 18 | #else |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +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 | |
| 22 | #pragma comment(lib, "dbghelp.lib") |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | using namespace sys; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 29 | //=== and must not be UNIX code. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 32 | static std::vector<HMODULE> OpenedHandles; |
| 33 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 34 | extern "C" { |
| 35 | static BOOL CALLBACK ELM_Callback(PSTR ModuleName, |
| 36 | ULONG ModuleBase, |
| 37 | ULONG ModuleSize, |
| 38 | PVOID UserContext) |
| 39 | { |
| 40 | // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded |
| 41 | // into the process. |
| 42 | if (stricmp(ModuleName, "msvci70") != 0 && |
| 43 | stricmp(ModuleName, "msvcirt") != 0 && |
| 44 | stricmp(ModuleName, "msvcp50") != 0 && |
| 45 | stricmp(ModuleName, "msvcp60") != 0 && |
| 46 | stricmp(ModuleName, "msvcp70") != 0 && |
| 47 | stricmp(ModuleName, "msvcr70") != 0 && |
| 48 | stricmp(ModuleName, "msvcrt") != 0 && |
| 49 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 50 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 51 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 52 | } |
| 53 | return TRUE; |
| 54 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 57 | DynamicLibrary::DynamicLibrary() : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 58 | handle = GetModuleHandle(NULL); |
| 59 | OpenedHandles.push_back((HMODULE)handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 63 | HMODULE a_handle = LoadLibrary(filename); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 64 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 65 | if (a_handle == 0) |
| 66 | ThrowError(std::string(filename) + ": Can't open : "); |
| 67 | |
| 68 | handle = a_handle; |
| 69 | OpenedHandles.push_back(a_handle); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 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 | |
| 91 | void DynamicLibrary::LoadLibraryPermanently(const char* filename) { |
| 92 | if (filename) { |
| 93 | HMODULE a_handle = LoadLibrary(filename); |
| 94 | |
| 95 | if (a_handle == 0) |
| 96 | ThrowError(std::string(filename) + ": Can't open : "); |
| 97 | |
| 98 | OpenedHandles.push_back(a_handle); |
| 99 | } else { |
| 100 | // When no file is specified, enumerate all DLLs and EXEs in the |
| 101 | // process. |
| 102 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 103 | } |
| 104 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 105 | // 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] | 106 | // it is loaded permanently. |
| 107 | } |
| 108 | |
| 109 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
| 110 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 111 | E = OpenedHandles.end(); I != E; ++I) { |
| 112 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
| 113 | if (ptr) |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 114 | return (void *) ptr; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 121 | assert(handle != 0 && "Invalid DynamicLibrary handle"); |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 122 | return (void *) GetProcAddress((HMODULE)handle, symbolName); |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } |
| 126 | |
| 127 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |