1 //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of DynamicLibrary.
12 //===----------------------------------------------------------------------===//
14 #include "WindowsSupport.h"
27 #if (HAVE_LIBIMAGEHLP != 1)
28 #error "libimagehlp.a should be present"
31 #pragma comment(lib, "dbghelp.lib")
37 //===----------------------------------------------------------------------===//
38 //=== WARNING: Implementation here must contain only Win32 specific code
39 //=== and must not be UNIX code.
40 //===----------------------------------------------------------------------===//
42 static DenseSet<HMODULE> *OpenedHandles;
45 ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, ULONG_PTR ModuleBase,
46 ULONG ModuleSize, PVOID UserContext) {
47 OpenedHandles->insert((HMODULE)ModuleBase);
51 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
52 std::string *errMsg) {
53 SmartScopedLock<true> lock(*SymbolsMutex);
56 // When no file is specified, enumerate all DLLs and EXEs in the process.
57 if (OpenedHandles == 0)
58 OpenedHandles = new DenseSet<HMODULE>();
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);
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();
73 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
76 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
77 return DynamicLibrary();
80 if (OpenedHandles == 0)
81 OpenedHandles = new DenseSet<HMODULE>();
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);
88 return DynamicLibrary(a_handle);
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)
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.
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); }
109 #include "explicit_symbols.inc"
111 #undef EXPLICIT_SYMBOL
112 #undef EXPLICIT_SYMBOL2
113 #undef INLINE_DEF_SYMBOL1
114 #undef INLINE_DEF_SYMBOL2
116 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
117 SmartScopedLock<true> Lock(*SymbolsMutex);
119 // First check symbols added via AddSymbol().
120 if (ExplicitSymbols.isConstructed()) {
121 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
123 if (i != ExplicitSymbols->end())
127 // Now search the libraries.
129 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
130 E = OpenedHandles->end(); I != E; ++I) {
131 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
133 return (void *)(intptr_t)ptr;
138 #define EXPLICIT_SYMBOL(SYM) \
139 if (!strcmp(symbolName, #SYM)) \
141 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
142 if (!strcmp(symbolName, #SYMFROM)) \
143 return (void *)&SYMTO;
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)
153 #include "explicit_symbols.inc"
156 #undef EXPLICIT_SYMBOL
157 #undef EXPLICIT_SYMBOL2
158 #undef INLINE_DEF_SYMBOL1
159 #undef INLINE_DEF_SYMBOL2
164 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
167 if (Data == &OpenedHandles)
168 return SearchForAddressOfSymbol(symbolName);
169 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);