Index: include/llvm/Support/DynamicLibrary.h =================================================================== --- include/llvm/Support/DynamicLibrary.h +++ include/llvm/Support/DynamicLibrary.h @@ -68,6 +68,14 @@ static DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg = nullptr); + /// Registers an externally loaded library. The library will be unloaded + /// when the program terminates. + /// + /// It is safe to call this function multiple times for the same library. + /// + /// \returns An empty \p DynamicLibrary on failure. + static DynamicLibrary addPermanentLibrary(void *handle); + /// This function permanently loads the dynamic library at the given path. /// Use this instead of getPermanentLibrary() when you won't need to get /// symbols from the library itself. Index: lib/Support/DynamicLibrary.cpp =================================================================== --- lib/Support/DynamicLibrary.cpp +++ lib/Support/DynamicLibrary.cpp @@ -9,8 +9,6 @@ // // This file implements the operating system DynamicLibrary concept. // -// FIXME: This file leaks ExplicitSymbols and OpenedHandles! -// //===----------------------------------------------------------------------===// #include "llvm/Support/DynamicLibrary.h" @@ -51,7 +49,7 @@ //=== independent code. //===----------------------------------------------------------------------===// -static DenseSet *OpenedHandles = nullptr; +static llvm::ManagedStatic > OpenedHandles; DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, std::string *errMsg) { @@ -69,15 +67,22 @@ if (!filename) handle = RTLD_DEFAULT; #endif - - if (!OpenedHandles) - OpenedHandles = new DenseSet(); - + DynamicLibrary dyLib = addPermanentLibrary(handle); // If we've already loaded this library, dlclose() the handle in order to // keep the internal refcount at +1. - if (!OpenedHandles->insert(handle).second) + if (!dyLib.isValid()) dlclose(handle); + return dyLib; +} + +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) { + SmartScopedLock lock(*SymbolsMutex); + + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert(handle).second) + return DynamicLibrary(); + return DynamicLibrary(handle); } @@ -121,7 +126,7 @@ #if defined(HAVE_DLFCN_H) && defined(HAVE_DLOPEN) // Now search the libraries. - if (OpenedHandles) { + if (OpenedHandles.isConstructed()) { for (DenseSet::iterator I = OpenedHandles->begin(), E = OpenedHandles->end(); I != E; ++I) { //lt_ptr ptr = lt_dlsym(*I, symbolName); Index: lib/Support/Windows/DynamicLibrary.inc =================================================================== --- lib/Support/Windows/DynamicLibrary.inc +++ lib/Support/Windows/DynamicLibrary.inc @@ -9,6 +9,8 @@ // // This file provides the Win32 specific implementation of DynamicLibrary. // +// FIXME: This file leaks OpenedHandles! +// //===----------------------------------------------------------------------===// #include "WindowsSupport.h" @@ -87,15 +89,25 @@ return DynamicLibrary(); } - if (OpenedHandles == 0) - OpenedHandles = new DenseSet(); - + DynamicLibrary dyLib = addPermanentLibrary(a_handle); // 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()) FreeLibrary(a_handle); - return DynamicLibrary(a_handle); + return dyLib; +} + +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) { + SmartScopedLock lock(*SymbolsMutex); + if (OpenedHandles == 0) + OpenedHandles = new DenseSet(); + + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert((const HMODULE)handle).second) + return DynamicLibrary(); + + return DynamicLibrary((HMODULE)handle); } // Stack probing routines are in the support library (e.g. libgcc), but we don't