1 //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source 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 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
46 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
48 if (stricmp(ModuleName, "msvci70") != 0 &&
49 stricmp(ModuleName, "msvcirt") != 0 &&
50 stricmp(ModuleName, "msvcp50") != 0 &&
51 stricmp(ModuleName, "msvcp60") != 0 &&
52 stricmp(ModuleName, "msvcp70") != 0 &&
53 stricmp(ModuleName, "msvcr70") != 0 &&
54 stricmp(ModuleName, "msvcrt") != 0 &&
55 stricmp(ModuleName, "msvcrt20") != 0 &&
56 stricmp(ModuleName, "msvcrt40") != 0) {
57 OpenedHandles.push_back((HMODULE)ModuleBase);
63 DynamicLibrary::DynamicLibrary() : handle(0) {
64 handle = GetModuleHandle(NULL);
65 OpenedHandles.push_back((HMODULE)handle);
68 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
69 HMODULE a_handle = LoadLibrary(filename);
72 ThrowError(std::string(filename) + ": Can't open : ");
75 OpenedHandles.push_back(a_handle);
78 DynamicLibrary::~DynamicLibrary() {
82 // GetModuleHandle() does not increment the ref count, so we must not free
83 // the handle to the executable.
84 if (handle != GetModuleHandle(NULL))
85 FreeLibrary((HMODULE)handle);
88 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
89 E = OpenedHandles.end(); I != E; ++I) {
91 // Note: don't use the swap/pop_back trick here. Order is important.
92 OpenedHandles.erase(I);
97 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
98 std::string *ErrMsg) {
100 HMODULE a_handle = LoadLibrary(filename);
103 return GetError(std::string(filename) + ": Can't open : ", ErrMsg);
105 OpenedHandles.push_back(a_handle);
107 // When no file is specified, enumerate all DLLs and EXEs in the
109 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
112 // Because we don't remember the handle, we will never free it; hence,
113 // it is loaded permanently.
117 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
118 // First check symbols added via AddSymbol().
119 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
120 if (I != g_symbols.end())
123 // Now search the libraries.
124 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
125 E = OpenedHandles.end(); I != E; ++I) {
126 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
134 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
135 assert(handle != 0 && "Invalid DynamicLibrary handle");
136 return (void *) GetProcAddress((HMODULE)handle, symbolName);