blob: 287a4c009e688b6109458bcdaa593a8c16b57a96 [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
Chris Lattneradcbce02006-07-07 17:12:36 +000097bool DynamicLibrary::LoadLibraryPermanently(const char *filename,
98 std::string *ErrMsg) {
Jeff Cohen1a466352004-12-24 07:57:09 +000099 if (filename) {
100 HMODULE a_handle = LoadLibrary(filename);
101
Jeff Cohen715bd762006-01-29 22:02:52 +0000102 if (a_handle == 0)
Chris Lattneradcbce02006-07-07 17:12:36 +0000103 return GetError(std::string(filename) + ": Can't open : ", ErrMsg);
Jeff Cohen1a466352004-12-24 07:57:09 +0000104
Jeff Cohen715bd762006-01-29 22:02:52 +0000105 OpenedHandles.push_back(a_handle);
Jeff Cohen1a466352004-12-24 07:57:09 +0000106 } else {
Jeff Cohen715bd762006-01-29 22:02:52 +0000107 // When no file is specified, enumerate all DLLs and EXEs in the
Jeff Cohen1a466352004-12-24 07:57:09 +0000108 // process.
109 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
110 }
111
Jeff Cohenc2b91622004-12-25 04:50:17 +0000112 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +0000113 // it is loaded permanently.
Chris Lattneradcbce02006-07-07 17:12:36 +0000114 return false;
Jeff Cohen1a466352004-12-24 07:57:09 +0000115}
116
117void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Jeff Cohen85046902006-01-30 04:33:51 +0000118 // First check symbols added via AddSymbol().
119 std::map<std::string, void *>::iterator I = g_symbols.find(symbolName);
120 if (I != g_symbols.end())
121 return I->second;
122
123 // Now search the libraries.
Jeff Cohen1a466352004-12-24 07:57:09 +0000124 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
125 E = OpenedHandles.end(); I != E; ++I) {
126 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
127 if (ptr)
Jeff Cohenc18671c2004-12-30 03:02:31 +0000128 return (void *) ptr;
Jeff Cohen1a466352004-12-24 07:57:09 +0000129 }
130
131 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000132}
133
134void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000135 assert(handle != 0 && "Invalid DynamicLibrary handle");
Jeff Cohenc18671c2004-12-30 03:02:31 +0000136 return (void *) GetProcAddress((HMODULE)handle, symbolName);
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000137}
138
139}
140