[compiler-rt] [llvm] [ORC][Runtime] Add dlupdate for coff (PR #115448)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 8 03:57:05 PDT 2025
https://github.com/SahilPatidar updated https://github.com/llvm/llvm-project/pull/115448
>From 0894687fb4ba7be1aa87cd996b6f15bbe032c838 Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Sat, 6 Jul 2024 14:00:13 +0530
Subject: [PATCH 1/5] [ORC][Runtime] Add `dlupdate` for COFF
---
compiler-rt/lib/orc/coff_platform.cpp | 75 +++++++++++++++++++
compiler-rt/lib/orc/coff_platform.h | 1 +
llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp | 1 +
3 files changed, 77 insertions(+)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 78144ec114a2f..31f358e3832d5 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,6 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
+ void *dlupdate(void *DSOHandle, int Mode);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -141,6 +142,10 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
+ Expected<void *> dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateFull(JITDylibState &JDS);
+ Error dlupdateInitialize(JITDylibState &JDS);
+
Error dlcloseImpl(void *DSOHandle);
Error dlcloseDeinitialize(JITDylibState &JDS);
@@ -265,6 +270,21 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
+void *COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+ ORC_RT_DEBUG({
+ std::string S;
+ printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
+ });
+ std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
+ if (auto H = dlupdateImpl(DSOHandle, Mode))
+ return *H;
+ else {
+ // FIXME: Make dlerror thread safe.
+ DLFcnError = toString(H.takeError());
+ return nullptr;
+ }
+}
+
int COFFPlatformRuntimeState::dlclose(void *DSOHandle) {
ORC_RT_DEBUG({
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -390,6 +410,57 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
+Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
+ int Mode) {
+ // Try to find JITDylib state by name.
+ auto *JDS = getJITDylibStateByHeader(DSOHandle);
+
+ if (!JDS) {
+ std::ostringstream ErrStream;
+ ErrStream << "No registered JITDylib for " << DSOHandle;
+ return make_error<StringError>(ErrStream.str());
+ }
+
+ if (!JDS->referenced())
+ return make_error<StringError>("dlupdate failed, JITDylib must be open.");
+
+ if (auto Err = dlupdateFull(*JDS))
+ return std::move(Err);
+
+ // Return the header address.
+ return JDS->Header;
+}
+
+Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
+ // Call back to the JIT to push the initializers.
+ Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
+ if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
+ SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
+ DepInfoMap,
+ ExecutorAddr::fromPtr(JDS.Header)))
+ return Err;
+ if (!DepInfoMap)
+ return DepInfoMap.takeError();
+
+ if (auto Err = dlupdateInitialize(JDS))
+ return Err;
+
+ return Error::success();
+}
+
+Error COFFPlatformRuntimeState::dlupdateInitialize(JITDylibState &JDS) {
+ ORC_RT_DEBUG({
+ printdbg("COFFPlatformRuntimeState::dlupdateInitialize(\"%s\")\n",
+ JDS.Name.c_str());
+ });
+
+ // Run static initializers.
+ JDS.CInitSection.RunAllNewAndFlush();
+ JDS.CXXInitSection.RunAllNewAndFlush();
+
+ return Error::success();
+}
+
Error COFFPlatformRuntimeState::dlcloseImpl(void *DSOHandle) {
// Try to find JITDylib state by header.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -667,6 +738,10 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
+void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
+ return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
+}
+
int __orc_rt_coff_jit_dlclose(void *header) {
return COFFPlatformRuntimeState::get().dlclose(header);
}
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index aae57bc6100e5..a437e82fe4b47 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,6 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
+ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
diff --git a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
index d3c5761fa9731..b8d71c5e9e957 100644
--- a/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp
@@ -361,6 +361,7 @@ COFFPlatform::standardRuntimeUtilityAliases() {
{"__orc_rt_run_program", "__orc_rt_coff_run_program"},
{"__orc_rt_jit_dlerror", "__orc_rt_coff_jit_dlerror"},
{"__orc_rt_jit_dlopen", "__orc_rt_coff_jit_dlopen"},
+ {"__orc_rt_jit_dlupdate", "__orc_rt_coff_jit_dlupdate"},
{"__orc_rt_jit_dlclose", "__orc_rt_coff_jit_dlclose"},
{"__orc_rt_jit_dlsym", "__orc_rt_coff_jit_dlsym"},
{"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}};
>From 7fe4ed6e2157cd1ddecd2f0f3d5ccb30f98a51ad Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Sat, 27 Jul 2024 12:12:58 +0530
Subject: [PATCH 2/5] Modify dlupdate to return error code instead of
dso_handle
---
compiler-rt/lib/orc/coff_platform.cpp | 25 +++++++++++--------------
compiler-rt/lib/orc/coff_platform.h | 2 +-
2 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 31f358e3832d5..4283f487d80e7 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,7 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
- void *dlupdate(void *DSOHandle, int Mode);
+ int dlupdate(void *DSOHandle, int Mode);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -142,7 +142,7 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
- Expected<void *> dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateImpl(void *DSOHandle, int Mode);
Error dlupdateFull(JITDylibState &JDS);
Error dlupdateInitialize(JITDylibState &JDS);
@@ -270,19 +270,18 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
-void *COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+int COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
ORC_RT_DEBUG({
std::string S;
printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
});
std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
- if (auto H = dlupdateImpl(DSOHandle, Mode))
- return *H;
- else {
+ if (auto Err = dlupdateImpl(DSOHandle, Mode)) {
// FIXME: Make dlerror thread safe.
- DLFcnError = toString(H.takeError());
- return nullptr;
+ DLFcnError = toString(std::move(Err));
+ return -1;
}
+ return 0;
}
int COFFPlatformRuntimeState::dlclose(void *DSOHandle) {
@@ -410,8 +409,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
-Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
- int Mode) {
+Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle, int Mode) {
// Try to find JITDylib state by name.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -425,10 +423,9 @@ Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
return make_error<StringError>("dlupdate failed, JITDylib must be open.");
if (auto Err = dlupdateFull(*JDS))
- return std::move(Err);
+ return Err;
- // Return the header address.
- return JDS->Header;
+ return Error::success();
}
Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
@@ -738,7 +735,7 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
-void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
+int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
}
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index a437e82fe4b47..e9ee52cd1d61a 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,7 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
-ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
+ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
>From 480db01a0da9c4675b46404c54bfc6e2debe38fb Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Tue, 5 Nov 2024 11:29:16 +0530
Subject: [PATCH 3/5] Remove mode arg
---
compiler-rt/lib/orc/coff_platform.cpp | 14 +++++++-------
compiler-rt/lib/orc/coff_platform.h | 2 +-
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 4283f487d80e7..11990826daceb 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -110,7 +110,7 @@ class COFFPlatformRuntimeState {
const char *dlerror();
void *dlopen(std::string_view Name, int Mode);
- int dlupdate(void *DSOHandle, int Mode);
+ int dlupdate(void *DSOHandle);
int dlclose(void *Header);
void *dlsym(void *Header, std::string_view Symbol);
@@ -142,7 +142,7 @@ class COFFPlatformRuntimeState {
Error dlopenFull(JITDylibState &JDS);
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
- Error dlupdateImpl(void *DSOHandle, int Mode);
+ Error dlupdateImpl(void *DSOHandle);
Error dlupdateFull(JITDylibState &JDS);
Error dlupdateInitialize(JITDylibState &JDS);
@@ -270,13 +270,13 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
}
}
-int COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
+int COFFPlatformRuntimeState::dlupdate(void *DSOHandle) {
ORC_RT_DEBUG({
std::string S;
printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
});
std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
- if (auto Err = dlupdateImpl(DSOHandle, Mode)) {
+ if (auto Err = dlupdateImpl(DSOHandle)) {
// FIXME: Make dlerror thread safe.
DLFcnError = toString(std::move(Err));
return -1;
@@ -409,7 +409,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
return Error::success();
}
-Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle, int Mode) {
+Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle) {
// Try to find JITDylib state by name.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -735,8 +735,8 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
return COFFPlatformRuntimeState::get().dlopen(path, mode);
}
-int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
- return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
+int __orc_rt_coff_jit_dlupdate(void *dso_handle) {
+ return COFFPlatformRuntimeState::get().dlupdate(dso_handle);
}
int __orc_rt_coff_jit_dlclose(void *header) {
diff --git a/compiler-rt/lib/orc/coff_platform.h b/compiler-rt/lib/orc/coff_platform.h
index e9ee52cd1d61a..e9891f0b92a8a 100644
--- a/compiler-rt/lib/orc/coff_platform.h
+++ b/compiler-rt/lib/orc/coff_platform.h
@@ -19,7 +19,7 @@
// dlfcn functions.
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
-ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
+ORC_RT_INTERFACE int __orc_rt_coff_jit_dlupdate(void *dso_handle);
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
const char *symbol);
>From 89a6822f27f3e06c3e4f9d7d35861566462f8f3c Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Fri, 8 Nov 2024 14:58:00 +0530
Subject: [PATCH 4/5] Update with changes from upstream
---
compiler-rt/lib/orc/coff_platform.cpp | 8 ++++----
compiler-rt/lib/orc/dlfcn_wrapper.cpp | 4 +---
llvm/lib/ExecutionEngine/Orc/LLJIT.cpp | 13 +++++--------
3 files changed, 10 insertions(+), 15 deletions(-)
diff --git a/compiler-rt/lib/orc/coff_platform.cpp b/compiler-rt/lib/orc/coff_platform.cpp
index 11990826daceb..4cce20e1323f3 100644
--- a/compiler-rt/lib/orc/coff_platform.cpp
+++ b/compiler-rt/lib/orc/coff_platform.cpp
@@ -410,7 +410,7 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
}
Error COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle) {
- // Try to find JITDylib state by name.
+ // Try to find JITDylib state by header.
auto *JDS = getJITDylibStateByHeader(DSOHandle);
if (!JDS) {
@@ -432,9 +432,9 @@ Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
// Call back to the JIT to push the initializers.
Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
- SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
- DepInfoMap,
- ExecutorAddr::fromPtr(JDS.Header)))
+ SPSExecutorAddr)>::
+ call(JITDispatch(&__orc_rt_coff_push_initializers_tag), DepInfoMap,
+ ExecutorAddr::fromPtr(JDS.Header)))
return Err;
if (!DepInfoMap)
return DepInfoMap.takeError();
diff --git a/compiler-rt/lib/orc/dlfcn_wrapper.cpp b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
index 02e37968cd9fd..51c5aed66fe4d 100644
--- a/compiler-rt/lib/orc/dlfcn_wrapper.cpp
+++ b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
@@ -42,8 +42,7 @@ __orc_rt_jit_dlopen_wrapper(const char *ArgData, size_t ArgSize) {
.release();
}
-#ifndef _WIN32
-ORC_RT_INTERFACE orc_rt_WrapperFunctionResult
+ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
__orc_rt_jit_dlupdate_wrapper(const char *ArgData, size_t ArgSize) {
return WrapperFunction<int32_t(SPSExecutorAddr)>::handle(
ArgData, ArgSize,
@@ -52,7 +51,6 @@ __orc_rt_jit_dlupdate_wrapper(const char *ArgData, size_t ArgSize) {
})
.release();
}
-#endif
ORC_RT_INTERFACE orc_rt_WrapperFunctionResult
__orc_rt_jit_dlclose_wrapper(const char *ArgData, size_t ArgSize) {
diff --git a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
index 67bb7dd8ad08f..7487526c5d059 100644
--- a/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LLJIT.cpp
@@ -617,14 +617,11 @@ Error ORCPlatformSupport::initialize(orc::JITDylib &JD) {
[](const JITDylibSearchOrder &SO) { return SO; });
StringRef WrapperToCall = "__orc_rt_jit_dlopen_wrapper";
bool dlupdate = false;
- const Triple &TT = ES.getTargetTriple();
- if (TT.isOSBinFormatMachO() || TT.isOSBinFormatELF()) {
- if (InitializedDylib.contains(&JD)) {
- WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
- dlupdate = true;
- } else
- InitializedDylib.insert(&JD);
- }
+ if (InitializedDylib.contains(&JD)) {
+ WrapperToCall = "__orc_rt_jit_dlupdate_wrapper";
+ dlupdate = true;
+ } else
+ InitializedDylib.insert(&JD);
if (auto WrapperAddr =
ES.lookup(MainSearchOrder, J.mangleAndIntern(WrapperToCall))) {
>From d677b1fe13da2b1403d7c565320f3cd996fb9b28 Mon Sep 17 00:00:00 2001
From: SahilPatidar <patidarsahil2001 at gmail.com>
Date: Mon, 6 Oct 2025 14:08:07 +0530
Subject: [PATCH 5/5] Refactor
---
compiler-rt/lib/orc/dlfcn_wrapper.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/orc/dlfcn_wrapper.cpp b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
index 51c5aed66fe4d..cb92a1fb19ca2 100644
--- a/compiler-rt/lib/orc/dlfcn_wrapper.cpp
+++ b/compiler-rt/lib/orc/dlfcn_wrapper.cpp
@@ -42,7 +42,7 @@ __orc_rt_jit_dlopen_wrapper(const char *ArgData, size_t ArgSize) {
.release();
}
-ORC_RT_INTERFACE orc_rt_CWrapperFunctionResult
+ORC_RT_INTERFACE orc_rt_WrapperFunctionResult
__orc_rt_jit_dlupdate_wrapper(const char *ArgData, size_t ArgSize) {
return WrapperFunction<int32_t(SPSExecutorAddr)>::handle(
ArgData, ArgSize,
More information about the llvm-commits
mailing list