blob: 2209e592091d8b313ebdcd96ffb05343e33bc173 [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__
Jeff Cohenc2b91622004-12-25 04:50:17 +000017#include <imagehlp.h>
18#else
Jeff Cohen1a466352004-12-24 07:57:09 +000019#include <dbghelp.h>
Jeff Cohenc2b91622004-12-25 04:50:17 +000020#endif
Jeff Cohen1a466352004-12-24 07:57:09 +000021
22#pragma comment(lib, "dbghelp.lib")
Reid Spencerebf8d0e2004-12-24 06:03:31 +000023
24namespace llvm {
25using namespace sys;
26
27//===----------------------------------------------------------------------===//
28//=== WARNING: Implementation here must contain only Win32 specific code
Jeff Cohen1a466352004-12-24 07:57:09 +000029//=== and must not be UNIX code.
Reid Spencerebf8d0e2004-12-24 06:03:31 +000030//===----------------------------------------------------------------------===//
31
Jeff Cohen1a466352004-12-24 07:57:09 +000032static std::vector<HMODULE> OpenedHandles;
33
Jeff Cohenc2b91622004-12-25 04:50:17 +000034extern "C" {
35 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
36 ULONG ModuleBase,
37 ULONG ModuleSize,
38 PVOID UserContext)
39 {
40 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
41 // into the process.
42 if (stricmp(ModuleName, "msvci70") != 0 &&
43 stricmp(ModuleName, "msvcirt") != 0 &&
44 stricmp(ModuleName, "msvcp50") != 0 &&
45 stricmp(ModuleName, "msvcp60") != 0 &&
46 stricmp(ModuleName, "msvcp70") != 0 &&
47 stricmp(ModuleName, "msvcr70") != 0 &&
48 stricmp(ModuleName, "msvcrt") != 0 &&
49 stricmp(ModuleName, "msvcrt20") != 0 &&
50 stricmp(ModuleName, "msvcrt40") != 0) {
51 OpenedHandles.push_back((HMODULE)ModuleBase);
52 }
53 return TRUE;
54 }
Jeff Cohen1a466352004-12-24 07:57:09 +000055}
56
Reid Spencerebf8d0e2004-12-24 06:03:31 +000057DynamicLibrary::DynamicLibrary() : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000058 handle = GetModuleHandle(NULL);
59 OpenedHandles.push_back((HMODULE)handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000060}
61
62DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Jeff Cohen1a466352004-12-24 07:57:09 +000063 HMODULE a_handle = LoadLibrary(filename);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000064
Jeff Cohen1a466352004-12-24 07:57:09 +000065 if (a_handle == 0)
66 ThrowError(std::string(filename) + ": Can't open : ");
67
68 handle = a_handle;
69 OpenedHandles.push_back(a_handle);
Reid Spencerebf8d0e2004-12-24 06:03:31 +000070}
71
72DynamicLibrary::~DynamicLibrary() {
Jeff Cohen1a466352004-12-24 07:57:09 +000073 if (handle == 0)
74 return;
75
76 // GetModuleHandle() does not increment the ref count, so we must not free
77 // the handle to the executable.
78 if (handle != GetModuleHandle(NULL))
79 FreeLibrary((HMODULE)handle);
80 handle = 0;
81
82 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
83 E = OpenedHandles.end(); I != E; ++I) {
84 if (*I == handle) {
85 // Note: don't use the swap/pop_back trick here. Order is important.
86 OpenedHandles.erase(I);
87 }
88 }
89}
90
91void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
92 if (filename) {
93 HMODULE a_handle = LoadLibrary(filename);
94
95 if (a_handle == 0)
96 ThrowError(std::string(filename) + ": Can't open : ");
97
98 OpenedHandles.push_back(a_handle);
99 } else {
100 // When no file is specified, enumerate all DLLs and EXEs in the
101 // process.
102 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
103 }
104
Jeff Cohenc2b91622004-12-25 04:50:17 +0000105 // Because we don't remember the handle, we will never free it; hence,
Jeff Cohen1a466352004-12-24 07:57:09 +0000106 // it is loaded permanently.
107}
108
109void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
110 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
111 E = OpenedHandles.end(); I != E; ++I) {
112 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
113 if (ptr)
Jeff Cohenc18671c2004-12-30 03:02:31 +0000114 return (void *) ptr;
Jeff Cohen1a466352004-12-24 07:57:09 +0000115 }
116
117 return 0;
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000118}
119
120void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
Jeff Cohen1a466352004-12-24 07:57:09 +0000121 assert(handle != 0 && "Invalid DynamicLibrary handle");
Jeff Cohenc18671c2004-12-30 03:02:31 +0000122 return (void *) GetProcAddress((HMODULE)handle, symbolName);
Reid Spencerebf8d0e2004-12-24 06:03:31 +0000123}
124
125}
126
127// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab