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