blob: c15c7623c26feffba2c4a3ad1a4c84e68b5a1893 [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"
Frederich Munch70c377a2017-04-24 19:55:16 +000015#include "llvm/Support/raw_ostream.h"
Jeff Cohenf365c332004-12-25 04:50:17 +000016
Frederich Munch70c377a2017-04-24 19:55:16 +000017#include <Psapi.h>
Frederich Munchb8c236a2017-04-24 03:33:30 +000018
Reid Spencer4befbf32004-12-24 06:03:31 +000019//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000020//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen039b4ab2004-12-24 07:57:09 +000021//=== and must not be UNIX code.
Reid Spencer4befbf32004-12-24 06:03:31 +000022//===----------------------------------------------------------------------===//
23
Jeff Cohen039b4ab2004-12-24 07:57:09 +000024
Frederich Munch70c377a2017-04-24 19:55:16 +000025DynamicLibrary::HandleSet::~HandleSet() {
26 for (void *Handle : Handles)
27 FreeLibrary(HMODULE(Handle));
28
29 // 'Process' should not be released on Windows.
30 assert((!Process || Process==this) && "Bad Handle");
Jeff Cohen039b4ab2004-12-24 07:57:09 +000031}
32
Frederich Munch70c377a2017-04-24 19:55:16 +000033void *DynamicLibrary::HandleSet::DLOpen(const char *File, std::string *Err) {
34 // Create the instance and return it to be the *Process* handle
35 // simillar to dlopen(NULL, RTLD_LAZY|RTLD_GLOBAL)
36 if (!File)
37 return &(*OpenedHandles);
Vassil Vassilev7f1c2552017-03-02 17:56:45 +000038
Frederich Munch70c377a2017-04-24 19:55:16 +000039 SmallVector<wchar_t, MAX_PATH> FileUnicode;
40 if (std::error_code ec = windows::UTF8ToUTF16(File, FileUnicode)) {
Frederich Munchb8c236a2017-04-24 03:33:30 +000041 SetLastError(ec.value());
Frederich Munch70c377a2017-04-24 19:55:16 +000042 MakeErrMsg(Err, std::string(File) + ": Can't convert to UTF-16");
43 return &DynamicLibrary::Invalid;
Frederich Munchb8c236a2017-04-24 03:33:30 +000044 }
45
Frederich Munch70c377a2017-04-24 19:55:16 +000046 HMODULE Handle = LoadLibraryW(FileUnicode.data());
47 if (Handle == NULL) {
48 MakeErrMsg(Err, std::string(File) + ": Can't open");
49 return &DynamicLibrary::Invalid;
Frederich Munchb8c236a2017-04-24 03:33:30 +000050 }
51
Frederich Munch70c377a2017-04-24 19:55:16 +000052 return reinterpret_cast<void*>(Handle);
Frederich Munch9f404572017-04-24 02:30:12 +000053}
54
Frederich Munch70c377a2017-04-24 19:55:16 +000055static DynamicLibrary::HandleSet *IsOpenedHandlesInstance(void *Handle) {
56 if (!OpenedHandles.isConstructed())
57 return nullptr;
58 DynamicLibrary::HandleSet &Inst = *OpenedHandles;
59 return Handle == &Inst ? &Inst : nullptr;
Frederich Munchb8c236a2017-04-24 03:33:30 +000060}
Frederich Munch9f404572017-04-24 02:30:12 +000061
Frederich Munch70c377a2017-04-24 19:55:16 +000062void DynamicLibrary::HandleSet::DLClose(void *Handle) {
63 if (HandleSet* HS = IsOpenedHandlesInstance(Handle))
64 HS->Process = nullptr; // Just drop the *Process* handle.
65 else
66 FreeLibrary((HMODULE)Handle);
67}
68
69static bool GetProcessModules(HANDLE H, DWORD &Bytes, HMODULE *Data = nullptr) {
70 // EnumProcessModules will fail on Windows 64 while MingW-32 doesn't have
71 // EnumProcessModulesEx.
72 if (
73#ifdef _WIN64
74 !EnumProcessModulesEx(H, Data, Bytes, &Bytes, LIST_MODULES_64BIT)
75#else
76 !EnumProcessModules(H, Data, Bytes, &Bytes)
77#endif
78 ) {
79 std::string Err;
80 if (MakeErrMsg(&Err, "EnumProcessModules failure"))
81 llvm::errs() << Err << "\n";
82 return false;
83 }
84 return true;
85}
86
87void *DynamicLibrary::HandleSet::DLSym(void *Handle, const char *Symbol) {
88 HandleSet* HS = IsOpenedHandlesInstance(Handle);
89 if (!HS)
90 return (void *)uintptr_t(GetProcAddress((HMODULE)Handle, Symbol));
91
92 // Could have done a dlclose on the *Process* handle
93 if (!HS->Process)
94 return nullptr;
95
96 // Trials indicate EnumProcessModulesEx is consistantly faster than using
97 // EnumerateLoadedModules64 or CreateToolhelp32Snapshot.
98 //
99 // | Handles | DbgHelp.dll | CreateSnapshot | EnumProcessModulesEx
100 // |=========|=============|========================================
101 // | 37 | 0.0000585 * | 0.0003031 | 0.0000152
102 // | 1020 | 0.0026310 * | 0.0121598 | 0.0002683
103 // | 2084 | 0.0149418 * | 0.0369936 | 0.0005610
104 //
105 // * Not including the load time of Dbghelp.dll (~.005 sec)
106 //
107 // There's still a case to somehow cache the result of EnumProcessModulesEx
108 // across invocations, but the complication of doing that properly...
109 // Possibly using LdrRegisterDllNotification to invalidate the cache?
110
111 DWORD Bytes = 0;
112 HMODULE Self = HMODULE(GetCurrentProcess());
113 if (!GetProcessModules(Self, Bytes))
114 return nullptr;
115
116 // Get the most recent list in case any modules added/removed between calls
117 // to EnumProcessModulesEx that gets the amount of, then copies the HMODULES.
118 // MSDN is pretty clear that if the module list changes during the call to
119 // EnumProcessModulesEx the results should not be used.
120 std::vector<HMODULE> Handles;
121 do {
122 assert(Bytes && ((Bytes % sizeof(HMODULE)) == 0) &&
123 "Should have at least one module and be aligned");
124 Handles.resize(Bytes / sizeof(HMODULE));
125 if (!GetProcessModules(Self, Bytes, Handles.data()))
126 return nullptr;
127 } while (Bytes != (Handles.size() * sizeof(HMODULE)));
128
129 // Try EXE first, mirroring what dlsym(dlopen(NULL)) does.
130 if (FARPROC Ptr = GetProcAddress(HMODULE(Handles.front()), Symbol))
131 return (void *) uintptr_t(Ptr);
132
133 if (Handles.size() > 1) {
134 // This is different behaviour than what Posix dlsym(dlopen(NULL)) does.
135 // Doing that here is causing real problems for the JIT where msvc.dll
136 // and ucrt.dll can define the same symbols. The runtime linker will choose
137 // symbols from ucrt.dll first, but iterating NOT in reverse here would
138 // mean that the msvc.dll versions would be returned.
139
140 for (auto I = Handles.rbegin(), E = Handles.rend()-1; I != E; ++I) {
141 if (FARPROC Ptr = GetProcAddress(HMODULE(*I), Symbol))
142 return (void *) uintptr_t(Ptr);
143 }
144 }
145 return nullptr;
146}
147
148
Anton Korobeynikov66362102008-02-22 10:08:31 +0000149// Stack probing routines are in the support library (e.g. libgcc), but we don't
150// have dynamic linking on windows. Provide a hook.
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000151#define EXPLICIT_SYMBOL(SYM) \
152 extern "C" { extern void *SYM; }
153#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
Anton Korobeynikov66362102008-02-22 10:08:31 +0000154
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000155#ifdef _M_IX86
156// Win32 on x86 implements certain single-precision math functions as macros.
157// These functions are not exported by the DLL, but will still be needed
158// for symbol-resolution by the JIT loader. Therefore, this Support libray
159// provides helper functions with the same implementation.
160
161#define INLINE_DEF_SYMBOL1(TYP, SYM) \
162 extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
163#define INLINE_DEF_SYMBOL2(TYP, SYM) \
164 extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
165#endif
166
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000167#include "explicit_symbols.inc"
168
169#undef EXPLICIT_SYMBOL
170#undef EXPLICIT_SYMBOL2
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000171#undef INLINE_DEF_SYMBOL1
172#undef INLINE_DEF_SYMBOL2
Anton Korobeynikov66362102008-02-22 10:08:31 +0000173
Frederich Munch70c377a2017-04-24 19:55:16 +0000174static void *DoSearch(const char *SymbolName) {
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000175
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000176#define EXPLICIT_SYMBOL(SYM) \
Frederich Munch70c377a2017-04-24 19:55:16 +0000177 if (!strcmp(SymbolName, #SYM)) \
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000178 return (void *)&SYM;
179#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
Frederich Munch70c377a2017-04-24 19:55:16 +0000180 if (!strcmp(SymbolName, #SYMFROM)) \
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000181 return (void *)&SYMTO;
182
183#ifdef _M_IX86
184#define INLINE_DEF_SYMBOL1(TYP, SYM) \
Frederich Munch70c377a2017-04-24 19:55:16 +0000185 if (!strcmp(SymbolName, #SYM)) \
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000186 return (void *)&inline_##SYM;
187#define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
188#endif
Anton Korobeynikov66362102008-02-22 10:08:31 +0000189
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000190 {
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000191#include "explicit_symbols.inc"
Anton Korobeynikov105682d2010-01-14 20:19:51 +0000192 }
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000193
Reid Kleckner9aeb0472014-11-13 23:32:52 +0000194#undef EXPLICIT_SYMBOL
195#undef EXPLICIT_SYMBOL2
196#undef INLINE_DEF_SYMBOL1
197#undef INLINE_DEF_SYMBOL2
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000198
Frederich Munch70c377a2017-04-24 19:55:16 +0000199 return nullptr;
Reid Spencer4befbf32004-12-24 06:03:31 +0000200}