Skip to content

Commit 0894687

Browse files
committed
[ORC][Runtime] Add dlupdate for COFF
1 parent bea0225 commit 0894687

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

compiler-rt/lib/orc/coff_platform.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class COFFPlatformRuntimeState {
110110

111111
const char *dlerror();
112112
void *dlopen(std::string_view Name, int Mode);
113+
void *dlupdate(void *DSOHandle, int Mode);
113114
int dlclose(void *Header);
114115
void *dlsym(void *Header, std::string_view Symbol);
115116

@@ -141,6 +142,10 @@ class COFFPlatformRuntimeState {
141142
Error dlopenFull(JITDylibState &JDS);
142143
Error dlopenInitialize(JITDylibState &JDS, COFFJITDylibDepInfoMap &DepInfo);
143144

145+
Expected<void *> dlupdateImpl(void *DSOHandle, int Mode);
146+
Error dlupdateFull(JITDylibState &JDS);
147+
Error dlupdateInitialize(JITDylibState &JDS);
148+
144149
Error dlcloseImpl(void *DSOHandle);
145150
Error dlcloseDeinitialize(JITDylibState &JDS);
146151

@@ -265,6 +270,21 @@ void *COFFPlatformRuntimeState::dlopen(std::string_view Path, int Mode) {
265270
}
266271
}
267272

273+
void *COFFPlatformRuntimeState::dlupdate(void *DSOHandle, int Mode) {
274+
ORC_RT_DEBUG({
275+
std::string S;
276+
printdbg("COFFPlatform::dlupdate(%p) (%s)\n", DSOHandle, S.c_str());
277+
});
278+
std::lock_guard<std::recursive_mutex> Lock(JDStatesMutex);
279+
if (auto H = dlupdateImpl(DSOHandle, Mode))
280+
return *H;
281+
else {
282+
// FIXME: Make dlerror thread safe.
283+
DLFcnError = toString(H.takeError());
284+
return nullptr;
285+
}
286+
}
287+
268288
int COFFPlatformRuntimeState::dlclose(void *DSOHandle) {
269289
ORC_RT_DEBUG({
270290
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -390,6 +410,57 @@ Error COFFPlatformRuntimeState::dlopenInitialize(
390410
return Error::success();
391411
}
392412

413+
Expected<void *> COFFPlatformRuntimeState::dlupdateImpl(void *DSOHandle,
414+
int Mode) {
415+
// Try to find JITDylib state by name.
416+
auto *JDS = getJITDylibStateByHeader(DSOHandle);
417+
418+
if (!JDS) {
419+
std::ostringstream ErrStream;
420+
ErrStream << "No registered JITDylib for " << DSOHandle;
421+
return make_error<StringError>(ErrStream.str());
422+
}
423+
424+
if (!JDS->referenced())
425+
return make_error<StringError>("dlupdate failed, JITDylib must be open.");
426+
427+
if (auto Err = dlupdateFull(*JDS))
428+
return std::move(Err);
429+
430+
// Return the header address.
431+
return JDS->Header;
432+
}
433+
434+
Error COFFPlatformRuntimeState::dlupdateFull(JITDylibState &JDS) {
435+
// Call back to the JIT to push the initializers.
436+
Expected<COFFJITDylibDepInfoMap> DepInfoMap((COFFJITDylibDepInfoMap()));
437+
if (auto Err = WrapperFunction<SPSExpected<SPSCOFFJITDylibDepInfoMap>(
438+
SPSExecutorAddr)>::call(&__orc_rt_coff_push_initializers_tag,
439+
DepInfoMap,
440+
ExecutorAddr::fromPtr(JDS.Header)))
441+
return Err;
442+
if (!DepInfoMap)
443+
return DepInfoMap.takeError();
444+
445+
if (auto Err = dlupdateInitialize(JDS))
446+
return Err;
447+
448+
return Error::success();
449+
}
450+
451+
Error COFFPlatformRuntimeState::dlupdateInitialize(JITDylibState &JDS) {
452+
ORC_RT_DEBUG({
453+
printdbg("COFFPlatformRuntimeState::dlupdateInitialize(\"%s\")\n",
454+
JDS.Name.c_str());
455+
});
456+
457+
// Run static initializers.
458+
JDS.CInitSection.RunAllNewAndFlush();
459+
JDS.CXXInitSection.RunAllNewAndFlush();
460+
461+
return Error::success();
462+
}
463+
393464
Error COFFPlatformRuntimeState::dlcloseImpl(void *DSOHandle) {
394465
// Try to find JITDylib state by header.
395466
auto *JDS = getJITDylibStateByHeader(DSOHandle);
@@ -667,6 +738,10 @@ void *__orc_rt_coff_jit_dlopen(const char *path, int mode) {
667738
return COFFPlatformRuntimeState::get().dlopen(path, mode);
668739
}
669740

741+
void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode) {
742+
return COFFPlatformRuntimeState::get().dlupdate(dso_handle, mode);
743+
}
744+
670745
int __orc_rt_coff_jit_dlclose(void *header) {
671746
return COFFPlatformRuntimeState::get().dlclose(header);
672747
}

compiler-rt/lib/orc/coff_platform.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// dlfcn functions.
2020
ORC_RT_INTERFACE const char *__orc_rt_coff_jit_dlerror();
2121
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlopen(const char *path, int mode);
22+
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlupdate(void *dso_handle, int mode);
2223
ORC_RT_INTERFACE int __orc_rt_coff_jit_dlclose(void *header);
2324
ORC_RT_INTERFACE void *__orc_rt_coff_jit_dlsym(void *header,
2425
const char *symbol);

llvm/lib/ExecutionEngine/Orc/COFFPlatform.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ COFFPlatform::standardRuntimeUtilityAliases() {
361361
{"__orc_rt_run_program", "__orc_rt_coff_run_program"},
362362
{"__orc_rt_jit_dlerror", "__orc_rt_coff_jit_dlerror"},
363363
{"__orc_rt_jit_dlopen", "__orc_rt_coff_jit_dlopen"},
364+
{"__orc_rt_jit_dlupdate", "__orc_rt_coff_jit_dlupdate"},
364365
{"__orc_rt_jit_dlclose", "__orc_rt_coff_jit_dlclose"},
365366
{"__orc_rt_jit_dlsym", "__orc_rt_coff_jit_dlsym"},
366367
{"__orc_rt_log_error", "__orc_rt_log_error_to_stderr"}};

0 commit comments

Comments
 (0)