blob: 4227844ae506b650c70154dffa6bf3cece66e038 [file] [log] [blame]
Reid Spencerebf8d0e2004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +00002//
Reid Spencerebf8d0e2004-12-24 06:03:31 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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. Spencer1f6efa32010-11-29 18:16:10 +00007//
Reid Spencerebf8d0e2004-12-24 06:03:31 +00008//===----------------------------------------------------------------------===//
9//
Jeff Cohenc2b91622004-12-25 04:50:17 +000010// This file provides the Win32 specific implementation of DynamicLibrary.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000011//
12//===----------------------------------------------------------------------===//
13
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000014#include "Windows.h"
Jeff Cohenc2b91622004-12-25 04:50:17 +000015
Jeff Cohen23a1cf32005-02-19 03:01:13 +000016#ifdef __MINGW32__
Reid Spencer48fdf912006-06-01 19:03:21 +000017 #include <imagehlp.h>
Jeff Cohenc2b91622004-12-25 04:50:17 +000018#else
Reid Spencer48fdf912006-06-01 19:03:21 +000019 #include <dbghelp.h>
Jeff Cohenc2b91622004-12-25 04:50:17 +000020#endif
Jeff Cohen1a466352004-12-24 07:57:09 +000021
Stefanus Du Toit5a4e11d2009-04-28 16:37:58 +000022#ifdef _MSC_VER
23 #include <ntverp.h>
24#endif
25
Reid Spencer48fdf912006-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 Spencerebf8d0e2004-12-24 06:03:31 +000033
34namespace llvm {
35using namespace sys;
36
37//===----------------------------------------------------------------------===//
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000038//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen1a466352004-12-24 07:57:09 +000039//=== and must not be UNIX code.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000040//===----------------------------------------------------------------------===//
41
Jeff Cohen1a466352004-12-24 07:57:09 +000042static std::vector<HMODULE> OpenedHandles;
43
Jeff Cohenc2b91622004-12-25 04:50:17 +000044extern "C" {
NAKAMURA Takumiad1b4532011-02-09 04:18:12 +000045
NAKAMURA Takumi6073a052011-05-01 13:29:49 +000046 static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
47 ULONG_PTR ModuleBase,
Jeff Cohenc2b91622004-12-25 04:50:17 +000048 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 &&
Anton Korobeynikovcd79df02006-12-19 15:24:18 +000059#ifndef __MINGW32__
60 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
61 // Otherwise, user should be aware, what he's doing :)
Jeff Cohenc2b91622004-12-25 04:50:17 +000062 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikovf4b6d882010-01-14 20:19:51 +000063#endif
Jeff Cohenc2b91622004-12-25 04:50:17 +000064 stricmp(ModuleName, "msvcrt20") != 0 &&
65 stricmp(ModuleName, "msvcrt40") != 0) {
66 OpenedHandles.push_back((HMODULE)ModuleBase);
67 }
68 return TRUE;
69 }
Jeff Cohen1a466352004-12-24 07:57:09 +000070}
71
Chris Lattneradcbce02006-07-07 17:12:36 +000072bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000073 std::string *ErrMsg) {
Jeff Cohen1a466352004-12-24 07:57:09 +000074 if (filename) {
75 HMODULE a_handle = LoadLibrary(filename);
76
Jeff Cohen715bd762006-01-29 22:02:52 +000077 if (a_handle == 0)
Reid Spencer05545752006-08-25 21:37:17 +000078 return MakeErrMsg(ErrMsg, std::string(filename) + ": Can't open : ");
Jeff Cohen1a466352004-12-24 07:57:09 +000079
Jeff Cohen715bd762006-01-29 22:02:52 +000080 OpenedHandles.push_back(a_handle);
Jeff Cohen1a466352004-12-24 07:57:09 +000081 } else {
Jeff Cohen715bd762006-01-29 22:02:52 +000082 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen1a466352004-12-24 07:57:09 +000083 // process.
84 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
85 }
86
Jeff Cohenc2b91622004-12-25 04:50:17 +000087 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +000088 // it is loaded permanently.
Chris Lattneradcbce02006-07-07 17:12:36 +000089 return false;
Jeff Cohen1a466352004-12-24 07:57:09 +000090}
91
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +000092// Stack probing routines are in the support library (e.g. libgcc), but we don't
93// have dynamic linking on windows. Provide a hook.
NAKAMURA Takumi1f6832a2011-02-05 15:11:53 +000094#define EXPLICIT_SYMBOL(SYM) \
95 extern "C" { extern void *SYM; }
96#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +000097
NAKAMURA Takumi1f6832a2011-02-05 15:11:53 +000098#include "explicit_symbols.inc"
99
100#undef EXPLICIT_SYMBOL
101#undef EXPLICIT_SYMBOL2
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +0000102
Jeff Cohen1a466352004-12-24 07:57:09 +0000103void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohen85046902006-01-30 04:33:51 +0000104 // First check symbols added via AddSymbol().
Chris Lattner01d9a1b2009-07-08 00:52:12 +0000105 if (ExplicitSymbols) {
Michael J. Spencer1f6efa32010-11-29 18:16:10 +0000106 std::map<std::string, void *>::iterator I =
Chris Lattner01d9a1b2009-07-08 00:52:12 +0000107 ExplicitSymbols->find(symbolName);
108 std::map<std::string, void *>::iterator E = ExplicitSymbols->end();
109 if (I != E)
110 return I->second;
111 }
Jeff Cohen85046902006-01-30 04:33:51 +0000112
113 // Now search the libraries.
Jeff Cohen1a466352004-12-24 07:57:09 +0000114 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
115 E = OpenedHandles.end(); I != E; ++I) {
116 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
Owen Andersonf37feb92009-06-25 18:12:44 +0000117 if (ptr) {
Jeff Cohenc18671c2004-12-30 03:02:31 +0000118 return (void *) ptr;
Owen Andersonf37feb92009-06-25 18:12:44 +0000119 }
Jeff Cohen1a466352004-12-24 07:57:09 +0000120 }
121
NAKAMURA Takumi1f6832a2011-02-05 15:11:53 +0000122 #define EXPLICIT_SYMBOL(SYM) \
123 if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
124 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
125 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
Anton Korobeynikov47ccf1a2008-02-22 10:08:31 +0000126
Anton Korobeynikov52488962007-06-25 07:12:14 +0000127 {
NAKAMURA Takumi1f6832a2011-02-05 15:11:53 +0000128 #include "explicit_symbols.inc"
Anton Korobeynikovf4b6d882010-01-14 20:19:51 +0000129 }
NAKAMURA Takumi1f6832a2011-02-05 15:11:53 +0000130
131 #undef EXPLICIT_SYMBOL
132 #undef EXPLICIT_SYMBOL2
Anton Korobeynikovcd79df02006-12-19 15:24:18 +0000133
Jeff Cohen1a466352004-12-24 07:57:09 +0000134 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000135}
136
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000137}