fix(relinker): write Linux PT_LOAD headers in ascending address order #1559
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1559"
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 ctj01 on 2026-10-08T12:33:00Z.
What
LinuxElfPatcherwrote the program header table as PT_PHDR, the header-block PT_LOAD, the kept original headers, the extra-block PT_LOAD, PT_DYNAMIC and PT_INTERP. The header block is placed after the extra block, so it has the highest address of all loadable segments while being the first PT_LOAD entry in the table. The ELF gABI requires loadable segment entries to appear in ascendingp_vaddrorder, and PT_PHDR and PT_INTERP to precede every loadable entry.Linux before 5.18 sizes the ET_DYN mapping as
last PT_LOAD p_vaddr + p_memsz - page_start(first PT_LOAD p_vaddr)in table order (total_mapping_size,fs/binfmt_elf.cv5.15). For a relinked executable that difference underflows,elf_mapfails after the point of no return, and the process dies with SIGSEGV before the dynamic linker runs. On 5.18 and later the kernel takes the minimum and maximum over all PT_LOAD entries, so the file loads, but the first mapping still reserves the range starting at the header block instead of the lowest segment.Kernel coverage, from
fs/binfmt_elf.cof each release:mainThe last row lacks the backport of the 5.18 AT_PHDR fix, so
AT_PHDRis computed as the first PT_LOAD'sp_vaddr - p_offsetpluse_phoff. That points into the header block only while the header block is the first PT_LOAD, which is exactly the order the table-ordertotal_mapping_sizerejects. Supporting those kernels would need the program header table moved to a segment whose file-to-address delta matches the lowest PT_LOAD; that is a separate change. No kernel runsmainand fails with this pull request.ProgramHeaderLayoutBuilder::WriteLayoutnow writes PT_PHDR, PT_INTERP, the kept original headers with their PT_LOAD entries sorted byp_vaddr(the Windows image builder already sorts them inWindowsLoadImage), the extra-block PT_LOAD, the header-block PT_LOAD and PT_DYNAMIC. The guest module writer already orders its segments this way.Rebased on
mainafter #1586 merged: itsPT_GNU_EH_FRAMEslot check and warning are kept, and a droppedPT_GNU_EH_FRAMEis removed from the kept headers before they are laid out.Tested
WSL2 Ubuntu 24.04 (kernel 5.15.167.4-microsoft-standard-WSL2, glibc 2.39, GCC 13.3), Linux build with
-DBUILD_TESTING=ON.linux_entry_argvfails there: the output exits with SIGSEGV (-11) instead of SIGTRAP. Reordering the PT_LOAD entries of that output by hand makes it run, which isolated the cause.main: 514/514 withctest --test-dir build -j8(23 GPU tests skipped by the WSL Vulkan device), includinglinux_entry_argv,linux_eh_frame_headerand the new test.linux_program_header_ordertest: a single-segment image, a three-segment image (text, rodata, data with bss) with a kept PT_OS_PROCPARAM entry, and the same image with its PT_LOAD entries out of order in the input. It checks PT_PHDR first, PT_INTERP before the first PT_LOAD, strictly ascending PT_LOAD addresses, the preserved non-load entry, that the 5.15 table-order span equals the address span, that PT_PHDR is covered by a PT_LOAD, and on Linux x86-64 it executes the outputs and expects SIGTRAP from the entry.No title tested.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-08T14:11:42Z.
Rebased on #1586, which changes the same lines of
WriteLayout, and marked it as a dependency. Its slot check forPT_GNU_EH_FRAMEis kept; a dropped header is now removed from the kept list before the table is laid out. Full suite on top of #1586: 454/454 on WSL2 (kernel 5.15), includinglinux_eh_frame_header.Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-08T17:22:02Z.
While the upstream runs wait for approval (#1631), the same Build workflow ran on the fork for this head (
aaf33add): https://github.com/ctj01/AnyPS5/actions/runs/37812797526linux_program_header_order,linux_entry_argvandlinux_eh_frame_headerincluded).Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-08T20:24:11Z.
The
ElfConstants.hppL64 clash with #1506 reported above comes from #1586, which this pull request is based on (PT_GNU_EH_FRAMEvs #1506'sPT_GNU_STACKon adjacent lines). The commit of this pull request does not changeElfConstants.hpp; the report will clear once #1586 and #1506 are reconciled or either one merges.Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-09T11:38:06Z.
Rebased on
mainnow that #1586 is merged; the dependency note is gone and the pull request is back to its single commit (1352b9b3). Full suite on currentmain(d70b8998) on WSL2, kernel 5.15: 514/514, includinglinux_entry_argv,linux_eh_frame_headerandjmprel_table_bounds.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T14:57:09Z.
Review: COMMENTED
Verified on Linux, with the artifact and the kernel source both in hand.
d70b8998(upstream/main) + head1352b9b3, merged clean.linux_program_header_orderPassed (0.11 s).ProgramHeaderLayoutBuilder.{cpp,hpp}reverted tomain, the PR's test kept): fails —AssertionError: ('single-load', [6, 1, 1, 1, 2, 3]), i.e.PT_LOADsecond andPT_INTERPlast, exactly the ordering the test exists to reject.Your kernel citation checks out — I read it rather than trusting it.
fs/binfmt_elf.cv5.15, line 402:first_idx/last_idxare chosen by table index, so the result genuinely depends on table order — the mechanism is real, not inferred. And v5.18 replaces it withmin/maxover allPT_LOADentries (line 404 of that version), so the second half of the description is right too.I then computed that function's value on the produced file (
/bin/truethrough each binary — 15 headers, but the first and lastPT_LOADin table order are what matter):mainPHDR, LOAD, PHDR, INTERP, LOAD x4 ...PHDR, INTERP, PHDR, INTERP, LOAD x4 ...PT_INTERPbefore every loadPT_LOAD p_vaddrascending0x11000first,0xf938last)0x0->0x11000)total_mapping_size(0xf938 + 0xd5d) - page_start(0x11000)= -2411 -> as u640xfffffffffffff695(0x11000 + 0x350) - 0=0x113500x113500x11350So the underflow you describe is reproducible by evaluation, not just by argument: on
mainthe kernel's own quantity goes negative, which is theelf_mapfailure. With the change both kernel eras agree on0x11350, which is the tidiest statement of why the reorder is correct.What I could not do: reproduce the SIGSEGV itself. This box runs 7.2.9, i.e. the >=5.18 form, so the failure mode is out of reach here and I am not claiming otherwise — the arithmetic above is the substitute, and it is the same quantity the pre-5.18 path computes.
Two notes. Pre-existing, not introduced here: the output carries two
PT_PHDRand twoPT_INTERPentries (one synthetic, one kept from the input) in both arms — harmless since both interpreter entries name the same path, but since this PR is the one rearranging that table it may be worth a line in the body. And for sequencing:#2274edits the same lines ofProgramHeaderLayoutBuilder.cpp(keeping<andkSyntheticProgramHeaderCount = 4), so whichever of the two lands second will need a rebase.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T14:57:57Z.
One more instrument, since the table order is the whole subject of this PR. REA's pwntools-elf adapter, run on both artefacts (digest-anchored; same pinned toolchain as the CI adapter — pwntools 4.15.0 / pyelftools 0.33 / unicorn 2.1.2):
sha256 a83fbd38…): type sequencePT_PHDR, PT_INTERP, PT_PHDR, PT_INTERP, PT_LOAD x4, PT_NOTE x3, PT_GNU_EH_FRAME, PT_LOAD, PT_LOAD, PT_DYNAMIC;PT_LOADvaddrs0x0, 0x2000, 0xb000, 0xebd0, 0xf938, 0x11000— ascending,PT_PHDRfirst,PT_INTERPbefore every load.main:PT_PHDR, PT_LOAD, PT_PHDR, PT_INTERP, …withPT_LOADvaddrs0x11000, 0x0, 0x2000, 0xb000, 0xebd0, 0xf938— not ascending, andPT_INTERPafter the loads.That is a third independent read of the same structure (the other two being
readelfand a direct PHDR parse), agreeing on both the order and the addresses.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-09T16:30:39Z.
@Dextroid17 thank you for the arm A/B run and for evaluating
total_mapping_sizeon a real output; the0x11350agreement is a cleaner statement than mine.Checking your kernel reading led me to a gap in my description, now corrected. Before 5.18,
AT_PHDRwasfirst PT_LOAD p_vaddr - p_offset + e_phoff. The 5.18 fix that derives it from the PT_LOAD containinge_phoffwas backported to stable only in 5.15.33 and 5.10.110 (5.15.32 and 5.10.109 still have the old form). So:mainand this PR run.mainfails at exec, this PR runs.total_mapping_sizeunderflows; with it last, the oldAT_PHDRno longer points at the table.No kernel goes from working to failing, but the description now says exactly that instead of claiming every 5.15 or 5.10.
On the duplicate
PT_PHDRandPT_INTERP: agreed, pre-existing and worth fixing, but in a separate PR. It matters more than it looks. glibc'srtld.ctakes the load bias from everyPT_PHDRin turn (l_addr = phdr - p_vaddr), so the last one wins, and here that is the input's entry, whosep_vaddrdoes not describe where the table is mapped. The kernel takes the firstPT_INTERP, which this PR makes the synthetic one. I'll open a follow-up that drops the input'sPT_PHDRandPT_INTERPinSegmentFilter.On #2274: it also edits these lines of
WriteLayout. I'll rebase whichever way they land.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T19:59:49Z.
The corrected description is the right granularity: it names which kernels move and, more importantly, states that no kernel goes from working to failing. Splitting 5.15.33-5.17 and 5.10.110+ (WSL2's 5.15.167 included) from older 5.15/5.10 is a much stronger claim than "every 5.15 or 5.10", because the older pair failing in both directions is a real bound rather than a hedge. I cannot add a kernel-version data point myself — this box runs 7.2 — so I am taking the backport versions on your reading.
On the
PT_PHDR/PT_INTERPfollow-up: thel_addr = phdr - p_vaddrpoint is what makes it a defect rather than tidiness, and the asymmetry you found is the interesting part — glibc's last-wins against the kernel's first-wins means the two consumers disagree about the same table. That is worth the separate PR, and dropping the input's entries inSegmentFiltersounds like the right place. Happy to run it independently when it exists; the relinker does not need a GPU, so I can do that properly here.On #2274: agreed, rebase whichever lands first, and I will re-check the ordering claim on whichever head survives.
Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-09T20:57:24Z.
Independent Windows and Arch WSL validation of head
1352b9b34d0301e71bc31e8260a16db73aed5b9b, applied unchanged to current main8b393b6ein isolated checkouts:x86_64-ucrt-posix-seh), CMake 4.3, Ninja, Python 3.12.14: Release relinker-only build,BUILD_TESTING=ON, 41/41 CTests passed, parallel 4, 30 s per-test timeout, 17.30 s total.6.6.87.2-microsoft-standard-WSL2, GCC 16.2.1, CMake 4.4.4, Python 3.14.7: the same Release relinker-only configuration, 44/44 CTests passed, parallel 4, 30 s per-test timeout, 4.71 s total. This includeslinux_program_header_orderand native Linux entry execution.Additional title observation: Demon's Souls stopped on an unmapped global during initialization with the main-generated Linux executable, whose first PT_LOAD was the high-address header block. After reconverting with this PR, it passes that access, reports the direct-memory budget, and maps its initial heaps. It then faults later with guest allocations placed at native high Linux addresses; I am investigating #861 next. This is startup progress, not a playable-title claim. The runtime libraries for this title observation were main
8b393b6eplus #2310 and #2054, distinct from the isolated relinker test branches above.No full runtime suite was run for this PR; these counts cover the complete relinker-only suites. Validation performed with OpenAI Codex assistance.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:04:44Z.
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/general/ProgramHeaderLayoutBuilder.cppL134-135, L144, L149-150Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T01:10:54Z.
Checked: merges cleanly onto current
main, relinker build + ctest 43/43 here;linux_program_header_orderfails whenProgramHeaderLayoutBuilderis reverted tomainand passes with the change. PT_PHDR/PT_INTERP first, PT_LOAD ascending, kept non-load entries preserved. Windows and WSL2 runs by @Mouhand-Kaddo and the kernel-source check by @Dextroid17 agree.Note: #2274 edits the same part of
WriteLayout; whichever lands second will need a rebase.Merging.