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,14 +67,15 @@ if (!filename) handle = RTLD_DEFAULT; #endif + return addPermanentLibrary(handle); +} - if (!OpenedHandles) - OpenedHandles = new DenseSet(); +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle) { + SmartScopedLock lock(*SymbolsMutex); - // If we've already loaded this library, dlclose() the handle in order to - // keep the internal refcount at +1. + // If we've already loaded this library, tell the caller. if (!OpenedHandles->insert(handle).second) - dlclose(handle); + return DynamicLibrary(); return DynamicLibrary(handle); } @@ -121,7 +120,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" @@ -98,6 +100,18 @@ return DynamicLibrary(a_handle); } +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 // have dynamic linking on windows. Provide a hook. #define EXPLICIT_SYMBOL(SYM) \