fix(relinker): run a guest DT_INIT of 0 as the module initializer #6684
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6684"
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 SandstromPL on 2026-10-16T04:39:46Z.
What
Until Dawn (CUSA02636) bundles
libc.prxandlibSceFios2.prx. SuperCatElvis reported stop 7 of the title's conversion on #3093, inlibc.prx:They then confirmed that both modules have DT_INIT (tag 12) present with value 0. Only
libc.prxhas a non-emptyDT_PREINIT_ARRAY(0xc2040, one entry).The guest reader stores DT_INIT as a plain address, and every user of it reads 0 as "no initializer" (GuestImageReader.cpp L263-L265 and L299, and since #6106 GuestModuleBuilder.cpp L313). In these modules 0 is the start of the text segment, ahead of the PLT at 0xc8, and no unwind function starts before the PLT. The loaders below call base + DT_INIT there. On
main, past the earlier stops:libc.prxfails with the message above, because the reader rejects a preinit array without an initializer;libSceFios2.prxconverts, but its initializer never runs. The Linux output gets noDT_INITstub, and the Windows and macOS startups skip it. Nothing reports this.What loaders do with a DT_INIT of 0:
obj->init = relocbase + d_ptrwhenever the tag is present (rtld.c L1533-L1536) and calls it when that sum is nonzero (L3292-L3298). For a loaded module that is code at its base. 9.0 does the same (L913-L914, L1480-L1481).kernbranch) stores relocbase + DT_INIT the same way, rejects a DT_INIT outside the text segment withENOEXEC(subr_dynlib.pas L1636-L1646), and reports that address to libkernel (kern_dynlib.pas L482-L486, L641-L644).sce_module/libc.prxandlibSceFios2.prxthis way (sysmodule_internal.cpp L137-L141).GuestImagenow hasHasInit, set when tag 12 is present. The reader, the writers and the module builder test it instead ofInit != 0:DT_INITstub, which passes the module arguments, for an initializer at 0.libkernel.prxdependency that #6106 adds for a module with an initializer, since the stub imports the module-argument getter and setter from it..ginitentry record it. Its RVA is 0x10000, so their own "RVA 0 means none" stays valid.A module whose DT_INIT is 0 while address 0 is not in an executable segment now fails with
Unmapped or inaccessible guest address 0.mainignored that DT_INIT.Not changed:
libc.prx, 0x3d040 inlibSceFios2.prx), so 0 still means none there.EntryPointCollector,StrictNidFilter) still skip a DT_INIT of 0. They only run on the executable.DT_PREINIT_ARRAYentries itself. It leaves them to the module's own initializer, and this PR only makes a DT_INIT of 0 count as that initializer. #6112 ran the entries before the initializer; it was merged and then reverted in #7646, because a title's ownlibc.prxalready runs its preinit entry from its initializer, so the entry ran twice (#7636, details below).create-fselfwrites DT_INIT only when it is nonzero (OELFGenDynlibData.go L956-L958), so modules built with it are not affected.Tested
Ubuntu 24.04, GCC 13.3, CMake 3.28, Ninja, Release relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), onmain324e0e48e, which this branch merges.The new
guest_zero_initializertest (relinker/tests/test_guest_zero_initializer.py) converts a synthetic module whose text segment maps file offset 0x1000 at address 0, with DT_INIT 0. The initializer at 0 readsfs:[0], calls the host'sRecordArgsand returns a value. There are two shapes, one with a one-entry preinit array (likelibc.prx) and one without (likelibSceFios2.prx). For each:libkernel.prxand has aDT_INITstub. On this x86-64 host the converted module loads, records the pending arguments, then records the initializer's return value.fs:load is left.fs:load is left. A--module-dirmodule's.ginitentry is nonzero.A third shape puts the text segment at 0x1000, so 0 is unmapped. It fails with
Unmapped or inaccessible guest address 0for all three targets.On
mainthe test fails withGuest PREINIT_ARRAY has no module initializer. Reverting one part at a time also fails it:libc.prx;.ginitline: entry 0;fs:load stays in the macOS and Windows modules.For the builder line I also ran a module with no dependencies and DT_INIT 0 under an executable that imports nothing from
libkernel.prx, with the host library aslibs/libkernel.prx. Without the line, the executable stops with a symbol lookup error, and the same module from--module-dirfails to load withundefined symbol: __aps5_get_pending_module_args_nid_no_patch. With it, both load and the initializer's result is recorded.Full relinker-only
ctest: 209/209 (208 onmain, plus this test). The new test passed 5 runs out of 5.Release ASan build (
-DANYPS5_ENABLE_ASAN=ON,ASAN_OPTIONS=detect_leaks=0, as in #7147): 209/209, and the new test passed 5 runs out of 5. Its Linux run loads the host library into Python with ctypes, so it now hasadd_asan_test_options(guest_zero_initializer verify_asan_link_order=0)like the other tests that do. Without that line it fails withASan runtime does not come first in initial library list.On a title: savvaniss ran a leave-one-out on Hasbro Family Fun Pack (CUSA03312) (comment, full table on #6977). On
mainf3059421f(before the revert) with #7660, #6619, #5138, #6381,3b158c1e4from #6977, #6354, #6357, this PR and #6618 merged, the Linux and--windowsconversions of the eboot and its 9 bundled modules complete. Without this PR, both stop inlibc.prxwithGuest PREINIT_ARRAY has no module initializer.On Until Dawn (CUSA02636): SuperCatElvis ran this PR on Windows 11 (RTX 3090 Ti, WinLibs GCC 15.2.0), on
mainof 2026-10-19 with #6595, #6618, #6977 and their own local fixes merged:libc.prxno longer stops withGuest PREINIT_ARRAY has no module initializer.libSceFios2.prx's initializer runs: itsSceFiosIOandSceFiosCallbackthreads come up, and file I/O works.With their other local fixes the title boots, creates and reads its save, and reaches its own loading screen. It then stops at a GCN
v_cmpxthat the recompiler doesn't implement yet.Merges:
376c04029) and then reverted in #7646 (e1248700d) after #7636, so its conflict with this PR and its deferred Windows thunk check no longer apply. With #6112 out ofmain, this branch mergesmain324e0e48eas is, with no conflicts (40a2e871a).0500362ffadds the ASan test option above (#7147 addedadd_asan_test_options), andf77a9eaa0is the module builder change (#6106 added that check tomainafter this PR was opened).mainplus this branch, five apply cleanly and25266b497has a conflict intest_guest_zero_initializer.pyonly, where both add lines around the native load. Its copy doesn't havef77a9eaa0, so it picks up thelibkernel.prxdependency from here.main, and this PR adds no conflict to any of them. Where one conflicts in a file this PR touches (#5138 inGuestImageReader.cpp, #2780 inGuestModuleBuilder.cpp, five incore/relinker/CMakeLists.txt), the conflicting hunks are the same with and without this PR.Why the preinit array is left to the initializer. SuperCatElvis disassembled Until Dawn's
libc.prxfrom 0 up to the PLT at 0xc8 (same comment). It is a plain module initializer taking(rdi, rsi, rdx):-1sentinel, and calls each entry.rdxis non-null, it tail-calls it with(rdi, rsi). Otherwise, if the qword at 0xbc890 is nonzero, it tail-jumps to 0x600; else it returns 0.So the module runs its own preinit entry, like the Wind Waker Recompiled (PPSA99309)
libc.prxthat milescoviello traced on #7636, where #6112 ran that entry a second time. That is why this PR only makes a DT_INIT of 0 count as the initializer and runs nothing else.The code from 0 to 0xc8 has no
fs:access. So for this module, the code-analysis entry point at 0 is not what makes it work. It covers an initializer at 0 that does use TLS, which the test checks.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)libc.prxandlibSceFios2.prx); CUSA03312 #7198 (Hasbro Family Fun Pack: stop 6 in savvaniss's conversion; in their leave-one-out, both the Linux and--windowsconversions stop inlibc.prxwithout this PR); CUSA02636 (no Game bring-up discussion): stop 7 of SuperCatElvis's conversion on #3093, in the bundledlibc.prx; this also runslibSceFios2.prx's initializer, whichmainskips. SuperCatElvis confirmed both on Windows with this PR, #6595, #6618, #6977 and local fixes. The title does not reach it onmain: stop 1, the eboot's jump tables (#6595), still stops it first. In the modules, stops 4 and 5 come before it (#5138; stop 5, a DTPMOD64 against the index-1 symbol, is fixed by a commit in #6977). Stops 6, 8, 9 and 10 (#6357, #6381, #6354) are now fixed onmain.🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-16T04:49:12Z.
Independent Linux validation at PR head
ab9d21afcfbc4b0ebeba3305b06d8d763b0e6c76: configured a clean Release relinker-only build with GCC 12.2 / Ninja, builtrelinkerandguest_module_args_host, then ranctest --test-dir /tmp/build-review-6684-pr -R "^guest_zero_initializer$" --output-on-failure: 1/1 passed.git diff --checkalso passes. I initially tried to run the Python test with the relinker executable as its host-module argument, which failed because that parameter needs the dedicated sharedguest_module_args_host.prx; after building the correct target, the registered test passes. CI also passes Ubuntu, Windows and macOS relinker jobs.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T05:32:19Z.
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/relinker/tests/test_guest_zero_initializer.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.txtcore/relinker/CMakeLists.txtcore/relinker/relinker/src/analysis/CodeInstructionCollector.cppcore/relinker/CMakeLists.txtcore/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/analysis/CodeInstructionCollector.cppcore/relinker/relinker/src/guest/GuestImageReader.cppcore/relinker/relinker/src/guest/GuestModuleBuilder.cppcore/relinker/relinker/tests/test_guest_zero_initializer.pycore/relinker/CMakeLists.txtcore/relinker/relinker/src/analysis/CodeInstructionCollector.cppcore/relinker/relinker/src/analysis/CodeInstructionCollector.cppcore/relinker/relinker/src/guest/GuestModuleBuilder.cppImported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-18T20:59:50Z.
Second-title data point: Hasbro Family Fun Pack (CUSA03312, my own dump) needs this PR, on Linux and Windows.
Relinker-only Release build, Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7. Starting from
mainf3059421f, I merged, in this order: #7660, #6619 (a9fa0e8b0), #5138 (5f5641732), #6381 (6f8d9a09d),3b158c1e4from #6977, #6354 (81561867f), #6357 (32fe3aa8b), #6684 (ab9d21afc) and #6618 (bf06a8e5c). With that stack, the Linux and--windowsconversions of the eboot and its 9 bundled modules complete. I then rebuilt the relinker once per member with only that member left out. Without this PR, both targets stop inlibc.prx:The other members each stop at their own step, so every one of them is needed for this title; the whole table is on #6977.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T21:22:47Z.
Thanks for the detailed leave-one-out run. It isolates this PR as necessary for Hasbro Family Fun Pack (CUSA03312) in the cumulative Linux/Windows conversion stack. Without #6684, both Linux and Windows conversions stop in libc.prx with “Guest PREINIT_ARRAY has no module initializer.” I tried to add the setup and result to the PR's Tested section, but GitHub denied the body edit with an admin-rights error; the full evidence remains here in your comment.
Imported from boykopovar/AnyPS5. Originally posted by SuperCatElvis on 2026-10-19T10:11:26Z.
Tested on Until Dawn (CUSA02636, 1.03 eboot) on Windows 11: this PR works, and with it plus a few others the game now reaches its own loading screen.
Setup: Windows 11, i9-12900KF, RTX 3090 Ti, MinGW (WinLibs GCC 15.2.0), cmake 4.3.1, ninja 1.13.2. Branch:
main(as of 2026-10-19) with #6684, #6595 (+ #6618) and #6977 merged, plus the local fixes listed below.This PR (#6684)
libc.prxno longer stops withGuest PREINIT_ARRAY has no module initializer.libSceFios2.prx's initializer runs: itsSceFiosIOandSceFiosCallbackthreads come up and file I/O works.To answer the question in the description, I disassembled
libc.prxfrom 0 up to the PLT at 0xc8. It is a plain module initializer taking(rdi, rsi, rdx):0xc2040..0xc2048, one slot), calling each non-null entry.0xc2020, skipping nulls and stopping at the-1sentinel, calling each entry.rdxis non-null it tail-calls it with(rdi, rsi). Otherwise, if the qword at0xbc890is non-zero, it tail-jumps to0x600, else it returns 0.There is no
fs:access anywhere in 0x0–0xc8.What else Until Dawn needed after #6684 / #6595 / #6977
The patches are in the attached
until-dawn-fixes.patch.txt(12 commits,git amon top ofmainwith the three PRs merged). In order of where the game stopped:Relinker, Windows: imported TLS relocations. Stopped with
Imported TLS relocation 16. The executable imports a TLS variable from the bundledlibc.prx. R_X86_64_DTPMOD64 / DTPOFF64 (16/17) against a named symbol are now passed through to the Windows binder and bound through.tlsrefs.Relinker, Windows: PS4 library names. 73 imports were unresolved because the title asks for
libSceGnmDriver_padebug,libSceSaveData,libSceSaveDataDialogandlibSceVideoRecording. These now also loadlibSceGnmDriver,libSceSaveData.native,libSceSaveDataDialog.nativeandlibSceVideoRecordingP.#6977 merged on current
main. Conflicts resolved inEvent.cpp,TextureDetiler.cpp,TextureFormattests andPipelineJournal.cpp(struct sizes changed, format bumped to 5). Duplicate VideoOut cursor stubs removed.libSceGnmDriver. Added:
GetTheTessellationFactorRingBufferBaseAddress. The console address is in the arena's system-reserved hole, so the title couldn't map it; the ring now goes at the highest free 2 MiB-aligned guest address and AGC is told throughsceAgcDriverSetTFRing.DrawInitDefaultHardwareState200, theUpdate{Ps,Vs,Gs,Hs}Shaderwriters, and the embedded VS/PS shaders.MapComputeQueue[WithPriority],UnmapComputeQueue, andDingDongsubmitting to AGC queues0x20+.RequestFlipAndSubmitDone,SetGsRingSizes, and the validation/trace/mip-stats tool queries.PS4 packets are now lowered for AGC:
Start-up services. Named the 46 imports that were only stubbed by NID, and implemented the ones called during start-up as signed-out / not-connected answers:
sceContentExportInit(PS4 param block: only malloc/free are checked, because the words after are garbage).sceNpGetAccount*by online id.sceVideoOutGetDeviceCapabilityInfo_,sceAppContentGetEntitlementKey.sceKernelPrintBacktraceWithModuleInfo.libkernel:
sceKernelAddReadEventon asceNetSocketsocket. This threwnon-socket descriptor not implemented. Readiness checks and equeue waits now resolve libSceNet sockets throughKernelFindSocketNative, aspoll/selectalready do.libkernel: PS4 direct memory size. The game maps all remaining direct memory as one pool at
0x1015a00000(fixed, no-overwrite), then puts its lump-loader pool at0x1200000000. With the PS5 size (13.5 GiB) the first pool was 0x2e90d0000 bytes and ran over0x1200000000, so the lump loader's fixed no-overwrite map got ENOMEM and the game trapped. PS4 titles (marked whenlibSceGnmDriverloads) now get 5248 MiB: the base console's 4800 MiB plus 448 MiB of room for what AnyPS5 maps before the entry point. The pool is now0xd10d0000bytes and the lump loader maps fine. I also added a log line naming the mapping that blocks a fixed no-overwrite map, which is what found this.Where it is now
The game boots, creates and reads its save (
GAMESAVE), loads Wwise and its asset lumps, and renders the loading screen (skull hourglass, bottom right) for a frame or two. Then it stops in the GCN shader recompiler:That looks like the next real gap: GCN
v_cmpx_*in VOPC form. Opcode 212 = 0xd4, which I read asv_cmpx_gt_u32in the SI/CI table.Still known gaps (they throw if hit):
sceGnmLogicalCuIndexToPhysicalCuIndex.sceSaveDataMountof another title's save.Full logs are available if useful.
until-dawn-fixes.patch.txt
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-19T19:21:28Z.
@SuperCatElvis thank you, that's exactly what this needed, and great progress on the title. I've added your run to the description and replaced the open question with your disassembly.
It settles the preinit question: the code at 0 walks the preinit array itself, like the Wind Waker Recompiled
libc.prxon #7636. So the relinker must not run that entry as well, and this PR only makes a DT_INIT of 0 count as the initializer. The missingfs:access means the code-analysis entry point at 0 isn't what makes this module work. It's there for an initializer at 0 that does use TLS, which the test covers.One note on item 6 of your patch, in case you upstream it: HQasper's #8057, opened today, changes the same function (
AddDescriptorEventinEqueue.cpp) so thatsceKernelAddReadEventaccepts pipe read ends. The socket case would need to sit next to that.AI-assisted: yes (Claude Code)