Windows/DynamicLibrary.inc: Split explicit symbols into explicit_symbols.inc.
[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 std::vector<HMODULE> OpenedHandles;
43
44 #ifdef _WIN64
45   typedef DWORD64 ModuleBaseType;
46 #else
47   typedef ULONG ModuleBaseType;
48 #endif
49
50 extern "C" {
51 // Use old callback if:
52 //  - Not using Visual Studio
53 //  - Visual Studio 2005 or earlier but only if we are not using the Windows SDK
54 //    or Windows SDK version is older than 6.0
55 // Use new callback if:
56 //  - Newer Visual Studio (comes with newer SDK).
57 //  - Visual Studio 2005 with Windows SDK 6.0+
58 #if !defined(_MSC_VER) || _MSC_VER < 1500 && (!defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 6000)
59   static BOOL CALLBACK ELM_Callback(PSTR  ModuleName,
60                                     ModuleBaseType ModuleBase,
61                                     ULONG ModuleSize,
62                                     PVOID UserContext)
63 #else
64   static BOOL CALLBACK ELM_Callback(PCSTR  ModuleName,
65                                     ModuleBaseType ModuleBase,
66                                     ULONG ModuleSize,
67                                     PVOID UserContext)
68 #endif
69   {
70     // Ignore VC++ runtimes prior to 7.1.  Somehow some of them get loaded
71     // into the process.
72     if (stricmp(ModuleName, "msvci70") != 0 &&
73         stricmp(ModuleName, "msvcirt") != 0 &&
74         stricmp(ModuleName, "msvcp50") != 0 &&
75         stricmp(ModuleName, "msvcp60") != 0 &&
76         stricmp(ModuleName, "msvcp70") != 0 &&
77         stricmp(ModuleName, "msvcr70") != 0 &&
78 #ifndef __MINGW32__
79         // Mingw32 uses msvcrt.dll by default. Don't ignore it.
80         // Otherwise, user should be aware, what he's doing :)
81         stricmp(ModuleName, "msvcrt") != 0 &&
82 #endif
83         stricmp(ModuleName, "msvcrt20") != 0 &&
84         stricmp(ModuleName, "msvcrt40") != 0) {
85       OpenedHandles.push_back((HMODULE)ModuleBase);
86     }
87     return TRUE;
88   }
89 }
90
91 bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
92                                             std::string *ErrMsg) {
93   if (filename) {
94     HMODULE a_handle = LoadLibrary(filename);
95
96     if (a_handle == 0)
97       return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
98
99     OpenedHandles.push_back(a_handle);
100   } else {
101     // When no file is specified, enumerate all DLLs and EXEs in the
102     // process.
103     EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
104   }
105
106   // Because we don't remember the handle, we will never free it; hence,
107   // it is loaded permanently.
108   return false;
109 }
110
111 // Stack probing routines are in the support library (e.g. libgcc), but we don't
112 // have dynamic linking on windows. Provide a hook.
113 #define EXPLICIT_SYMBOL(SYM)                    \
114   extern "C" { extern void *SYM; }
115 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
116
117 #include "explicit_symbols.inc"
118
119 #undef EXPLICIT_SYMBOL
120 #undef EXPLICIT_SYMBOL2
121
122 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
123   // First check symbols added via AddSymbol().
124   if (ExplicitSymbols) {
125     std::map<std::string, void *>::iterator I =
126       ExplicitSymbols->find(symbolName);
127     std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
128     if (I != E)
129       return I->second;
130   }
131
132   // Now search the libraries.
133   for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
134        E = OpenedHandles.end(); I != E; ++I) {
135     FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
136     if (ptr) {
137       return (void *) ptr;
138     }
139   }
140
141   #define EXPLICIT_SYMBOL(SYM)                    \
142     if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
143   #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO)        \
144     if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
145
146   {
147     #include "explicit_symbols.inc"
148   }
149
150   #undef EXPLICIT_SYMBOL
151   #undef EXPLICIT_SYMBOL2
152
153   return 0;
154 }
155
156 }