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