blob: aa04268406d45832034de1e5baefc9697940369c [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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
Stefanus Du Toitf0d13d52009-04-28 16:37:58 +000022#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#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
34namespace llvm {
35using namespace sys;
36
37//===----------------------------------------------------------------------===//
38//=== WARNING: Implementation here must contain only Win32 specific code
39//=== and must not be UNIX code.
40//===----------------------------------------------------------------------===//
41
42static std::vector<HMODULE> OpenedHandles;
43
Chuck Rose III57c33da2007-11-21 00:37:56 +000044#ifdef _WIN64
45 typedef DWORD64 ModuleBaseType;
46#else
47 typedef ULONG ModuleBaseType;
48#endif
49
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050extern "C" {
Stefanus Du Toitf0d13d52009-04-28 16:37:58 +000051// 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)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III57c33da2007-11-21 00:37:56 +000060 ModuleBaseType ModuleBase,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 ULONG ModuleSize,
62 PVOID UserContext)
Anton Korobeynikov71da0382008-02-22 10:08:31 +000063#else
64 static BOOL CALLBACK ELM_Callback(PCSTR ModuleName,
65 ModuleBaseType ModuleBase,
66 ULONG ModuleSize,
67 PVOID UserContext)
68#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 {
Owen Anderson132a2f22009-06-25 18:12:44 +000070 llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
72 // into the process.
73 if (stricmp(ModuleName, "msvci70") != 0 &&
74 stricmp(ModuleName, "msvcirt") != 0 &&
75 stricmp(ModuleName, "msvcp50") != 0 &&
76 stricmp(ModuleName, "msvcp60") != 0 &&
77 stricmp(ModuleName, "msvcp70") != 0 &&
78 stricmp(ModuleName, "msvcr70") != 0 &&
79#ifndef __MINGW32__
80 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
81 // Otherwise, user should be aware, what he's doing :)
82 stricmp(ModuleName, "msvcrt") != 0 &&
83#endif
84 stricmp(ModuleName, "msvcrt20") != 0 &&
85 stricmp(ModuleName, "msvcrt40") != 0) {
86 OpenedHandles.push_back((HMODULE)ModuleBase);
87 }
88 return TRUE;
89 }
90}
91
92DynamicLibrary::DynamicLibrary() : handle(0) {
Owen Anderson132a2f22009-06-25 18:12:44 +000093 SmartScopedWriter<true> Writer(&SymbolsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 handle = GetModuleHandle(NULL);
95 OpenedHandles.push_back((HMODULE)handle);
96}
97
98DynamicLibrary::~DynamicLibrary() {
Owen Anderson132a2f22009-06-25 18:12:44 +000099 llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 if (handle == 0)
101 return;
102
103 // GetModuleHandle() does not increment the ref count, so we must not free
104 // the handle to the executable.
105 if (handle != GetModuleHandle(NULL))
106 FreeLibrary((HMODULE)handle);
107 handle = 0;
108
109 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
110 E = OpenedHandles.end(); I != E; ++I) {
111 if (*I == handle) {
112 // Note: don't use the swap/pop_back trick here. Order is important.
113 OpenedHandles.erase(I);
114 }
115 }
116}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117
118bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
Owen Anderson132a2f22009-06-25 18:12:44 +0000119 std::string *ErrMsg) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 if (filename) {
Owen Anderson132a2f22009-06-25 18:12:44 +0000121 llvm::sys::SmartScopedWriter<true> Writer(&SymbolsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 HMODULE a_handle = LoadLibrary(filename);
123
124 if (a_handle == 0)
125 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
126
127 OpenedHandles.push_back(a_handle);
128 } else {
129 // When no file is specified, enumerate all DLLs and EXEs in the
130 // process.
131 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
132 }
133
134 // Because we don't remember the handle, we will never free it; hence,
135 // it is loaded permanently.
136 return false;
137}
138
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000139// Stack probing routines are in the support library (e.g. libgcc), but we don't
140// have dynamic linking on windows. Provide a hook.
141#if defined(__MINGW32__) || defined (_MSC_VER)
142 #define EXPLICIT_SYMBOL(SYM) \
143 if (!strcmp(symbolName, #SYM)) return (void*)&SYM
144 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
145 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO
146 #define EXPLICIT_SYMBOL_DEF(SYM) \
147 extern "C" { extern void *SYM; }
148
149 #if defined(__MINGW32__)
150 EXPLICIT_SYMBOL_DEF(_alloca);
151 EXPLICIT_SYMBOL_DEF(__main);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000152 EXPLICIT_SYMBOL_DEF(__ashldi3);
153 EXPLICIT_SYMBOL_DEF(__ashrdi3);
154 EXPLICIT_SYMBOL_DEF(__cmpdi2);
155 EXPLICIT_SYMBOL_DEF(__divdi3);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000156 EXPLICIT_SYMBOL_DEF(__fixdfdi);
157 EXPLICIT_SYMBOL_DEF(__fixsfdi);
158 EXPLICIT_SYMBOL_DEF(__fixunsdfdi);
159 EXPLICIT_SYMBOL_DEF(__fixunssfdi);
160 EXPLICIT_SYMBOL_DEF(__floatdidf);
161 EXPLICIT_SYMBOL_DEF(__floatdisf);
162 EXPLICIT_SYMBOL_DEF(__lshrdi3);
163 EXPLICIT_SYMBOL_DEF(__moddi3);
164 EXPLICIT_SYMBOL_DEF(__udivdi3);
165 EXPLICIT_SYMBOL_DEF(__umoddi3);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000166 #elif defined(_MSC_VER)
167 EXPLICIT_SYMBOL_DEF(_alloca_probe);
168 #endif
169#endif
170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
172 // First check symbols added via AddSymbol().
Owen Anderson132a2f22009-06-25 18:12:44 +0000173 SymbolsLock.reader_acquire();
174 std::map<std::string, void *>::iterator I = symbols.find(symbolName);
175 std::map<std::string, void *>::iterator E = symbols.end();
176 SymbolsLock.reader_release();
177 if (I != E)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 return I->second;
179
180 // Now search the libraries.
Owen Anderson132a2f22009-06-25 18:12:44 +0000181 SymbolsLock.writer_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
183 E = OpenedHandles.end(); I != E; ++I) {
184 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
Owen Anderson132a2f22009-06-25 18:12:44 +0000185 if (ptr) {
186 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 return (void *) ptr;
Owen Anderson132a2f22009-06-25 18:12:44 +0000188 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
Owen Anderson132a2f22009-06-25 18:12:44 +0000190 SymbolsLock.writer_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
192#if defined(__MINGW32__)
193 {
194 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000195 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000196 EXPLICIT_SYMBOL(__ashldi3);
197 EXPLICIT_SYMBOL(__ashrdi3);
198 EXPLICIT_SYMBOL(__cmpdi2);
199 EXPLICIT_SYMBOL(__divdi3);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000200 EXPLICIT_SYMBOL(__fixdfdi);
201 EXPLICIT_SYMBOL(__fixsfdi);
202 EXPLICIT_SYMBOL(__fixunsdfdi);
203 EXPLICIT_SYMBOL(__fixunssfdi);
204 EXPLICIT_SYMBOL(__floatdidf);
205 EXPLICIT_SYMBOL(__floatdisf);
206 EXPLICIT_SYMBOL(__lshrdi3);
207 EXPLICIT_SYMBOL(__moddi3);
208 EXPLICIT_SYMBOL(__udivdi3);
209 EXPLICIT_SYMBOL(__umoddi3);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 EXPLICIT_SYMBOL2(alloca, _alloca);
212#undef EXPLICIT_SYMBOL
213#undef EXPLICIT_SYMBOL2
214#undef EXPLICIT_SYMBOL_DEF
215 }
216#elif defined(_MSC_VER)
217 {
218 EXPLICIT_SYMBOL2(alloca, _alloca_probe);
219 EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
220#undef EXPLICIT_SYMBOL
221#undef EXPLICIT_SYMBOL2
222#undef EXPLICIT_SYMBOL_DEF
223 }
224#endif
225
226 return 0;
227}
228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229}
230