This file is empty.
[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 __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 #ifdef _WIN64
41   typedef DWORD64 ModuleBaseType;
42 #else
43   typedef ULONG ModuleBaseType;
44 #endif
45
46 extern "C" {
47 #if !defined(_MSC_VER) || _MSC_VER < 1500
48   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
49                                     ModuleBaseType ModuleBase,
50                                     ULONG ModuleSize,
51                                     PVOID UserContext)
52 #else
53   static BOOL CALLBACK ELM_Callback(PCSTR  ModuleName,
54                                     ModuleBaseType ModuleBase,
55                                     ULONG ModuleSize,
56                                     PVOID UserContext)
57 #endif
58   {
59     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
60     // into the process.
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 &&
67 #ifndef __MINGW32__
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 &&
71 #endif        
72         stricmp(ModuleName, "msvcrt20") != 0 &&
73         stricmp(ModuleName, "msvcrt40") != 0) {
74       OpenedHandles.push_back((HMODULE)ModuleBase);
75     }
76     return TRUE;
77   }
78 }
79
80 DynamicLibrary::DynamicLibrary() : handle(0) {
81   handle = GetModuleHandle(NULL);
82   OpenedHandles.push_back((HMODULE)handle);
83 }
84
85 DynamicLibrary::~DynamicLibrary() {
86   if (handle == 0)
87     return;
88
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);
93   handle = 0;
94
95   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
96        E = OpenedHandles.end(); I != E; ++I) {
97     if (*I == handle) {
98       // Note: don't use the swap/pop_back trick here. Order is important.
99       OpenedHandles.erase(I);
100     }
101   }
102 }
103  
104 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
105                                             std::string *ErrMsg) {
106   if (filename) {
107     HMODULE a_handle = LoadLibrary(filename);
108
109     if (a_handle == 0)
110       return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
111
112     OpenedHandles.push_back(a_handle);
113   } else {
114     // When no file is specified, enumerate all DLLs and EXEs in the
115     // process.
116     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
117   }
118
119   // Because we don't remember the handle, we will never free it; hence,
120   // it is loaded permanently.
121   return false;
122 }
123
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; }
133
134   #if defined(__MINGW32__)
135     EXPLICIT_SYMBOL_DEF(_alloca);
136     EXPLICIT_SYMBOL_DEF(__main);
137     EXPLICIT_SYMBOL_DEF(__ashldi3);
138     EXPLICIT_SYMBOL_DEF(__ashrdi3);
139     EXPLICIT_SYMBOL_DEF(__cmpdi2);
140     EXPLICIT_SYMBOL_DEF(__divdi3);
141     EXPLICIT_SYMBOL_DEF(__eprintf);
142     EXPLICIT_SYMBOL_DEF(__fixdfdi);
143     EXPLICIT_SYMBOL_DEF(__fixsfdi);
144     EXPLICIT_SYMBOL_DEF(__fixunsdfdi);
145     EXPLICIT_SYMBOL_DEF(__fixunssfdi);
146     EXPLICIT_SYMBOL_DEF(__floatdidf);
147     EXPLICIT_SYMBOL_DEF(__floatdisf);
148     EXPLICIT_SYMBOL_DEF(__lshrdi3);
149     EXPLICIT_SYMBOL_DEF(__moddi3);
150     EXPLICIT_SYMBOL_DEF(__udivdi3);
151     EXPLICIT_SYMBOL_DEF(__umoddi3);
152   #elif defined(_MSC_VER)
153     EXPLICIT_SYMBOL_DEF(_alloca_probe);
154   #endif
155 #endif
156
157 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
158   // First check symbols added via AddSymbol().
159   std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
160   if (I != g_symbols.end())
161     return I->second;
162
163   // Now search the libraries.
164   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
165        E = OpenedHandles.end(); I != E; ++I) {
166     FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
167     if (ptr)
168       return (void *) ptr;
169   }
170
171 #if defined(__MINGW32__)
172   {
173     EXPLICIT_SYMBOL(_alloca);
174     EXPLICIT_SYMBOL(__main);
175     EXPLICIT_SYMBOL(__ashldi3);
176     EXPLICIT_SYMBOL(__ashrdi3);
177     EXPLICIT_SYMBOL(__cmpdi2);
178     EXPLICIT_SYMBOL(__divdi3);
179     EXPLICIT_SYMBOL(__eprintf);
180     EXPLICIT_SYMBOL(__fixdfdi);
181     EXPLICIT_SYMBOL(__fixsfdi);
182     EXPLICIT_SYMBOL(__fixunsdfdi);
183     EXPLICIT_SYMBOL(__fixunssfdi);
184     EXPLICIT_SYMBOL(__floatdidf);
185     EXPLICIT_SYMBOL(__floatdisf);
186     EXPLICIT_SYMBOL(__lshrdi3);
187     EXPLICIT_SYMBOL(__moddi3);
188     EXPLICIT_SYMBOL(__udivdi3);
189     EXPLICIT_SYMBOL(__umoddi3);
190
191     EXPLICIT_SYMBOL2(alloca, _alloca);
192 #undef EXPLICIT_SYMBOL
193 #undef EXPLICIT_SYMBOL2
194 #undef EXPLICIT_SYMBOL_DEF    
195   }
196 #elif defined(_MSC_VER)
197   {
198     EXPLICIT_SYMBOL2(alloca, _alloca_probe);
199     EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
200 #undef EXPLICIT_SYMBOL
201 #undef EXPLICIT_SYMBOL2
202 #undef EXPLICIT_SYMBOL_DEF    
203   }  
204 #endif
205
206   return 0;
207 }
208
209 }
210