blob: 5ed0b709fa688885ab7bb44d244539c10fc699cf [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
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000014#include "WindowsSupport.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
Jordy Rosea19917d2011-08-17 00:29:32 +000042static DenseSet<HMODULE> *OpenedHandles;
Jeff Cohen039b4ab2004-12-24 07:57:09 +000043
Jeff Cohenf365c332004-12-25 04:50:17 +000044extern "C" {
NAKAMURA Takumi684ef5e2011-02-09 04:18:12 +000045
NAKAMURA Takumi4471f822011-05-01 13:29:49 +000046 static BOOL CALLBACK ELM_Callback(WIN32_ELMCB_PCSTR ModuleName,
47 ULONG_PTR ModuleBase,
Jeff Cohenf365c332004-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 Korobeynikoveef04ba2006-12-19 15:24:18 +000059#ifndef __MINGW32__
60 // Mingw32 uses msvcrt.dll by default. Don't ignore it.
Eric Christopher650c8f22014-05-20 17:11:11 +000061 // Otherwise the user should be aware what they are doing.
Jeff Cohenf365c332004-12-25 04:50:17 +000062 stricmp(ModuleName, "msvcrt") != 0 &&
Anton Korobeynikov105682d2010-01-14 20:19:51 +000063#endif
Jeff Cohenf365c332004-12-25 04:50:17 +000064 stricmp(ModuleName, "msvcrt20") != 0 &&
65 stricmp(ModuleName, "msvcrt40") != 0) {
Jordy Rose1a3ca922011-08-17 00:59:50 +000066 OpenedHandles->insert((HMODULE)ModuleBase);
Jeff Cohenf365c332004-12-25 04:50:17 +000067 }
68 return TRUE;
69 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +000070}
71
Jordy Rosea19917d2011-08-17 00:29:32 +000072DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename,
73 std::string *errMsg) {
Filip Pizlo57093e82013-09-18 16:40:14 +000074 SmartScopedLock<true> lock(*SymbolsMutex);
Jordy Rose5765f4c2011-08-22 19:01:52 +000075
Jordy Rosea19917d2011-08-17 00:29:32 +000076 if (!filename) {
77 // When no file is specified, enumerate all DLLs and EXEs in the process.
Jordy Rosea19917d2011-08-17 00:29:32 +000078 if (OpenedHandles == 0)
79 OpenedHandles = new DenseSet<HMODULE>();
Jeff Cohen039b4ab2004-12-24 07:57:09 +000080
Jeff Cohen039b4ab2004-12-24 07:57:09 +000081 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
Jordy Rosea19917d2011-08-17 00:29:32 +000082 // Dummy library that represents "search all handles".
83 // This is mostly to ensure that the return value still shows up as "valid".
84 return DynamicLibrary(&OpenedHandles);
85 }
David Majnemer61eae2e2013-10-07 01:00:07 +000086
87 SmallVector<wchar_t, MAX_PATH> filenameUnicode;
Rafael Espindolaadccf862014-06-12 21:53:57 +000088 if (std::error_code ec = windows::UTF8ToUTF16(filename, filenameUnicode)) {
David Majnemer61eae2e2013-10-07 01:00:07 +000089 SetLastError(ec.value());
90 MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16: ");
91 return DynamicLibrary();
92 }
Jordy Rosea19917d2011-08-17 00:29:32 +000093
David Majnemer61eae2e2013-10-07 01:00:07 +000094 HMODULE a_handle = LoadLibraryW(filenameUnicode.data());
Jordy Rosea19917d2011-08-17 00:29:32 +000095
96 if (a_handle == 0) {
Jordy Rose1a3ca922011-08-17 00:59:50 +000097 MakeErrMsg(errMsg, std::string(filename) + ": Can't open : ");
Jordy Rosea19917d2011-08-17 00:29:32 +000098 return DynamicLibrary();
Jeff Cohen039b4ab2004-12-24 07:57:09 +000099 }
100
Jordy Rosea19917d2011-08-17 00:29:32 +0000101 if (OpenedHandles == 0)
102 OpenedHandles = new DenseSet<HMODULE>();
103
104 // If we've already loaded this library, FreeLibrary() the handle in order to
105 // keep the internal refcount at +1.
106 if (!OpenedHandles->insert(a_handle).second)
107 FreeLibrary(a_handle);
108
109 return DynamicLibrary(a_handle);
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000110}
111
Anton Korobeynikov66362102008-02-22 10:08:31 +0000112// Stack probing routines are in the support library (e.g. libgcc), but we don't
113// have dynamic linking on windows. Provide a hook.
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000114#define EXPLICIT_SYMBOL(SYM) \
115 extern "C" { extern void *SYM; }
116#define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) EXPLICIT_SYMBOL(SYMTO)
Anton Korobeynikov66362102008-02-22 10:08:31 +0000117
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000118#include "explicit_symbols.inc"
119
120#undef EXPLICIT_SYMBOL
121#undef EXPLICIT_SYMBOL2
Anton Korobeynikov66362102008-02-22 10:08:31 +0000122
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000123void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
Filip Pizlo57093e82013-09-18 16:40:14 +0000124 SmartScopedLock<true> Lock(*SymbolsMutex);
Jordy Rosea19917d2011-08-17 00:29:32 +0000125
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000126 // First check symbols added via AddSymbol().
Filip Pizlo57093e82013-09-18 16:40:14 +0000127 if (ExplicitSymbols.isConstructed()) {
Jordy Rosea19917d2011-08-17 00:29:32 +0000128 StringMap<void *>::iterator i = ExplicitSymbols->find(symbolName);
129
130 if (i != ExplicitSymbols->end())
131 return i->second;
Chris Lattnere4807c52009-07-08 00:52:12 +0000132 }
Jeff Cohenbaeb39c2006-01-30 04:33:51 +0000133
134 // Now search the libraries.
Jordy Rosea19917d2011-08-17 00:29:32 +0000135 if (OpenedHandles) {
136 for (DenseSet<HMODULE>::iterator I = OpenedHandles->begin(),
137 E = OpenedHandles->end(); I != E; ++I) {
138 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
139 if (ptr) {
140 return (void *)(intptr_t)ptr;
141 }
Owen Anderson021c3b02009-06-25 18:12:44 +0000142 }
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000143 }
144
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000145 #define EXPLICIT_SYMBOL(SYM) \
146 if (!strcmp(symbolName, #SYM)) return (void*)&SYM;
147 #define EXPLICIT_SYMBOL2(SYMFROM, SYMTO) \
148 if (!strcmp(symbolName, #SYMFROM)) return (void*)&SYMTO;
Anton Korobeynikov66362102008-02-22 10:08:31 +0000149
Anton Korobeynikovec9038bc2007-06-25 07:12:14 +0000150 {
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000151 #include "explicit_symbols.inc"
Anton Korobeynikov105682d2010-01-14 20:19:51 +0000152 }
NAKAMURA Takumi03a541f2011-02-05 15:11:53 +0000153
154 #undef EXPLICIT_SYMBOL
155 #undef EXPLICIT_SYMBOL2
Anton Korobeynikoveef04ba2006-12-19 15:24:18 +0000156
Jeff Cohen039b4ab2004-12-24 07:57:09 +0000157 return 0;
Reid Spencer4befbf32004-12-24 06:03:31 +0000158}
159
Jordy Rosea19917d2011-08-17 00:29:32 +0000160
161void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) {
162 if (!isValid())
163 return NULL;
164 if (Data == &OpenedHandles)
165 return SearchForAddressOfSymbol(symbolName);
166 return (void *)(intptr_t)GetProcAddress((HMODULE)Data, symbolName);
167}
168
169
Reid Spencer4befbf32004-12-24 06:03:31 +0000170}