fix(relinker): reject main-executable PT_LOAD flags outside PF_R, PF_W and PF_X #13862

Merged
roboterspinne merged 3 commits from upstream-pr/13862 into main 2026-11-03 05:09:30 +00:00
roboterspinne commented 2026-10-31 10:03:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-31T10:03:57Z.


What

On 45e39c7ad1 (line numbers below refer to it) a PT_LOAD whose p_flags has a bit other than PF_R, PF_W and PF_X is refused by every reader except the main executable's path to Linux output: WindowsLoadImage throws Unsupported PT_LOAD permission flags for (header.Flags & ~7u) != 0 (WindowsLoadImage.cpp:23-24), which --windows and --macos both build (MacOsMachOPatcher.cpp:65), and the guest image reader fails such a segment as Invalid load segment (GuestImageReader.cpp:48). RelinkerPipeline::Relink reads the executable's program headers (line 81) and walks them (line 89) without that check, and for Linux output ProgramHeaderLayoutBuilder::_fixLoadFlags returns originalFlags | PF_R (line 128), so the unknown bits are copied into the output's p_flags. An executable whose writable PT_LOAD has p_flags 0xe, 0x100006 or 0x80000006 converted with exit 0 for Linux, --lazy-binding and --linux-fixed-base 0x400000, and the output's PT_LOAD kept 0xe, 0x100006 and 0x80000006; an executable PT_LOAD with p_flags 0xd also converted for Linux. --windows and --macos failed on all of them with Unsupported PT_LOAD permission flags.

  • Rule. The pipeline's first loop over the main executable's program headers now throws Unsupported PT_LOAD permission flags at the segment's file offset for every PT_LOAD with (p_flags & ~7) != 0, writable or executable, on every target and before any output is built. The message and the offset are those of WindowsLoadImage, so an input that fails only on its flags fails on Windows and macOS with the same text as before, one step earlier; an input that also has another PT_LOAD error that WindowsLoadImage checks first, such as Empty PT_LOAD segment, now reports the flags first. PF_R, PF_W and PF_X in any combination, including 0, are unchanged, and so are the flags of every other segment type and guest modules.
  • Why reject and not mask. The ELF gABI reserves PF_MASKOS (0x0ff00000) and PF_MASKPROC (0xf0000000) for operating-system and processor flags, and Linux ignores bits it does not know. No flag in these ranges is defined anywhere in the repository, and Windows output, macOS output and guest modules already refuse all of them, so an executable that converts for Windows today has none; this applies the same rule to Linux output instead of keeping one target that passes unknown bits through.

pr_overlap.py --branch finds no shared lines with the open pull requests it can compare and cannot compare #10792 (607bcb9291) and #10861 (2ac648f312), which also change RelinkerPipeline.cpp but do not merge with main. Checked by hand: a trial merge of this branch with each of them conflicts only in the files that main with it already conflicts in (SyscallScanner.cpp for #10792; CodeInstructionCollector.cpp and docs/dev/debt/relinker.md for #10861), and RelinkerPipeline.cpp merges cleanly. In that file #10792 inserts after line 30 and changes lines 463 to 469, and #10861 inserts after lines 5, 7 and 30 and changes lines 434 to 471, 538 to 561 and 673, all away from line 89. The check sits in the pipeline rather than next to the PT_LOAD size check of ElfReader.cpp:143 because ElfReader also reads guest modules, whose reader reports such a segment as Invalid load segment, and because open #11993 changes the start of ElfReader::ReadProgramHeaders.

Tested

