1//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file provides the Win32 specific implementation of DynamicLibrary. 11// 12//===----------------------------------------------------------------------===// 13 14#include "WindowsSupport.h" 15 16#ifdef __MINGW32__ 17 #include <imagehlp.h> 18#else 19 #include <dbghelp.h> 20#endif 21 22#ifdef _MSC_VER 23 #include <ntverp.h> 24#endif 25 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 33 34namespace llvm { 35using namespace sys; 36 37//===----------------------------------------------------------------------===// 38//=== WARNING: Implementation here must contain only Win32 specific code 39//=== and must not be UNIX code. 40//===----------------------------------------------------------------------===// 41 42static DenseSet<HMODULE> *OpenedHandles; 43 44static BOOL CALLBACK 45ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, ULONG_PTR ModuleBase, 46 ULONG ModuleSize, PVOID UserContext) { 47 OpenedHandles->insert((HMODULE)ModuleBase); 48 return TRUE; 49} 50 51DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, 52 std::string *errMsg) { 53 SmartScopedLock<true> lock(*SymbolsMutex); 54 55 if (!filename) { 56 // When no file is specified, enumerate all DLLs and EXEs in the process. 57 if (OpenedHandles == 0) 58 OpenedHandles = new DenseSet<HMODULE>(); 59 60 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); 61 // Dummy library that represents "search all handles". 62 // This is mostly to ensure that the return value still shows up as "valid". 63 return DynamicLibrary(&OpenedHandles); 64 } 65 66 SmallVector<wchar_t, MAX_PATH> filenameUnicode; 67 if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) { 68 SetLastError(ec.value()); 69 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: "); 70 return DynamicLibrary(); 71 } 72 73 HMODULE a_handle = LoadLibraryW(filenameUnicode.data()); 74 75 if (a_handle == 0) { 76 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : "); 77 return DynamicLibrary(); 78 } 79 80 if (OpenedHandles == 0) 81 OpenedHandles = new DenseSet<HMODULE>(); 82 83 // If we've already loaded this library, FreeLibrary() the handle in order to 84 // keep the internal refcount at +1. 85 if (!OpenedHandles->insert(a_handle).second) 86 FreeLibrary(a_handle); 87 88 return DynamicLibrary(a_handle); 89} 90 91// Stack probing routines are in the support library (e.g. libgcc), but we don't 92// have dynamic linking on windows. Provide a hook. 93#define EXPLICIT_SYMBOL(SYM) \ 94 extern "C" { extern void *SYM; } 95#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO) 96 97#include "explicit_symbols.inc" 98 99#undef EXPLICIT_SYMBOL 100#undef EXPLICIT_SYMBOL2 101 102void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { 103 SmartScopedLock<true> Lock(*SymbolsMutex); 104 105 // First check symbols added via AddSymbol(). 106 if (ExplicitSymbols.isConstructed()) { 107 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName); 108 109 if (i != ExplicitSymbols->end()) 110 return i->second; 111 } 112 113 // Now search the libraries. 114 if (OpenedHandles) { 115 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(), 116 E = OpenedHandles->end(); I != E; ++I) { 117 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); 118 if (ptr) { 119 return (void *)(intptr_t)ptr; 120 } 121 } 122 } 123 124 #define EXPLICIT_SYMBOL(SYM) \ 125 if (!strcmp(symbolName, #SYM)) return (void*)&SYM; 126 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ 127 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO; 128 129 { 130 #include "explicit_symbols.inc" 131 } 132 133 #undef EXPLICIT_SYMBOL 134 #undef EXPLICIT_SYMBOL2 135 136 return 0; 137} 138 139 140void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { 141 if (!isValid()) 142 return NULL; 143 if (Data == &OpenedHandles) 144 return SearchForAddressOfSymbol(symbolName); 145 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName); 146} 147 148 149} 150