blob: 5b4d5a5244a7b5465c1ae85bf0bf3a8a36ccef53 [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);
137 #elif defined(_MSC_VER)
138 EXPLICIT_SYMBOL_DEF(_alloca_probe);
139 #endif
140#endif
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
143 // First check symbols added via AddSymbol().
144 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
145 if (I != g_symbols.end())
146 return I->second;
147
148 // Now search the libraries.
149 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
150 E = OpenedHandles.end(); I != E; ++I) {
151 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
152 if (ptr)
153 return (void *) ptr;
154 }
155
156#if defined(__MINGW32__)
157 {
158 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov71da0382008-02-22 10:08:31 +0000159 EXPLICIT_SYMBOL(__main);
160
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 EXPLICIT_SYMBOL2(alloca, _alloca);
162#undef EXPLICIT_SYMBOL
163#undef EXPLICIT_SYMBOL2
164#undef EXPLICIT_SYMBOL_DEF
165 }
166#elif defined(_MSC_VER)
167 {
168 EXPLICIT_SYMBOL2(alloca, _alloca_probe);
169 EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
170#undef EXPLICIT_SYMBOL
171#undef EXPLICIT_SYMBOL2
172#undef EXPLICIT_SYMBOL_DEF
173 }
174#endif
175
176 return 0;
177}
178
179void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
180 assert(handle != 0 && "Invalid DynamicLibrary handle");
181 return (void *) GetProcAddress((HMODULE)handle, symbolName);
182}
183
184}
185