blob: d214ba0e727fa97216b02784bc3e35d65e2aca10 [file] [log] [blame]
Reid Spencerebf8d0e2004-12-24 06:03:31 +00001//===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Jeff Cohen1a466352004-12-24 07:57:09 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencerebf8d0e2004-12-24 06:03:31 +00006// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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
14#include "Win32.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
Reid Spencer48fdf912006-06-01 19:03:21 +000022#ifdef __MINGW32__
23 #if (HAVE_LIBIMAGEHLP != 1)
24 #error "libimagehlp.a should be present"
25 #endif
26#else
27 #pragma comment(lib, "dbghelp.lib")
28#endif
Reid Spencerebf8d0e2004-12-24 06:03:31 +000029
30namespace llvm {
31using namespace sys;
32
33//===----------------------------------------------------------------------===//
34//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen1a466352004-12-24 07:57:09 +000035//=== and must not be UNIX code.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000036//===----------------------------------------------------------------------===//
37
Jeff Cohen1a466352004-12-24 07:57:09 +000038static std::vector<HMODULE> OpenedHandles;
39
Jeff Cohenc2b91622004-12-25 04:50:17 +000040extern "C" {
41 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
42 ULONG ModuleBase,
43 ULONG ModuleSize,
44 PVOID UserContext)
45 {
46 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
47 // into the process.
48 if (stricmp(ModuleName, "msvci70") != 0 &&
49 stricmp(ModuleName, "msvcirt") != 0 &&
50 stricmp(ModuleName, "msvcp50") != 0 &&
51 stricmp(ModuleName, "msvcp60") != 0 &&
52 stricmp(ModuleName, "msvcp70") != 0 &&
53 stricmp(ModuleName, "msvcr70") != 0 &&
54 stricmp(ModuleName, "msvcrt") != 0 &&
55 stricmp(ModuleName, "msvcrt20") != 0 &&
56 stricmp(ModuleName, "msvcrt40") != 0) {
57 OpenedHandles.push_back((HMODULE)ModuleBase);
58 }
59 return TRUE;
60 }
Jeff Cohen1a466352004-12-24 07:57:09 +000061}
62
Reid Spencerebf8d0e2004-12-24 06:03:31 +000063DynamicLibrary::DynamicLibrary() : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000064 handle = GetModuleHandle(NULL);
65 OpenedHandles.push_back((HMODULE)handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000066}
67
68DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000069 HMODULE a_handle = LoadLibrary(filename);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000070
Jeff Cohen1a466352004-12-24 07:57:09 +000071 if (a_handle == 0)
72 ThrowError(std::string(filename) + ": Can't open : ");
73
74 handle = a_handle;
75 OpenedHandles.push_back(a_handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000076}
77
78DynamicLibrary::~DynamicLibrary() {
Jeff Cohen1a466352004-12-24 07:57:09 +000079 if (handle == 0)
80 return;
81
82 // GetModuleHandle() does not increment the ref count, so we must not free
83 // the handle to the executable.
84 if (handle != GetModuleHandle(NULL))
85 FreeLibrary((HMODULE)handle);
86 handle = 0;
87
88 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
89 E = OpenedHandles.end(); I != E; ++I) {
90 if (*I == handle) {
91 // Note: don't use the swap/pop_back trick here. Order is important.
92 OpenedHandles.erase(I);
93 }
94 }
95}
96
97void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
98 if (filename) {
99 HMODULE a_handle = LoadLibrary(filename);
100
Jeff Cohen715bd762006-01-29 22:02:52 +0000101 if (a_handle == 0)
102 ThrowError(std::string(filename) + ": Can't open : ");
Jeff Cohen1a466352004-12-24 07:57:09 +0000103
Jeff Cohen715bd762006-01-29 22:02:52 +0000104 OpenedHandles.push_back(a_handle);
Jeff Cohen1a466352004-12-24 07:57:09 +0000105 } else {
Jeff Cohen715bd762006-01-29 22:02:52 +0000106 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen1a466352004-12-24 07:57:09 +0000107 // process.
108 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
109 }
110
Jeff Cohenc2b91622004-12-25 04:50:17 +0000111 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +0000112 // it is loaded permanently.
113}
114
115void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohen85046902006-01-30 04:33:51 +0000116 // First check symbols added via AddSymbol().
117 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
118 if (I != g_symbols.end())
119 return I->second;
120
121 // Now search the libraries.
Jeff Cohen1a466352004-12-24 07:57:09 +0000122 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
123 E = OpenedHandles.end(); I != E; ++I) {
124 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
125 if (ptr)
Jeff Cohenc18671c2004-12-30 03:02:31 +0000126 return (void *) ptr;
Jeff Cohen1a466352004-12-24 07:57:09 +0000127 }
128
129 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000130}
131
132void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000133 assert(handle != 0 && "Invalid DynamicLibrary handle");
Jeff Cohenc18671c2004-12-30 03:02:31 +0000134 return (void *) GetProcAddress((HMODULE)handle, symbolName);
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000135}
136
137}
138