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 //===----------------------------------------------------------------------===//
23 #if (HAVE_LIBIMAGEHLP != 1)
24 #error "libimagehlp.a should be present"
27 #pragma comment(lib, "dbghelp.lib")
33 //===----------------------------------------------------------------------===//
34 //=== WARNING: Implementation here must contain only Win32 specific code
35 //=== and must not be UNIX code.
36 //===----------------------------------------------------------------------===//
38 static std::vector<HMODULE> OpenedHandles;
41 typedef DWORD64 ModuleBaseType;
43 typedef ULONG ModuleBaseType;
47 #if !defined(_MSC_VER) || _MSC_VER < 1500
48 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
49 ModuleBaseType ModuleBase,
53 static BOOL CALLBACK ELM_Callback(PCSTR ModuleName,
54 ModuleBaseType ModuleBase,
59 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
61 if (stricmp(ModuleName, "msvci70") != 0 &&
62 stricmp(ModuleName, "msvcirt") != 0 &&
63 stricmp(ModuleName, "msvcp50") != 0 &&
64 stricmp(ModuleName, "msvcp60") != 0 &&
65 stricmp(ModuleName, "msvcp70") != 0 &&
66 stricmp(ModuleName, "msvcr70") != 0 &&
68 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
69 // Otherwise, user should be aware, what he's doing :)
70 stricmp(ModuleName, "msvcrt") != 0 &&
72 stricmp(ModuleName, "msvcrt20") != 0 &&
73 stricmp(ModuleName, "msvcrt40") != 0) {
74 OpenedHandles.push_back((HMODULE)ModuleBase);
80 DynamicLibrary::DynamicLibrary() : handle(0) {
81 handle = GetModuleHandle(NULL);
82 OpenedHandles.push_back((HMODULE)handle);
85 DynamicLibrary::~DynamicLibrary() {
89 // GetModuleHandle() does not increment the ref count, so we must not free
90 // the handle to the executable.
91 if (handle != GetModuleHandle(NULL))
92 FreeLibrary((HMODULE)handle);
95 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
96 E = OpenedHandles.end(); I != E; ++I) {
98 // Note: don't use the swap/pop_back trick here. Order is important.
99 OpenedHandles.erase(I);
104 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
105 std::string *ErrMsg) {
107 HMODULE a_handle = LoadLibrary(filename);
110 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
112 OpenedHandles.push_back(a_handle);
114 // When no file is specified, enumerate all DLLs and EXEs in the
116 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
119 // Because we don't remember the handle, we will never free it; hence,
120 // it is loaded permanently.
124 // Stack probing routines are in the support library (e.g. libgcc), but we don't
125 // have dynamic linking on windows. Provide a hook.
126 #if defined(__MINGW32__) || defined (_MSC_VER)
127 #define EXPLICIT_SYMBOL(SYM) \
128 if (!strcmp(symbolName, #SYM)) return (void*)&SYM
129 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
130 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO
131 #define EXPLICIT_SYMBOL_DEF(SYM) \
132 extern "C" { extern void *SYM; }
134 #if defined(__MINGW32__)
135 EXPLICIT_SYMBOL_DEF(_alloca);
136 EXPLICIT_SYMBOL_DEF(__main);
137 #elif defined(_MSC_VER)
138 EXPLICIT_SYMBOL_DEF(_alloca_probe);
142 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
143 // First check symbols added via AddSymbol().
144 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
145 if (I != g_symbols.end())
148 // Now search the libraries.
149 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
150 E = OpenedHandles.end(); I != E; ++I) {
151 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
156 #if defined(__MINGW32__)
158 EXPLICIT_SYMBOL(_alloca);
159 EXPLICIT_SYMBOL(__main);
161 EXPLICIT_SYMBOL2(alloca, _alloca);
162 #undef EXPLICIT_SYMBOL
163 #undef EXPLICIT_SYMBOL2
164 #undef EXPLICIT_SYMBOL_DEF
166 #elif defined(_MSC_VER)
168 EXPLICIT_SYMBOL2(alloca, _alloca_probe);
169 EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
170 #undef EXPLICIT_SYMBOL
171 #undef EXPLICIT_SYMBOL2
172 #undef EXPLICIT_SYMBOL_DEF