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(getMutex());
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 HMODULE a_handle = LoadLibrary(filename);
90 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
91 return DynamicLibrary();
94 if (OpenedHandles == 0)
95 OpenedHandles = new DenseSet<HMODULE>();
97 // If we've already loaded this library, FreeLibrary() the handle in order to
98 // keep the internal refcount at +1.
99 if (!OpenedHandles->insert(a_handle).second)
100 FreeLibrary(a_handle);
102 return DynamicLibrary(a_handle);
105 // Stack probing routines are in the support library (e.g. libgcc), but we don't
106 // have dynamic linking on windows. Provide a hook.
107 #define EXPLICIT_SYMBOL(SYM) \
108 extern "C" { extern void *SYM; }
109 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
111 #include "explicit_symbols.inc"
113 #undef EXPLICIT_SYMBOL
114 #undef EXPLICIT_SYMBOL2
116 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
117 SmartScopedLock<true> Lock(getMutex());
119 // First check symbols added via AddSymbol().
120 if (ExplicitSymbols) {
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)) return (void*)&SYM;
140 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
141 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
144 #include "explicit_symbols.inc"
147 #undef EXPLICIT_SYMBOL
148 #undef EXPLICIT_SYMBOL2
154 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
157 if (Data == &OpenedHandles)
158 return SearchForAddressOfSymbol(symbolName);
159 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);