more getting windows to build.
[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 is distributed under the University of Illinois Open Source
6 // 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 _MSC_VER
23  #include <ntverp.h>
24 #endif
25
26 #ifdef __MINGW32__
27  #if (HAVE_LIBIMAGEHLP != 1)
28   #error "libimagehlp.a should be present"
29  #endif
30 #else
31  #pragma comment(lib, "dbghelp.lib")
32 #endif
33
34 namespace llvm {
35 using namespace sys;
36
37 //===----------------------------------------------------------------------===//
38 //=== WARNING: Implementation here must contain only Win32 specific code 
39 //===          and must not be UNIX code.
40 //===----------------------------------------------------------------------===//
41
42 static std::vector<HMODULE> OpenedHandles;
43
44 #ifdef _WIN64
45   typedef DWORD64 ModuleBaseType;
46 #else
47   typedef ULONG ModuleBaseType;
48 #endif
49
50 extern "C" {
51 // Use old callback if:
52 //  - Not using Visual Studio
53 //  - Visual Studio 2005 or earlier but only if we are not using the Windows SDK 
54 //    or Windows SDK version is older than 6.0
55 // Use new callback if:
56 //  - Newer Visual Studio (comes with newer SDK).
57 //  - Visual Studio 2005 with Windows SDK 6.0+
58 #if !defined(_MSC_VER) || _MSC_VER < 1500 && (!defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 6000)
59   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
60                                     ModuleBaseType ModuleBase,
61                                     ULONG ModuleSize,
62                                     PVOID UserContext)
63 #else
64   static BOOL CALLBACK ELM_Callback(PCSTR  ModuleName,
65                                     ModuleBaseType ModuleBase,
66                                     ULONG ModuleSize,
67                                     PVOID UserContext)
68 #endif
69   {
70     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
71     // into the process.
72     if (stricmp(ModuleName, "msvci70") != 0 &&
73         stricmp(ModuleName, "msvcirt") != 0 &&
74         stricmp(ModuleName, "msvcp50") != 0 &&
75         stricmp(ModuleName, "msvcp60") != 0 &&
76         stricmp(ModuleName, "msvcp70") != 0 &&
77         stricmp(ModuleName, "msvcr70") != 0 &&
78 #ifndef __MINGW32__
79         // Mingw32 uses msvcrt.dll by default. Don't ignore it.
80         // Otherwise, user should be aware, what he's doing :)
81         stricmp(ModuleName, "msvcrt") != 0 &&
82 #endif        
83         stricmp(ModuleName, "msvcrt20") != 0 &&
84         stricmp(ModuleName, "msvcrt40") != 0) {
85       OpenedHandles.push_back((HMODULE)ModuleBase);
86     }
87     return TRUE;
88   }
89 }
90
91 DynamicLibrary::DynamicLibrary() : handle(0) {
92   handle = GetModuleHandle(NULL);
93   OpenedHandles.push_back((HMODULE)handle);
94 }
95
96 DynamicLibrary::~DynamicLibrary() {
97   if (handle == 0)
98     return;
99
100   // GetModuleHandle() does not increment the ref count, so we must not free
101   // the handle to the executable.
102   if (handle != GetModuleHandle(NULL))
103     FreeLibrary((HMODULE)handle);
104   handle = 0;
105
106   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
107        E = OpenedHandles.end(); I != E; ++I) {
108     if (*I == handle) {
109       // Note: don't use the swap/pop_back trick here. Order is important.
110       OpenedHandles.erase(I);
111     }
112   }
113 }
114  
115 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
116                                             std::string *ErrMsg) {                                            
117   if (filename) {
118     HMODULE a_handle = LoadLibrary(filename);
119
120     if (a_handle == 0)
121       return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
122
123     OpenedHandles.push_back(a_handle);
124   } else {
125     // When no file is specified, enumerate all DLLs and EXEs in the
126     // process.
127     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
128   }
129
130   // Because we don't remember the handle, we will never free it; hence,
131   // it is loaded permanently.
132   return false;
133 }
134
135 // Stack probing routines are in the support library (e.g. libgcc), but we don't
136 // have dynamic linking on windows. Provide a hook.
137 #if defined(__MINGW32__) || defined (_MSC_VER)
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
142   #define EXPLICIT_SYMBOL_DEF(SYM)                \
143     extern "C" { extern void *SYM; }
144
145   #if defined(__MINGW32__)
146     EXPLICIT_SYMBOL_DEF(_alloca);
147     EXPLICIT_SYMBOL_DEF(__main);
148     EXPLICIT_SYMBOL_DEF(__ashldi3);
149     EXPLICIT_SYMBOL_DEF(__ashrdi3);
150     EXPLICIT_SYMBOL_DEF(__cmpdi2);
151     EXPLICIT_SYMBOL_DEF(__divdi3);
152     EXPLICIT_SYMBOL_DEF(__fixdfdi);
153     EXPLICIT_SYMBOL_DEF(__fixsfdi);
154     EXPLICIT_SYMBOL_DEF(__fixunsdfdi);
155     EXPLICIT_SYMBOL_DEF(__fixunssfdi);
156     EXPLICIT_SYMBOL_DEF(__floatdidf);
157     EXPLICIT_SYMBOL_DEF(__floatdisf);
158     EXPLICIT_SYMBOL_DEF(__lshrdi3);
159     EXPLICIT_SYMBOL_DEF(__moddi3);
160     EXPLICIT_SYMBOL_DEF(__udivdi3);
161     EXPLICIT_SYMBOL_DEF(__umoddi3);
162   #elif defined(_MSC_VER)
163     EXPLICIT_SYMBOL_DEF(_alloca_probe);
164   #endif
165 #endif
166
167 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
168   // First check symbols added via AddSymbol().
169   if (ExplicitSymbols) {
170     std::map<std::string, void *>::iterator I = 
171       ExplicitSymbols->find(symbolName);
172     std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
173     if (I != E)
174       return I->second;
175   }
176
177   // Now search the libraries.
178   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
179        E = OpenedHandles.end(); I != E; ++I) {
180     FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
181     if (ptr) {
182       return (void *) ptr;
183     }
184   }
185
186 #if defined(__MINGW32__)
187   {
188     EXPLICIT_SYMBOL(_alloca);
189     EXPLICIT_SYMBOL(__main);
190     EXPLICIT_SYMBOL(__ashldi3);
191     EXPLICIT_SYMBOL(__ashrdi3);
192     EXPLICIT_SYMBOL(__cmpdi2);
193     EXPLICIT_SYMBOL(__divdi3);
194     EXPLICIT_SYMBOL(__fixdfdi);
195     EXPLICIT_SYMBOL(__fixsfdi);
196     EXPLICIT_SYMBOL(__fixunsdfdi);
197     EXPLICIT_SYMBOL(__fixunssfdi);
198     EXPLICIT_SYMBOL(__floatdidf);
199     EXPLICIT_SYMBOL(__floatdisf);
200     EXPLICIT_SYMBOL(__lshrdi3);
201     EXPLICIT_SYMBOL(__moddi3);
202     EXPLICIT_SYMBOL(__udivdi3);
203     EXPLICIT_SYMBOL(__umoddi3);
204
205     EXPLICIT_SYMBOL2(alloca, _alloca);
206 #undef EXPLICIT_SYMBOL
207 #undef EXPLICIT_SYMBOL2
208 #undef EXPLICIT_SYMBOL_DEF    
209   }
210 #elif defined(_MSC_VER)
211   {
212     EXPLICIT_SYMBOL2(alloca, _alloca_probe);
213     EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
214 #undef EXPLICIT_SYMBOL
215 #undef EXPLICIT_SYMBOL2
216 #undef EXPLICIT_SYMBOL_DEF    
217   }  
218 #endif
219
220   return 0;
221 }
222
223 }
224