fix(relinker): drop the input PT_PHDR and PT_INTERP from Linux output #2814

Closed
ctj01 wants to merge 1 commit from upstream-pr/2814 into main
ctj01 commented 2026-10-10 16:00:24 +00:00 (Migrated from github.com)

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


What

Follow-up to #1559, raised in its review by @Dextroid17.

LinuxElfPatcher writes its own PT_PHDR and PT_INTERP, but SegmentFilter also kept the input's. Inputs from a standard toolchain carry both, for example ELFs linked with lld such as the ps5-payload-dev SDK homebrew in #2136.

  • 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 was the input's entry, whose p_vaddr is where the table sat before relinking. The load bias came out wrong and the executable crashed at start.
  • PT_INTERP: the kernel uses the first one. Since #1559 that is the synthetic entry, so a lone input PT_INTERP was harmless, but it still produced two entries, which the gABI forbids.
  • Slots: both copies took program header slots. An input with PT_PHDR and PT_INTERP and exactly enough slots for its own headers failed with "Not enough program header slots".

SegmentFilter::ShouldSkip now drops the input's PT_PHDR and PT_INTERP, as it drops PT_DYNAMIC. The guest module writer already keeps only PT_LOAD, PT_TLS, PT_GNU_EH_FRAME and PT_GNU_STACK, so it is unchanged. Windows output does not use SegmentFilter.

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, on main ba2a0a31.

Before the change, with synthetic lld-style inputs:

Input carries main
neither runs
PT_INTERP only runs, two PT_INTERP in the output
PT_PHDR only SIGSEGV at start
both relink fails: need 10 slots, available 9

New linux_input_program_headers test covers those four inputs 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 whole table, and matches the address of the PT_LOAD that maps the table;
  • PT_INTERP names /lib64/ld-linux-x86-64.so.2 and precedes the first PT_LOAD;
  • on Linux x86-64, the output runs and stops at the entry's int3.

It fails without the SegmentFilter change (two PT_INTERP entries on the second case) and passes with it. Full suite: 699/699 with ctest --test-dir build -j8.

No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4812537537 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2814). Originally posted by [ctj01](https://github.com/ctj01) on 2026-10-10T16:00:24Z. --- ### What Follow-up to #1559, raised in its review by @Dextroid17. `LinuxElfPatcher` writes its own PT_PHDR and PT_INTERP, but `SegmentFilter` also kept the input's. Inputs from a standard toolchain carry both, for example ELFs linked with lld such as the ps5-payload-dev SDK homebrew in #2136. - **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 was the input's entry, whose `p_vaddr` is where the table sat before relinking. The load bias came out wrong and the executable crashed at start. - **PT_INTERP:** the kernel uses the first one. Since #1559 that is the synthetic entry, so a lone input PT_INTERP was harmless, but it still produced two entries, which the gABI forbids. - **Slots:** both copies took program header slots. An input with PT_PHDR and PT_INTERP and exactly enough slots for its own headers failed with "Not enough program header slots". `SegmentFilter::ShouldSkip` now drops the input's PT_PHDR and PT_INTERP, as it drops PT_DYNAMIC. The guest module writer already keeps only PT_LOAD, PT_TLS, PT_GNU_EH_FRAME and PT_GNU_STACK, so it is unchanged. Windows output does not use `SegmentFilter`. ### 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`, on `main` ba2a0a31. Before the change, with synthetic lld-style inputs: | Input carries | `main` | |---|---| | neither | runs | | PT_INTERP only | runs, two PT_INTERP in the output | | PT_PHDR only | SIGSEGV at start | | both | relink fails: need 10 slots, available 9 | New `linux_input_program_headers` test covers those four inputs 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 whole table, and matches the address of the PT_LOAD that maps the table; - PT_INTERP names `/lib64/ld-linux-x86-64.so.2` and precedes the first PT_LOAD; - on Linux x86-64, the output runs and stops at the entry's `int3`. It fails without the `SegmentFilter` change (two PT_INTERP entries on the second case) and passes with it. Full suite: 699/699 with `ctest --test-dir build -j8`. No title tested. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
ctj01 commented 2026-10-10 16:00:26 +00:00 (Migrated from github.com)

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


@Dextroid17 this is the PT_PHDR/PT_INTERP follow-up from #1559 you offered to run independently. The new linux_input_program_headers test holds the four input shapes from the description; the phdr-only case is the one that crashed at start through glibc's last-PT_PHDR load bias.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099419807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2814#issuecomment-6099419807). Originally posted by [ctj01](https://github.com/ctj01) on 2026-10-10T16:00:26Z. --- @Dextroid17 this is the `PT_PHDR`/`PT_INTERP` follow-up from #1559 you offered to run independently. The new `linux_input_program_headers` test holds the four input shapes from the description; the `phdr-only` case is the one that crashed at start through glibc's last-`PT_PHDR` load bias.
github-actions[bot] commented 2026-10-10 16:00:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T16:00:35Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099421261 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2814#issuecomment-6099421261). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T16:00:35Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.
ctj01 commented 2026-10-10 16:02:41 +00:00 (Migrated from github.com)

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


Closed by the intake rule; the reproduction and this branch are tracked in #2816.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099443619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2814#issuecomment-6099443619). Originally posted by [ctj01](https://github.com/ctj01) on 2026-10-10T16:02:41Z. --- Closed by the intake rule; the reproduction and this branch are tracked in #2816.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2814
No description provided.