Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 1 | //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 10 | // This file provides the Win32 specific implementation of DynamicLibrary. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Win32.h" |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 15 | |
Jeff Cohen | 23a1cf3 | 2005-02-19 03:01:13 +0000 | [diff] [blame] | 16 | #ifdef __MINGW32__ |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 17 | #include <imagehlp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 18 | #else |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 19 | #include <dbghelp.h> |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 20 | #endif |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 21 | |
Stefanus Du Toit | 5a4e11d | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 22 | #ifdef _MSC_VER |
| 23 | #include <ntverp.h> |
| 24 | #endif |
| 25 | |
Reid Spencer | 48fdf91 | 2006-06-01 19:03:21 +0000 | [diff] [blame] | 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 |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
| 35 | using namespace sys; |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | //=== WARNING: Implementation here must contain only Win32 specific code |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 39 | //=== and must not be UNIX code. |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 42 | static std::vector<HMODULE> OpenedHandles; |
| 43 | |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 44 | #ifdef _WIN64 |
| 45 | typedef DWORD64 ModuleBaseType; |
| 46 | #else |
| 47 | typedef ULONG ModuleBaseType; |
| 48 | #endif |
| 49 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 50 | extern "C" { |
Stefanus Du Toit | 5a4e11d | 2009-04-28 16:37:58 +0000 | [diff] [blame] | 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) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 59 | static BOOL CALLBACK ELM_Callback(PSTR ModuleName, |
Chuck Rose III | 0ccb930 | 2007-11-21 00:37:56 +0000 | [diff] [blame] | 60 | ModuleBaseType ModuleBase, |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 61 | ULONG ModuleSize, |
| 62 | PVOID UserContext) |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 63 | #else |
| 64 | static BOOL CALLBACK ELM_Callback(PCSTR ModuleName, |
| 65 | ModuleBaseType ModuleBase, |
| 66 | ULONG ModuleSize, |
| 67 | PVOID UserContext) |
| 68 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 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 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 78 | #ifndef __MINGW32__ |
| 79 | // Mingw32 uses msvcrt.dll by default. Don't ignore it. |
| 80 | // Otherwise, user should be aware, what he's doing :) |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 81 | stricmp(ModuleName, "msvcrt") != 0 && |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 82 | #endif |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 83 | stricmp(ModuleName, "msvcrt20") != 0 && |
| 84 | stricmp(ModuleName, "msvcrt40") != 0) { |
| 85 | OpenedHandles.push_back((HMODULE)ModuleBase); |
| 86 | } |
| 87 | return TRUE; |
| 88 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 91 | bool DynamicLibrary::LoadLibraryPermanently(const char *filename, |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 92 | std::string *ErrMsg) { |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 93 | if (filename) { |
| 94 | HMODULE a_handle = LoadLibrary(filename); |
| 95 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 96 | if (a_handle == 0) |
Reid Spencer | 0554575 | 2006-08-25 21:37:17 +0000 | [diff] [blame] | 97 | return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : "); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 98 | |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 99 | OpenedHandles.push_back(a_handle); |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 100 | } else { |
Jeff Cohen | 715bd76 | 2006-01-29 22:02:52 +0000 | [diff] [blame] | 101 | // When no file is specified, enumerate all DLLs and EXEs in the |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 102 | // process. |
| 103 | EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0); |
| 104 | } |
| 105 | |
Jeff Cohen | c2b9162 | 2004-12-25 04:50:17 +0000 | [diff] [blame] | 106 | // Because we don't remember the handle, we will never free it; hence, |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 107 | // it is loaded permanently. |
Chris Lattner | adcbce0 | 2006-07-07 17:12:36 +0000 | [diff] [blame] | 108 | return false; |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 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 | #if defined(__MINGW32__) || defined (_MSC_VER) |
| 114 | #define EXPLICIT_SYMBOL(SYM) \ |
| 115 | if (!strcmp(symbolName, #SYM)) return (void*)&SYM |
| 116 | #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \ |
| 117 | if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO |
| 118 | #define EXPLICIT_SYMBOL_DEF(SYM) \ |
| 119 | extern "C" { extern void *SYM; } |
| 120 | |
| 121 | #if defined(__MINGW32__) |
| 122 | EXPLICIT_SYMBOL_DEF(_alloca); |
| 123 | EXPLICIT_SYMBOL_DEF(__main); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 124 | EXPLICIT_SYMBOL_DEF(__ashldi3); |
| 125 | EXPLICIT_SYMBOL_DEF(__ashrdi3); |
| 126 | EXPLICIT_SYMBOL_DEF(__cmpdi2); |
| 127 | EXPLICIT_SYMBOL_DEF(__divdi3); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 128 | EXPLICIT_SYMBOL_DEF(__fixdfdi); |
| 129 | EXPLICIT_SYMBOL_DEF(__fixsfdi); |
| 130 | EXPLICIT_SYMBOL_DEF(__fixunsdfdi); |
| 131 | EXPLICIT_SYMBOL_DEF(__fixunssfdi); |
| 132 | EXPLICIT_SYMBOL_DEF(__floatdidf); |
| 133 | EXPLICIT_SYMBOL_DEF(__floatdisf); |
| 134 | EXPLICIT_SYMBOL_DEF(__lshrdi3); |
| 135 | EXPLICIT_SYMBOL_DEF(__moddi3); |
| 136 | EXPLICIT_SYMBOL_DEF(__udivdi3); |
| 137 | EXPLICIT_SYMBOL_DEF(__umoddi3); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 138 | #elif defined(_MSC_VER) |
| 139 | EXPLICIT_SYMBOL_DEF(_alloca_probe); |
| 140 | #endif |
| 141 | #endif |
| 142 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 143 | void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 144 | // First check symbols added via AddSymbol(). |
Chris Lattner | 01d9a1b | 2009-07-08 00:52:12 +0000 | [diff] [blame] | 145 | if (ExplicitSymbols) { |
| 146 | std::map<std::string, void *>::iterator I = |
| 147 | ExplicitSymbols->find(symbolName); |
| 148 | std::map<std::string, void *>::iterator E = ExplicitSymbols->end(); |
| 149 | if (I != E) |
| 150 | return I->second; |
| 151 | } |
Jeff Cohen | 8504690 | 2006-01-30 04:33:51 +0000 | [diff] [blame] | 152 | |
| 153 | // Now search the libraries. |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 154 | for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(), |
| 155 | E = OpenedHandles.end(); I != E; ++I) { |
| 156 | FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName); |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 157 | if (ptr) { |
Jeff Cohen | c18671c | 2004-12-30 03:02:31 +0000 | [diff] [blame] | 158 | return (void *) ptr; |
Owen Anderson | f37feb9 | 2009-06-25 18:12:44 +0000 | [diff] [blame] | 159 | } |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 162 | #if defined(__MINGW32__) |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 163 | { |
| 164 | EXPLICIT_SYMBOL(_alloca); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 165 | EXPLICIT_SYMBOL(__main); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 166 | EXPLICIT_SYMBOL(__ashldi3); |
| 167 | EXPLICIT_SYMBOL(__ashrdi3); |
| 168 | EXPLICIT_SYMBOL(__cmpdi2); |
| 169 | EXPLICIT_SYMBOL(__divdi3); |
Anton Korobeynikov | 232a4ab | 2008-06-06 07:20:07 +0000 | [diff] [blame] | 170 | EXPLICIT_SYMBOL(__fixdfdi); |
| 171 | EXPLICIT_SYMBOL(__fixsfdi); |
| 172 | EXPLICIT_SYMBOL(__fixunsdfdi); |
| 173 | EXPLICIT_SYMBOL(__fixunssfdi); |
| 174 | EXPLICIT_SYMBOL(__floatdidf); |
| 175 | EXPLICIT_SYMBOL(__floatdisf); |
| 176 | EXPLICIT_SYMBOL(__lshrdi3); |
| 177 | EXPLICIT_SYMBOL(__moddi3); |
| 178 | EXPLICIT_SYMBOL(__udivdi3); |
| 179 | EXPLICIT_SYMBOL(__umoddi3); |
Anton Korobeynikov | 47ccf1a | 2008-02-22 10:08:31 +0000 | [diff] [blame] | 180 | |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 181 | EXPLICIT_SYMBOL2(alloca, _alloca); |
| 182 | #undef EXPLICIT_SYMBOL |
| 183 | #undef EXPLICIT_SYMBOL2 |
| 184 | #undef EXPLICIT_SYMBOL_DEF |
| 185 | } |
Anton Korobeynikov | 5248896 | 2007-06-25 07:12:14 +0000 | [diff] [blame] | 186 | #elif defined(_MSC_VER) |
| 187 | { |
| 188 | EXPLICIT_SYMBOL2(alloca, _alloca_probe); |
| 189 | EXPLICIT_SYMBOL2(_alloca, _alloca_probe); |
| 190 | #undef EXPLICIT_SYMBOL |
| 191 | #undef EXPLICIT_SYMBOL2 |
| 192 | #undef EXPLICIT_SYMBOL_DEF |
| 193 | } |
Anton Korobeynikov | cd79df0 | 2006-12-19 15:24:18 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
Jeff Cohen | 1a46635 | 2004-12-24 07:57:09 +0000 | [diff] [blame] | 196 | return 0; |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Reid Spencer | ebf8d0e | 2004-12-24 06:03:31 +0000 | [diff] [blame] | 199 | } |
| 200 | |