2209e592091d8b313ebdcd96ffb05343e33bc173
[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 #pragma comment(lib, "dbghelp.lib")
23
24 namespace llvm {
25 using namespace sys;
26
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only Win32 specific code 
29 //===          and must not be UNIX code.
30 //===----------------------------------------------------------------------===//
31
32 static std::vector<HMODULE> OpenedHandles;
33
34 extern "C" {
35   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
36                                     ULONG ModuleBase,
37                                     ULONG ModuleSize,
38                                     PVOID UserContext)
39   {
40     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
41     // into the process.
42     if (stricmp(ModuleName, "msvci70") != 0 &&
43         stricmp(ModuleName, "msvcirt") != 0 &&
44         stricmp(ModuleName, "msvcp50") != 0 &&
45         stricmp(ModuleName, "msvcp60") != 0 &&
46         stricmp(ModuleName, "msvcp70") != 0 &&
47         stricmp(ModuleName, "msvcr70") != 0 &&
48         stricmp(ModuleName, "msvcrt") != 0 &&
49         stricmp(ModuleName, "msvcrt20") != 0 &&
50         stricmp(ModuleName, "msvcrt40") != 0) {
51       OpenedHandles.push_back((HMODULE)ModuleBase);
52     }
53     return TRUE;
54   }
55 }
56
57 DynamicLibrary::DynamicLibrary() : handle(0) {
58   handle = GetModuleHandle(NULL);
59   OpenedHandles.push_back((HMODULE)handle);
60 }
61
62 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
63   HMODULE a_handle = LoadLibrary(filename);
64
65   if (a_handle == 0)
66     ThrowError(std::string(filename) + ": Can't open : ");
67
68   handle = a_handle;
69   OpenedHandles.push_back(a_handle);
70 }
71
72 DynamicLibrary::~DynamicLibrary() {
73   if (handle == 0)
74     return;
75
76   // GetModuleHandle() does not increment the ref count, so we must not free
77   // the handle to the executable.
78   if (handle != GetModuleHandle(NULL))
79     FreeLibrary((HMODULE)handle);
80   handle = 0;
81
82   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
83        E = OpenedHandles.end(); I != E; ++I) {
84     if (*I == handle) {
85       // Note: don't use the swap/pop_back trick here. Order is important.
86       OpenedHandles.erase(I);
87     }
88   }
89 }
90
91 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
92   if (filename) {
93     HMODULE a_handle = LoadLibrary(filename);
94
95         if (a_handle == 0)
96                 ThrowError(std::string(filename) + ": Can't open : ");
97
98         OpenedHandles.push_back(a_handle);
99   } else {
100         // When no file is specified, enumerate all DLLs and EXEs in the
101     // process.
102     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
103   }
104
105   // Because we don't remember the handle, we will never free it; hence,
106   // it is loaded permanently.
107 }
108
109 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
110   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
111        E = OpenedHandles.end(); I != E; ++I) {
112     FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
113     if (ptr)
114       return (void *) ptr;
115   }
116
117   return 0;
118 }
119
120 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
121   assert(handle != 0 && "Invalid DynamicLibrary handle");
122   return (void *) GetProcAddress((HMODULE)handle, symbolName);
123 }
124
125 }
126
127 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab