blob: 050689483deb403a615879517abd0517aecedfec [file] [log] [blame]
Reid Spencer4befbf32004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer4befbf32004-12-24 06:03:31 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer4befbf32004-12-24 06:03:31 +00008//===----------------------------------------------------------------------===//
9//
Jeff Cohenf365c332004-12-25 04:50:17 +000010// This file provides the Win32 specific implementation of DynamicLibrary.
Reid Spencer4befbf32004-12-24 06:03:31 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000014#include "WindowsSupport.h"
Jeff Cohenf365c332004-12-25 04:50:17 +000015
Jeff Cohen07e22ba2005-02-19 03:01:13 +000016#ifdef __MINGW32__
Reid Spencer187b4ad2006-06-01 19:03:21 +000017 #include <imagehlp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000018#else
Reid Spencer187b4ad2006-06-01 19:03:21 +000019 #include <dbghelp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000020#endif
Jeff Cohen039b4ab2004-12-24 07:57:09 +000021
Stefanus Du Toitd2b7be62009-04-28 16:37:58 +000022#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
Reid Spencer4befbf32004-12-24 06:03:31 +000026namespace llvm {
27using namespace sys;
28
29//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000030//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen039b4ab2004-12-24 07:57:09 +000031//=== and must not be UNIX code.
Reid Spencer4befbf32004-12-24 06:03:31 +000032//===----------------------------------------------------------------------===//
33
Leny Kholodovbebb27b2015-07-02 14:34:57 +000034typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
35static fpEnumerateLoadedModules fEnumerateLoadedModules;
Jordy Rosea19917d2011-08-17 00:29:32 +000036static DenseSet<HMODULE> *OpenedHandles;
Jeff Cohen039b4ab2004-12-24 07:57:09 +000037
Leny Kholodovbebb27b2015-07-02 14:34:57 +000038static bool loadDebugHelp(void) {
39 HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
40 if (hLib) {
41 fEnumerateLoadedModules = (fpEnumerateLoadedModules)
42 ::GetProcAddress(hLib, "EnumerateLoadedModules64");
43 }
44 return fEnumerateLoadedModules != 0;
45}
46
NAKAMURA Takumibbae11b2014-09-23 01:09:46 +000047static BOOL CALLBACK
NAKAMURA Takumi2de1b322016-03-07 00:13:09 +000048ELM_Callback(PCSTR ModuleName, DWORD64 ModuleBase,
Yaron Kerenfb069082014-09-22 21:40:15 +000049 ULONG ModuleSize, PVOID UserContext) {
50 OpenedHandles->insert((HMODULE)ModuleBase);
51 return TRUE;
Jeff Cohen039b4ab2004-12-24 07:57:09 +000052}
53
Jordy Rosea19917d2011-08-17 00:29:32 +000054DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
55 std::string *errMsg) {
Filip Pizlo57093e82013-09-18 16:40:14 +000056 SmartScopedLock<true> lock(*SymbolsMutex);
Jordy Rose5765f4c2011-08-22 19:01:52 +000057
Jordy Rosea19917d2011-08-17 00:29:32 +000058 if (!filename) {
59 // When no file is specified, enumerate all DLLs and EXEs in the process.
Jordy Rosea19917d2011-08-17 00:29:32 +000060 if (OpenedHandles == 0)
61 OpenedHandles = new DenseSet<HMODULE>();
Jeff Cohen039b4ab2004-12-24 07:57:09 +000062
Davide Italiano05402672015-12-01 05:33:24 +000063 if (!fEnumerateLoadedModules) {
64 if (!loadDebugHelp()) {
65 assert(false && "These APIs should always be available");
66 return DynamicLibrary();
67 }
68 }
Leny Kholodovbebb27b2015-07-02 14:34:57 +000069
70 fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
Jordy Rosea19917d2011-08-17 00:29:32 +000071 // Dummy library that represents "search all handles".
72 // This is mostly to ensure that the return value still shows up as "valid".
73 return DynamicLibrary(&OpenedHandles);
74 }
David Majnemer61eae2e2013-10-07 01:00:07 +000075
76 SmallVector<wchar_t, MAX_PATH> filenameUnicode;
Rafael Espindolaadccf862014-06-12 21:53:57 +000077 if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
David Majnemer61eae2e2013-10-07 01:00:07 +000078 SetLastError(ec.value());
Paul Robinsonaf19bc32015-11-23 17:34:20 +000079 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
David Majnemer61eae2e2013-10-07 01:00:07 +000080 return DynamicLibrary();
81 }
Jordy Rosea19917d2011-08-17 00:29:32 +000082
David Majnemer61eae2e2013-10-07 01:00:07 +000083 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
Jordy Rosea19917d2011-08-17 00:29:32 +000084
85 if (a_handle == 0) {
Paul Robinsonaf19bc32015-11-23 17:34:20 +000086 MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
Jordy Rosea19917d2011-08-17 00:29:32 +000087 return DynamicLibrary();
Jeff Cohen039b4ab2004-12-24 07:57:09 +000088 }
89
Jordy Rosea19917d2011-08-17 00:29:32 +000090 if (OpenedHandles == 0)
91 OpenedHandles = new DenseSet<HMODULE>();
92
93 // If we've already loaded this library, FreeLibrary() the handle in order to
94 // keep the internal refcount at +1.
95 if (!OpenedHandles->insert(a_handle).second)
96 FreeLibrary(a_handle);
97
98 return DynamicLibrary(a_handle);
Jeff Cohen039b4ab2004-12-24 07:57:09 +000099}
100
Anton Korobeynikov66362102008-02-22 10:08:31 +0000101// Stack probing routines are in the support library (e.g. libgcc), but we don't
102// have dynamic linking on windows. Provide a hook.
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000103#define EXPLICIT_SYMBOL(SYM) \
104 extern "C" { extern void *SYM; }
105#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
Anton Korobeynikov66362102008-02-22 10:08:31 +0000106
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000107#ifdef _M_IX86
108// Win32 on x86 implements certain single-precision math functions as macros.
109// These functions are not exported by the DLL, but will still be needed
110// for symbol-resolution by the JIT loader. Therefore, this Support libray
111// provides helper functions with the same implementation.
112
113#define INLINE_DEF_SYMBOL1(TYP, SYM) \
114 extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
115#define INLINE_DEF_SYMBOL2(TYP, SYM) \
116 extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
117#endif
118
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000119#include "explicit_symbols.inc"
120
121#undef EXPLICIT_SYMBOL
122#undef EXPLICIT_SYMBOL2
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000123#undef INLINE_DEF_SYMBOL1
124#undef INLINE_DEF_SYMBOL2
Anton Korobeynikov66362102008-02-22 10:08:31 +0000125
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000126void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Filip Pizlo57093e82013-09-18 16:40:14 +0000127 SmartScopedLock<true> Lock(*SymbolsMutex);
Jordy Rosea19917d2011-08-17 00:29:32 +0000128
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000129 // First check symbols added via AddSymbol().
Filip Pizlo57093e82013-09-18 16:40:14 +0000130 if (ExplicitSymbols.isConstructed()) {
Jordy Rosea19917d2011-08-17 00:29:32 +0000131 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
132
133 if (i != ExplicitSymbols->end())
134 return i->second;
Chris Lattnere4807c52009-07-08 00:52:12 +0000135 }
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000136
137 // Now search the libraries.
Jordy Rosea19917d2011-08-17 00:29:32 +0000138 if (OpenedHandles) {
139 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
140 E = OpenedHandles->end(); I != E; ++I) {
141 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
142 if (ptr) {
143 return (void *)(intptr_t)ptr;
144 }
Owen Anderson021c3b02009-06-25 18:12:44 +0000145 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000146 }
147
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000148#define EXPLICIT_SYMBOL(SYM) \
149 if (!strcmp(symbolName, #SYM)) \
150 return (void *)&SYM;
151#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
152 if (!strcmp(symbolName, #SYMFROM)) \
153 return (void *)&SYMTO;
154
155#ifdef _M_IX86
156#define INLINE_DEF_SYMBOL1(TYP, SYM) \
157 if (!strcmp(symbolName, #SYM)) \
158 return (void *)&inline_##SYM;
159#define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
160#endif
Anton Korobeynikov66362102008-02-22 10:08:31 +0000161
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000162 {
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000163#include "explicit_symbols.inc"
Anton Korobeynikov105682d2010-01-14 20:19:51 +0000164 }
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000165
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000166#undef EXPLICIT_SYMBOL
167#undef EXPLICIT_SYMBOL2
168#undef INLINE_DEF_SYMBOL1
169#undef INLINE_DEF_SYMBOL2
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000170
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000171 return 0;
Reid Spencer4befbf32004-12-24 06:03:31 +0000172}
173
Jordy Rosea19917d2011-08-17 00:29:32 +0000174void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
175 if (!isValid())
176 return NULL;
177 if (Data == &OpenedHandles)
178 return SearchForAddressOfSymbol(symbolName);
179 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
180}
181
Reid Spencer4befbf32004-12-24 06:03:31 +0000182}