5a7b21920a9e2bf73815ba939f2b732e66f72dd9
[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 "Windows.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 #ifdef __MINGW32__
27  #if (HAVE_LIBIMAGEHLP != 1)
28   #error "libimagehlp.a should be present"
29  #endif
30 #else
31  #pragma comment(lib, "dbghelp.lib")
32 #endif
33
34 namespace llvm {
35 using namespace sys;
36
37 //===----------------------------------------------------------------------===//
38 //=== WARNING: Implementation here must contain only Win32 specific code
39 //===          and must not be UNIX code.
40 //===----------------------------------------------------------------------===//
41
42 static DenseSet<HMODULE> *OpenedHandles;
43
44 extern "C" {
45
46   static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
47                                     ULONG_PTR ModuleBase,
48                                     ULONG ModuleSize,
49                                     PVOID UserContext)
50   {
51     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
52     // into the process.
53     if (stricmp(ModuleName, "msvci70") != 0 &&
54         stricmp(ModuleName, "msvcirt") != 0 &&
55         stricmp(ModuleName, "msvcp50") != 0 &&
56         stricmp(ModuleName, "msvcp60") != 0 &&
57         stricmp(ModuleName, "msvcp70") != 0 &&
58         stricmp(ModuleName, "msvcr70") != 0 &&
59 #ifndef __MINGW32__
60         // Mingw32 uses msvcrt.dll by default. Don't ignore it.
61         // Otherwise, user should be aware, what he's doing :)
62         stricmp(ModuleName, "msvcrt") != 0 &&
63 #endif
64         stricmp(ModuleName, "msvcrt20") != 0 &&
65         stricmp(ModuleName, "msvcrt40") != 0) {
66       OpenedHandles->insert((HMODULE)ModuleBase);
67     }
68     return TRUE;
69   }
70 }
71
72 DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
73                                                    std::string *errMsg) {
74   SmartScopedLock<true> lock(*SymbolsMutex);
75
76   if (!filename) {
77     // When no file is specified, enumerate all DLLs and EXEs in the process.
78     if (OpenedHandles == 0)
79       OpenedHandles = new DenseSet<HMODULE>();
80
81     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
82     // Dummy library that represents "search all handles".
83     // This is mostly to ensure that the return value still shows up as "valid".
84     return DynamicLibrary(&OpenedHandles);
85   }
86
87   SmallVector<wchar_t, MAX_PATH> filenameUnicode;
88   if (error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
89     SetLastError(ec.value());
90     MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
91     return DynamicLibrary();
92   }
93   
94   HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
95
96   if (a_handle == 0) {
97     MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
98     return DynamicLibrary();
99   }
100
101   if (OpenedHandles == 0)
102     OpenedHandles = new DenseSet<HMODULE>();
103
104   // If we've already loaded this library, FreeLibrary() the handle in order to
105   // keep the internal refcount at +1.
106   if (!OpenedHandles->insert(a_handle).second)
107     FreeLibrary(a_handle);
108
109   return DynamicLibrary(a_handle);
110 }
111
112 // Stack probing routines are in the support library (e.g. libgcc), but we don't
113 // have dynamic linking on windows. Provide a hook.
114 #define EXPLICIT_SYMBOL(SYM)                    \
115   extern "C" { extern void *SYM; }
116 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
117
118 #include "explicit_symbols.inc"
119
120 #undef EXPLICIT_SYMBOL
121 #undef EXPLICIT_SYMBOL2
122
123 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
124   SmartScopedLock<true> Lock(*SymbolsMutex);
125
126   // First check symbols added via AddSymbol().
127   if (ExplicitSymbols.isConstructed()) {
128     StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
129
130     if (i != ExplicitSymbols->end())
131       return i->second;
132   }
133
134   // Now search the libraries.
135   if (OpenedHandles) {
136     for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
137          E = OpenedHandles->end(); I != E; ++I) {
138       FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
139       if (ptr) {
140         return (void *)(intptr_t)ptr;
141       }
142     }
143   }
144
145   #define EXPLICIT_SYMBOL(SYM)                    \
146     if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
147   #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
148     if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
149
150   {
151     #include "explicit_symbols.inc"
152   }
153
154   #undef EXPLICIT_SYMBOL
155   #undef EXPLICIT_SYMBOL2
156
157   return 0;
158 }
159
160
161 void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
162   if (!isValid())
163     return NULL;
164   if (Data == &OpenedHandles)
165     return SearchForAddressOfSymbol(symbolName);
166   return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
167 }
168
169
170 }