OSDN Git Service

Revert "Refactor DynamicLibrary so searching for a symbol will have a defined order"
[android-x86/external-llvm.git] / lib / Support / Windows / DynamicLibrary.inc
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
26 namespace llvm {
27
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only Win32 specific code
30 //===          and must not be UNIX code.
31 //===----------------------------------------------------------------------===//
32
33 typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID);
34 static fpEnumerateLoadedModules fEnumerateLoadedModules;
35 static llvm::ManagedStatic<DenseSet<HMODULE> > OpenedHandles;
36
37 static bool loadDebugHelp(void) {
38   HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll");
39   if (hLib) {
40     fEnumerateLoadedModules = (fpEnumerateLoadedModules)
41       ::GetProcAddress(hLib, "EnumerateLoadedModules64");
42   }
43   return fEnumerateLoadedModules != 0;
44 }
45
46 static BOOL CALLBACK
47 ELM_Callback(PCSTR ModuleName, DWORD64 ModuleBase,
48              ULONG ModuleSize, PVOID UserContext) {
49   OpenedHandles->insert((HMODULE)ModuleBase);
50   return TRUE;
51 }
52
53 sys::DynamicLibrary
54 sys::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 (!fEnumerateLoadedModules) {
61       if (!loadDebugHelp()) {
62         assert(false && "These APIs should always be available");
63         return DynamicLibrary();
64       }
65     }
66
67     fEnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
68     // Dummy library that represents "search all handles".
69     // This is mostly to ensure that the return value still shows up as "valid".
70     return DynamicLibrary(&OpenedHandles);
71   }
72
73   SmallVector<wchar_t, MAX_PATH> filenameUnicode;
74   if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
75     SetLastError(ec.value());
76     MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16");
77     return DynamicLibrary();
78   }
79
80   HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
81
82   if (a_handle == 0) {
83     MakeErrMsg(errMsg, std::string(filename) + ": Can't open");
84     return DynamicLibrary();
85   }
86
87   // If we've already loaded this library, FreeLibrary() the handle in order to
88   // keep the internal refcount at +1.
89   if (!OpenedHandles->insert(a_handle).second)
90     FreeLibrary(a_handle);
91
92   return DynamicLibrary(a_handle);
93 }
94
95 sys::DynamicLibrary
96 sys::DynamicLibrary::addPermanentLibrary(void *handle, std::string *errMsg) {
97   SmartScopedLock<true> lock(*SymbolsMutex);
98   // If we've already loaded this library, tell the caller.
99   if (!OpenedHandles->insert((HMODULE)handle).second) {
100     MakeErrMsg(errMsg, "Library already loaded");
101     return DynamicLibrary();
102   }
103
104   return DynamicLibrary(handle);
105 }
106
107 // Stack probing routines are in the support library (e.g. libgcc), but we don't
108 // have dynamic linking on windows. Provide a hook.
109 #define EXPLICIT_SYMBOL(SYM)                    \
110   extern "C" { extern void *SYM; }
111 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
112
113 #ifdef _M_IX86
114 // Win32 on x86 implements certain single-precision math functions as macros.
115 // These functions are not exported by the DLL, but will still be needed
116 // for symbol-resolution by the JIT loader. Therefore, this Support libray
117 // provides helper functions with the same implementation.
118
119 #define INLINE_DEF_SYMBOL1(TYP, SYM)                                           \
120   extern "C" TYP inline_##SYM(TYP _X) { return SYM(_X); }
121 #define INLINE_DEF_SYMBOL2(TYP, SYM)                                           \
122   extern "C" TYP inline_##SYM(TYP _X, TYP _Y) { return SYM(_X, _Y); }
123 #endif
124
125 #include "explicit_symbols.inc"
126
127 #undef EXPLICIT_SYMBOL
128 #undef EXPLICIT_SYMBOL2
129 #undef INLINE_DEF_SYMBOL1
130 #undef INLINE_DEF_SYMBOL2
131
132 void *sys::DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) {
133   SmartScopedLock<true> Lock(*SymbolsMutex);
134
135   // First check symbols added via AddSymbol().
136   if (ExplicitSymbols.isConstructed()) {
137     StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
138
139     if (i != ExplicitSymbols->end())
140       return i->second;
141   }
142
143   // Now search the libraries.
144   if (OpenedHandles.isConstructed()) {
145     for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
146          E = OpenedHandles->end(); I != E; ++I) {
147       FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
148       if (ptr) {
149         return (void *)(intptr_t)ptr;
150       }
151     }
152   }
153
154 #define EXPLICIT_SYMBOL(SYM)                                                   \
155   if (!strcmp(symbolName, #SYM))                                               \
156     return (void *)&SYM;
157 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)                                       \
158   if (!strcmp(symbolName, #SYMFROM))                                           \
159     return (void *)&SYMTO;
160
161 #ifdef _M_IX86
162 #define INLINE_DEF_SYMBOL1(TYP, SYM)                                           \
163   if (!strcmp(symbolName, #SYM))                                               \
164     return (void *)&inline_##SYM;
165 #define INLINE_DEF_SYMBOL2(TYP, SYM) INLINE_DEF_SYMBOL1(TYP, SYM)
166 #endif
167
168   {
169 #include "explicit_symbols.inc"
170   }
171
172 #undef EXPLICIT_SYMBOL
173 #undef EXPLICIT_SYMBOL2
174 #undef INLINE_DEF_SYMBOL1
175 #undef INLINE_DEF_SYMBOL2
176
177   return 0;
178 }
179
180 void *sys::DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
181   if (!isValid())
182     return NULL;
183   if (Data == &OpenedHandles)
184     return SearchForAddressOfSymbol(symbolName);
185   return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
186 }
187
188 }