From 44693083be3864c3e1196bc6f3d6672cf186fdee Mon Sep 17 00:00:00 2001 From: Vassil Vassilev Date: Tue, 28 Feb 2017 07:11:59 +0000 Subject: Allow externally dlopen-ed libraries to be registered as permanent libraries. This is also useful in cases when llvm is in a shared library. First we dlopen the llvm shared library and then we register it as a permanent library in order to keep the JIT and other services working. Patch reviewed by Vedant Kumar (D29955)! llvm-svn: 296442 --- llvm/lib/Support/Windows/DynamicLibrary.inc | 37 +++++++++++++++++++---------- 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'llvm/lib/Support/Windows/DynamicLibrary.inc') diff --git a/llvm/lib/Support/Windows/DynamicLibrary.inc b/llvm/lib/Support/Windows/DynamicLibrary.inc index 87dde46de3c..264754942ba 100644 --- a/llvm/lib/Support/Windows/DynamicLibrary.inc +++ b/llvm/lib/Support/Windows/DynamicLibrary.inc @@ -9,8 +9,6 @@ // // This file provides the Win32 specific implementation of DynamicLibrary. // -// FIXME: This file leaks OpenedHandles! -// //===----------------------------------------------------------------------===// #include "WindowsSupport.h" @@ -35,7 +33,7 @@ using namespace sys; typedef BOOL (WINAPI *fpEnumerateLoadedModules)(HANDLE,PENUMLOADED_MODULES_CALLBACK64,PVOID); static fpEnumerateLoadedModules fEnumerateLoadedModules; -static DenseSet *OpenedHandles; +static llvm::ManagedStatic > OpenedHandles; static bool loadDebugHelp(void) { HMODULE hLib = ::LoadLibraryW(L"Dbghelp.dll"); @@ -59,9 +57,6 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, if (!filename) { // When no file is specified, enumerate all DLLs and EXEs in the process. - if (OpenedHandles == 0) - OpenedHandles = new DenseSet(); - if (!fEnumerateLoadedModules) { if (!loadDebugHelp()) { assert(false && "These APIs should always be available"); @@ -81,7 +76,7 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, MakeErrMsg(errMsg, std::string(filename) + ": Can't convert to UTF-16"); return DynamicLibrary(); } - + HMODULE a_handle = LoadLibraryW(filenameUnicode.data()); if (a_handle == 0) { @@ -89,15 +84,33 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, return DynamicLibrary(); } - if (OpenedHandles == 0) - OpenedHandles = new DenseSet(); - + DynamicLibrary dyLib = addPermanentLibraryWithLock(a_handle, lock, !filename); // If we've already loaded this library, FreeLibrary() the handle in order to // keep the internal refcount at +1. - if (!OpenedHandles->insert(a_handle).second) + if (!dyLib.isValid()) { + MakeErrMsg(errMsg, std::string(filename) + ": Already loaded"); FreeLibrary(a_handle); + } + + return dyLib; +} + +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) { + SmartScopedLock lock(*SymbolsMutex); + return addPermanentLibraryWithLock(handle, lock); +} + +DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle, + sys::SmartScopedLock &, + bool isMainExec) { + // If we've already loaded this library, tell the caller. + // FIXME: Note that multiple requests for adding the main executable is not + // considered as an error. If we don't want to treat the main executable as a + // special case we need to do a cleanup in the MCJIT tests and API. + if (!OpenedHandles->insert((const HMODULE)handle).second && !isMainExec) + return DynamicLibrary(); - return DynamicLibrary(a_handle); + return DynamicLibrary((HMODULE)handle); } // Stack probing routines are in the support library (e.g. libgcc), but we don't -- cgit v1.2.3