blob: c76032f62d06e12bc6bec31ff557aed1dee88617 [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
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
30namespace llvm {
31using namespace sys;
32
33//===----------------------------------------------------------------------===//
34//=== WARNING: Implementation here must contain only Win32 specific code
35//=== and must not be UNIX code.
36//===----------------------------------------------------------------------===//
37
38static std::vector<HMODULE> OpenedHandles;
39
Chuck Rose III57c33da2007-11-21 00:37:56 +000040#ifdef _WIN64
41 typedef DWORD64 ModuleBaseType;
42#else
43 typedef ULONG ModuleBaseType;
44#endif
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046extern "C" {
Anton Korobeynikov71da0382008-02-22 10:08:31 +000047#if !defined(_MSC_VER) || _MSC_VER < 1500
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III57c33da2007-11-21 00:37:56 +000049 ModuleBaseType ModuleBase,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 ULONG ModuleSize,
51 PVOID UserContext)
Anton Korobeynikov71da0382008-02-22 10:08:31 +000052#else
53 static BOOL CALLBACK ELM_Callback(PCSTR ModuleName,
54 ModuleBaseType ModuleBase,
55 ULONG ModuleSize,
56 PVOID UserContext)
57#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 {
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
80DynamicLibrary::DynamicLibrary() : handle(0) {
81 handle = GetModuleHandle(NULL);
82 OpenedHandles.push_back((HMODULE)handle);
83}
84
85DynamicLibrary::~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}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
104bool 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
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000124// 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);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000137 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);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000152 #elif defined(_MSC_VER)
153 EXPLICIT_SYMBOL_DEF(_alloca_probe);
154 #endif
155#endif
156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157void* 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);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000174 EXPLICIT_SYMBOL(__main);
Anton Korobeynikov9b5f8012008-06-06 07:20:07 +0000175 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);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209}
210