blob: 2c14366c0761d7a1a22d495d14141dedb7b59ef3 [file] [log] [blame]
Reid Spencer4befbf32004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Reid Spencer4befbf32004-12-24 06:03:31 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Michael J. Spencer447762d2010-11-29 18:16:10 +00007//
Reid Spencer4befbf32004-12-24 06:03:31 +00008//===----------------------------------------------------------------------===//
9//
Jeff Cohenf365c332004-12-25 04:50:17 +000010// This file provides the Win32 specific implementation of DynamicLibrary.
Reid Spencer4befbf32004-12-24 06:03:31 +000011//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "Windows.h"
Jeff Cohenf365c332004-12-25 04:50:17 +000015
Jeff Cohen07e22ba2005-02-19 03:01:13 +000016#ifdef __MINGW32__
Reid Spencer187b4ad2006-06-01 19:03:21 +000017 #include <imagehlp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000018#else
Reid Spencer187b4ad2006-06-01 19:03:21 +000019 #include <dbghelp.h>
Jeff Cohenf365c332004-12-25 04:50:17 +000020#endif
Jeff Cohen039b4ab2004-12-24 07:57:09 +000021
Stefanus Du Toitd2b7be62009-04-28 16:37:58 +000022#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
Reid Spencer187b4ad2006-06-01 19:03:21 +000026#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 Spencer4befbf32004-12-24 06:03:31 +000033
34namespace llvm {
35using namespace sys;
36
37//===----------------------------------------------------------------------===//
Michael J. Spencer447762d2010-11-29 18:16:10 +000038//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen039b4ab2004-12-24 07:57:09 +000039//=== and must not be UNIX code.
Reid Spencer4befbf32004-12-24 06:03:31 +000040//===----------------------------------------------------------------------===//
41
Jeff Cohen039b4ab2004-12-24 07:57:09 +000042static std::vector<HMODULE> OpenedHandles;
43
Chuck Rose III07b57d22007-11-21 00:37:56 +000044#ifdef _WIN64
45 typedef DWORD64 ModuleBaseType;
46#else
47 typedef ULONG ModuleBaseType;
48#endif
49
Jeff Cohenf365c332004-12-25 04:50:17 +000050extern "C" {
Stefanus Du Toitd2b7be62009-04-28 16:37:58 +000051// Use old callback if:
52// - Not using Visual Studio
Michael J. Spencer447762d2010-11-29 18:16:10 +000053// - Visual Studio 2005 or earlier but only if we are not using the Windows SDK
Stefanus Du Toitd2b7be62009-04-28 16:37:58 +000054// 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+
NAKAMURA Takumi684ef5e2011-02-09 04:18:12 +000058#if defined(_MSC_VER)
59 #if _MSC_VER < 1500 && (!defined(VER_PRODUCTBUILD) || VER_PRODUCTBUILD < 6000)
60 #define OLD_ELM_CALLBACK_DECL 1
61 #endif
62#elif defined(__MINGW64__)
63 // Use new callback.
64#elif defined(__MINGW32__)
65 #define OLD_ELM_CALLBACK_DECL 1
66#endif
67
68#ifdef OLD_ELM_CALLBACK_DECL
Jeff Cohenf365c332004-12-25 04:50:17 +000069 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
Chuck Rose III07b57d22007-11-21 00:37:56 +000070 ModuleBaseType ModuleBase,
Jeff Cohenf365c332004-12-25 04:50:17 +000071 ULONG ModuleSize,
72 PVOID UserContext)
Anton Korobeynikov66362102008-02-22 10:08:31 +000073#else
74 static BOOL CALLBACK ELM_Callback(PCSTR ModuleName,
75 ModuleBaseType ModuleBase,
76 ULONG ModuleSize,
77 PVOID UserContext)
78#endif
Jeff Cohenf365c332004-12-25 04:50:17 +000079 {
80 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
81 // into the process.
82 if (stricmp(ModuleName, "msvci70") != 0 &&
83 stricmp(ModuleName, "msvcirt") != 0 &&
84 stricmp(ModuleName, "msvcp50") != 0 &&
85 stricmp(ModuleName, "msvcp60") != 0 &&
86 stricmp(ModuleName, "msvcp70") != 0 &&
87 stricmp(ModuleName, "msvcr70") != 0 &&
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +000088#ifndef __MINGW32__
89 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
90 // Otherwise, user should be aware, what he's doing :)
Jeff Cohenf365c332004-12-25 04:50:17 +000091 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikov105682d2010-01-14 20:19:51 +000092#endif
Jeff Cohenf365c332004-12-25 04:50:17 +000093 stricmp(ModuleName, "msvcrt20") != 0 &&
94 stricmp(ModuleName, "msvcrt40") != 0) {
95 OpenedHandles.push_back((HMODULE)ModuleBase);
96 }
97 return TRUE;
98 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +000099}
100
Chris Lattner8c8858a2006-07-07 17:12:36 +0000101bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
Michael J. Spencer447762d2010-11-29 18:16:10 +0000102 std::string *ErrMsg) {
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000103 if (filename) {
104 HMODULE a_handle = LoadLibrary(filename);
105
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000106 if (a_handle == 0)
Reid Spencer50eac3b2006-08-25 21:37:17 +0000107 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000108
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000109 OpenedHandles.push_back(a_handle);
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000110 } else {
Jeff Cohen8ee89c72006-01-29 22:02:52 +0000111 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000112 // process.
113 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
114 }
115
Jeff Cohenf365c332004-12-25 04:50:17 +0000116 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000117 // it is loaded permanently.
Chris Lattner8c8858a2006-07-07 17:12:36 +0000118 return false;
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000119}
120
Anton Korobeynikov66362102008-02-22 10:08:31 +0000121// Stack probing routines are in the support library (e.g. libgcc), but we don't
122// have dynamic linking on windows. Provide a hook.
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000123#define EXPLICIT_SYMBOL(SYM) \
124 extern "C" { extern void *SYM; }
125#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
Anton Korobeynikov66362102008-02-22 10:08:31 +0000126
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000127#include "explicit_symbols.inc"
128
129#undef EXPLICIT_SYMBOL
130#undef EXPLICIT_SYMBOL2
Anton Korobeynikov66362102008-02-22 10:08:31 +0000131
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000132void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000133 // First check symbols added via AddSymbol().
Chris Lattnere4807c52009-07-08 00:52:12 +0000134 if (ExplicitSymbols) {
Michael J. Spencer447762d2010-11-29 18:16:10 +0000135 std::map<std::string, void *>::iterator I =
Chris Lattnere4807c52009-07-08 00:52:12 +0000136 ExplicitSymbols->find(symbolName);
137 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
138 if (I != E)
139 return I->second;
140 }
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000141
142 // Now search the libraries.
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000143 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
144 E = OpenedHandles.end(); I != E; ++I) {
145 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
Owen Anderson021c3b02009-06-25 18:12:44 +0000146 if (ptr) {
Jeff Cohena6d9c142004-12-30 03:02:31 +0000147 return (void *) ptr;
Owen Anderson021c3b02009-06-25 18:12:44 +0000148 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000149 }
150
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000151 #define EXPLICIT_SYMBOL(SYM) \
152 if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
153 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
154 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
Anton Korobeynikov66362102008-02-22 10:08:31 +0000155
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000156 {
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000157 #include "explicit_symbols.inc"
Anton Korobeynikov105682d2010-01-14 20:19:51 +0000158 }
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000159
160 #undef EXPLICIT_SYMBOL
161 #undef EXPLICIT_SYMBOL2
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000162
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000163 return 0;
Reid Spencer4befbf32004-12-24 06:03:31 +0000164}
165
Reid Spencer4befbf32004-12-24 06:03:31 +0000166}