fix(relinker): keep a segment's zero-filled tail zero when the next PT_LOAD starts on its page #6681
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6681"
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 milescoviello on 2026-10-16T04:23:57Z.
What
Nuvio Max'd 0.5.27-native-experimental (PPSA99288, the catalog update of 10-16) segfaults at launch on Linux, before its first log line:
The mutex is a global in the title's bss at 0x5495038, and it holds 0xfde69bc4f9278673, not zero. The title's last two PT_LOADs are:
The next segment starts at 0x5495c20, on the page that holds the last 0xc20 bytes of the data segment's bss. The kernel maps each PT_LOAD from the start of its page, so page 0x5495000 is mapped again from file page 0x3acd000. The bss tail then reads the file bytes in front of the next segment, which are the end of the data segment's own file bytes. 0xfde69bc4f9278673 is the 8 bytes at file offset 0x3acd038. On the console the bss is zero. #4913 described the same page sharing for the relinker's own extra PT_LOAD, which
mainnow starts on a page of its own (GuestLoadPageEnd). Here it's two of the title's own segments. Windows output isn't affected:WindowsLoadImagecopies each segment's file bytes into the image and leaves the rest zero.LinuxElfPatchernow checks each PT_LOAD that starts inside a page. It compares the bytes the kernel maps in front of the segment with what the segments below put there: their file bytes, and zero for their zero-filled part. If they differ, the segment is appended to the output at an offset with the same page offset. In front of it go that page's correct bytes, and its header points there. If they already match, nothing changes.Tested
Linux (Ubuntu 24.04, GCC 13.3, Release), kernel 6.8, RTX 4070 Ti, NVIDIA 595.99.
linux_load_pages(new, no GPU): it patches small ELFs throughLinuxElfPatcherand maps the output's PT_LOADs the way the kernel does, page by page, zeroing past the file size. Then it requires every segment's memory to hold its file bytes and zeros. It has three cases: bss alone below the next segment, file bytes and bss below it, and zeros already in front of it (the segment must stay where it is). Withmain'sLinuxElfPatcher.cppit fails withbss sharing a page with the next segment: byte at 0x2000 reads 0xaa, not 0x00. With this change it passes 3 of 3.-E "^agc_|shader|spirv|recompiler|request_serializer", 667 tests, the relinker fixture tests included): all pass, and the video-out ones skip as onmain.main's relinker and this one (homebrew, plus MGS4 PPSA18194 and Steins;Gate PPSA36591), all 10 outputs are byte-identical. These 13 have nonzero bytes in a bss tail onmainand zero with this change (nonzero bytes onmain): PPSA99288 (1122), PPSA69420 (122), PPSA99621 (89), PPSA99728 (82), PPSA99015 (62), PPSA99082 (62), PPSA99110 (49), PPSA99305 (31), PPSA99641 (19), PPSA99007 (18), PPSA98766 (16), PPSA99313 (15), PPSA99003 (3). Their outputs grow by the size of the moved segment (0.05 to 2.9 MB). Apart from Nuvio, I don't know whether any of them reads those bytes before writing them.Nuvio Max'd 0.5.27-native-experimental (PPSA99288), 90 s with scripted pad input. Both builds use the same
main5d9dcc42dlibraries and differ only in the relinker that converted the title. Three runs each:main5d9dcc42dWith this PR it goes through the same startup stages as 0.5.23, the previous catalog build, up to
startup stage=input-ready(first picture at about 5 s). Its network requests fail only because my sandbox has no network. 0.5.23's file in front of that segment holds zeros, which is why it ran onmain.@simonbbby checked it under the real kernel (Ubuntu 24.04, kernel 7.0.0-38). They built static PIEs from the test's three layouts, kept the patcher's PT_LOADs, and added a checker that reads the earlier segment's file bytes, its whole zero-filled tail and the next segment's bytes. With the parent patcher the two affected layouts exit with the bss mismatch in 5 of 5 runs, and with this PR all three layouts match in 5 of 5. The test also fails on
main'sLinuxElfPatcher.cppthere at the same0x2000 reads 0xaa, not 0x00assertion.@snoozedDev found a retail title that stops on this: Grand Theft Auto III – The Definitive Edition (PPSA03527 v01.007.000). On
mainaf0fff762its main thread segfaults about a second after the swapchain is created, and with this PR (headce0b7672d) it runs (Linux, GCC 16.2.1, RTX 3090).Not checked on macOS or on a console.
@screamm cross-checked the relinker side on Windows.
linux_load_pagespasses 3 of 3 and fails withmain'sLinuxElfPatcher.cpp, and the 102 relinker tests pass. Over 51 local titles, Linux output changes in 6, each by one PT_LOAD moving to a page-congruent offset with its bytes unchanged. Windows output is identical.main0bc3003f8it hits SIGSEGV inscePthreadMutexLockin 2 of 2 runs. The mutex is in the bss tail of theRWsegment, whose page was mapped from the next segment's file page. With this PR (24ec391c0) both runs get past it, to the headlessDisplayWindowstop.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T04:35:04Z.
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/linux/LinuxElfPatcher.cppL95Some 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/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-16T06:26:44Z.
Independent real Linux kernel loading control at exact head
ce0b7672d33157c0eea55281109eb0fd3745e7f4, using synthetic ELF inputs only. Ubuntu 24.04 / kernel 7.0.0-38 / GCC 13.3, Ryzen 9 9950X.Compiled the production patcher and the same support sources listed for
linux_load_pages_tests. The unchanged stock test passes with the PR and fails with onlyLinuxElfPatcher.cppreplaced byHEAD^at the reported0x2000 reads 0xaa, not 0x00assertion.I reused the three fixture layout geometries in a separate generator, with a 123-byte position-independent x86-64 checker in an enlarged synthetic text segment and a readable final segment. ELF version/header-size fields are initialized. After
Patcher().Patch, the harness changes only the entry point to that checker and changesPT_INTERPtoPT_NULL, producing an asset-free static PIE that needs no runtime libraries. The patcher's emitted PT_LOADs are preserved. Each image is executed directly, so the kernel maps those segments.The checker reads the earlier segment's file bytes, its entire BSS tail and the next segment's file bytes. It exits 41 for an earlier-file mismatch, 42 for a BSS mismatch, 43 for a next-file mismatch, or 0 when all match.
The already-correct case keeps the next segment at file offset
0x2c20on both versions. The two affected cases move from0x1c20to0x4c20, and from0x2c20to0x5c20, respectively. A traced execution shows successfulexecvefollowed byexit(42)for the parent andexit(0)for the PR.This confirms the zero-tail repair under the actual kernel's PT_LOAD mapping, with intact file-data controls and an unchanged-layout positive control. No repository source changed. Scope: these synthetic static PIEs and the stock patcher test; no full relinker/runtime, dynamic-linker startup, title replay or macOS run.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-16T06:28:53Z.
Thanks @simonbbby, that's the check I couldn't do in the test, since the test models the kernel's mapping instead of using it. I added your results to the description.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by snoozedDev on 2026-10-17T02:57:19Z.
A retail title that stops on this on
main: Grand Theft Auto III – The Definitive Edition (PPSA03527 v01.007.000, retail disc). This PR (head ce0b7672d) fixes it.Tree:
mainaf0fff762with #6236 (merged since), usingAPS5_PLAYGO_UNLISTED_CHUNKS=absentto get past #6205. Linux (kernel 7.2.5), GCC 16.2.1, Release, RTX 3090, NVIDIA 610.57.04, insidegamescope --backend headless.The stop: the main thread segfaults about a second after the swapchain is created. Cold, that is at 579 s, after 575 s of shader registration. Warm, it is 3 to 9 s after launch. From the kernel log of the cold run:
The fault address was the same in all three runs where I captured it (one cold, two warm, the warm ones through a
SIGSEGVhandler that the write-watch handler chains to). The faulting instruction iscmpq $0x0,0x20(%rdi)at vaddr 0x1c36c21 in the convertedapp.elf(file offset 0x1c3ac21, as in the kernel line), withrdi = 0x800000001. The function at 0x45faf20 loads that value from the global at 0x6690020. The title only initializes that global while it reads zero, so it uses the nonzero value as an object pointer.Why it is nonzero: the last two
PT_LOADs of the eboot, whose offsets and addressesmain's relinker keeps:app.elf)The kernel maps page 0x6690000 again from file page 0x5fc8000, so the last 0xfc0 bytes of the bss hold file bytes: 72 nonzero qwords. The qword at file offset 0x5fc8020 is 0x800000001, the value the title reads at 0x6690020.
With this PR (cherry-picked, relinker rebuilt, eboot reconverted), the segment moves to file offset 0x7c78fc0 with zeros in front of it, and the fault did not occur in any of 7 runs (six warm, one cold). Six ran to their time limits (240 to 1500 s), with #6814 or
APS5_NO_LIBRARY_PREBUILD=1. The seventh, warm with neither, ended at 9 s on #6805; the run data for that stop is on #6814. Anapp.elfconverted bymain's relinker, run with the same libraries and settings as a 900 sAPS5_NO_LIBRARY_PREBUILD=1run, faults again at 0x800000021 3 s in.linux_load_pagespasses on Linux.AI-assisted: yes (Claude Code, Claude Opus 5.5), operating under my supervision; I am accountable for this report and will respond to review.
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T03:31:26Z.
Thanks @snoozedDev, a retail title that stops on this, with the fault address traced back to the zero-filled tail, is the case this was missing. I added it to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T11:37:31Z.
Cross-validation on Windows 11 Pro 26220, WinLibs MinGW-w64 GCC 15.2, PR head
ce0b7672d, scratch-merged onto main673f7406c(no conflict). I can only check the relinker side here; I did not run a title on Linux. No GPU is involved in any of this.linux_load_pages,--repeat until-fail:3: 3 of 3. The 102 relinker tests also pass 3 of 3 on a tree with #6681 and #6713 merged.LinuxElfPatcher.cppfrom main (test kept)linux_load_pagesfails withbyte at 0x2000 reads 0xaa, not 0x00; with the change it passes.tools/check_conventions.py --base origin/mainandgit diff --check: pass.eboot.elf(--windows, same flags, registries on): all 312 output files have identical SHA-256, and 50 relink while PPSA99995 stops with the sameUnsupported Windows guest TLS instructionin both. The PR touches no Windows code, so this is what I expected.e_shoffand the moved segment'sp_offsetin the ELF header. A page-by-page model of the kernel mapping finds 122, 18, 62, 49, 82 and 8 nonzero bytes in the bss tails of main's outputs and 0 in the PR's. The same model finds none in the other 45 inputs, which is why they do not change. The counts match your list for the titles we both have.Not checked: running any title on Linux (GTA III and Nuvio 0.5.27 are not available here), macOS, a full-tree build, the non-GPU suite.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-18T14:30:38Z.
An update on the title evidence, since the catalog has moved on. Nuvio Max'd builds after 0.5.27 no longer crash on
main, so the current catalog build won't show the problem:maina62c724c38EXIT-139at 0 s)0.5.28 has the same layout with 1520 bytes, all zero (not run). All four still end the zero-filled tail of the
RWsegment on the page where the nextPT_LOAD(flags 0) starts. In the later builds, though, the file bytesmain's relinker maps over that tail are already zero. That's the case the description counts for 80 of the 93 eboots, where nothing changes. Samemainlibraries for both relinkers, scripted pad input. So 0.5.27 is still the reproducer, and it's the copy the description's numbers come from. The unit test covers the layout either way.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-18T22:08:48Z.
A reproducer in the current catalog: Régie5 0.6.0 (PPSA99520, catalog release of 10-17). Linux, Ubuntu 24.04 container, GCC 13, Release, relinker and every library rebuilt, 30 s per run.
main0bc3003f8, 2 of 2: SIGSEGV right after its ownlistening port chosen by the systemlog line, inscePthreadMutexLock(mov 0x58(%rbx),%rax,rbx 0x000002f2000002ae), called from the title'spthread_mutex_lock. The mutex is at0x1c25228, in the bss of theRWPT_LOADat0x18b0000(FileSiz0x5550, MemSiz0x3755c8, so the bss ends at0x1c255c8). The nextPT_LOAD(RWE) starts at0x1c255d0, on the same page, from file offset0x18b95d0. So page0x1c25000is mapped from file page0x18b9000, and the 16 bytes at file offset0x18b9228are000002f2000002ae 000002f800000326, exactly what the mutex and the word after it hold.main+ this PR at24ec391c0(clean merge), 2 of 2: no fault; it gets past that point to the headlessDisplayWindowstop, as the earlier Régie5 build did. In the output theRWEsegment's file offset is now0x1a0d5d0, so the bss tail's page no longer comes from the file.Régie5 0.5.1, the previous catalog build, didn't crash there, so this is the same layout luck as Nuvio Max'd 0.5.27 against its later builds.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-19T01:06:12Z.
Thanks @babyhuey, a second title in the current catalog with the same page-sharing layout is good to have. I added the Régie5 0.6.0 run to the description.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T08:50:42Z.
Checked the new
SeparateSharedLoadPagesagainst the diff: file ranges are bounds-checked, moved segments keep the same page offset, and segments whose page already reads right are left in place.linux_load_pagescovers the three layouts, including the no-move case, and fails onmain'sLinuxElfPatcher.cpp. Real-kernel, Windows and title confirmations in the thread (@simonbbby, @screamm, @snoozedDev, @babyhuey) cover the rest. Relinker CI is green on all three platforms; merged.