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 @@ -53,12 +53,15 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, std::string *errMsg) { - SmartScopedLock lock(*SymbolsMutex); + void *handle = nullptr; + { + SmartScopedLock lock(*SymbolsMutex); - void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL); - if (!handle) { - if (errMsg) *errMsg = dlerror(); - return DynamicLibrary(); + void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL); + if (!handle) { + if (errMsg) *errMsg = dlerror(); + return DynamicLibrary(); + } } #ifdef __CYGWIN__ @@ -67,14 +70,25 @@ 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()) { + if (errMsg) + *errMsg = std::string(filename) + ": Library already registered"; + SmartScopedLock lock(*SymbolsMutex); 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); } Index: lib/Support/Windows/DynamicLibrary.inc =================================================================== --- lib/Support/Windows/DynamicLibrary.inc +++ lib/Support/Windows/DynamicLibrary.inc @@ -89,15 +89,27 @@ 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()) { + MakeErrMsg(errMsg, std::string(filename) + ": Already loaded"); FreeLibrary(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(a_handle); + return DynamicLibrary((HMODULE)handle); } // Stack probing routines are in the support library (e.g. libgcc), but we don't