2004-12-24 14:03:31 +08:00
|
|
|
//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-12-24 14:03:31 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2010-11-30 02:16:10 +08:00
|
|
|
//
|
2004-12-24 14:03:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2004-12-25 12:50:17 +08:00
|
|
|
// This file provides the Win32 specific implementation of DynamicLibrary.
|
2004-12-24 14:03:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-02-13 05:26:20 +08:00
|
|
|
#include "WindowsSupport.h"
|
2004-12-25 12:50:17 +08:00
|
|
|
|
2005-02-19 11:01:13 +08:00
|
|
|
#ifdef __MINGW32__
|
2006-06-02 03:03:21 +08:00
|
|
|
#include <imagehlp.h>
|
2004-12-25 12:50:17 +08:00
|
|
|
#else
|
2006-06-02 03:03:21 +08:00
|
|
|
#include <dbghelp.h>
|
2004-12-25 12:50:17 +08:00
|
|
|
#endif
|
2004-12-24 15:57:09 +08:00
|
|
|
|
2009-04-29 00:37:58 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <ntverp.h>
|
|
|
|
#endif
|
|
|
|
|
2004-12-24 14:03:31 +08:00
|
|
|
namespace llvm {
|
|
|
|
using namespace sys;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2010-11-30 02:16:10 +08:00
|
|
|
//=== WARNING: Implementation here must contain only Win32 specific code
|
2004-12-24 15:57:09 +08:00
|
|
|
//=== and must not be UNIX code.
|
2004-12-24 14:03:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-07-02 22:34:57 +08:00
|
|
|
typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
|
|
|
|
static fpEnumerateLoadedModules fEnumerateLoadedModules;
|
2011-08-17 08:29:32 +08:00
|
|
|
static DenseSet<HMODULE> *OpenedHandles;
|
2004-12-24 15:57:09 +08:00
|
|
|
|
2015-07-02 22:34:57 +08:00
|
|
|
static bool loadDebugHelp(void) {
|
|
|
|
HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
|
|
|
|
if (hLib) {
|
|
|
|
fEnumerateLoadedModules = (fpEnumerateLoadedModules)
|
|
|
|
::GetProcAddress(hLib, "EnumerateLoadedModules64");
|
|
|
|
}
|
|
|
|
return fEnumerateLoadedModules != 0;
|
|
|
|
}
|
|
|
|
|
2014-09-23 09:09:46 +08:00
|
|
|
static BOOL CALLBACK
|
2015-07-02 22:34:57 +08:00
|
|
|
ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, DWORD64 ModuleBase,
|
2014-09-23 05:40:15 +08:00
|
|
|
ULONG ModuleSize, PVOID UserContext) {
|
|
|
|
OpenedHandles->insert((HMODULE)ModuleBase);
|
|
|
|
return TRUE;
|
2004-12-24 15:57:09 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 08:29:32 +08:00
|
|
|
DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
|
|
|
|
std::string *errMsg) {
|
2013-09-19 00:40:14 +08:00
|
|
|
SmartScopedLock<true> lock(*SymbolsMutex);
|
2011-08-23 03:01:52 +08:00
|
|
|
|
2011-08-17 08:29:32 +08:00
|
|
|
if (!filename) {
|
|
|
|
// When no file is specified, enumerate all DLLs and EXEs in the process.
|
|
|
|
if (OpenedHandles == 0)
|
|
|
|
OpenedHandles = new DenseSet<HMODULE>();
|
2004-12-24 15:57:09 +08:00
|
|
|
|
2015-07-02 22:34:57 +08:00
|
|
|
if (!fEnumerateLoadedModules) {
|
|
|
|
if (!loadDebugHelp()) {
|
|
|
|
assert(false && "These APIs should always be available");
|
|
|
|
return DynamicLibrary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
|
2011-08-17 08:29:32 +08:00
|
|
|
// Dummy library that represents "search all handles".
|
|
|
|
// This is mostly to ensure that the return value still shows up as "valid".
|
|
|
|
return DynamicLibrary(&OpenedHandles);
|
|
|
|
}
|
2013-10-07 09:00:07 +08:00
|
|
|
|
|
|
|
SmallVector<wchar_t, MAX_PATH> filenameUnicode;
|
2014-06-13 05:53:57 +08:00
|
|
|
if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
|
2013-10-07 09:00:07 +08:00
|
|
|
SetLastError(ec.value());
|
|
|
|
MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
|
|
|
|
return DynamicLibrary();
|
|
|
|
}
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2013-10-07 09:00:07 +08:00
|
|
|
HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
|
2011-08-17 08:29:32 +08:00
|
|
|
|
|
|
|
if (a_handle == 0) {
|
2011-08-17 08:59:50 +08:00
|
|
|
MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
|
2011-08-17 08:29:32 +08:00
|
|
|
return DynamicLibrary();
|
2004-12-24 15:57:09 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 08:29:32 +08:00
|
|
|
if (OpenedHandles == 0)
|
|
|
|
OpenedHandles = new DenseSet<HMODULE>();
|
|
|
|
|
|
|
|
// If we've already loaded this library, FreeLibrary() the handle in order to
|
|
|
|
// keep the internal refcount at +1.
|
|
|
|
if (!OpenedHandles->insert(a_handle).second)
|
|
|
|
FreeLibrary(a_handle);
|
|
|
|
|
|
|
|
return DynamicLibrary(a_handle);
|
2004-12-24 15:57:09 +08:00
|
|
|
}
|
|
|
|
|
2008-02-22 18:08:31 +08:00
|
|
|
// Stack probing routines are in the support library (e.g. libgcc), but we don't
|
|
|
|
// have dynamic linking on windows. Provide a hook.
|
2011-02-05 23:11:53 +08:00
|
|
|
#define EXPLICIT_SYMBOL(SYM) \
|
|
|
|
extern "C" { extern void *SYM; }
|
|
|
|
#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
|
|
|
|
|
Fix symbol resolution of floating point libc builtins in MCJIT
Fix for LLI failure on Windows\X86: http://llvm.org/PR5053
LLI.exe crashes on Windows\X86 when single precession floating point
intrinsics like the following are used: acos, asin, atan, atan2, ceil,
copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh,
sqrt, tan, tanh
The above intrinsics are defined as inline-expansions in math.h, and are
not exported by msvcr120.dll (Win32 API GetProcAddress returns null).
For an FREM instruction, the JIT compiler generates a call to a stub for
the fmodf() intrinsic, and adds a relocation to fixup at load time. The
loader searches the libraries for the function, but fails because the
symbol is not exported. So, the call target remains NULL and the
execution crashes.
Since the math functions are loaded at JIT/runtime, the JIT can patch
CALL instruction directly instead of the searching the libraries'
exported symbols. However, this fix caused build failures due to
unresolved symbols like _fmodf at link time.
Therefore, the current fix defines helper functions in the Runtime
link/load library to perform the above operations. The address of these
helper functions are used to patch up the CALL instruction at load time.
Reviewers: lhames, rnk
Reviewed By: rnk
Differential Revision: http://reviews.llvm.org/D5387
Patch by Swaroop Sridhar!
llvm-svn: 221947
2014-11-14 07:32:52 +08:00
|
|
|
#ifdef _M_IX86
|
|
|
|
// Win32 on x86 implements certain single-precision math functions as macros.
|
|
|
|
// These functions are not exported by the DLL, but will still be needed
|
|
|
|
// for symbol-resolution by the JIT loader. Therefore, this Support libray
|
|
|
|
// provides helper functions with the same implementation.
|
|
|
|
|
|
|
|
#define INLINE_DEF_SYMBOL1(TYP, SYM) \
|
|
|
|
extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
|
|
|
|
#define INLINE_DEF_SYMBOL2(TYP, SYM) \
|
|
|
|
extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 23:11:53 +08:00
|
|
|
#include "explicit_symbols.inc"
|
|
|
|
|
|
|
|
#undef EXPLICIT_SYMBOL
|
|
|
|
#undef EXPLICIT_SYMBOL2
|
Fix symbol resolution of floating point libc builtins in MCJIT
Fix for LLI failure on Windows\X86: http://llvm.org/PR5053
LLI.exe crashes on Windows\X86 when single precession floating point
intrinsics like the following are used: acos, asin, atan, atan2, ceil,
copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh,
sqrt, tan, tanh
The above intrinsics are defined as inline-expansions in math.h, and are
not exported by msvcr120.dll (Win32 API GetProcAddress returns null).
For an FREM instruction, the JIT compiler generates a call to a stub for
the fmodf() intrinsic, and adds a relocation to fixup at load time. The
loader searches the libraries for the function, but fails because the
symbol is not exported. So, the call target remains NULL and the
execution crashes.
Since the math functions are loaded at JIT/runtime, the JIT can patch
CALL instruction directly instead of the searching the libraries'
exported symbols. However, this fix caused build failures due to
unresolved symbols like _fmodf at link time.
Therefore, the current fix defines helper functions in the Runtime
link/load library to perform the above operations. The address of these
helper functions are used to patch up the CALL instruction at load time.
Reviewers: lhames, rnk
Reviewed By: rnk
Differential Revision: http://reviews.llvm.org/D5387
Patch by Swaroop Sridhar!
llvm-svn: 221947
2014-11-14 07:32:52 +08:00
|
|
|
#undef INLINE_DEF_SYMBOL1
|
|
|
|
#undef INLINE_DEF_SYMBOL2
|
2008-02-22 18:08:31 +08:00
|
|
|
|
2004-12-24 15:57:09 +08:00
|
|
|
void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
|
2013-09-19 00:40:14 +08:00
|
|
|
SmartScopedLock<true> Lock(*SymbolsMutex);
|
2011-08-17 08:29:32 +08:00
|
|
|
|
2006-01-30 12:33:51 +08:00
|
|
|
// First check symbols added via AddSymbol().
|
2013-09-19 00:40:14 +08:00
|
|
|
if (ExplicitSymbols.isConstructed()) {
|
2011-08-17 08:29:32 +08:00
|
|
|
StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
|
|
|
|
|
|
|
|
if (i != ExplicitSymbols->end())
|
|
|
|
return i->second;
|
2009-07-08 08:52:12 +08:00
|
|
|
}
|
2006-01-30 12:33:51 +08:00
|
|
|
|
|
|
|
// Now search the libraries.
|
2011-08-17 08:29:32 +08:00
|
|
|
if (OpenedHandles) {
|
|
|
|
for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
|
|
|
|
E = OpenedHandles->end(); I != E; ++I) {
|
|
|
|
FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
|
|
|
|
if (ptr) {
|
|
|
|
return (void *)(intptr_t)ptr;
|
|
|
|
}
|
2009-06-26 02:12:44 +08:00
|
|
|
}
|
2004-12-24 15:57:09 +08:00
|
|
|
}
|
|
|
|
|
Fix symbol resolution of floating point libc builtins in MCJIT
Fix for LLI failure on Windows\X86: http://llvm.org/PR5053
LLI.exe crashes on Windows\X86 when single precession floating point
intrinsics like the following are used: acos, asin, atan, atan2, ceil,
copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh,
sqrt, tan, tanh
The above intrinsics are defined as inline-expansions in math.h, and are
not exported by msvcr120.dll (Win32 API GetProcAddress returns null).
For an FREM instruction, the JIT compiler generates a call to a stub for
the fmodf() intrinsic, and adds a relocation to fixup at load time. The
loader searches the libraries for the function, but fails because the
symbol is not exported. So, the call target remains NULL and the
execution crashes.
Since the math functions are loaded at JIT/runtime, the JIT can patch
CALL instruction directly instead of the searching the libraries'
exported symbols. However, this fix caused build failures due to
unresolved symbols like _fmodf at link time.
Therefore, the current fix defines helper functions in the Runtime
link/load library to perform the above operations. The address of these
helper functions are used to patch up the CALL instruction at load time.
Reviewers: lhames, rnk
Reviewed By: rnk
Differential Revision: http://reviews.llvm.org/D5387
Patch by Swaroop Sridhar!
llvm-svn: 221947
2014-11-14 07:32:52 +08:00
|
|
|
#define EXPLICIT_SYMBOL(SYM) \
|
|
|
|
if (!strcmp(symbolName, #SYM)) \
|
|
|
|
return (void *)&SYM;
|
|
|
|
#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
|
|
|
|
if (!strcmp(symbolName, #SYMFROM)) \
|
|
|
|
return (void *)&SYMTO;
|
|
|
|
|
|
|
|
#ifdef _M_IX86
|
|
|
|
#define INLINE_DEF_SYMBOL1(TYP, SYM) \
|
|
|
|
if (!strcmp(symbolName, #SYM)) \
|
|
|
|
return (void *)&inline_##SYM;
|
|
|
|
#define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
|
|
|
|
#endif
|
2011-02-05 23:11:53 +08:00
|
|
|
|
2007-06-25 15:12:14 +08:00
|
|
|
{
|
Fix symbol resolution of floating point libc builtins in MCJIT
Fix for LLI failure on Windows\X86: http://llvm.org/PR5053
LLI.exe crashes on Windows\X86 when single precession floating point
intrinsics like the following are used: acos, asin, atan, atan2, ceil,
copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh,
sqrt, tan, tanh
The above intrinsics are defined as inline-expansions in math.h, and are
not exported by msvcr120.dll (Win32 API GetProcAddress returns null).
For an FREM instruction, the JIT compiler generates a call to a stub for
the fmodf() intrinsic, and adds a relocation to fixup at load time. The
loader searches the libraries for the function, but fails because the
symbol is not exported. So, the call target remains NULL and the
execution crashes.
Since the math functions are loaded at JIT/runtime, the JIT can patch
CALL instruction directly instead of the searching the libraries'
exported symbols. However, this fix caused build failures due to
unresolved symbols like _fmodf at link time.
Therefore, the current fix defines helper functions in the Runtime
link/load library to perform the above operations. The address of these
helper functions are used to patch up the CALL instruction at load time.
Reviewers: lhames, rnk
Reviewed By: rnk
Differential Revision: http://reviews.llvm.org/D5387
Patch by Swaroop Sridhar!
llvm-svn: 221947
2014-11-14 07:32:52 +08:00
|
|
|
#include "explicit_symbols.inc"
|
2010-01-15 04:19:51 +08:00
|
|
|
}
|
2011-02-05 23:11:53 +08:00
|
|
|
|
Fix symbol resolution of floating point libc builtins in MCJIT
Fix for LLI failure on Windows\X86: http://llvm.org/PR5053
LLI.exe crashes on Windows\X86 when single precession floating point
intrinsics like the following are used: acos, asin, atan, atan2, ceil,
copysign, cos, cosh, exp, floor, fmin, fmax, fmod, log, pow, sin, sinh,
sqrt, tan, tanh
The above intrinsics are defined as inline-expansions in math.h, and are
not exported by msvcr120.dll (Win32 API GetProcAddress returns null).
For an FREM instruction, the JIT compiler generates a call to a stub for
the fmodf() intrinsic, and adds a relocation to fixup at load time. The
loader searches the libraries for the function, but fails because the
symbol is not exported. So, the call target remains NULL and the
execution crashes.
Since the math functions are loaded at JIT/runtime, the JIT can patch
CALL instruction directly instead of the searching the libraries'
exported symbols. However, this fix caused build failures due to
unresolved symbols like _fmodf at link time.
Therefore, the current fix defines helper functions in the Runtime
link/load library to perform the above operations. The address of these
helper functions are used to patch up the CALL instruction at load time.
Reviewers: lhames, rnk
Reviewed By: rnk
Differential Revision: http://reviews.llvm.org/D5387
Patch by Swaroop Sridhar!
llvm-svn: 221947
2014-11-14 07:32:52 +08:00
|
|
|
#undef EXPLICIT_SYMBOL
|
|
|
|
#undef EXPLICIT_SYMBOL2
|
|
|
|
#undef INLINE_DEF_SYMBOL1
|
|
|
|
#undef INLINE_DEF_SYMBOL2
|
2006-12-19 23:24:18 +08:00
|
|
|
|
2004-12-24 15:57:09 +08:00
|
|
|
return 0;
|
2004-12-24 14:03:31 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 08:29:32 +08:00
|
|
|
void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
|
|
|
|
if (!isValid())
|
|
|
|
return NULL;
|
|
|
|
if (Data == &OpenedHandles)
|
|
|
|
return SearchForAddressOfSymbol(symbolName);
|
|
|
|
return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
|
|
|
|
}
|
|
|
|
|
2004-12-24 14:03:31 +08:00
|
|
|
}
|