DynamicLibrary.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file provides the Win32 specific implementation of DynamicLibrary.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "WindowsSupport.h"
  14. #ifdef __MINGW32__
  15. #include <imagehlp.h>
  16. #else
  17. #include <dbghelp.h>
  18. #endif
  19. #ifdef _MSC_VER
  20. #include <ntverp.h>
  21. #endif
  22. namespace llvm {
  23. using namespace sys;
  24. //===----------------------------------------------------------------------===//
  25. //=== WARNING: Implementation here must contain only Win32 specific code
  26. //=== and must not be UNIX code.
  27. //===----------------------------------------------------------------------===//
  28. typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
  29. static fpEnumerateLoadedModules fEnumerateLoadedModules;
  30. static DenseSet<HMODULE> *OpenedHandles;
  31. static bool loadDebugHelp(void) {
  32. HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
  33. if (hLib) {
  34. fEnumerateLoadedModules = (fpEnumerateLoadedModules)
  35. ::GetProcAddress(hLib, "EnumerateLoadedModules64");
  36. }
  37. return fEnumerateLoadedModules != 0;
  38. }
  39. static BOOL CALLBACK
  40. ELM_Callback(WIN32_ELMCB_PCSTR ModuleName, DWORD64 ModuleBase,
  41. ULONG ModuleSize, PVOID UserContext) {
  42. OpenedHandles->insert((HMODULE)ModuleBase);
  43. return TRUE;
  44. }
  45. DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
  46. std::string *errMsg) {
  47. SmartScopedLock<true> lock(*SymbolsMutex);
  48. if (!filename) {
  49. // When no file is specified, enumerate all DLLs and EXEs in the process.
  50. if (OpenedHandles == 0)
  51. OpenedHandles = new DenseSet<HMODULE>();
  52. if (!fEnumerateLoadedModules) {
  53. if (!loadDebugHelp()) {
  54. assert(false && "These APIs should always be available");
  55. return DynamicLibrary();
  56. }
  57. }
  58. fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
  59. // Dummy library that represents "search all handles".
  60. // This is mostly to ensure that the return value still shows up as "valid".
  61. return DynamicLibrary(&OpenedHandles);
  62. }
  63. SmallVector<wchar_t, MAX_PATH> filenameUnicode;
  64. if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
  65. SetLastError(ec.value());
  66. MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
  67. return DynamicLibrary();
  68. }
  69. HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
  70. if (a_handle == 0) {
  71. MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
  72. return DynamicLibrary();
  73. }
  74. if (OpenedHandles == 0)
  75. OpenedHandles = new DenseSet<HMODULE>();
  76. // If we've already loaded this library, FreeLibrary() the handle in order to
  77. // keep the internal refcount at +1.
  78. if (!OpenedHandles->insert(a_handle).second)
  79. FreeLibrary(a_handle);
  80. return DynamicLibrary(a_handle);
  81. }
  82. // Stack probing routines are in the support library (e.g. libgcc), but we don't
  83. // have dynamic linking on windows. Provide a hook.
  84. #define EXPLICIT_SYMBOL(SYM) \
  85. extern "C" { extern void *SYM; }
  86. #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
  87. #ifdef _M_IX86
  88. // Win32 on x86 implements certain single-precision math functions as macros.
  89. // These functions are not exported by the DLL, but will still be needed
  90. // for symbol-resolution by the JIT loader. Therefore, this Support libray
  91. // provides helper functions with the same implementation.
  92. #define INLINE_DEF_SYMBOL1(TYP, SYM) \
  93. extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
  94. #define INLINE_DEF_SYMBOL2(TYP, SYM) \
  95. extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
  96. #endif
  97. #include "explicit_symbols.inc"
  98. #undef EXPLICIT_SYMBOL
  99. #undef EXPLICIT_SYMBOL2
  100. #undef INLINE_DEF_SYMBOL1
  101. #undef INLINE_DEF_SYMBOL2
  102. void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
  103. SmartScopedLock<true> Lock(*SymbolsMutex);
  104. // First check symbols added via AddSymbol().
  105. if (ExplicitSymbols.isConstructed()) {
  106. StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
  107. if (i != ExplicitSymbols->end())
  108. return i->second;
  109. }
  110. // Now search the libraries.
  111. if (OpenedHandles) {
  112. for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
  113. E = OpenedHandles->end(); I != E; ++I) {
  114. FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
  115. if (ptr) {
  116. return (void *)(intptr_t)ptr;
  117. }
  118. }
  119. }
  120. #define EXPLICIT_SYMBOL(SYM) \
  121. if (!strcmp(symbolName, #SYM)) \
  122. return (void *)&SYM;
  123. #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
  124. if (!strcmp(symbolName, #SYMFROM)) \
  125. return (void *)&SYMTO;
  126. #ifdef _M_IX86
  127. #define INLINE_DEF_SYMBOL1(TYP, SYM) \
  128. if (!strcmp(symbolName, #SYM)) \
  129. return (void *)&inline_##SYM;
  130. #define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
  131. #endif
  132. {
  133. #include "explicit_symbols.inc"
  134. }
  135. #undef EXPLICIT_SYMBOL
  136. #undef EXPLICIT_SYMBOL2
  137. #undef INLINE_DEF_SYMBOL1
  138. #undef INLINE_DEF_SYMBOL2
  139. return 0;
  140. }
  141. void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
  142. if (!isValid())
  143. return NULL;
  144. if (Data == &OpenedHandles)
  145. return SearchForAddressOfSymbol(symbolName);
  146. return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
  147. }
  148. }