For PR797:
[oota-llvm.git] / lib / System / Win32 / DynamicLibrary.inc
1 //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides the Win32 specific implementation of DynamicLibrary.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Win32.h"
15
16 #ifdef __MINGW32__
17  #include <imagehlp.h>
18 #else
19  #include <dbghelp.h>
20 #endif
21
22 #ifdef __MINGW32__
23  #if (HAVE_LIBIMAGEHLP != 1)
24   #error "libimagehlp.a should be present"
25  #endif
26 #else
27  #pragma comment(lib, "dbghelp.lib")
28 #endif
29
30 namespace llvm {
31 using namespace sys;
32
33 //===----------------------------------------------------------------------===//
34 //=== WARNING: Implementation here must contain only Win32 specific code 
35 //===          and must not be UNIX code.
36 //===----------------------------------------------------------------------===//
37
38 static std::vector<HMODULE> OpenedHandles;
39
40 extern "C" {
41   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
42                                     ULONG ModuleBase,
43                                     ULONG ModuleSize,
44                                     PVOID UserContext)
45   {
46     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
47     // into the process.
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);
58     }
59     return TRUE;
60   }
61 }
62
63 DynamicLibrary::DynamicLibrary() : handle(0) {
64   handle = GetModuleHandle(NULL);
65   OpenedHandles.push_back((HMODULE)handle);
66 }
67
68 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
69   HMODULE a_handle = LoadLibrary(filename);
70
71   if (a_handle == 0)
72     ThrowError(std::string(filename) + ": Can't open : ");
73
74   handle = a_handle;
75   OpenedHandles.push_back(a_handle);
76 }
77
78 DynamicLibrary::~DynamicLibrary() {
79   if (handle == 0)
80     return;
81
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);
86   handle = 0;
87
88   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
89        E = OpenedHandles.end(); I != E; ++I) {
90     if (*I == handle) {
91       // Note: don't use the swap/pop_back trick here. Order is important.
92       OpenedHandles.erase(I);
93     }
94   }
95 }
96
97 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
98                                             std::string *ErrMsg) {
99   if (filename) {
100     HMODULE a_handle = LoadLibrary(filename);
101
102     if (a_handle == 0)
103       return GetError(std::string(filename) + ": Can't open : ", ErrMsg);
104
105     OpenedHandles.push_back(a_handle);
106   } else {
107     // When no file is specified, enumerate all DLLs and EXEs in the
108     // process.
109     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
110   }
111
112   // Because we don't remember the handle, we will never free it; hence,
113   // it is loaded permanently.
114   return false;
115 }
116
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())
121     return I->second;
122
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);
127     if (ptr)
128       return (void *) ptr;
129   }
130
131   return 0;
132 }
133
134 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
135   assert(handle != 0 && "Invalid DynamicLibrary handle");
136   return (void *) GetProcAddress((HMODULE)handle, symbolName);
137 }
138
139 }
140