blob: 0bb59c9e103cb44d6a6c2994f355f878e0dc1f7d [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" {
47 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III0ccb9302007-11-21 00:37:56 +000048 ModuleBaseType ModuleBase,
Jeff Cohenc2b91622004-12-25 04:50:17 +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 &&
Anton Korobeynikovcd79df02006-12-19 15:24:18 +000060#ifndef __MINGW32__
61 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
62 // Otherwise, user should be aware, what he's doing :)
Jeff Cohenc2b91622004-12-25 04:50:17 +000063 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikovcd79df02006-12-19 15:24:18 +000064#endif
Jeff Cohenc2b91622004-12-25 04:50:17 +000065 stricmp(ModuleName, "msvcrt20") != 0 &&
66 stricmp(ModuleName, "msvcrt40") != 0) {
67 OpenedHandles.push_back((HMODULE)ModuleBase);
68 }
69 return TRUE;
70 }
Jeff Cohen1a466352004-12-24 07:57:09 +000071}
72
Reid Spencerebf8d0e2004-12-24 06:03:31 +000073DynamicLibrary::DynamicLibrary() : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000074 handle = GetModuleHandle(NULL);
75 OpenedHandles.push_back((HMODULE)handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000076}
77
Reid Spencerebf8d0e2004-12-24 06:03:31 +000078DynamicLibrary::~DynamicLibrary() {
Jeff Cohen1a466352004-12-24 07:57:09 +000079 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
Anton Korobeynikov52488962007-06-25 07:12:14 +000097// 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)
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000100 #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; }
Anton Korobeynikov52488962007-06-25 07:12:14 +0000106
107 #if defined(__MINGW32__)
108 EXPLICIT_SYMBOL_DEF(_alloca);
109 #elif defined(_MSC_VER)
110 EXPLICIT_SYMBOL_DEF(_alloca_probe);
111 #endif
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000112#endif
113
Chris Lattneradcbce02006-07-07 17:12:36 +0000114bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
115 std::string *ErrMsg) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000116 if (filename) {
117 HMODULE a_handle = LoadLibrary(filename);
118
Jeff Cohen715bd762006-01-29 22:02:52 +0000119 if (a_handle == 0)
Reid Spencer05545752006-08-25 21:37:17 +0000120 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
Jeff Cohen1a466352004-12-24 07:57:09 +0000121
Jeff Cohen715bd762006-01-29 22:02:52 +0000122 OpenedHandles.push_back(a_handle);
Jeff Cohen1a466352004-12-24 07:57:09 +0000123 } else {
Jeff Cohen715bd762006-01-29 22:02:52 +0000124 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen1a466352004-12-24 07:57:09 +0000125 // process.
126 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
127 }
128
Jeff Cohenc2b91622004-12-25 04:50:17 +0000129 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +0000130 // it is loaded permanently.
Chris Lattneradcbce02006-07-07 17:12:36 +0000131 return false;
Jeff Cohen1a466352004-12-24 07:57:09 +0000132}
133
134void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohen85046902006-01-30 04:33:51 +0000135 // 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.
Jeff Cohen1a466352004-12-24 07:57:09 +0000141 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)
Jeff Cohenc18671c2004-12-30 03:02:31 +0000145 return (void *) ptr;
Jeff Cohen1a466352004-12-24 07:57:09 +0000146 }
147
Anton Korobeynikov52488962007-06-25 07:12:14 +0000148#if defined(__MINGW32__)
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000149 {
150 EXPLICIT_SYMBOL(_alloca);
151 EXPLICIT_SYMBOL2(alloca, _alloca);
152#undef EXPLICIT_SYMBOL
153#undef EXPLICIT_SYMBOL2
154#undef EXPLICIT_SYMBOL_DEF
155 }
Anton Korobeynikov52488962007-06-25 07:12:14 +0000156#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 }
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000164#endif
165
Jeff Cohen1a466352004-12-24 07:57:09 +0000166 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000167}
168
169void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000170 assert(handle != 0 && "Invalid DynamicLibrary handle");
Jeff Cohenc18671c2004-12-30 03:02:31 +0000171 return (void *) GetProcAddress((HMODULE)handle, symbolName);
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000172}
173
174}
175