blob: 5b4d5a5244a7b5465c1ae85bf0bf3a8a36ccef53 [file] [log] [blame]
Reid Spencerebf8d0e2004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Spencerebf8d0e2004-12-24 06:03:31 +00007//
8//===----------------------------------------------------------------------===//
9//
Jeff Cohenc2b91622004-12-25 04:50:17 +000010// This file provides the Win32 specific implementation of DynamicLibrary.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "Win32.h"
Jeff Cohenc2b91622004-12-25 04:50:17 +000015
Jeff Cohen23a1cf32005-02-19 03:01:13 +000016#ifdef __MINGW32__
Reid Spencer48fdf912006-06-01 19:03:21 +000017 #include <imagehlp.h>
Jeff Cohenc2b91622004-12-25 04:50:17 +000018#else
Reid Spencer48fdf912006-06-01 19:03:21 +000019 #include <dbghelp.h>
Jeff Cohenc2b91622004-12-25 04:50:17 +000020#endif
Jeff Cohen1a466352004-12-24 07:57:09 +000021
Reid Spencer48fdf912006-06-01 19:03:21 +000022#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
Reid Spencerebf8d0e2004-12-24 06:03:31 +000029
30namespace llvm {
31using namespace sys;
32
33//===----------------------------------------------------------------------===//
34//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen1a466352004-12-24 07:57:09 +000035//=== and must not be UNIX code.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000036//===----------------------------------------------------------------------===//
37
Jeff Cohen1a466352004-12-24 07:57:09 +000038static std::vector<HMODULE> OpenedHandles;
39
Chuck Rose III0ccb9302007-11-21 00:37:56 +000040#ifdef _WIN64
41 typedef DWORD64 ModuleBaseType;
42#else
43 typedef ULONG ModuleBaseType;
44#endif
45
Jeff Cohenc2b91622004-12-25 04:50:17 +000046extern "C" {
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +000047#if !defined(_MSC_VER) || _MSC_VER < 1500
Jeff Cohenc2b91622004-12-25 04:50:17 +000048 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III0ccb9302007-11-21 00:37:56 +000049 ModuleBaseType ModuleBase,
Jeff Cohenc2b91622004-12-25 04:50:17 +000050 ULONG ModuleSize,
51 PVOID UserContext)
Anton Korobeynikov47ccf1a2008-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
Jeff Cohenc2b91622004-12-25 04:50:17 +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 &&
Anton Korobeynikovcd79df02006-12-19 15:24:18 +000067#ifndef __MINGW32__
68 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
69 // Otherwise, user should be aware, what he's doing :)
Jeff Cohenc2b91622004-12-25 04:50:17 +000070 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikovcd79df02006-12-19 15:24:18 +000071#endif
Jeff Cohenc2b91622004-12-25 04:50:17 +000072 stricmp(ModuleName, "msvcrt20") != 0 &&
73 stricmp(ModuleName, "msvcrt40") != 0) {
74 OpenedHandles.push_back((HMODULE)ModuleBase);
75 }
76 return TRUE;
77 }
Jeff Cohen1a466352004-12-24 07:57:09 +000078}
79
Reid Spencerebf8d0e2004-12-24 06:03:31 +000080DynamicLibrary::DynamicLibrary() : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000081 handle = GetModuleHandle(NULL);
82 OpenedHandles.push_back((HMODULE)handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000083}
84
Reid Spencerebf8d0e2004-12-24 06:03:31 +000085DynamicLibrary::~DynamicLibrary() {
Jeff Cohen1a466352004-12-24 07:57:09 +000086 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}
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000103
Chris Lattneradcbce02006-07-07 17:12:36 +0000104bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
105 std::string *ErrMsg) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000106 if (filename) {
107 HMODULE a_handle = LoadLibrary(filename);
108
Jeff Cohen715bd762006-01-29 22:02:52 +0000109 if (a_handle == 0)
Reid Spencer05545752006-08-25 21:37:17 +0000110 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
Jeff Cohen1a466352004-12-24 07:57:09 +0000111
Jeff Cohen715bd762006-01-29 22:02:52 +0000112 OpenedHandles.push_back(a_handle);
Jeff Cohen1a466352004-12-24 07:57:09 +0000113 } else {
Jeff Cohen715bd762006-01-29 22:02:52 +0000114 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen1a466352004-12-24 07:57:09 +0000115 // process.
116 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
117 }
118
Jeff Cohenc2b91622004-12-25 04:50:17 +0000119 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +0000120 // it is loaded permanently.
Chris Lattneradcbce02006-07-07 17:12:36 +0000121 return false;
Jeff Cohen1a466352004-12-24 07:57:09 +0000122}
123
Anton Korobeynikov47ccf1a2008-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
Jeff Cohen1a466352004-12-24 07:57:09 +0000142void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohen85046902006-01-30 04:33:51 +0000143 // 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.
Jeff Cohen1a466352004-12-24 07:57:09 +0000149 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)
Jeff Cohenc18671c2004-12-30 03:02:31 +0000153 return (void *) ptr;
Jeff Cohen1a466352004-12-24 07:57:09 +0000154 }
155
Anton Korobeynikov52488962007-06-25 07:12:14 +0000156#if defined(__MINGW32__)
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000157 {
158 EXPLICIT_SYMBOL(_alloca);
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +0000159 EXPLICIT_SYMBOL(__main);
160
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000161 EXPLICIT_SYMBOL2(alloca, _alloca);
162#undef EXPLICIT_SYMBOL
163#undef EXPLICIT_SYMBOL2
164#undef EXPLICIT_SYMBOL_DEF
165 }
Anton Korobeynikov52488962007-06-25 07:12:14 +0000166#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 }
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000174#endif
175
Jeff Cohen1a466352004-12-24 07:57:09 +0000176 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000177}
178
179void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000180 assert(handle != 0 && "Invalid DynamicLibrary handle");
Jeff Cohenc18671c2004-12-30 03:02:31 +0000181 return (void *) GetProcAddress((HMODULE)handle, symbolName);
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000182}
183
184}
185