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#ifdef _M_IX86 98// Win32 on x86 implements certain single-precision math functions as macros. 99// These functions are not exported by the DLL, but will still be needed 100// for symbol-resolution by the JIT loader. Therefore, this Support libray 101// provides helper functions with the same implementation. 102 103#define INLINE_DEF_SYMBOL1(TYP, SYM) \ 104 extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); } 105#define INLINE_DEF_SYMBOL2(TYP, SYM) \ 106 extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); } 107#endif 108 109#include "explicit_symbols.inc" 110 111#undef EXPLICIT_SYMBOL 112#undef EXPLICIT_SYMBOL2 113#undef INLINE_DEF_SYMBOL1 114#undef INLINE_DEF_SYMBOL2 115 116void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { 117 SmartScopedLock<true> Lock(*SymbolsMutex); 118 119 // First check symbols added via AddSymbol(). 120 if (ExplicitSymbols.isConstructed()) { 121 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName); 122 123 if (i != ExplicitSymbols->end()) 124 return i->second; 125 } 126 127 // Now search the libraries. 128 if (OpenedHandles) { 129 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(), 130 E = OpenedHandles->end(); I != E; ++I) { 131 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); 132 if (ptr) { 133 return (void *)(intptr_t)ptr; 134 } 135 } 136 } 137 138#define EXPLICIT_SYMBOL(SYM) \ 139 if (!strcmp(symbolName, #SYM)) \ 140 return (void *)&SYM; 141#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ 142 if (!strcmp(symbolName, #SYMFROM)) \ 143 return (void *)&SYMTO; 144 145#ifdef _M_IX86 146#define INLINE_DEF_SYMBOL1(TYP, SYM) \ 147 if (!strcmp(symbolName, #SYM)) \ 148 return (void *)&inline_##SYM; 149#define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM) 150#endif 151 152 { 153#include "explicit_symbols.inc" 154 } 155 156#undef EXPLICIT_SYMBOL 157#undef EXPLICIT_SYMBOL2 158#undef INLINE_DEF_SYMBOL1 159#undef INLINE_DEF_SYMBOL2 160 161 return 0; 162} 163 164void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { 165 if (!isValid()) 166 return NULL; 167 if (Data == &OpenedHandles) 168 return SearchForAddressOfSymbol(symbolName); 169 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName); 170} 171 172} 173