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 //===----------------------------------------------------------------------===//
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;
46 static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
51 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
53 if (stricmp(ModuleName, "msvci70") != 0 &&
54 stricmp(ModuleName, "msvcirt") != 0 &&
55 stricmp(ModuleName, "msvcp50") != 0 &&
56 stricmp(ModuleName, "msvcp60") != 0 &&
57 stricmp(ModuleName, "msvcp70") != 0 &&
58 stricmp(ModuleName, "msvcr70") != 0 &&
60 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
61 // Otherwise, user should be aware, what he's doing :)
62 stricmp(ModuleName, "msvcrt") != 0 &&
64 stricmp(ModuleName, "msvcrt20") != 0 &&
65 stricmp(ModuleName, "msvcrt40") != 0) {
66 OpenedHandles->insert((HMODULE)ModuleBase);
72 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
73 std::string *errMsg) {
74 SmartScopedLock<true> lock(*SymbolsMutex);
77 // When no file is specified, enumerate all DLLs and EXEs in the process.
78 if (OpenedHandles == 0)
79 OpenedHandles = new DenseSet<HMODULE>();
81 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
82 // Dummy library that represents "search all handles".
83 // This is mostly to ensure that the return value still shows up as "valid".
84 return DynamicLibrary(&OpenedHandles);
87 SmallVector<wchar_t, MAX_PATH> filenameUnicode;
88 if (error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
89 SetLastError(ec.value());
90 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
91 return DynamicLibrary();
94 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
97 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
98 return DynamicLibrary();
101 if (OpenedHandles == 0)
102 OpenedHandles = new DenseSet<HMODULE>();
104 // If we've already loaded this library, FreeLibrary() the handle in order to
105 // keep the internal refcount at +1.
106 if (!OpenedHandles->insert(a_handle).second)
107 FreeLibrary(a_handle);
109 return DynamicLibrary(a_handle);
112 // Stack probing routines are in the support library (e.g. libgcc), but we don't
113 // have dynamic linking on windows. Provide a hook.
114 #define EXPLICIT_SYMBOL(SYM) \
115 extern "C" { extern void *SYM; }
116 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
118 #include "explicit_symbols.inc"
120 #undef EXPLICIT_SYMBOL
121 #undef EXPLICIT_SYMBOL2
123 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
124 SmartScopedLock<true> Lock(*SymbolsMutex);
126 // First check symbols added via AddSymbol().
127 if (ExplicitSymbols.isConstructed()) {
128 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
130 if (i != ExplicitSymbols->end())
134 // Now search the libraries.
136 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
137 E = OpenedHandles->end(); I != E; ++I) {
138 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
140 return (void *)(intptr_t)ptr;
145 #define EXPLICIT_SYMBOL(SYM) \
146 if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
147 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
148 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
151 #include "explicit_symbols.inc"
154 #undef EXPLICIT_SYMBOL
155 #undef EXPLICIT_SYMBOL2
161 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
164 if (Data == &OpenedHandles)
165 return SearchForAddressOfSymbol(symbolName);
166 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);