summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/Windows/DynamicLibrary.inc
diff options
context:
space:
mode:
authorVassil Vassilev <v.g.vassilev@gmail.com>2017-02-28 07:11:59 +0000
committerVassil Vassilev <v.g.vassilev@gmail.com>2017-02-28 07:11:59 +0000
commit44693083be3864c3e1196bc6f3d6672cf186fdee (patch)
tree9b342b6c1f1155d50b9b5c09fc4fb0119f94832b /llvm/lib/Support/Windows/DynamicLibrary.inc
parentcf66ea3845faeace3b928e948118e04ac62809cb (diff)
downloadbcm5719-llvm-44693083be3864c3e1196bc6f3d6672cf186fdee.tar.gz
bcm5719-llvm-44693083be3864c3e1196bc6f3d6672cf186fdee.zip
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
Diffstat (limited to 'llvm/lib/Support/Windows/DynamicLibrary.inc')
-rw-r--r--llvm/lib/Support/Windows/DynamicLibrary.inc37
1 files changed, 25 insertions, 12 deletions
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<HMODULE> *OpenedHandles;
+static llvm::ManagedStatic<DenseSet<HMODULE> > 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<HMODULE>();
-
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<HMODULE>();
-
+ 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<true> lock(*SymbolsMutex);
+ return addPermanentLibraryWithLock(handle, lock);
+}
+
+DynamicLibrary DynamicLibrary::addPermanentLibraryWithLock(void *handle,
+ sys::SmartScopedLock<true> &,
+ 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
OpenPOWER on IntegriCloud