• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
14#include "WindowsSupport.h"
15
16#ifdef __MINGW32__
17 #include <imagehlp.h>
18#else
19 #include <dbghelp.h>
20#endif
21
22#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
26namespace llvm {
27using namespace sys;
28
29//===----------------------------------------------------------------------===//
30//=== WARNING: Implementation here must contain only Win32 specific code
31//===          and must not be UNIX code.
32//===----------------------------------------------------------------------===//
33
34typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
35static fpEnumerateLoadedModules fEnumerateLoadedModules;
36static DenseSet<HMODULE> *OpenedHandles;
37
38static 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
47static BOOL CALLBACK
48ELM_Callback(PCSTR ModuleName, DWORD64 ModuleBase,
49             ULONG ModuleSize, PVOID UserContext) {
50  OpenedHandles->insert((HMODULE)ModuleBase);
51  return TRUE;
52}
53
54DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
55                                                   std::string *errMsg) {
56  SmartScopedLock<true> lock(*SymbolsMutex);
57
58  if (!filename) {
59    // When no file is specified, enumerate all DLLs and EXEs in the process.
60    if (OpenedHandles == 0)
61      OpenedHandles = new DenseSet<HMODULE>();
62
63    if (!fEnumerateLoadedModules) {
64      if (!loadDebugHelp()) {
65        assert(false && "These APIs should always be available");
66        return DynamicLibrary();
67      }
68    }
69
70    fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
71    // 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  }
75
76  SmallVector<wchar_t, MAX_PATH> filenameUnicode;
77  if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
78    SetLastError(ec.value());
79    MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
80    return DynamicLibrary();
81  }
82
83  HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
84
85  if (a_handle == 0) {
86    MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
87    return DynamicLibrary();
88  }
89
90  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);
99}
100
101// Stack probing routines are in the support library (e.g. libgcc), but we don't
102// have dynamic linking on windows. Provide a hook.
103#define EXPLICIT_SYMBOL(SYM)                    \
104  extern "C" { extern void *SYM; }
105#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
106
107#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
119#include "explicit_symbols.inc"
120
121#undef EXPLICIT_SYMBOL
122#undef EXPLICIT_SYMBOL2
123#undef INLINE_DEF_SYMBOL1
124#undef INLINE_DEF_SYMBOL2
125
126void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
127  SmartScopedLock<true> Lock(*SymbolsMutex);
128
129  // First check symbols added via AddSymbol().
130  if (ExplicitSymbols.isConstructed()) {
131    StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
132
133    if (i != ExplicitSymbols->end())
134      return i->second;
135  }
136
137  // Now search the libraries.
138  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      }
145    }
146  }
147
148#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
161
162  {
163#include "explicit_symbols.inc"
164  }
165
166#undef EXPLICIT_SYMBOL
167#undef EXPLICIT_SYMBOL2
168#undef INLINE_DEF_SYMBOL1
169#undef INLINE_DEF_SYMBOL2
170
171  return 0;
172}
173
174void *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
182}
183