[llvm] [orc-rt] Add Windows dynamic library support (PR #225076)

Jared Wyles via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 23:12:38 PDT 2026


https://github.com/jaredwy updated https://github.com/llvm/llvm-project/pull/225076

>From 3702751331e6ca45b33b163f91567eeaa83154ea Mon Sep 17 00:00:00 2001
From: Jared Wyles <jared.wyles at gmail.com>
Date: Mon, 21 Sep 2026 22:51:12 +1000
Subject: [PATCH] [orc-rt] Add Windows dynamic library support

---
 .../bedrock/sys/windows/DynamicLibrary.cpp    | 126 ++++++++++++++++--
 orc-rt/test/unit/CMakeLists.txt               |   6 +-
 .../unit/Inputs/NativeDylibManagerTestLib.cpp |   6 +-
 .../unit/Inputs/RTTICrossDylibTestLib.cpp     |   8 +-
 .../test/unit/Inputs/RTTICrossDylibTestLib.h  |  17 +--
 orc-rt/test/unit/Inputs/TestVisibility.h      |  14 ++
 6 files changed, 144 insertions(+), 33 deletions(-)
 create mode 100644 orc-rt/test/unit/Inputs/TestVisibility.h

diff --git a/orc-rt/lib/bedrock/sys/windows/DynamicLibrary.cpp b/orc-rt/lib/bedrock/sys/windows/DynamicLibrary.cpp
index c311fdc5006dca..835d9d65a0be5d 100644
--- a/orc-rt/lib/bedrock/sys/windows/DynamicLibrary.cpp
+++ b/orc-rt/lib/bedrock/sys/windows/DynamicLibrary.cpp
@@ -1,4 +1,4 @@
-//===- DynamicLibrary.cpp -------------------------------------------------===//
+//===- DynamicLibrary.cpp - Windows dynamic library operations ------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -7,22 +7,132 @@
 //===----------------------------------------------------------------------===//
 
 #include "orc-rt-internal/bedrock/sys/DynamicLibrary.h"
+#include "orc-rt-internal/support/sys/WinErrorToORCError.h"
+
+#include <cassert>
+#include <cstdint>
+
+#include <psapi.h>
+#include <windows.h>
 
 namespace orc_rt::sys {
 
-void *globalLookupHandle() { return nullptr; }
+namespace {
+
+// Window has no equivalent of RTLD_DEFAULT. nullptr means "current exe",
+// so to avoid confusiion use a sentinal address.
+// Keep the sentinel in an anonymous namespace so that it has internal linkage
+char GlobalLookupSentinel;
+bool isGlobalLookupHandle(void *Handle) {
+  return Handle == &GlobalLookupSentinel;
+}
+
+std::optional<void *> lookupSymbol(HMODULE Handle, const std::string &Name) {
+  if (auto Addr = GetProcAddress(Handle, Name.c_str()))
+    return reinterpret_cast<void *>(reinterpret_cast<uintptr_t>(Addr));
+
+  return std::nullopt;
+}
+
+bool getProcessModules(std::vector<HMODULE> &Modules) {
+  HANDLE Process = GetCurrentProcess();
+
+  DWORD BytesNeeded = 0;
+  if (!EnumProcessModulesEx(Process, nullptr, 0, &BytesNeeded,
+                            LIST_MODULES_64BIT))
+    return false;
+
+  for (;;) {
+    assert(BytesNeeded % sizeof(HMODULE) == 0);
+
+    Modules.resize(BytesNeeded / sizeof(HMODULE));
+
+    DWORD NewBytesNeeded = 0;
+    if (!EnumProcessModulesEx(
+            Process, Modules.data(),
+            static_cast<DWORD>(Modules.size() * sizeof(HMODULE)),
+            &NewBytesNeeded, LIST_MODULES_64BIT))
+      return false;
+
+    if (NewBytesNeeded <= Modules.size() * sizeof(HMODULE)) {
+      Modules.resize(NewBytesNeeded / sizeof(HMODULE));
+      return true;
+    }
+
+    // The module list changed between the size query and enumeration.
+    BytesNeeded = NewBytesNeeded;
+  }
+}
+
+} // namespace
+
+void *globalLookupHandle() { return &GlobalLookupSentinel; }
+Expected<void *> loadLibrary(const std::string &Path) {
+  assert(!Path.empty() && "loadLibrary doesn't support empty paths");
 
-Expected<void *> loadLibrary(const std::string &) {
-  return make_error<StringError>("Windows loadLibrary not implemented");
+  HMODULE Handle = LoadLibraryA(Path.c_str());
+  if (!Handle) {
+    std::string Prefix = "error loading \"" + Path + "\"";
+    return generateErrorFromGetLastError(Prefix);
+  }
+
+  return reinterpret_cast<void *>(Handle);
 }
 
-Error unloadLibrary(void *) {
-  return make_error<StringError>("Windows unloadLibrary not implemented");
+Error unloadLibrary(void *Handle) {
+  assert(Handle && "invalid library handle");
+  assert(!isGlobalLookupHandle(Handle) &&
+         "global lookup handle must not be unloaded");
+
+  if (!FreeLibrary(static_cast<HMODULE>(Handle)))
+    return generateErrorFromGetLastError("error unloading library");
+
+  return Error::success();
 }
 
 std::vector<std::optional<void *>>
-lookupLibrarySymbols(void *, const std::vector<std::string> &Names) {
-  return std::vector<std::optional<void *>>(Names.size(), std::nullopt);
+lookupLibrarySymbols(void *Handle, const std::vector<std::string> &Names) {
+  std::vector<std::optional<void *>> Result;
+  Result.reserve(Names.size());
+
+  if (isGlobalLookupHandle(Handle)) {
+    std::vector<HMODULE> Modules;
+
+    if (!getProcessModules(Modules)) {
+      Result.resize(Names.size(), std::nullopt);
+      return Result;
+    }
+
+    for (const auto &Name : Names) {
+      std::optional<void *> Addr;
+
+      // Search the executable first.
+      if (!Modules.empty())
+        Addr = lookupSymbol(Modules.front(), Name);
+
+      // Match LLVM's existing Windows DynamicLibrary behavior by searching
+      // loaded DLLs in reverse order.
+      if (!Addr && Modules.size() > 1) {
+        for (auto I = Modules.rbegin(), E = Modules.rend() - 1; I != E; ++I) {
+          Addr = lookupSymbol(*I, Name);
+          if (Addr)
+            break;
+        }
+      }
+
+      Result.push_back(Addr);
+    }
+
+    return Result;
+  }
+
+  assert(Handle && "invalid library handle");
+
+  HMODULE Library = static_cast<HMODULE>(Handle);
+  for (const auto &Name : Names)
+    Result.push_back(lookupSymbol(Library, Name));
+
+  return Result;
 }
 
 } // namespace orc_rt::sys
diff --git a/orc-rt/test/unit/CMakeLists.txt b/orc-rt/test/unit/CMakeLists.txt
index 856fefd370bf60..cd59ca68493589 100644
--- a/orc-rt/test/unit/CMakeLists.txt
+++ b/orc-rt/test/unit/CMakeLists.txt
@@ -141,6 +141,7 @@ add_library(NativeDylibManagerTestLib SHARED
   Inputs/NativeDylibManagerTestLib.cpp)
 set_target_properties(NativeDylibManagerTestLib PROPERTIES
   PREFIX ""
+  DEFINE_SYMBOL ORC_RT_TEST_DLL_EXPORTS
   FOLDER "orc-rt/Tests")
 target_compile_options(NativeDylibManagerTestLib PRIVATE
   ${ORC_RT_COMPILE_FLAGS})
@@ -160,10 +161,11 @@ set_target_properties(RTTICrossDylibTestLib PROPERTIES
   PREFIX ""
   CXX_VISIBILITY_PRESET hidden
   VISIBILITY_INLINES_HIDDEN ON
+  DEFINE_SYMBOL ORC_RT_TEST_DLL_EXPORTS
   FOLDER "orc-rt/Tests")
 
-target_compile_definitions(RTTICrossDylibTestLib PRIVATE
-  RTTI_CROSS_DYLIB_TEST_LIB_EXPORTS)
+set_target_properties(RTTICrossDylibTestLib PROPERTIES
+  DEFINE_SYMBOL ORC_RT_TEST_DLL_EXPORTS)
 
 target_compile_options(RTTICrossDylibTestLib PRIVATE ${ORC_RT_COMPILE_FLAGS})
 
diff --git a/orc-rt/test/unit/Inputs/NativeDylibManagerTestLib.cpp b/orc-rt/test/unit/Inputs/NativeDylibManagerTestLib.cpp
index 681bdb0b857504..5e750fa17e9025 100644
--- a/orc-rt/test/unit/Inputs/NativeDylibManagerTestLib.cpp
+++ b/orc-rt/test/unit/Inputs/NativeDylibManagerTestLib.cpp
@@ -1,10 +1,6 @@
 // A minimal shared library for NativeDylibManager tests.
 
-#if defined(_WIN32)
-#define TEST_EXPORT __declspec(dllexport)
-#else
-#define TEST_EXPORT __attribute__((visibility("default")))
-#endif
+#include "TestVisibility.h"
 
 extern "C" TEST_EXPORT int NativeDylibManagerTestFunc() { return 42; }
 extern "C" TEST_EXPORT int NativeDylibManagerTestFunc2() { return 7; }
diff --git a/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.cpp b/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.cpp
index e6c3c2eb912fbe..bae5eca45ff8d9 100644
--- a/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.cpp
+++ b/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.cpp
@@ -9,13 +9,11 @@
 using namespace orc_rt;
 using orc_rt_test::CrossDylibTestError;
 
-extern "C" RTTI_TEST_EXPORT ErrorInfoBase *
-rttiCrossDylibTest_makeError(int Code) {
+extern "C" TEST_EXPORT ErrorInfoBase *rttiCrossDylibTest_makeError(int Code) {
   return new CrossDylibTestError(Code);
 }
 
-extern "C" RTTI_TEST_EXPORT void
-rttiCrossDylibTest_destroyError(ErrorInfoBase *E) {
+extern "C" TEST_EXPORT void rttiCrossDylibTest_destroyError(ErrorInfoBase *E) {
   delete E;
 }
 
@@ -23,7 +21,7 @@ rttiCrossDylibTest_destroyError(ErrorInfoBase *E) {
 // confirm the two libraries are genuinely using distinct identities (i.e.
 // that the test below exercises the cross-library strcmp path, not the
 // same-library pointer-equality fast path).
-extern "C" RTTI_TEST_EXPORT const void *rttiCrossDylibTest_libraryID() {
+extern "C" TEST_EXPORT const void *rttiCrossDylibTest_libraryID() {
   CrossDylibTestError E(0);
   return E.libraryID();
 }
diff --git a/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.h b/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.h
index 5a3a990a632d78..2291cbd8a61953 100644
--- a/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.h
+++ b/orc-rt/test/unit/Inputs/RTTICrossDylibTestLib.h
@@ -15,24 +15,15 @@
 #ifndef ORC_RT_TEST_RTTICROSSDYLIBTESTLIB_H
 #define ORC_RT_TEST_RTTICROSSDYLIBTESTLIB_H
 
+#include "TestVisibility.h"
 #include "orc-rt/support/Error.h"
 
-#if defined(_WIN32)
-#if defined(RTTI_CROSS_DYLIB_TEST_LIB_EXPORTS)
-#define RTTI_TEST_EXPORT __declspec(dllexport)
-#else
-#define RTTI_TEST_EXPORT __declspec(dllimport)
-#endif
-#else
-#define RTTI_TEST_EXPORT __attribute__((visibility("default")))
-#endif
-
-extern "C" RTTI_TEST_EXPORT orc_rt::ErrorInfoBase *
+extern "C" TEST_EXPORT orc_rt::ErrorInfoBase *
 rttiCrossDylibTest_makeError(int Code);
 
-extern "C" RTTI_TEST_EXPORT void
+extern "C" TEST_EXPORT void
 rttiCrossDylibTest_destroyError(orc_rt::ErrorInfoBase *E);
 
-extern "C" RTTI_TEST_EXPORT const void *rttiCrossDylibTest_libraryID();
+extern "C" TEST_EXPORT const void *rttiCrossDylibTest_libraryID();
 
 #endif // ORC_RT_TEST_RTTICROSSDYLIBTESTLIB_H
diff --git a/orc-rt/test/unit/Inputs/TestVisibility.h b/orc-rt/test/unit/Inputs/TestVisibility.h
new file mode 100644
index 00000000000000..f1d896e6ab18fd
--- /dev/null
+++ b/orc-rt/test/unit/Inputs/TestVisibility.h
@@ -0,0 +1,14 @@
+#ifndef ORC_RT_TEST_UNIT_INPUTS_TESTVISIBILITY_H
+#define ORC_RT_TEST_UNIT_INPUTS_TESTVISIBILITY_H
+
+#if defined(_WIN32)
+#if defined(ORC_RT_TEST_DLL_EXPORTS)
+#define TEST_EXPORT __declspec(dllexport)
+#else
+#define TEST_EXPORT __declspec(dllimport)
+#endif
+#else
+#define TEST_EXPORT __attribute__((visibility("default")))
+#endif
+
+#endif // ORC_RT_TEST_UNIT_INPUTS_TESTVISIBILITY_H



More information about the llvm-commits mailing list