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"
29 //===----------------------------------------------------------------------===//
30 //=== WARNING: Implementation here must contain only Win32 specific code
31 //=== and must not be UNIX code.
32 //===----------------------------------------------------------------------===//
34 typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
35 static fpEnumerateLoadedModules fEnumerateLoadedModules;
36 static DenseSet<HMODULE> *OpenedHandles;
38 static bool loadDebugHelp(void) {
39 HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
41 fEnumerateLoadedModules = (fpEnumerateLoadedModules)
42 ::GetProcAddress(hLib, "EnumerateLoadedModules64");
44 return fEnumerateLoadedModules != 0;
48 ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, DWORD64 ModuleBase,
49 ULONG ModuleSize, PVOID UserContext) {
50 OpenedHandles->insert((HMODULE)ModuleBase);
54 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
55 std::string *errMsg) {
56 SmartScopedLock<true> lock(*SymbolsMutex);
59 // When no file is specified, enumerate all DLLs and EXEs in the process.
60 if (OpenedHandles == 0)
61 OpenedHandles = new DenseSet<HMODULE>();
63 if (!fEnumerateLoadedModules)
64 assert(loadDebugHelp() && "These APIs should always be available");
66 fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
67 // Dummy library that represents "search all handles".
68 // This is mostly to ensure that the return value still shows up as "valid".
69 return DynamicLibrary(&OpenedHandles);
72 SmallVector<wchar_t, MAX_PATH> filenameUnicode;
73 if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
74 SetLastError(ec.value());
75 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
76 return DynamicLibrary();
79 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
82 MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
83 return DynamicLibrary();
86 if (OpenedHandles == 0)
87 OpenedHandles = new DenseSet<HMODULE>();
89 // If we've already loaded this library, FreeLibrary() the handle in order to
90 // keep the internal refcount at +1.
91 if (!OpenedHandles->insert(a_handle).second)
92 FreeLibrary(a_handle);
94 return DynamicLibrary(a_handle);
97 // Stack probing routines are in the support library (e.g. libgcc), but we don't
98 // have dynamic linking on windows. Provide a hook.
99 #define EXPLICIT_SYMBOL(SYM) \
100 extern "C" { extern void *SYM; }
101 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
104 // Win32 on x86 implements certain single-precision math functions as macros.
105 // These functions are not exported by the DLL, but will still be needed
106 // for symbol-resolution by the JIT loader. Therefore, this Support libray
107 // provides helper functions with the same implementation.
109 #define INLINE_DEF_SYMBOL1(TYP, SYM) \
110 extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
111 #define INLINE_DEF_SYMBOL2(TYP, SYM) \
112 extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
115 #include "explicit_symbols.inc"
117 #undef EXPLICIT_SYMBOL
118 #undef EXPLICIT_SYMBOL2
119 #undef INLINE_DEF_SYMBOL1
120 #undef INLINE_DEF_SYMBOL2
122 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
123 SmartScopedLock<true> Lock(*SymbolsMutex);
125 // First check symbols added via AddSymbol().
126 if (ExplicitSymbols.isConstructed()) {
127 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
129 if (i != ExplicitSymbols->end())
133 // Now search the libraries.
135 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
136 E = OpenedHandles->end(); I != E; ++I) {
137 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
139 return (void *)(intptr_t)ptr;
144 #define EXPLICIT_SYMBOL(SYM) \
145 if (!strcmp(symbolName, #SYM)) \
147 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
148 if (!strcmp(symbolName, #SYMFROM)) \
149 return (void *)&SYMTO;
152 #define INLINE_DEF_SYMBOL1(TYP, SYM) \
153 if (!strcmp(symbolName, #SYM)) \
154 return (void *)&inline_##SYM;
155 #define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
159 #include "explicit_symbols.inc"
162 #undef EXPLICIT_SYMBOL
163 #undef EXPLICIT_SYMBOL2
164 #undef INLINE_DEF_SYMBOL1
165 #undef INLINE_DEF_SYMBOL2
170 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
173 if (Data == &OpenedHandles)
174 return SearchForAddressOfSymbol(symbolName);
175 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);