fix(relinker): accept byte-congruent PT_LOAD overlaps by trimming the earlier segment #12591
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12591"
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 tablefrogpillow on 2026-10-28T21:46:09Z.
What
Some homebrew eboots place a read-only
PT_LOADwhose tail runs over thePT_GNU_RELROstart of the following read-writePT_LOAD, with byte-identical content across the overlap. The relinker refused every such title withOverlapping PT_LOAD memory rangeson both the Linux and Windows paths (#12183: PPSA99770; also PPSA99573, PPSA92013).When an earlier
PT_LOAD's tail overlaps a later one, the overlapping bytes are identical, and the overlap does not extend past the later segment, the earlier segment is trimmed to end where the later one starts, so the later segment owns the shared range. On Linux the preservedPT_GNU_RELROis made read-only after relocation byld.so, matching the earlier read-only mapping; the Windows image builder has no RELRO step, so the shared range stays writable (noted indocs/dev/debt/relinker.md, beside the existing uncovered-PT_SCE_RELROnote).An overlap is still refused when the two segments share a start address, when it extends past the later segment, when the bytes differ (the #10564 case), and — because the Windows image ORs page permissions — when one segment is executable and the other writable and the later segment does not start on a page boundary, which would otherwise share a read-write-execute page. The rule is the same on both loaders, so their acceptance decision matches. The congruence check is bounded to the file-backed part of the overlap and compares spans rather than walking the implicit zero bytes of a large bss overlap.
Tested
Linux (Ubuntu 24.04) and Windows output, Photo Splat VR (PPSA99770, open-source homebrew):
relinker --registry --skip-sce-module eboot.elf app.elfand--windowsboth succeed (previouslyFAIL: Overlapping PT_LOAD memory ranges (offset 0x4288000)). The output has no overlappingPT_LOADs and the earlier segment ends exactly at the later segment's start (0x4688000), withPT_GNU_RELROpreserved. The relinked title also runs (RADV, Mesa 25.2.8): guest runtime, threads and Vulkan device initialise, the first scene loads (288683 splats) and the VR eye buffers are created, and it stops atsceHmd2Initialize(no VR headset on the test box) — a hardware-absence gate, not a segment fault.core/relinker/relinker/tests/test_executable_segments.pyadds cases that run on every target ([],--lazy-binding,--linux-fixed-base,--windows,--macos): byte-congruent tail and bss overlaps are accepted with the earlier segment trimmed to the later start and no overlappingPT_LOADs in the output; a write-execute overlap at an unaligned boundary is refused while an aligned one is accepted with the Windows image carrying no write-execute section; and differing bytes, an overlap past the later end, a non-zero file byte over the later segment's bss, and a shared start address are refused. A no-overlap control keeps the old path.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 repositoryImported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T22:09:28Z.
Review: COMMENTED
Thanks. I ran this on macOS arm64 (relinker-only build,
--linuxdefault /--windows/--macos) at efddf52138, which already includes current main319621ba2e:executable_segments+linux_gnu_segmentsare green,check_conventions --base upstream/mainis clean, and it merges cleanly onto main.pr_overlaponly reports thedocs/dev/debt/relinker.mdsharers it can't compare (#5021, #10861, #11192).Overlapping PT_LOAD memory ranges (offset 0x1800)), so the test does exercise the change.1. The byte compare runs over zero-fill as well as file bytes. The loop goes to
previousEndone byte at a time, even past bothFileSizes, where both sides are known to read 0. If an earlier segment'sp_memszoverlaps a large.bss, that turns into billions of iterations. Same fixture each time, earlier segmentp_filesz 0x602, bothp_memszraised:The same image without the overlap fails in 0.00 s. If the compare stops at the end of the file-backed bytes, every case is instant and nothing else changes (both suites green, 391/391):
(Same edit in
WindowsLoadImage.cppwithsegment/source.)2. Mutants. I tried 12 single-edit mutants against the PR's test:
FileSizeclamp, and "previous side reads zero" (both).FileSizeclamp.The compare and extends-past survivors exist because the existing
overlap-next/overlap-past-endcases are refused by the same-start/extends-past arms before the byte compare is reached, and nothing checks the Windows trim's effect. The test-only diff below kills all of them except the Windows clamp. That one looks equivalent to me: with an unclampedFileSize,copy_nwrites the earlier segment's bytes into the later range, the bytes are congruent by construction, and the later segment copies over them anyway. It is green on head, and with main's sources it fails as before.test diff (appended before the final print)
The last case is a congruent overlap into a writable segment. With the Windows trim removed, an executable+writable section runs past
0x12000into the writable range, and the assertion fails. With the trim, it stops at0x12000.3. Nit on the protection wording. The body says the overlap "leaves no page executable-and-writable". That is true for PPSA99770 because its shared start (
0x4688000) is page-aligned. If the later segment starts mid-page,pageFlags |=still ORs the boundary page. The fixture's.elf1@0x11000page already comes outXWbecause its segments0x1200/0x1800share a page. Main does the same for sub-page adjacency without any overlap, so this isn't a regression; maybe say "page-aligned" in the body or the debt line.Tested: macOS 26 arm64, AppleClang, relinker-only Release, Python 3.13.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T22:22:23Z.
Review: CHANGES_REQUESTED
The approach (trim the earlier segment to the later one's start when the overlap is byte-identical and ends inside the later segment) reads correctly, and the same-start and extends-past refusals hold. What is still open, all from the review above:
WindowsLoadImage.cppORs the page flags per page, so an R+X segment congruent with a following R+W one that starts mid-page yields an RWX page wheremainrefused the image, and Linux gives that page the later segment's protection instead. Accept only layouts that cannot create a W+X page (refuse when one side has X and the other W unless the boundary is page-aligned), with output-permission tests for an aligned and an unaligned boundary, as @oneandonlydean asked.LinuxElfPatcher.cpp:146,WindowsLoadImage.cpp:39) walk topreviousEndone byte at a time past bothFileSizes, where both sides are zero. @rooklz measured 33.9 s for a 64 GiB overlap on--windows/--macos. Stop at the end of the file-backed bytes (their diff:backedEnd = min(previousEnd, max(previous.MappedAddress + previous.FileSize, current.MappedAddress + current.FileSize))), or compare in chunks.overlap-next/overlap-past-endcases are refused by the same-start/extends-past arms, so removing the extends-past check or the byte compare, or the Windows trim, still passes. Add the cases from the two reviews: differing bytes inside the overlap, an earlier segment past the later one's end, bss zero-against-zero accepted and non-zero file bytes against the later bss refused, exact trimmed sizes, a no-overlap control, and the aligned/unaligned permission cases. @rooklz's test diff covers several of these.docs/dev/debt/relinker.md:31): keep only the PPSA ids, drop the title names.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T15:16:50Z.
Review: APPROVED
Reviewed — approved
The overlap acceptance merges cleanly, builds, passes the new and existing relinker tests, and the new test fails on
main's sources; the points from the earlier reviews are addressed. The two notes below don't block the merge: a refusal arm no test covers, and a debt entry that is narrower than what the code accepts.What I ran
Head
324cbd20ebf47fbbff692ef27ae24beb6c638deb, merged with--no-ffontomainbb04a4f1ea0175e9e4f149f0359a764d62ff3296in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF. Relinker-only change, so no GPU tests.Results
maindocs/dev/debt/relinker.mdauto-merged)git diff --checkcheck_conventions.py --base origin/mainAll conventions checks passed.relinkerexecutable_segments0.13 sbuild/core/relinkerexecutable_segmentsfails onmain's sources--windows/--macos--lazy-binding,--windowsand--macostargets (Load segment virtual range overflowed/PT_LOAD address overflow)Mutant results
Failedmeans the test caught the mutant.*-curzeroreplaces the laststd::all_of(later segment's file bytes under the earlier segment's bss) withtrue.win-filesize-clampremoves the WindowsFileSizeclamp. rooklz already argued that one is equivalent, because the later segment's copy overwrites the congruent bytes.Negative control output
Aligned R+X over RW layout, Linux output (from the `overlap-write-execute-aligned` fixture)
Negative control
I restored
LinuxElfPatcher.cppandWindowsLoadImage.cppfrommain, kept the new test and rebuilt.executable_segmentsfails at the first new case,congruent-tail-overlap, withOverlapping PT_LOAD memory ranges (offset 0x1800). With the PR files restored it passes.Findings
Blocking
None.
Non-blocking
One refusal arm has no test on either loader. Code:
LinuxElfPatcher.cpp:167andWindowsLoadImage.cpp:59. This is the case where the earlier segment's bss lies over file bytes of the later segment, and those bytes must be zero. If the check is replaced withtrue,executable_segmentsstill passes (lin-curzeroandwin-curzeroabove). The code handles the case correctly:overlap_image(0x20, 0x602)(earlier bss over the later segment's90 c3) is refused on all five targets. Butoverlap-nonzero-over-bsscovers only the mirror case (earlier file bytes over the later bss). Suggested fix: add("overlap-bss-over-nonzero-file", overlap_image(0x20, 0x602))to the refused list.The debt entry is narrower than what the code accepts. The entry describes "a read-only
PT_LOAD" over aPT_GNU_RELROstart. The code accepts any congruent overlap except an unaligned write-execute one, and the overlapped range then gets the later segment's permissions on both loaders. Two consequences are not in the entry:overlap-write-execute-alignedfixture,0x2000..0x2002was in the R+X segment and comes out RW only (Linux output above).PT_GNU_RELROstay writable, as they do on Windows.Also, CONTRIBUTING asks that an entry state the gap first. This one starts with the implemented behaviour, and it does not say that the console's treatment of overlapping
PT_LOADs (which segment's protection wins) is unchecked. Suggested wording: lead with "The protection the console gives a range mapped by two byte-identical overlappingPT_LOADs is not checked; the relinker gives it the later segment's protection (an earlier executable segment loses execute, an earlier read-only one becomes writable outsidePT_GNU_RELRO, and on Windows inside it as well)". Then list the refused cases.Not verified
PT_LOADs, which would settle whether the later segment's protection is the right one.