diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/Support/DynamicLibrary.h | 11 | ||||
-rw-r--r-- | llvm/lib/Support/DynamicLibrary.cpp | 12 | ||||
-rw-r--r-- | llvm/lib/Support/Windows/DynamicLibrary.inc | 12 |
3 files changed, 35 insertions, 0 deletions
diff --git a/llvm/include/llvm/Support/DynamicLibrary.h b/llvm/include/llvm/Support/DynamicLibrary.h index a7d22212dbd..eda98b928a6 100644 --- a/llvm/include/llvm/Support/DynamicLibrary.h +++ b/llvm/include/llvm/Support/DynamicLibrary.h @@ -14,6 +14,8 @@ #ifndef LLVM_SUPPORT_DYNAMICLIBRARY_H #define LLVM_SUPPORT_DYNAMICLIBRARY_H +#include "llvm/Support/Mutex.h" + #include <string> namespace llvm { @@ -68,6 +70,15 @@ namespace sys { 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 if the library was already loaded. + static DynamicLibrary addPermanentLibrary(void *handle, + std::string *errMsg = nullptr); + /// 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. diff --git a/llvm/lib/Support/DynamicLibrary.cpp b/llvm/lib/Support/DynamicLibrary.cpp index 74d94d33d5a..92ce6185306 100644 --- a/llvm/lib/Support/DynamicLibrary.cpp +++ b/llvm/lib/Support/DynamicLibrary.cpp @@ -76,6 +76,18 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, return DynamicLibrary(handle); } +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle, + std::string *errMsg) { + SmartScopedLock<true> lock(*SymbolsMutex); + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert(handle).second) { + if (errMsg) *errMsg = "Library already loaded"; + return DynamicLibrary(); + } + + return DynamicLibrary(handle); +} + void *DynamicLibrary::getAddressOfSymbol(const char *symbolName) { if (!isValid()) return nullptr; diff --git a/llvm/lib/Support/Windows/DynamicLibrary.inc b/llvm/lib/Support/Windows/DynamicLibrary.inc index b22c5ee7e42..a0d1d0536d3 100644 --- a/llvm/lib/Support/Windows/DynamicLibrary.inc +++ b/llvm/lib/Support/Windows/DynamicLibrary.inc @@ -92,6 +92,18 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, return DynamicLibrary(a_handle); } +DynamicLibrary DynamicLibrary::addPermanentLibrary(void *handle, + std::string *errMsg) { + SmartScopedLock<true> lock(*SymbolsMutex); + // If we've already loaded this library, tell the caller. + if (!OpenedHandles->insert(handle).second) { + MakeErrMsg(errMsg, "Library already loaded"); + return DynamicLibrary(); + } + + return DynamicLibrary(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) \ |