[llvm] [orc-rt] Refactor dynamic library lookup API (PR #224611)
Jared Wyles via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 18 04:57:25 PDT 2026
https://github.com/jaredwy created https://github.com/llvm/llvm-project/pull/224611
This is a refactor of the dylib work to make the windows implementation a bit better.l
The sys layer now exposes raw platform library handles and distinct APIs for library-specific and global symbol lookup.
This is because there is no real equiv on windows, so made it a bit more type safe and added a few more asserts around the types now that we can check what we have in terms of handles.
>From 1024d64a4c08a942cc6fd3f50d3093991ea6f54c Mon Sep 17 00:00:00 2001
From: Jared Wyles <jared.wyles at gmail.com>
Date: Fri, 18 Sep 2026 21:55:09 +1000
Subject: [PATCH] [orc-rt] Refactor dynamic library lookup API
---
.../bedrock/sys/DynamicLibrary.h | 22 +++---
.../orc-rt/bedrock/NativeDylibManager.h | 10 +++
orc-rt/lib/bedrock/NativeDylibManager.cpp | 77 +++++++++++++++----
.../lib/bedrock/sys/posix/DynamicLibrary.cpp | 64 +++++++++------
4 files changed, 121 insertions(+), 52 deletions(-)
diff --git a/orc-rt/include/orc-rt-internal/bedrock/sys/DynamicLibrary.h b/orc-rt/include/orc-rt-internal/bedrock/sys/DynamicLibrary.h
index 62b6d74a71b0f..d06cc613ea921 100644
--- a/orc-rt/include/orc-rt-internal/bedrock/sys/DynamicLibrary.h
+++ b/orc-rt/include/orc-rt-internal/bedrock/sys/DynamicLibrary.h
@@ -25,23 +25,23 @@
namespace orc_rt::sys {
-/// Returns a handle that looks up symbols in every library loaded into the
-/// process.
-void *globalLookupHandle();
+using DylibHandle = void *;
+using SymbolLookupResult = std::vector<std::optional<void *>>;
/// Load the library at the given path. Path must not be empty.
-Expected<void *> loadLibrary(const std::string &Path);
+Expected<DylibHandle> loadLibrary(const std::string &Path);
/// Unload a library previously returned by loadLibrary.
-Error unloadLibrary(void *Handle);
+Error unloadLibrary(DylibHandle Handle);
/// Look Names up in Handle, returning one result per name in order.
-///
-/// A result is nullopt if the name is not present in the library, and a
-/// (possibly null) address if it is: a symbol genuinely located at address zero
-/// is reported as null rather than as missing.
-std::vector<std::optional<void *>>
-lookupLibrarySymbols(void *Handle, const std::vector<std::string> &Names);
+SymbolLookupResult
+lookupLibrarySymbols(DylibHandle Handle,
+ const std::vector<std::string> &Names);
+
+/// Look Names up across libraries loaded into the process.
+SymbolLookupResult
+lookupGlobalSymbols(const std::vector<std::string> &Names);
} // namespace orc_rt::sys
diff --git a/orc-rt/include/orc-rt/bedrock/NativeDylibManager.h b/orc-rt/include/orc-rt/bedrock/NativeDylibManager.h
index e7a921c13aa1b..f6e1e0d8ba8ec 100644
--- a/orc-rt/include/orc-rt/bedrock/NativeDylibManager.h
+++ b/orc-rt/include/orc-rt/bedrock/NativeDylibManager.h
@@ -31,6 +31,16 @@ class Session;
class NativeDylibManager : public Service {
public:
enum LookupFlags { RequiredSymbol, WeaklyReferencedSymbol };
+
+ struct DylibHandle {
+ enum class Kind {
+ Global,
+ Library,
+ };
+
+ Kind K;
+ void *LibraryHandle = nullptr;
+ };
using SymbolLookupSet = std::vector<std::pair<std::string, LookupFlags>>;
/// Create a NativeDylibManager, adding associated symbols to the given
diff --git a/orc-rt/lib/bedrock/NativeDylibManager.cpp b/orc-rt/lib/bedrock/NativeDylibManager.cpp
index d26f92e24851e..655ce25130f6e 100644
--- a/orc-rt/lib/bedrock/NativeDylibManager.cpp
+++ b/orc-rt/lib/bedrock/NativeDylibManager.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "orc-rt/bedrock/NativeDylibManager.h"
+
#include "orc-rt-internal/bedrock/sys/DynamicLibrary.h"
#include "orc-rt/bedrock/Session.h"
@@ -20,15 +21,16 @@ Expected<std::unique_ptr<NativeDylibManager>>
NativeDylibManager::Create(Session &S, SimpleSymbolTable &ST,
const char *InstanceName,
SimpleSymbolTable::MutatorFn AddInterface) {
-
std::unique_ptr<NativeDylibManager> Instance(new NativeDylibManager(S));
SimpleSymbolTable NDMST;
if (auto Err = AddInterface(NDMST))
return Err;
+
std::pair<SymbolNameSpec, const void *> InstanceSym[] = {
{SymbolNameSpec::c(InstanceName),
static_cast<const void *>(Instance.get())}};
+
if (auto Err = NDMST.addUnique(InstanceSym))
return std::move(Err);
@@ -38,38 +40,79 @@ NativeDylibManager::Create(Session &S, SimpleSymbolTable &ST,
return std::move(Instance);
}
-void NativeDylibManager::load(OnLoadCompleteFn &&OnComplete, std::string Path) {
- // Empty path -> global handle; no shutdown callback (RTLD_DEFAULT
- // mustn't be dlclose'd).
- if (Path.empty())
- return OnComplete(sys::globalLookupHandle());
+void NativeDylibManager::load(OnLoadCompleteFn &&OnComplete,
+ std::string Path) {
+ // Empty path means "global lookup scope".
+ if (Path.empty()) {
+ static DylibHandle GlobalHandle{DylibHandle::Kind::Global, nullptr};
+ return OnComplete(static_cast<void *>(&GlobalHandle));
+ }
auto H = sys::loadLibrary(Path);
if (!H)
return OnComplete(H.takeError());
- // Capture S by reference, rather than this, so that the callback remains
- // valid even if the NativeDylibManager is destroyed prior to shutdown.
- S.addOnShutdown([&S = this->S, Handle = *H]() {
- if (auto Err = sys::unloadLibrary(Handle))
+ auto Handle = std::make_unique<DylibHandle>(
+ DylibHandle{DylibHandle::Kind::Library, *H});
+
+ DylibHandle *Dylib = Handle.get();
+ assert(Dylib && "failed to create dylib handle");
+ if (!Dylib)
+ return OnComplete(
+ make_error<StringError>("failed to create dylib handle"));
+
+ // Capture S by reference rather than this so the callback remains valid even
+ // if NativeDylibManager is destroyed prior to shutdown.
+ S.addOnShutdown([&S = this->S, Handle = std::move(Handle)]() mutable {
+ DylibHandle *Dylib = Handle.get();
+ assert(Dylib && "dylib handle unexpectedly null");
+ if (!Dylib)
+ return;
+
+ assert(Dylib->K == DylibHandle::Kind::Library &&
+ "global dylib handle must not be unloaded");
+ assert(Dylib->LibraryHandle && "invalid library handle");
+
+ if (auto Err = sys::unloadLibrary(Dylib->LibraryHandle))
S.reportError(std::move(Err));
});
- OnComplete(std::move(H));
+
+ OnComplete(static_cast<void *>(Dylib));
}
void NativeDylibManager::lookup(OnLookupCompleteFn &&OnLookupComplete,
void *Handle, SymbolLookupSet Symbols) {
+ DylibHandle *Dylib = static_cast<DylibHandle *>(Handle);
+
+ assert(Dylib && "invalid dylib handle");
+ if (!Dylib)
+ return OnLookupComplete(
+ make_error<StringError>("invalid dylib handle"));
+
std::vector<std::string> Names;
Names.reserve(Symbols.size());
for (auto &S : Symbols)
Names.push_back(std::move(S.first));
- auto Addrs = sys::lookupLibrarySymbols(Handle, Names);
-
- // Convert weak-missing entries (empty optional from lookupLibrarySymbols)
- // to a present zero address. This matches the resolve semantics of
- // llvm::orc::rt_bootstrap::SimpleExecutorDylibManager: an empty optional
- // in the result signals a missing required symbol, while a missing
+ sys::SymbolLookupResult Addrs;
+
+ switch (Dylib->K) {
+ case DylibHandle::Kind::Global:
+ Addrs = sys::lookupGlobalSymbols(Names);
+ break;
+ case DylibHandle::Kind::Library:
+ assert(Dylib->LibraryHandle && "invalid library handle");
+ if (!Dylib->LibraryHandle)
+ return OnLookupComplete(
+ make_error<StringError>("invalid library handle"));
+ Addrs = sys::lookupLibrarySymbols(Dylib->LibraryHandle, Names);
+ break;
+ }
+
+ // Convert weak-missing entries (empty optional) to a present zero address.
+ // This matches the resolve semantics of
+ // llvm::orc::rt_bootstrap::SimpleExecutorDylibManager: an empty optional in
+ // the result signals a missing required symbol, while a missing
// weakly-referenced symbol is reported as a zero address.
for (size_t I = 0, E = Symbols.size(); I != E; ++I)
if (!Addrs[I] && Symbols[I].second == WeaklyReferencedSymbol)
diff --git a/orc-rt/lib/bedrock/sys/posix/DynamicLibrary.cpp b/orc-rt/lib/bedrock/sys/posix/DynamicLibrary.cpp
index fbde604159d4e..9b6badd81b1c5 100644
--- a/orc-rt/lib/bedrock/sys/posix/DynamicLibrary.cpp
+++ b/orc-rt/lib/bedrock/sys/posix/DynamicLibrary.cpp
@@ -17,6 +17,7 @@
#include <cassert>
#include <dlfcn.h>
+#include <sstream>
namespace orc_rt::sys {
@@ -41,37 +42,39 @@ std::optional<const char *> toDLSymName(const std::string &Name) {
} // namespace
-void *globalLookupHandle() { return RTLD_DEFAULT; }
-Expected<void *> loadLibrary(const std::string &Path) {
- assert(!Path.empty() && "loadLibrary doesn't support empty paths");
- void *H = dlopen(Path.c_str(), RTLD_LAZY | RTLD_LOCAL);
- if (H == nullptr)
- return make_error<StringError>((StringOutputStream()
- << "error loading \"" << Path
- << "\": " << dlerror())
- .str());
+Expected<DylibHandle>
+loadLibrary(const std::string &Path) {
+ assert(!Path.empty() && "hostOSLoadLibrary doesn't support empty paths");
- return H;
+ void *Handle = dlopen(Path.c_str(), RTLD_LAZY | RTLD_LOCAL);
+ if (!Handle) {
+ std::ostringstream ErrMsg;
+ ErrMsg << "error loading \"" << Path << "\": " << dlerror();
+ return orc_rt::make_error<orc_rt::StringError>(ErrMsg.str());
+ }
+
+ return Handle;
}
-Error unloadLibrary(void *Handle) {
+orc_rt::Error
+unloadLibrary(DylibHandle Handle) {
+ assert(Handle && "invalid library handle");
+
if (dlclose(Handle) != 0)
- return make_error<StringError>((StringOutputStream()
- << "error unloading " << Handle << ": "
- << dlerror())
- .str());
- return Error::success();
+ return orc_rt::make_error<orc_rt::StringError>(
+ (std::ostringstream()
+ << "error unloading " << Handle << ": " << dlerror())
+ .str());
+
+ return orc_rt::Error::success();
}
-std::vector<std::optional<void *>>
-lookupLibrarySymbols(void *Handle, const std::vector<std::string> &Names) {
- std::vector<std::optional<void *>> Result;
+SymbolLookupResult
+hostOSLookup(void *Handle, const std::vector<std::string> &Names) {
+ SymbolLookupResult Result;
Result.reserve(Names.size());
- // Reset dlerror so we can distinguish "dlsym returned null because the
- // symbol is present at address 0" from "dlsym returned null because the
- // symbol isn't in the library" via per-iteration dlerror() checks.
- dlerror();
+
for (const auto &Name : Names) {
auto LookupName = toDLSymName(Name);
if (!LookupName) {
@@ -86,7 +89,20 @@ lookupLibrarySymbols(void *Handle, const std::vector<std::string> &Names) {
else
Result.push_back(std::nullopt);
}
+
return Result;
}
-} // namespace orc_rt::sys
+SymbolLookupResult
+lookupLibrarySymbols(DylibHandle Handle,
+ const std::vector<std::string> &Names) {
+ assert(Handle && "invalid library handle");
+
+ return hostOSLookup(Handle, Names);
+}
+
+SymbolLookupResult
+lookupGlobalSymbols(const std::vector<std::string> &Names) {
+ return hostOSLookup(RTLD_DEFAULT, Names);
+}
+} // namespace
More information about the llvm-commits
mailing list