fix(relinker): move a Linux PT_LOAD whose file offset is not page-congruent with its address #11487
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11487"
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 roboterspinne on 2026-10-26T17:07:35Z.
What
The Linux patcher kept a
PT_LOADwhosep_offsetandp_vaddrdiffer modulo the 4 KiB page at its input offset, so the output cannot be mapped:elf_mapinfs/binfmt_elf.cpassesp_offset - ELF_PAGEOFFSET(p_vaddr)tovm_mmap, andvm_mmapinmm/util.creturns-EINVALwhen that offset is not page aligned. On untouched4017310678,test_linux_fixed_base.fixture()with the segment bytes moved from offset 0x4000 to 0x4010 (vaddr 0,p_align0x10 or 0x4000) relinks with exit 0 to an outputPT_LOAD(offset 0x4010, vaddr 0x400000), and to (0x4010, 0x0) with--lazy-binding.tools/unwrap_self.pyproduces such input: it places each region at the next offset congruent to its vaddr modulop_align(when that is a power of two), starting at 0x400 or the end of the program header table, so a first segment withp_align0x10 at vaddr 0 lands at offset 0x400.SeparateSharedLoadPagesaPT_LOADwith file bytes whosep_offset % 0x1000 != p_vaddr % 0x1000is copied to the end of the file, together with the bytes lower segments place in its first page, through the path that already moves a segment whose shared first page would read other bytes. That path now places the segment at the next offset congruent to its address modulo the page size, with one resize instead of a byte loop, and sets itsp_aligntomin(p_align, 0x1000), so for a power-of-twop_alignthe file honours the congruence its header states:fs/binfmt_elf.cmaps every segment fromp_offset - (p_vaddr % 0x1000)(elf_map) and readsp_alignonly inmaximum_alignment, which skips a value that is not a power of two and aligns theET_DYNload bias to the largest remaining value among allPT_LOADheaders; forET_EXECit is never called. The output therefore no longer grows withp_align: on the merge277ffb4d7c, before5890fc1820, an incongruent segment withp_align0x80000000 produced a 2,147,488,432-byte--lazy-bindingoutput and 0x8000000000000000 ended instd::bad_alloc, and the same padding, onmainsince #6681, gave a 2,147,496,368-byte--lazy-bindingoutput on the merge277ffb4d7c, whose path for a congruent segment is the one onmain, for a segment withp_align0x80000000 whose shared first page would read other bytes; now each of these outputs is at most three pages larger than the input. At a fixed base, every inputPT_LOADp_alignis checked against the base before any segment is moved, inRequireBaseAlignmentwith the messageRebaseProgramHeadersalready uses, so ap_alignthe base is not a multiple of fails before padding and the set of accepted inputs is unchanged. The gABI requiresp_offsetandp_vaddrto be congruent modulo the page size. Congruent segments take the same path as before; segments without file bytes are not moved; other program headers that point into a moved segment keep their file offsets, as on the existing path. The default base 0x400000 and every--linux-fixed-basevalue are 64 KiB aligned, so the loweredp_alignpasses after rebasing. The header blockPT_LOADthat the patcher adds at offset 0 has carried the largest power-of-twop_alignof the keptPT_LOADs since06b3413961;SeparateSharedLoadPagesnow also returns the largest power-of-twop_alignabove the page that it lowered,Patchpasses it in the new fieldMovedLoadAlignmentofProgramHeaderLayoutRequest, andWriteLayoutaligns the header block to at least that value. The output'smaximum_alignment, and with it theET_DYNload bias alignment, is therefore that of the input whether or not a segment moved, while the moved copy itself honours only the page; the padding before the copy stays below a page for every alignment. In--lazy-bindingoutput, as for a kept segment onmain, ap_alignof 0x8000000000000000 puts the header block at that address, where Linux cannot map it; default and fixed-base output refuse such ap_alignbefore any segment moves.docs/dev/debt/relinker.mdrecords that the moved segment's ownp_alignis the page. The fix is not in the reader: the--windowsoutput of the moved input is byte-identical to that of the congruent input, and--macosconverts it; neither output changes with this PR.LinuxLoadPageTests.cpp(linux_load_pages) gets seven cases, each with a code segment at 0x400. Three check the move: a segment at a page start with its offset 0x10 into a page, a segment at 0x1220 with offset 0x1210 whose first page a lower data and bss segment shares, and a segment at 0x2300 whose offset 0x100 lies below its page offset. Each maps the output as the kernel does and compares every byte of every input segment, with distinct fill bytes so a copy from the wrong offset shows; on untouched code each of them alone fails withlinux load pages: PT_LOAD offset and address differ within a page. Four checkp_align:LargeAlignmentMovesToThePagemoves an incongruent segment withp_align0x80000000, 0x4000 and 0x8000000000000000 under--lazy-binding, andLargeAlignmentOnASharedPagemoves a segment withp_align0x80000000 whose shared first page would read other bytes; both require an output at most three pages larger than the input, the same mapped bytes, a movedp_alignof 0x1000 and, computed as the kernel'smaximum_alignmentdoes, a largest outputp_alignequal to the input's, with every outputPT_LOADcongruent modulo its ownp_align.CongruentLargeAlignmentOnASharedPageuses the three loads from oneandonlydean's review: code at 0x400, data at 0x1000 whose bss ends at 0x5c20, and a read-only segment at 0x5c20 with offset 0x1c20 andp_align0x4000, congruent modulo that alignment, whose first page would read the data segment's file bytes. Under--lazy-bindingit requires the same, the moved segment at an offset congruent to 0x5c20 modulo the page, and the header blockPT_LOADat an address that is a multiple of 0x4000 withp_align0x4000; at base 0x400000 it requires a largest outputp_alignof 0x4000 as well.LargeAlignmentAtAFixedBaseIsRefusedrequires that base 0x400000 refusesp_align0x80000000 and 0x8000000000000000 withis not a multiple of the PT_LOAD alignment.pr_overlap.pyreports no shared lines with open pull requests that merge withmain, among them #10917 and #7532, which also changeLinuxElfPatcher.cpp, and #10662, which also changesProgramHeaderLayoutBuilder.cpp. It cannot compare this branch with #5021, #10518, #10861, #11628, #11661, #11689, #11698, #11991, #12678, #12910, #14107 and #14212 (docs/dev/debt/relinker.md), because those do not merge withmain; checked by hand at their current heads, a trial merge of this head with each of them conflicts only in files where the trial merge ofmainated1e0ed08fwith it also conflicts, except with #14212, and the trial merges of this head and ofmainwith #5021 or #10861 conflict in the same single block ofdocs/dev/debt/relinker.md. #14212 puts a blank line between debt entries; its trial merge with this head conflicts only where this PR adds its entry, as it did withffa036cbec, and whichever of the two merges second keeps the entry and the blank line.git merge-treeof this head is also clean against #10479, #10518 and the author's open pull requests #11479, #11488, #11583, #11883, #12341, #13861, #13862, #13914, #13915, #13916, #13977 and #14134. #10564 and #12591, both merged, check and trim overlappingPT_LOADranges at lines 139 to 176 ofSeparateSharedLoadPages(line 128 ofcore/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp); that runs before the loop this PR changes and is unchanged here. In that function this PR changes the skip condition at line 180 and the start ofdiffersat line 195 on main (lines 181 to 182 and 197 here), replaces the padding loop at lines 200 to 202 on main with oneIo::AlignBuffercall (line 202 here), and lowers the movedp_alignand returns the largest power-of-two value above the page that it lowered (lines 128, 177 and 208 to 212 here). Outside it, the fixed-basep_aligncheck ofRebaseProgramHeaders(lines 329 to 331 on main) moves intoRequireBaseAlignment(lines 324 to 327 here), whichPatchalso calls for every inputPT_LOADjust beforeSeparateSharedLoadPages(lines 632 to 635 here), andPatchpasses the returned value to the layout (lines 636 and 650 here).ProgramHeaderLayoutRequest.hppgains the last fieldMovedLoadAlignment(line 22), which onlyLinuxElfPatchersets;ProgramLayoutRangeTests.cppinitialises the struct by position and leaves it 0.ProgramHeaderLayoutBuilder.cppreads it just before the loop that06b3413961added (lines 232 to 233 here). These line numbers refer tomainated1e0ed08fand to this head. #10518, still open, rejects ap_alignthat is not a power of two inElfReader::ReadProgramHeaders; this PR no longer relies on it to bound the output size, because the move path no longer pads top_align. Its debt entry indocs/dev/debt/relinker.mddescribes the byte-by-byte padding of the move path tomax(p_align, 0x1000), which this PR removes, so that entry will need rewording after whichever of the two merges second;git merge-treeof this branch with #10518 (b77442817d) is clean. #10923, merged since, changed onlytools/unwrap_self.pyand its test. It aligns a region to the largest power-of-twop_alignamong the headers it contains, not to its own alone. It still never aligns to the page, so a first segment withp_align0x10 at vaddr 0 and no stricter header inside still lands at offset 0x400. #12307, merged on 30 Oct, changes the rel32 displacement arithmetic ofTracePrologueand_appendTrampolineinLinuxElfPatcher.cppand appends two trampoline cases toLinuxLoadPageTests.cpp; it touches none of the lines this PR changes, and the mergeffa036cbeckeeps both sets of tests.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.4017310678; no relinker changes up toe05ba6feeb, where the change was first tested): on untouched4017310678the three existinglinux_load_pagescases pass; each new case run alone, and all three together, fail withlinux load pages: PT_LOAD offset and address differ within a page. With the change one05ba6feeblinux_load_pagespasses in 0.03 s.p_aligncases that5890fc1820added (LargeAlignmentMovesToThePage,LargeAlignmentOnASharedPageandLargeAlignmentAtAFixedBaseIsRefused) on the merge277ffb4d7cwithout5890fc1820, in that order in one process:linux_load_pagesfails after 13 s at the first of them withlinux load pages: output grew to 2147488432 bytes for alignment 2147483648; withLargeAlignmentOnASharedPagerun first it fails withlinux load pages: output grew to 2147496368 bytes on the shared page path.LargeAlignmentAtAFixedBaseIsRefusedwas not run on that code, whose padding for its 0x8000000000000000 case would run untilstd::bad_alloc; on that code path its 0x80000000 case is padded inSeparateSharedLoadPagesand then refused byRebaseProgramHeaderswith the same message, so the case checks that the set of accepted inputs stays the same. With the fix (5890fc1820)linux_load_pagespasses in 0.26 s.94e4031147on the code offfa036cbec:linux_load_pagesfails atLargeAlignmentMovesToThePagewithlinux load pages: the largest PT_LOAD alignment is 4096, not 2147483648, andCongruentLargeAlignmentOnASharedPagerun alone fails withlinux load pages: the largest PT_LOAD alignment fell to 4096. With the change both pass.test_linux_fixed_base.fixture()withp_align0x10 and 0x4000 and on the same input with its segment bytes andPT_DYNAMICmoved up by 0x10 (segment offset 0x4010), each with--skip-sce-moduleas default Linux,--lazy-binding,--windowsand--macosoutput, onmainated1e0ed08f, onffa036cbecand on94e4031147: the congruent inputs and all--windowsand--macosoutputs have the same SHA-256 on all three. For the moved inputsmainleaves the Linux and--lazy-bindingPT_LOADat offset 0x4010, and both heads copy it to offset 0x9000 (output 0xa1b0 bytes instead of 0x8488 and 0x8478); the 0x1000 bytes mapped at the segment's address are the same in every Linux and--lazy-bindingoutput of the four inputs on all three. Withp_align0x10 the two heads give identical outputs. Withp_align0x4000ffa036cbeclowered the segment top_align0x1000 and the header blockPT_LOADwith it (vaddr 0x402000 withp_align0x1000, 0x2000 with--lazy-binding), so the largest outputp_alignfell to 0x1000;94e4031147keeps the segment atp_align0x1000 and puts the header block back at vaddr 0x404000 (0x4000 with--lazy-binding) withp_align0x4000, as onmain.images()intools/relinker_fuzz_seeds.pybuilds from the relinker's Python tests, plustest_linux_fixed_base.fixture()with itsPT_LOADp_alignset to 0x10, 0x4000 and 0x10000, each relinked with--skip-sce-moduleas default Linux and--lazy-bindingoutput (110 runs) bymainated1e0ed08f, byffa036cbecand by94e4031147: all 110 runs have the same exit code, failure text and output SHA-256 on all three (108 outputs;test_guest_own_tls_relocationsfails on all three withUnnamed import symbol is not a null symbol entry). None of these inputs moves a segment.mainat88795ed448into the branch without conflicts (277ffb4d7c), andmainated1e0ed08finffa036cbec: the only conflict was inLinuxLoadPageTests.cpp, where #12307 and this PR both append test functions and their calls inmain(); both are kept,main's first.LinuxElfPatcher.cppanddocs/dev/debt/relinker.mdmerged automatically. Both trampoline cases of #12307 pass onffa036cbec, andTrampolineDisplacementAcrossInt64Maxruns against the move path of this PR: it uses aPT_LOADat offset 0x400 and vaddr 0x7FFFFFFFFFFFE000, which this PR now copies to the end of the file; the site is rewritten at its input offset before the copy, so the case reads the same bytes.94e4031147:ctest --test-dir build-relinker -j 5: 534 tests, 529 passed, among themlinux_load_pages(0.20 s),program_layout_ranges,linux_load_alignmentandlinux_fixed_base;long_pathsskipped;select_testsand its three partsselect_tests_SelectGraphStateTests,select_tests_SelectBuildTestsandselect_tests_SelectArgumentTestsfail with the Windows symlink fixture'sFileExistsError, as in a full-build run onmainat73f4f7e06eon this machine; this PR changes nothing undertools/.python tools/check_conventions.py --base eb5b2c13df(main) with this title and description passed on94e4031147.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 MarcZX10 on 2026-10-26T17:12:06Z.
Review: CHANGES_REQUESTED
I reviewed the loader-mapping change and its three new byte-preservation cases. The targeted tests are detailed, but the new padding path has an unbounded case that needs handling before merge.
When
!congruent,differsis now forced true and this reaches the byte-at-a-time loop below usingmax(p_align, 0x1000). For a page-incongruent segment with a large power-of-twop_align(for example, 0x80000000), this can append nearly 2 GiB before the later fixed-base validation rejects the image; with the lazy path it can still produce a multi-gigabyte output. This is the same padding-before-rebase failure documented in open #10518, whose current proposed validation rejects non-power-of-two values but explicitly leaves large power-of-two alignments unresolved. The new trigger makes the padding path run for every page-incongruent segment, even if no lower segment shares its page.Please make this new path reject or otherwise bound unsupported alignment values before padding, and add a regression proving a large alignment fails promptly without allocating a huge output. If the intended protection is to rely on #10518 for non-power-of-two values, declare that merge dependency; it does not cover the large power-of-two case above.
I also checked synchronization: the PR head is 37 commits behind current
main(check_main_sync.py --strict), although the current-main commits since its merge base do not touch the two changed files and the merge preview is clean. Please sync the branch and rerun the focused relinker tests after the guard is addressed. Hosted relinker checks for this head are still pending; product jobs are skipped by path policy.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T22:06:54Z.
Review: APPROVED
Reviewing exact head
5890fc1820ffd074850a2ecf376ababb3a95e567.What I ran
mainhas not changed the touched Linux patcher, load-page test, or debt files since the PR's clean merge of88795ed448.Results
Findings
Blocking
None.
Non-blocking
p_alignto the page bound. Fixed-base alignment is validated against each original input header before this normalization, preserving the prior accept/reject behavior. The regression suite covers mismatched offsets, shared pages, byte-for-byte mapped results, huge alignments, and fixed-base refusal.LinuxLoadPageTests.cpp. Coordinate the merge order and retain both sets of regression cases.Sources
Not verified
To get this to a good state
No blocking issue remains in the reviewed head. Retain the test coverage when resolving #12307's overlapping test edits.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T16:21:19Z.
Review: APPROVED
Re-checked — new head, looks good
The updated head merges current main, retains the approved loader fix, and preserves the added regression cases.
What I ran
Re-checked head
ffa036cbecc3516dcb0e3f2dcf7cc0b1065a4585against PR baseed1e0ed08fc2452192df91793a33dd0cc7ad5212. The read-only preview onto current main9304896d89f070f65b8f38bd3a8024d5c19f8fe1merged cleanly (tree5ef694b110e8da4508a6c842cb449a607f7b3619). The updated diff retains the six page-alignment cases and incorporates the upstream #12307 test additions. Environment: Linux x86_64, Python 3.11.2, GCC 12.2.0.Results
Unedited command output
Negative control
I did not rerun the negative controls on this merge head. The author reports that the relinker tests and counter-checks passed on
ffa036cbecc3516dcb0e3f2dcf7cc0b1065a4585; I did not independently reproduce them.Not verified
I did not build or run CTest locally. At final refresh, the hosted conventions check was queued and eligibility and labels were in progress. I did not test Linux or macOS behavior.