fix(relinker): run guest preinit arrays before the module initializer #6112
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6112"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-14T22:36:33Z.
What
Converted guest modules never run their
DT_PREINIT_ARRAYentries. The reader validates the array and rejects one without an initializer, and the unused-NID filter keeps the array's targets as roots, but no writer emitted or ran it: the Linux output only tagsDT_INIT_ARRAY/DT_FINI_ARRAY, and the macOS and Windows startup stubs only call the initializer and the init array.ld.soruns a shared object's preinit array only for the main executable (_dl_initreads it frommain_map), so the Linux output cannot use the tag. The preinit entries now run from the module'sDT_INITstub before the initializer, with zeroed argument registers and a null check per slot, the way the init array entries are called. The macOS and Windows startup stubs call them before the initializer. A deferred Windows module exports a thunk as its.ginitentry that runs the preinit entries before the initializer, since the deferred loader's slot list runs after the entry. The reader records the entries like the init and fini arrays.A module with a preinit array and no initializer is still rejected: the Linux stub that would run the entries exists only with one.
Tested
Windows 11, WinLibs GCC 15.2.0 r7, Release, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, CMake 4.4.4, Ninja):python3 tools/check_conventions.py --base origin/main: all checks passedctest: 168/168 passed (167 onmainplus the newguest_preinit)guest_preinitconverts a synthetic module with a two-entry preinit array whose entries record their calls:DT_INITstub runs the entries before the initializer, and the static check decodes the stub and its branch targets. On a Linux host the converted module loads and records(0, 0)for each preinit entry, then the initializer with the pending arguments, then its return value, in order..ginitentry RVA is the emitted thunk, its slots hold the array slots and the initializer, and the base relocations cover the slots.0x12840is missing from the entry code).Linux native execution of the new test runs in the relinker matrix on Linux hosts; it was not run here (Windows host only), and the other relinker tests skip native execution on Windows as on
main.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-15T03:02:49Z.
Declared the merge order for the overlap: the checklist now reads
Depends on: #5559, #5614. This one touches the same regions as #5559 (core/relinker/CMakeLists.txt,docs/dev/debt/relinker.md) and #5614 (the Linux stub emission inLinuxGuestModuleWriter.cpp), but there is no functional dependency in either direction, so the declaration is just the order: theirs first, and I will rebase this one ontomainonce either lands.@JJenkx if you would rather have this one go first, say so and I will flip the declaration.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:14:57Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cppL111-112core/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cppL111-112core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cppL3, L157, L203core/relinker/relinker/tests/test_guest_preinit.pydocs/dev/debt/relinker.mdL6core/relinker/relinker/tests/test_guest_preinit.pySome comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mddocs/dev/ARCHITECTURE.mdcore/relinker/CMakeLists.txtdocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mdcore/relinker/CMakeLists.txtdocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mddocs/dev/ARCHITECTURE.mdcore/relinker/CMakeLists.txtcore/relinker/domain/include/domain/GuestRuntime.hppcore/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cppcore/relinker/elfpatcher/src/macos/MacOsGuestModuleWriter.cppcore/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cppcore/relinker/relinker/include/relinker/guest/GuestImage.hppcore/relinker/relinker/src/guest/GuestImageReader.cppdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtcore/relinker/elfpatcher/include/elfpatcher/macos/MacOsImage.hppcore/relinker/elfpatcher/src/macos/MacOsImage.cppdocs/dev/debt/relinker.mdcore/relinker/CMakeLists.txtdocs/dev/debt/relinker.mdImported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-16T02:57:55Z.
Re-ran after #5559 merged and the overlap list refreshed: this now shares
docs/dev/debt/relinker.mdwith #6328 andLinuxGuestModuleWriter.cppplusrelinker.mdwith #6586, so the checklist readsDepends on: #5614, #6328, #6586(#5559 dropped now that it has merged). The declaration is merge order only, not a functional dependency: I will rebase this ontomainonce either lands. @aaro56 @JJenkx if you would rather have this one go first, say so and I will flip the declaration.Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-16T04:50:00Z.
@ayam04, a heads-up about an overlap with #6684. That PR treats a DT_INIT of 0 as the module initializer: Until Dawn's bundled
libc.prxandlibSceFios2.prxboth have tag 12 present with value 0 (SuperCatElvis on #3093). It addsGuestImage::HasInit, set when tag 12 is present, and uses it in place ofInit != 0/Init == 0.Merged with bf4a202ac:
MacOsGuestModuleWriter.cppconflicts at L112. Your newPreInitArrayRvasline sits next to theinput.InitRvaline that #6684 changes. Keeping both lines, withinput.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;, resolves it.WindowsGuestModuleWriter.cppL160, in the deferred.ginitthunk, merges cleanly but still testsguest.Init == 0. A--module-dirmodule shaped likelibc.prx(DT_INIT 0 and a one-entry preinit array) then fails withGuest preinit array has no initializer. It needsif (!guest.HasInit). The Linux check at L156 is fine as it is, because it tests the stub address.With both changes, the relinker-only suite passes 184/184 on Ubuntu 24.04 with GCC 13.3, including
guest_preinitand #6684'sguest_zero_initializer. With only the conflict resolved,guest_zero_initializerfails on that deferred case. Whichever of the two lands second makes the change; if yours goes first, I'll make it in #6684.An open question, not a finding: the loaders I checked start a module only through DT_INIT. fpPS4's kern
digest_dynamicignores DT_PREINIT_ARRAY (subr_dynlib.pas L1594-L1595), and shadPS4 calls base + DT_INIT (module.cpp L104). Iflibc.prx's DT_INIT code walks its own preinit array, running the entries from the stub as well would run them twice. I've asked SuperCatElvis on #6684 to disassemblelibc.prxfrom 0 to the PLT at 0xc8, which should answer it.AI Assisted
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T23:28:41Z.
Checked the Linux stub (stack alignment, rel32 ranges, null check per slot), the macOS and Windows startup calls and the Windows deferred thunk (size, slots, relocations). Relinker ctest scope is green,
guest_preinitruns the converted module natively on Linux in the right order and fails when the code change is reverted; docs updated. Merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-17T02:42:44Z.
Thanks for the heads-up and for testing the combination, @SandstromPL. Agreed on both edits and on the land-order split:
MacOsGuestModuleWriter.cpp: keep the newPreInitArrayRvasline and takeinput.InitRva = guest.HasInit ? image.GetRva(guest.Init) : 0;.WindowsGuestModuleWriter.cpp: in the deferred.ginitthunk, the preinit guard becomesif (!guest.HasInit) throw Domain::RelinkerException("Guest preinit array has no initializer");. That guard is this PR's line, so it is the one piece the merge leaves on us; theIo::AppendU32(lifecycle.Data, ...)first word andruntime.InitRvacome from #6684 cleanly.I'm not pushing anything right now so the green CI on
41cfc8bstays valid. If #6684 lands first I'll rebase ontomain, apply exactly the above, and re-run the relinker scope before pushing.On the open question: agreed it is the right thing to check. The stub runs the preinit entries with zeroed argument registers and a null check per slot, before the initializer. If the disassembly shows
libc.prx'sDT_INITwalks its own preinit array, the entries would run twice for that module and I would rather adjust the stub than guess, so I will follow SuperCatElvis's answer on #6684.@adriadam10 the full Build on
41cfc8bfinished green, along with Conventions, so nothing is blocking the merge from our side.Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-17T02:51:12Z.
Thanks, @ayam04, that matches what I tested. I merged
mainand this PR into #6684 and resolved the macOS line that way, with the thunk guard on!guest.HasInit. The relinker suite passes 188/188, includingguest_preinitandguest_zero_initializer. Without the guard change,guest_zero_initializerfails on its--module-dirpreinit case. If this PR lands first, I'll push exactly that to #6684, so there's nothing for you to do in that order.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T14:27:15Z.
A note for when this is rebased: #7147 (now on
main) addedadd_asan_test_options, so the CPU-side tests pass in anANYPS5_ENABLE_ASANbuild.guest_module_args,guest_deferred_dependenciesandguest_module_finiload the instrumentedguest_module_args_host.prxinto Python, so they getverify_asan_link_order=0. The calls are grouped right afterguest_module_fini'sadd_test.guest_preinitloads the same host, so in an ASan build it will need the same line:Without it, it fails there with
ASan runtime does not come first in initial library list. Normal builds are unaffected, because the function does nothing outside ASan builds.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-18T02:44:59Z.
Thanks @milescoviello. Rebased on main in da00f6f3: guest_preinit now goes through add_asan_test_options(guest_preinit verify_asan_link_order=0), the same as the sibling module tests, so it should pass in an ANYPS5_ENABLE_ASAN build now. No other changes in the rebase.
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-18T07:15:45Z.
@ayam04 one thing in the Windows preinit thunk (
.ginitc, WindowsGuestModuleWriter.cpp), checked on da00f6f3a.The thunk pushes r12, r13, r14 but pops them in the same order. This is
test_guest_preinit.py'sflag_module, converted as a deferred--module-dirmodule:So the caller gets its r12 and r14 back swapped. Both are callee-saved under SysV and Win64. The only caller is
InitializeDeferredModulein DynamicLoader.cpp, which calls the entry word as anAPS5_VABIfunction and keeps usingbase,table,countand the lock state after the call. If the compiler keeps any of them in r12 or r14, opening a deferred module that has a preinit array corrupts the loader's state. The startup path is not affected: it calls the preinit entries directly.The fix keeps the length, so
codeSize, the slot RVAs and the test offsets stay the same:With that applied (Ubuntu 24.04, GCC 13.3, relinker-only Release), the converted thunk ends in
pop %r14; pop %r13; pop %r12; retand all 196 relinker tests pass. Without the test change,guest_preinitfails at the pinned bytes. Not run on Windows.No test runs the thunk today:
check_windows_deferredmatches bytes, thentrun takes the startup path, andGuestDeferredModuleFixture.Shas no thunk. A Windows test that opens a converted preinit module throughdlopenwould catch this kind of thing, if you want to add one.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ayam04 on 2026-10-18T15:43:46Z.
Good catch @SandstromPL, and thanks for the repro. Fixed in f294956: the thunk now ends
pop %r14; pop %r13; pop %r12; retinstead of popping in push order, andtest_guest_preinitpins the new bytes. The two hunks are exactly what you posted.The failure mode you describe is the one that matters here:
InitializeDeferredModulein DynamicLoader.cpp keepsbase,table,countand the lock state live across the call into the thunk, andInitializeDeferredModuleis a normal C++ function, so any of those living in r12 or r14 comes back swapped after the first deferred module with a preinit array is opened. Win64 and SysV both treat r12 and r14 as nonvolatile, so the thunk has to restore them in reverse push order.Length is unchanged, so
codeSize, the slot RVAs and the test offsets are the same, and the startup path from.ginitis untouched.Verified locally on Windows (mingw g++, relinker Release):
ctest -R ^guest_preinit$passes and the wider guest/windows/amd64 set is 38/38. The Linux byte dump matches your14001404f: pop %r14 / pop %r13 / pop %r12 / ret.On a dlopen test for the converted preinit module: agreed that nothing runs the thunk today. Happy to add one in this PR if @milescoviello wants it here, otherwise it can land as a follow-up so this stays a two-hunk fix.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T17:53:03Z.
@ayam04 the dlopen test point came from @SandstromPL, not me, so it's their call. For my part, a follow-up is fine and keeps this a two-hunk fix.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T18:33:27Z.
Merged while @adriadam10 is away: the review passed and full CI is green on this head.
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-18T18:46:44Z.
@ayam04 @oneandonlydean since this merge, every title I run on Linux aborts at start-up with
Thread destructor callback is already registered. The first parent of the merge,d7a455a90, is fine. Details and the bisect are in #7636.AI-assisted: yes