blob: 1ddf6cea0b32fe00ce659631cc49bae9cc59918d [file] [log] [blame]
Reid Spencer4befbf32004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer4befbf32004-12-24 06:03:31 +00007//
8//===----------------------------------------------------------------------===//
9//
Jeff Cohenf365c332004-12-25 04:50:17 +000010// This file provides the Win32 specific implementation of DynamicLibrary.
Reid Spencer4befbf32004-12-24 06:03:31 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Win32.h"
Jeff Cohenf365c332004-12-25 04:50:17 +000015
Jeff Cohen07e22ba2005-02-19 03:01:13 +000016#ifdef __MINGW32__
Reid Spencer187b4ad2006-06-01 19:03:21 +000017 #include <imagehlp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000018#else
Reid Spencer187b4ad2006-06-01 19:03:21 +000019 #include <dbghelp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000020#endif
Jeff Cohen039b4ab2004-12-24 07:57:09 +000021
Stefanus Du Toitd2b7be62009-04-28 16:37:58 +000022#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
Reid Spencer187b4ad2006-06-01 19:03:21 +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
Reid Spencer4befbf32004-12-24 06:03:31 +000033
34namespace llvm {
35using namespace sys;
36
37//===----------------------------------------------------------------------===//
38//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen039b4ab2004-12-24 07:57:09 +000039//=== and must not be UNIX code.
Reid Spencer4befbf32004-12-24 06:03:31 +000040//===----------------------------------------------------------------------===//
41
Jeff Cohen039b4ab2004-12-24 07:57:09 +000042static std::vector<HMODULE> OpenedHandles;
43
Chuck Rose III07b57d22007-11-21 00:37:56 +000044#ifdef _WIN64
45 typedef DWORD64 ModuleBaseType;
46#else
47 typedef ULONG ModuleBaseType;
48#endif
49
Jeff Cohenf365c332004-12-25 04:50:17 +000050extern "C" {
Stefanus Du Toitd2b7be62009-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)
Jeff Cohenf365c332004-12-25 04:50:17 +000059 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III07b57d22007-11-21 00:37:56 +000060 ModuleBaseType ModuleBase,
Jeff Cohenf365c332004-12-25 04:50:17 +000061 ULONG ModuleSize,
62 PVOID UserContext)
Anton Korobeynikov66362102008-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
Jeff Cohenf365c332004-12-25 04:50:17 +000069 {
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 &&
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +000078#ifndef __MINGW32__
79 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
80 // Otherwise, user should be aware, what he's doing :)
Jeff Cohenf365c332004-12-25 04:50:17 +000081 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +000082#endif
Jeff Cohenf365c332004-12-25 04:50:17 +000083 stricmp(ModuleName, "msvcrt20") != 0 &&
84 stricmp(ModuleName, "msvcrt40") != 0) {
85 OpenedHandles.push_back((HMODULE)ModuleBase);
86 }
87 return TRUE;
88 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +000089}
90
Reid Spencer4befbf32004-12-24 06:03:31 +000091DynamicLibrary::DynamicLibrary() : handle(0) {
Jeff Cohen039b4ab2004-12-24 07:57:09 +000092 handle = GetModuleHandle(NULL);
93 OpenedHandles.push_back((HMODULE)handle);
Reid Spencer4befbf32004-12-24 06:03:31 +000094}
95
Reid Spencer4befbf32004-12-24 06:03:31 +000096DynamicLibrary::~DynamicLibrary() {
Jeff Cohen039b4ab2004-12-24 07:57:09 +000097 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}
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000114
Chris Lattner8c8858a2006-07-07 17:12:36 +0000115bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
116 std::string *ErrMsg) {
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000117 if (filename) {
118 HMODULE a_handle = LoadLibrary(filename);
119
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000120 if (a_handle == 0)
Reid Spencer50eac3b2006-08-25 21:37:17 +0000121 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000122
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000123 OpenedHandles.push_back(a_handle);
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000124 } else {
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000125 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000126 // process.
127 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
128 }
129
Jeff Cohenf365c332004-12-25 04:50:17 +0000130 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000131 // it is loaded permanently.
Chris Lattner8c8858a2006-07-07 17:12:36 +0000132 return false;
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000133}
134
Anton Korobeynikov66362102008-02-22 10:08:31 +0000135// 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);
Anton Korobeynikov5e864402008-06-06 07:20:07 +0000148 EXPLICIT_SYMBOL_DEF(__ashldi3);
149 EXPLICIT_SYMBOL_DEF(__ashrdi3);
150 EXPLICIT_SYMBOL_DEF(__cmpdi2);
151 EXPLICIT_SYMBOL_DEF(__divdi3);
Anton Korobeynikov5e864402008-06-06 07:20:07 +0000152 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);
Anton Korobeynikov66362102008-02-22 10:08:31 +0000162 #elif defined(_MSC_VER)
163 EXPLICIT_SYMBOL_DEF(_alloca_probe);
164 #endif
165#endif
166
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000167void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000168 // First check symbols added via AddSymbol().
Chris Lattnerdfb67352009-01-29 18:53:28 +0000169 std::map<std::string, void *>::iterator I = g_symbols().find(symbolName);
170 if (I != g_symbols().end())
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000171 return I->second;
172
173 // Now search the libraries.
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000174 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
175 E = OpenedHandles.end(); I != E; ++I) {
176 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
177 if (ptr)
Jeff Cohena6d9c142004-12-30 03:02:31 +0000178 return (void *) ptr;
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000179 }
180
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000181#if defined(__MINGW32__)
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000182 {
183 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov66362102008-02-22 10:08:31 +0000184 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov5e864402008-06-06 07:20:07 +0000185 EXPLICIT_SYMBOL(__ashldi3);
186 EXPLICIT_SYMBOL(__ashrdi3);
187 EXPLICIT_SYMBOL(__cmpdi2);
188 EXPLICIT_SYMBOL(__divdi3);
Anton Korobeynikov5e864402008-06-06 07:20:07 +0000189 EXPLICIT_SYMBOL(__fixdfdi);
190 EXPLICIT_SYMBOL(__fixsfdi);
191 EXPLICIT_SYMBOL(__fixunsdfdi);
192 EXPLICIT_SYMBOL(__fixunssfdi);
193 EXPLICIT_SYMBOL(__floatdidf);
194 EXPLICIT_SYMBOL(__floatdisf);
195 EXPLICIT_SYMBOL(__lshrdi3);
196 EXPLICIT_SYMBOL(__moddi3);
197 EXPLICIT_SYMBOL(__udivdi3);
198 EXPLICIT_SYMBOL(__umoddi3);
Anton Korobeynikov66362102008-02-22 10:08:31 +0000199
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000200 EXPLICIT_SYMBOL2(alloca, _alloca);
201#undef EXPLICIT_SYMBOL
202#undef EXPLICIT_SYMBOL2
203#undef EXPLICIT_SYMBOL_DEF
204 }
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000205#elif defined(_MSC_VER)
206 {
207 EXPLICIT_SYMBOL2(alloca, _alloca_probe);
208 EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
209#undef EXPLICIT_SYMBOL
210#undef EXPLICIT_SYMBOL2
211#undef EXPLICIT_SYMBOL_DEF
212 }
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000213#endif
214
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000215 return 0;
Reid Spencer4befbf32004-12-24 06:03:31 +0000216}
217
Reid Spencer4befbf32004-12-24 06:03:31 +0000218}
219