Relinker: Linux output keeps the input PT_PHDR and PT_INTERP; an input PT_PHDR crashes at start #2816

Closed
opened 2026-10-10 16:02:30 +00:00 by ctj01 · 1 comment
ctj01 commented 2026-10-10 16:02:30 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by ctj01 on 2026-10-10T16:02:30Z.


What

The Linux output keeps the input's PT_PHDR and PT_INTERP next to the ones LinuxElfPatcher writes, because SegmentFilter::ShouldSkip (core/relinker/elfpatcher/src/general/SegmentFilter.cpp) only drops SCE segments, PT_DYNAMIC and some PT_NOTEs. Inputs from a standard toolchain carry both entries, for example ELFs linked with lld such as the ps5-payload-dev SDK homebrew in #2136. Raised in the review of #1559.

  • PT_PHDR: glibc's rtld.c sets the main map's load bias from every PT_PHDR in turn (l_addr = phdr - p_vaddr), so the last one wins. That is the input's entry, whose p_vaddr is where the table sat before relinking. The load bias comes out wrong and the executable crashes at start.
  • PT_INTERP: the kernel uses the first one, which is the synthetic entry since #1559, so a lone input PT_INTERP is harmless. The output still has two, which the gABI forbids.
  • Slots: both copies take program header slots. An input with both entries and exactly enough slots for its own headers fails to relink.

Reproduction

Synthetic lld-style inputs (one PT_LOAD covering the ELF header at address 0, text, data, PT_DYNAMIC, nine header slots), relinked with --skip-sce-module on main ba2a0a31 and run on WSL2 Ubuntu 24.04 (kernel 5.15.167, glibc 2.39):

Input carries Result on main
neither runs
PT_INTERP only runs, two PT_INTERP in the output
PT_PHDR only SIGSEGV at start
both FAIL: Not enough program header slots: need 10, available 9

Proposed fix

Drop both entries in SegmentFilter::ShouldSkip, as it drops PT_DYNAMIC:

if (ph.Type == PT_DYNAMIC || ph.Type == PT_PHDR || ph.Type == PT_INTERP) return true;

The guest module writer already keeps only PT_LOAD, PT_TLS, PT_GNU_EH_FRAME and PT_GNU_STACK, and Windows output does not use SegmentFilter.

A branch with the change and a regression test is ready: https://github.com/ctj01/AnyPS5/tree/fix/relinker-drop-input-phdr-interp (commit 5701d048). It was opened as #2814 and closed by the PR intake rule. The test linux_input_program_headers covers the four inputs above, plus both entries with exactly eight slots. For each output it checks:

  • exactly one PT_PHDR and one PT_INTERP;
  • PT_PHDR is first, covers the table, and matches the PT_LOAD that maps it;
  • PT_INTERP names the host interpreter and precedes the first PT_LOAD;
  • on Linux x86-64, the output runs to its entry.

It fails without the change and passes with it. The full suite passes with the change: 699/699 on that WSL2 setup.

Anyone who can open pull requests is welcome to take the commit as is.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5792914318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2816). Originally posted by [ctj01](https://github.com/ctj01) on 2026-10-10T16:02:30Z. --- ### What The Linux output keeps the input's `PT_PHDR` and `PT_INTERP` next to the ones `LinuxElfPatcher` writes, because `SegmentFilter::ShouldSkip` (`core/relinker/elfpatcher/src/general/SegmentFilter.cpp`) only drops SCE segments, `PT_DYNAMIC` and some `PT_NOTE`s. Inputs from a standard toolchain carry both entries, for example ELFs linked with lld such as the ps5-payload-dev SDK homebrew in #2136. Raised in the review of #1559. - **`PT_PHDR`:** glibc's `rtld.c` sets the main map's load bias from every `PT_PHDR` in turn (`l_addr = phdr - p_vaddr`), so the last one wins. That is the input's entry, whose `p_vaddr` is where the table sat before relinking. The load bias comes out wrong and the executable crashes at start. - **`PT_INTERP`:** the kernel uses the first one, which is the synthetic entry since #1559, so a lone input `PT_INTERP` is harmless. The output still has two, which the gABI forbids. - **Slots:** both copies take program header slots. An input with both entries and exactly enough slots for its own headers fails to relink. ### Reproduction Synthetic lld-style inputs (one `PT_LOAD` covering the ELF header at address 0, text, data, `PT_DYNAMIC`, nine header slots), relinked with `--skip-sce-module` on `main` ba2a0a31 and run on WSL2 Ubuntu 24.04 (kernel 5.15.167, glibc 2.39): | Input carries | Result on `main` | |---|---| | neither | runs | | `PT_INTERP` only | runs, two `PT_INTERP` in the output | | `PT_PHDR` only | SIGSEGV at start | | both | `FAIL: Not enough program header slots: need 10, available 9` | ### Proposed fix Drop both entries in `SegmentFilter::ShouldSkip`, as it drops `PT_DYNAMIC`: ```cpp if (ph.Type == PT_DYNAMIC || ph.Type == PT_PHDR || ph.Type == PT_INTERP) return true; ``` The guest module writer already keeps only `PT_LOAD`, `PT_TLS`, `PT_GNU_EH_FRAME` and `PT_GNU_STACK`, and Windows output does not use `SegmentFilter`. A branch with the change and a regression test is ready: https://github.com/ctj01/AnyPS5/tree/fix/relinker-drop-input-phdr-interp (commit `5701d048`). It was opened as #2814 and closed by the PR intake rule. The test `linux_input_program_headers` covers the four inputs above, plus both entries with exactly eight slots. For each output it checks: - exactly one `PT_PHDR` and one `PT_INTERP`; - `PT_PHDR` is first, covers the table, and matches the `PT_LOAD` that maps it; - `PT_INTERP` names the host interpreter and precedes the first `PT_LOAD`; - on Linux x86-64, the output runs to its entry. It fails without the change and passes with it. The full suite passes with the change: 699/699 on that WSL2 setup. Anyone who can open pull requests is welcome to take the commit as is. Written with AI assistance.
danzaio commented 2026-10-10 17:19:01 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by danzaio on 2026-10-10T17:19:01Z.


Validated the carried branch on Windows: MinGW 15.2 relinker-only build, linux_input_program_headers passes (structural checks; the run-until-entry half is Linux-only). Red fails without the one-line filter, green passes with it. The branch is here if a collaborator wants to carry it: danzaio/AnyPS5 fix/relinker-phdr-interp. AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6100158254 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/2816#issuecomment-6100158254). Originally posted by [danzaio](https://github.com/danzaio) on 2026-10-10T17:19:01Z. --- Validated the carried branch on Windows: MinGW 15.2 relinker-only build, linux_input_program_headers passes (structural checks; the run-until-entry half is Linux-only). Red fails without the one-line filter, green passes with it. The branch is here if a collaborator wants to carry it: danzaio/AnyPS5 fix/relinker-phdr-interp. AI-assisted: yes.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#2816
No description provided.