blob: 83c67dcfd99fe33d4d0466768c2dd20c45f92449 [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//
5// This file was developed by Jeff Cohen and is distributed under the
6// University of Illinois Open Source 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
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" {
47 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III57c33da2007-11-21 00:37:56 +000048 ModuleBaseType ModuleBase,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 ULONG ModuleSize,
50 PVOID UserContext)
51 {
52 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
53 // into the process.
54 if (stricmp(ModuleName, "msvci70") != 0 &&
55 stricmp(ModuleName, "msvcirt") != 0 &&
56 stricmp(ModuleName, "msvcp50") != 0 &&
57 stricmp(ModuleName, "msvcp60") != 0 &&
58 stricmp(ModuleName, "msvcp70") != 0 &&
59 stricmp(ModuleName, "msvcr70") != 0 &&
60#ifndef __MINGW32__
61 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
62 // Otherwise, user should be aware, what he's doing :)
63 stricmp(ModuleName, "msvcrt") != 0 &&
64#endif
65 stricmp(ModuleName, "msvcrt20") != 0 &&
66 stricmp(ModuleName, "msvcrt40") != 0) {
67 OpenedHandles.push_back((HMODULE)ModuleBase);
68 }
69 return TRUE;
70 }
71}
72
73DynamicLibrary::DynamicLibrary() : handle(0) {
74 handle = GetModuleHandle(NULL);
75 OpenedHandles.push_back((HMODULE)handle);
76}
77
78DynamicLibrary::~DynamicLibrary() {
79 if (handle == 0)
80 return;
81
82 // GetModuleHandle() does not increment the ref count, so we must not free
83 // the handle to the executable.
84 if (handle != GetModuleHandle(NULL))
85 FreeLibrary((HMODULE)handle);
86 handle = 0;
87
88 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
89 E = OpenedHandles.end(); I != E; ++I) {
90 if (*I == handle) {
91 // Note: don't use the swap/pop_back trick here. Order is important.
92 OpenedHandles.erase(I);
93 }
94 }
95}
96
97// Stack probing routines are in the support library (e.g. libgcc), but we don't
98// have dynamic linking on windows. Provide a hook.
99#if defined(__MINGW32__) || defined (_MSC_VER)
100 #define EXPLICIT_SYMBOL(SYM) \
101 if (!strcmp(symbolName, #SYM)) return (void*)&SYM
102 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
103 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO
104 #define EXPLICIT_SYMBOL_DEF(SYM) \
105 extern "C" { extern void *SYM; }
106
107 #if defined(__MINGW32__)
108 EXPLICIT_SYMBOL_DEF(_alloca);
109 #elif defined(_MSC_VER)
110 EXPLICIT_SYMBOL_DEF(_alloca_probe);
111 #endif
112#endif
113
114bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
115 std::string *ErrMsg) {
116 if (filename) {
117 HMODULE a_handle = LoadLibrary(filename);
118
119 if (a_handle == 0)
120 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
121
122 OpenedHandles.push_back(a_handle);
123 } else {
124 // When no file is specified, enumerate all DLLs and EXEs in the
125 // process.
126 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
127 }
128
129 // Because we don't remember the handle, we will never free it; hence,
130 // it is loaded permanently.
131 return false;
132}
133
134void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
135 // First check symbols added via AddSymbol().
136 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
137 if (I != g_symbols.end())
138 return I->second;
139
140 // Now search the libraries.
141 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
142 E = OpenedHandles.end(); I != E; ++I) {
143 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
144 if (ptr)
145 return (void *) ptr;
146 }
147
148#if defined(__MINGW32__)
149 {
150 EXPLICIT_SYMBOL(_alloca);
151 EXPLICIT_SYMBOL2(alloca, _alloca);
152#undef EXPLICIT_SYMBOL
153#undef EXPLICIT_SYMBOL2
154#undef EXPLICIT_SYMBOL_DEF
155 }
156#elif defined(_MSC_VER)
157 {
158 EXPLICIT_SYMBOL2(alloca, _alloca_probe);
159 EXPLICIT_SYMBOL2(_alloca, _alloca_probe);
160#undef EXPLICIT_SYMBOL
161#undef EXPLICIT_SYMBOL2
162#undef EXPLICIT_SYMBOL_DEF
163 }
164#endif
165
166 return 0;
167}
168
169void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
170 assert(handle != 0 && "Invalid DynamicLibrary handle");
171 return (void *) GetProcAddress((HMODULE)handle, symbolName);
172}
173
174}
175