Windows 11, WinLibs GCC 15.2.0, relinker-only build (-DANYPS5_RELINKER_ONLY=ON, Ninja, Release), based on 45e39c7ad1 (main at c3c45d73ab changed none of these files and merges cleanly):

  • executable_segments gains a case that sets 0x8, 0x100000 and 0x80000000 on top of the flags of the writable (PF_R | PF_W) and of the executable (PF_R | PF_X) PT_LOAD of its fixture and expects exit 2, Unsupported PT_LOAD permission flags and no output for Linux, --lazy-binding, --linux-fixed-base 0x400000, --windows and --macos. Built with the pipeline file of 45e39c7ad1 it fails at the first case (exit 0 on Linux); on c408446182 it passes.
  • ctest --test-dir build-relinker -j 5 on 0e7140fa4d (c408446182 only adds the executable-segment case to that test): 543 tests, 538 passed, long_paths skipped; select_tests and its three parts select_tests_SelectGraphStateTests, select_tests_SelectBuildTests and select_tests_SelectArgumentTests fail with the Windows symlink fixture's FileExistsError, as on main on this machine; this change touches nothing under tools/.
  • Follow-up commit 4d6f5458c0 (after review): the case now expects the whole diagnostic line, FAIL: Unsupported PT_LOAD permission flags (offset 0x200) for the executable PT_LOAD, whose p_offset is 0x200, and FAIL: Unsupported PT_LOAD permission flags with no offset for the writable one, whose p_offset is 0 (core/relinker/main.cpp appends the offset only when it is nonzero). ctest --test-dir build -R '^executable_segments$' --output-on-failure: 1/1 passed (2.78 s). Counter-checks: run against a relinker built from main (without the new check) it fails at the first case, (64, 8, []); with the check temporarily throwing offset 0 instead of ph.Offset (not committed) it fails at (176, 8, []).
  • python tools/check_conventions.py --base upstream/main --head HEAD with this title and description passed on 4d6f5458c0.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5096546449 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13862). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-31T10:03:57Z. --- ### What On 45e39c7ad1 (line numbers below refer to it) a `PT_LOAD` whose `p_flags` has a bit other than `PF_R`, `PF_W` and `PF_X` is refused by every reader except the main executable's path to Linux output: `WindowsLoadImage` throws `Unsupported PT_LOAD permission flags` for `(header.Flags & ~7u) != 0` (`WindowsLoadImage.cpp:23-24`), which `--windows` and `--macos` both build (`MacOsMachOPatcher.cpp:65`), and the guest image reader fails such a segment as `Invalid load segment` (`GuestImageReader.cpp:48`). `RelinkerPipeline::Relink` reads the executable's program headers (line 81) and walks them (line 89) without that check, and for Linux output `ProgramHeaderLayoutBuilder::_fixLoadFlags` returns `originalFlags | PF_R` (line 128), so the unknown bits are copied into the output's `p_flags`. An executable whose writable `PT_LOAD` has `p_flags` `0xe`, `0x100006` or `0x80000006` converted with exit 0 for Linux, `--lazy-binding` and `--linux-fixed-base 0x400000`, and the output's `PT_LOAD` kept `0xe`, `0x100006` and `0x80000006`; an executable `PT_LOAD` with `p_flags` `0xd` also converted for Linux. `--windows` and `--macos` failed on all of them with `Unsupported PT_LOAD permission flags`. - **Rule.** The pipeline's first loop over the main executable's program headers now throws `Unsupported PT_LOAD permission flags` at the segment's file offset for every `PT_LOAD` with `(p_flags & ~7) != 0`, writable or executable, on every target and before any output is built. The message and the offset are those of `WindowsLoadImage`, so an input that fails only on its flags fails on Windows and macOS with the same text as before, one step earlier; an input that also has another `PT_LOAD` error that `WindowsLoadImage` checks first, such as `Empty PT_LOAD segment`, now reports the flags first. `PF_R`, `PF_W` and `PF_X` in any combination, including 0, are unchanged, and so are the flags of every other segment type and guest modules. - **Why reject and not mask.** The ELF gABI reserves `PF_MASKOS` (`0x0ff00000`) and `PF_MASKPROC` (`0xf0000000`) for operating-system and processor flags, and Linux ignores bits it does not know. No flag in these ranges is defined anywhere in the repository, and Windows output, macOS output and guest modules already refuse all of them, so an executable that converts for Windows today has none; this applies the same rule to Linux output instead of keeping one target that passes unknown bits through. `pr_overlap.py --branch` finds no shared lines with the open pull requests it can compare and cannot compare #10792 (607bcb9291) and #10861 (2ac648f312), which also change `RelinkerPipeline.cpp` but do not merge with `main`. Checked by hand: a trial merge of this branch with each of them conflicts only in the files that `main` with it already conflicts in (`SyscallScanner.cpp` for #10792; `CodeInstructionCollector.cpp` and `docs/dev/debt/relinker.md` for #10861), and `RelinkerPipeline.cpp` merges cleanly. In that file #10792 inserts after line 30 and changes lines 463 to 469, and #10861 inserts after lines 5, 7 and 30 and changes lines 434 to 471, 538 to 561 and 673, all away from line 89. The check sits in the pipeline rather than next to the `PT_LOAD` size check of `ElfReader.cpp:143` because `ElfReader` also reads guest modules, whose reader reports such a segment as `Invalid load segment`, and because open #11993 changes the start of `ElfReader::ReadProgramHeaders`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2.0, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON`, Ninja, Release), based on 45e39c7ad1 (`main` at c3c45d73ab changed none of these files and merges cleanly): - `executable_segments` gains a case that sets `0x8`, `0x100000` and `0x80000000` on top of the flags of the writable (`PF_R | PF_W`) and of the executable (`PF_R | PF_X`) `PT_LOAD` of its fixture and expects exit 2, `Unsupported PT_LOAD permission flags` and no output for Linux, `--lazy-binding`, `--linux-fixed-base 0x400000`, `--windows` and `--macos`. Built with the pipeline file of 45e39c7ad1 it fails at the first case (exit 0 on Linux); on c408446182 it passes. - `ctest --test-dir build-relinker -j 5` on 0e7140fa4d (c408446182 only adds the executable-segment case to that test): 543 tests, 538 passed, `long_paths` skipped; `select_tests` and its three parts `select_tests_SelectGraphStateTests`, `select_tests_SelectBuildTests` and `select_tests_SelectArgumentTests` fail with the Windows symlink fixture's `FileExistsError`, as on `main` on this machine; this change touches nothing under `tools/`. - Follow-up commit 4d6f5458c0 (after review): the case now expects the whole diagnostic line, `FAIL: Unsupported PT_LOAD permission flags (offset 0x200)` for the executable `PT_LOAD`, whose `p_offset` is 0x200, and `FAIL: Unsupported PT_LOAD permission flags` with no offset for the writable one, whose `p_offset` is 0 (`core/relinker/main.cpp` appends the offset only when it is nonzero). `ctest --test-dir build -R '^executable_segments$' --output-on-failure`: 1/1 passed (2.78 s). Counter-checks: run against a relinker built from `main` (without the new check) it fails at the first case, `(64, 8, [])`; with the check temporarily throwing offset 0 instead of `ph.Offset` (not committed) it fails at `(176, 8, [])`. - `python tools/check_conventions.py --base upstream/main --head HEAD` with this title and description passed on 4d6f5458c0. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
