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"
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //=== and must not be UNIX code.
31 //===----------------------------------------------------------------------===//
33 typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
34 static fpEnumerateLoadedModules fEnumerateLoadedModules;
35 static llvm::ManagedStatic<DenseSet<HMODULE> > OpenedHandles;
37 static bool loadDebugHelp(void) {
38 HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
40 fEnumerateLoadedModules = (fpEnumerateLoadedModules)
41 ::GetProcAddress(hLib, "EnumerateLoadedModules64");
43 return fEnumerateLoadedModules != 0;
47 ELM_Callback(PCSTR ModuleName, DWORD64 ModuleBase,
48 ULONG ModuleSize, PVOID UserContext) {
49 OpenedHandles->insert((HMODULE)ModuleBase);
54 sys::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 (!fEnumerateLoadedModules) {
61 if (!loadDebugHelp()) {
62 assert(false && "These APIs should always be available");
63 return DynamicLibrary();
67 fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
68 // Dummy library that represents "search all handles".
69 // This is mostly to ensure that the return value still shows up as "valid".
70 return DynamicLibrary(&OpenedHandles);
73 SmallVector<wchar_t, MAX_PATH> filenameUnicode;
74 if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
75 SetLastError(ec.value());
76 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
77 return DynamicLibrary();
80 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
83 MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
84 return DynamicLibrary();
87 // If we've already loaded this library, FreeLibrary() the handle in order to
88 // keep the internal refcount at +1.
89 if (!OpenedHandles->insert(a_handle).second)
90 FreeLibrary(a_handle);
92 return DynamicLibrary(a_handle);
96 sys::DynamicLibrary::addPermanentLibrary(void *handle, std::string *errMsg) {
97 SmartScopedLock<true> lock(*SymbolsMutex);
98 // If we've already loaded this library, tell the caller.
99 if (!OpenedHandles->insert((HMODULE)handle).second) {
100 MakeErrMsg(errMsg, "Library already loaded");
101 return DynamicLibrary();
104 return DynamicLibrary(handle);
107 // Stack probing routines are in the support library (e.g. libgcc), but we don't
108 // have dynamic linking on windows. Provide a hook.
109 #define EXPLICIT_SYMBOL(SYM) \
110 extern "C" { extern void *SYM; }
111 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
114 // Win32 on x86 implements certain single-precision math functions as macros.
115 // These functions are not exported by the DLL, but will still be needed
116 // for symbol-resolution by the JIT loader. Therefore, this Support libray
117 // provides helper functions with the same implementation.
119 #define INLINE_DEF_SYMBOL1(TYP, SYM) \
120 extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
121 #define INLINE_DEF_SYMBOL2(TYP, SYM) \
122 extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
125 #include "explicit_symbols.inc"
127 #undef EXPLICIT_SYMBOL
128 #undef EXPLICIT_SYMBOL2
129 #undef INLINE_DEF_SYMBOL1
130 #undef INLINE_DEF_SYMBOL2
132 void *sys::DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
133 SmartScopedLock<true> Lock(*SymbolsMutex);
135 // First check symbols added via AddSymbol().
136 if (ExplicitSymbols.isConstructed()) {
137 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
139 if (i != ExplicitSymbols->end())
143 // Now search the libraries.
144 if (OpenedHandles.isConstructed()) {
145 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
146 E = OpenedHandles->end(); I != E; ++I) {
147 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
149 return (void *)(intptr_t)ptr;
154 #define EXPLICIT_SYMBOL(SYM) \
155 if (!strcmp(symbolName, #SYM)) \
157 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
158 if (!strcmp(symbolName, #SYMFROM)) \
159 return (void *)&SYMTO;
162 #define INLINE_DEF_SYMBOL1(TYP, SYM) \
163 if (!strcmp(symbolName, #SYM)) \
164 return (void *)&inline_##SYM;
165 #define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
169 #include "explicit_symbols.inc"
172 #undef EXPLICIT_SYMBOL
173 #undef EXPLICIT_SYMBOL2
174 #undef INLINE_DEF_SYMBOL1
175 #undef INLINE_DEF_SYMBOL2
180 void *sys::DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
183 if (Data == &OpenedHandles)
184 return SearchForAddressOfSymbol(symbolName);
185 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);