adam25136 commented 2026-10-31 21:58:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-10-31T21:58:53Z.

Review: COMMENTED


Reviewed c408446182 merged into main f445f826a9. AI-assisted (Claude); each point checked by hand.

The description's claims match the tree (WindowsLoadImage.cpp:23-24, GuestImageReader.cpp:48, ProgramHeaderLayoutBuilder.cpp:126-128), and Conversion.cpp:95 is the only construction of RelinkerPipeline, so every main-executable conversion reaches the new check. The new case fails against main's relinker (header 64, 0x8, Linux output, exit 0) and passes on this branch.

Non-blocking: test_executable_segments.py:146 matches only the message, while the description says the error is reported at the segment's file offset as WindowsLoadImage does. main.cpp:483 prints the offset only when it is nonzero, so the header == 176 cases can pin it with "Unsupported PT_LOAD permission flags (offset 0x200)"; Linux and --windows output both print it on this branch. The header == 64 segment has p_offset 0 and prints none.

Looks correct to me.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5725123234 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13862#pullrequestreview-5725123234). Originally posted by [adam25136](https://github.com/adam25136) on 2026-10-31T21:58:53Z. **Review:** COMMENTED --- Reviewed `c408446182` merged into `main` `f445f826a9`. AI-assisted (Claude); each point checked by hand. The description's claims match the tree (`WindowsLoadImage.cpp:23-24`, `GuestImageReader.cpp:48`, `ProgramHeaderLayoutBuilder.cpp:126-128`), and `Conversion.cpp:95` is the only construction of `RelinkerPipeline`, so every main-executable conversion reaches the new check. The new case fails against `main`'s relinker (`header` 64, `0x8`, Linux output, exit 0) and passes on this branch. Non-blocking: `test_executable_segments.py:146` matches only the message, while the description says the error is reported at the segment's file offset as `WindowsLoadImage` does. `main.cpp:483` prints the offset only when it is nonzero, so the `header == 176` cases can pin it with `"Unsupported PT_LOAD permission flags (offset 0x200)"`; Linux and `--windows` output both print it on this branch. The `header == 64` segment has `p_offset` 0 and prints none. Looks correct to me.
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!13862
No description provided.