fix(relinker): require the Linux entry, relocation offsets and DT_PLTGOT in PT_LOAD memory #10402

Merged
roboterspinne merged 6 commits from upstream-pr/10402 into main 2026-10-26 03:30:07 +00:00
roboterspinne commented 2026-10-23 21:33:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-23T21:33:38Z.


What

The Linux output did not check that e_entry, relocation offsets and DT_PLTGOT lie in PT_LOAD memory: the relink returned 0 and the output crashed at load. Windows and macOS already reject such inputs through WindowsLoadImage (WindowsLoadImage.cpp:56-70, used for relocation targets in WindowsRelocationBuilder.cpp:31 and for DT_PLTGOT in WindowsPePatcher.cpp:43-44 and MacOsMachOPatcher.cpp:65-66).

LinuxElfPatcher::Patch now checks, before both output paths (fixed base and --lazy-binding) and with the same rule as GetRva (the whole range inside one PT_LOAD's MemorySize):

  • e_entry: 1 byte in a PT_LOAD with PF_X, or in a PT_LOAD whose p_flags is 0, because the Linux output maps such a segment RWX (ProgramHeaderLayoutBuilder::_fixLoadFlags, since bc14e4743c).
  • Every RELA and JMPREL r_offset, over the bytes ld.so writes: 4 for PC32 and 32, 8 otherwise. R_X86_64_NONE never reaches this check: extractRela drops it (RelinkerPipeline.cpp:353 on main), SysVDynamicSectionBuilder rebuilds both tables from the extracted references (SysVDynamicSectionBuilder.cpp:153 and 163), and extractLocal appends only RELATIVE and TLS entries. R_X86_64_COPY does not reach it either, because #10658 rejects it in validation. R_X86_64_32S does not reach it, because ValidationPolicy rejects type 11 (#9220) and #10357 removed the constant.
  • DT_PLTGOT when a JMPREL table exists: 8 bytes, or 24 with --lazy-binding, where glibc's elf_machine_runtime_setup writes GOT[1] and GOT[2].

Messages name the field, for example e_entry 0x7ffff000 is not inside an executable PT_LOAD segment and RELA r_offset 0xffc (8 bytes) does not fit inside a PT_LOAD segment. ElfReader turns a PT_SCE_RELRO that no PT_LOAD covers into a PT_LOAD before Patch, so the usual PS5 GOT layout stays accepted. Windows, macOS, guest modules and the Linux output layout are unchanged.

test_linux_load_alignment.py and test_linux_eh_frame_header.py set e_entry to 0x4000, a file offset that their only PT_LOAD (p_flags R|X, p_offset 0x4000, p_vaddr 0, p_memsz 0x1000) maps at virtual address 0. The virtual address 0x4000 lies outside every PT_LOAD, so it is still rejected after the p_flags 0 change (e_entry 0x4000 is not inside an executable PT_LOAD segment). They now use 0 and check the same things as before.

Tested

Ubuntu 24.04 x86_64, glibc 2.39, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.

  • New linux_load_bounds, in the default mode and with --lazy-binding (case list as of the fourth commit, which was tested on Windows only, see below; the rejected cases are unchanged since the first commit): 15 rejected cases (entry outside a load or in a non-executable one; RELA and JMPREL offsets outside a load or crossing its end; DT_PLTGOT outside a load; lazy DT_PLTGOT too close to its end) and 19 accepted ones (4-byte types in the last bytes of a load, offsets in .bss and in an uncovered PT_SCE_RELRO, an entry in a PT_LOAD whose p_flags is 0 with its output segment checked to be RWX, eager DT_PLTGOT at 0xff0). With LinuxElfPatcher.cpp from main, all 15 rejected cases relink with rc 0.
  • First commit: ctest --test-dir build -j2: 241/241 passed.
  • On main, the test_linux_entry_argv fixture with e_entry = 0x7ffff000 and the test_optional_plt fixture with a RELATIVE r_offset = 0x7ffff000 relink with rc 0 and the outputs exit with SIGSEGV. On this branch both fail with rc 2 and the messages above.
  • git merge-tree of this branch with #7532, #10564, #10607 and #10658, which also change LinuxElfPatcher.cpp, is clean.
  • python3 tools/check_conventions.py --base upstream/main with this title and description passed.
  • Second commit, Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build: linux_load_bounds passes in both modes (0.69 s); ctest -j 6: 235/236 passed, the one failure is select_tests, a known local Windows failure (it cannot create its symlink fixture); check_conventions.py with this title and description passed.
  • Third commit, after merging current main (dca72a82da), same host and build: unused_filter_indirect expects the load bounds error for the wrapped GOT slot of got-slot-address-size-underflow and still checks that the filter keeps its import; against a main relinker that case relinks with rc 0. ctest -j 6: 241/244 passed, long_paths skipped; select_tests (the known local Windows failure) and patch_advisor_unit (unchanged from main, fails here because the build path contains spaces) are unrelated; check_conventions.py with this title and description passed.
  • Fourth commit (66473085c2), after review: same host and build, after merging current main (4930ee9e55, merging upstream/main c7d6660ef8). linux_load_bounds adds entry-in-flagless-load in both modes and checks that the output PT_LOAD holding the original entry is RWX; it drops none-outside, which also relinks with rc 0 now that the skip is gone. Counter-check: with the b15f3eac86 relinker the new case fails in both modes with rc 2 and FAIL: e_entry 0x200 is not inside an executable PT_LOAD segment; with this commit it passes. e_entry 0x4000 in both fixtures still fails with e_entry 0x4000 is not inside an executable PT_LOAD segment, so their edits stay. Passed: linux_load_bounds (1.25 s), unused_filter_indirect, linux_eh_frame_header, linux_load_alignment, jmprel_table_bounds, entry_point_bounds, linux_fixed_base, game_patches, init_array_roots, gnu_hash_analysis, linux_load_pages, optional_plt and every other linux_* test. Full suite: ctest --test-dir build-relinker -j 3 on 4930ee9e55: 262 tests, 259 passed, long_paths skipped; failures known on this machine on main as well: select_tests (Windows symlink fixture), patch_advisor_unit (path with spaces); both fail again when rerun alone. check_conventions.py --base upstream/main (c7d6660ef8) with this title and description passed.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4989525439 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10402). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-23T21:33:38Z. --- ### What The Linux output did not check that `e_entry`, relocation offsets and DT_PLTGOT lie in PT_LOAD memory: the relink returned 0 and the output crashed at load. Windows and macOS already reject such inputs through `WindowsLoadImage` (`WindowsLoadImage.cpp:56-70`, used for relocation targets in `WindowsRelocationBuilder.cpp:31` and for DT_PLTGOT in `WindowsPePatcher.cpp:43-44` and `MacOsMachOPatcher.cpp:65-66`). `LinuxElfPatcher::Patch` now checks, before both output paths (fixed base and `--lazy-binding`) and with the same rule as `GetRva` (the whole range inside one PT_LOAD's `MemorySize`): - `e_entry`: 1 byte in a PT_LOAD with `PF_X`, or in a PT_LOAD whose `p_flags` is 0, because the Linux output maps such a segment RWX (`ProgramHeaderLayoutBuilder::_fixLoadFlags`, since bc14e4743c). - Every RELA and JMPREL `r_offset`, over the bytes ld.so writes: 4 for `PC32` and `32`, 8 otherwise. `R_X86_64_NONE` never reaches this check: `extractRela` drops it (`RelinkerPipeline.cpp:353` on `main`), `SysVDynamicSectionBuilder` rebuilds both tables from the extracted references (`SysVDynamicSectionBuilder.cpp:153` and `163`), and `extractLocal` appends only RELATIVE and TLS entries. `R_X86_64_COPY` does not reach it either, because #10658 rejects it in validation. `R_X86_64_32S` does not reach it, because `ValidationPolicy` rejects type 11 (#9220) and #10357 removed the constant. - DT_PLTGOT when a JMPREL table exists: 8 bytes, or 24 with `--lazy-binding`, where glibc's `elf_machine_runtime_setup` writes GOT[1] and GOT[2]. Messages name the field, for example `e_entry 0x7ffff000 is not inside an executable PT_LOAD segment` and `RELA r_offset 0xffc (8 bytes) does not fit inside a PT_LOAD segment`. `ElfReader` turns a `PT_SCE_RELRO` that no PT_LOAD covers into a PT_LOAD before `Patch`, so the usual PS5 GOT layout stays accepted. Windows, macOS, guest modules and the Linux output layout are unchanged. `test_linux_load_alignment.py` and `test_linux_eh_frame_header.py` set `e_entry` to 0x4000, a file offset that their only PT_LOAD (`p_flags` R|X, `p_offset` 0x4000, `p_vaddr` 0, `p_memsz` 0x1000) maps at virtual address 0. The virtual address 0x4000 lies outside every PT_LOAD, so it is still rejected after the `p_flags` 0 change (`e_entry 0x4000 is not inside an executable PT_LOAD segment`). They now use 0 and check the same things as before. ### 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. --> Ubuntu 24.04 x86_64, glibc 2.39, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew. - New `linux_load_bounds`, in the default mode and with `--lazy-binding` (case list as of the fourth commit, which was tested on Windows only, see below; the rejected cases are unchanged since the first commit): 15 rejected cases (entry outside a load or in a non-executable one; RELA and JMPREL offsets outside a load or crossing its end; DT_PLTGOT outside a load; lazy DT_PLTGOT too close to its end) and 19 accepted ones (4-byte types in the last bytes of a load, offsets in `.bss` and in an uncovered `PT_SCE_RELRO`, an entry in a PT_LOAD whose `p_flags` is 0 with its output segment checked to be RWX, eager DT_PLTGOT at 0xff0). With `LinuxElfPatcher.cpp` from `main`, all 15 rejected cases relink with rc 0. - First commit: `ctest --test-dir build -j2`: 241/241 passed. - On `main`, the `test_linux_entry_argv` fixture with `e_entry = 0x7ffff000` and the `test_optional_plt` fixture with a RELATIVE `r_offset = 0x7ffff000` relink with rc 0 and the outputs exit with SIGSEGV. On this branch both fail with rc 2 and the messages above. - `git merge-tree` of this branch with #7532, #10564, #10607 and #10658, which also change `LinuxElfPatcher.cpp`, is clean. - `python3 tools/check_conventions.py --base upstream/main` with this title and description passed. - Second commit, Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build: `linux_load_bounds` passes in both modes (0.69 s); `ctest -j 6`: 235/236 passed, the one failure is `select_tests`, a known local Windows failure (it cannot create its symlink fixture); `check_conventions.py` with this title and description passed. - Third commit, after merging current `main` (dca72a82da), same host and build: `unused_filter_indirect` expects the load bounds error for the wrapped GOT slot of `got-slot-address-size-underflow` and still checks that the filter keeps its import; against a `main` relinker that case relinks with rc 0. `ctest -j 6`: 241/244 passed, `long_paths` skipped; `select_tests` (the known local Windows failure) and `patch_advisor_unit` (unchanged from `main`, fails here because the build path contains spaces) are unrelated; `check_conventions.py` with this title and description passed. - Fourth commit (66473085c2), after review: same host and build, after merging current `main` (4930ee9e55, merging upstream/main c7d6660ef8). `linux_load_bounds` adds `entry-in-flagless-load` in both modes and checks that the output PT_LOAD holding the original entry is RWX; it drops `none-outside`, which also relinks with rc 0 now that the skip is gone. Counter-check: with the b15f3eac86 relinker the new case fails in both modes with rc 2 and `FAIL: e_entry 0x200 is not inside an executable PT_LOAD segment`; with this commit it passes. `e_entry` 0x4000 in both fixtures still fails with `e_entry 0x4000 is not inside an executable PT_LOAD segment`, so their edits stay. Passed: `linux_load_bounds` (1.25 s), `unused_filter_indirect`, `linux_eh_frame_header`, `linux_load_alignment`, `jmprel_table_bounds`, `entry_point_bounds`, `linux_fixed_base`, `game_patches`, `init_array_roots`, `gnu_hash_analysis`, `linux_load_pages`, `optional_plt` and every other `linux_*` test. Full suite: `ctest --test-dir build-relinker -j 3` on 4930ee9e55: 262 tests, 259 passed, `long_paths` skipped; failures known on this machine on `main` as well: `select_tests` (Windows symlink fixture), `patch_advisor_unit` (path with spaces); both fail again when rerun alone. `check_conventions.py --base upstream/main` (c7d6660ef8) with this title and description passed. ### 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 (Claude Code)
MarcZX10 commented 2026-10-23 21:59:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T21:59:36Z.

Review: APPROVED


Reviewed the Linux PT_LOAD validation and the new synthetic test matrix. The checks are applied before fixed-base and lazy-binding output, skip R_X86_64_NONE, account for PC32/32 and COPY widths, and include BSS/RELRO cases. The existing tests were corrected from file offsets to their mapped virtual entry addresses. Hosted relinker matrix and conventions pass; full product builds are skipped by repository policy. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5634899257 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10402#pullrequestreview-5634899257). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T21:59:36Z. **Review:** APPROVED --- Reviewed the Linux PT_LOAD validation and the new synthetic test matrix. The checks are applied before fixed-base and lazy-binding output, skip R_X86_64_NONE, account for PC32/32 and COPY widths, and include BSS/RELRO cases. The existing tests were corrected from file offsets to their mapped virtual entry addresses. Hosted relinker matrix and conventions pass; full product builds are skipped by repository policy. No blocking issue found.
MarcZX10 commented 2026-10-23 22:08:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:08:03Z.

Review: CHANGES_REQUESTED


Blocking correctness issue in the new relocation-width check: RelocationWidth() returns 8 for R_X86_64_32S, but the x86-64 psABI specifies a 32-bit relocation field for that type. This repository accepts R_X86_64_32S in RebasedRelocations(), so a valid 32S relocation whose 4-byte field ends at the PT_LOAD boundary will be rejected as requiring 8 mapped bytes. Please classify R_X86_64_32S as width 4 and add a boundary fixture analogous to abs32-past-load-end that succeeds when the 4-byte field fits. The ABI’s relocation table lists R_X86_64_32S as word32: https://refspecs.linuxfoundation.org/elf/x86_64-SysV-psABI.pdf

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5634957447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10402#pullrequestreview-5634957447). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T22:08:03Z. **Review:** CHANGES_REQUESTED --- Blocking correctness issue in the new relocation-width check: `RelocationWidth()` returns 8 for `R_X86_64_32S`, but the x86-64 psABI specifies a 32-bit relocation field for that type. This repository accepts `R_X86_64_32S` in `RebasedRelocations()`, so a valid 32S relocation whose 4-byte field ends at the PT_LOAD boundary will be rejected as requiring 8 mapped bytes. Please classify `R_X86_64_32S` as width 4 and add a boundary fixture analogous to `abs32-past-load-end` that succeeds when the 4-byte field fits. The ABI’s relocation table lists `R_X86_64_32S` as `word32`: https://refspecs.linuxfoundation.org/elf/x86_64-SysV-psABI.pdf
ahmadmuj commented 2026-10-25 09:56:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T09:56:11Z.

Review: COMMENTED


Checked head b15f3eac86, merged locally onto main 16025845e5 (clean merge). Relinker-only Release build, Ubuntu 24.04, GCC 13.

What I ran:

  • Full relinker ctest on the merge: 266/266 pass.
  • Same merge with main's LinuxElfPatcher.cpp: linux_load_bounds and unused_filter_indirect fail, and all 15 rejected runs relink with rc 0. So the new test still guards the change on current main.
  • The 32S request from b10424a47b doesn't apply anymore. ValidationPolicy.cpp:9-22 only accepts NONE, 64, PC32, GLOB_DAT, JUMP_SLOT, RELATIVE, 32, DTPMOD64, DTPOFF64 and TPOFF64, and R_X86_64_32S isn't in the tree. #10658 is merged, so COPY never reaches Patch either. RelocationWidth matches the psABI field size for every accepted type.
  • RequireLoaded can't wrap: address - MappedAddress < MemorySize is checked before the size subtraction.

Two notes, neither blocking:

  1. An entry in a PT_LOAD with p_flags 0 is now rejected, but the output maps that segment RWX (_fixLoadFlags, ProgramHeaderLayoutBuilder.cpp:127). The entry check reads the original flags instead. Repro: fixture(linux=True) from test_jmprel_table_bounds.py with the first PT_LOAD's p_flags set to 0. On main it relinks with rc 0 and readelf -l shows that segment as RWE. On this branch it fails with e_entry 0x200 is not inside an executable PT_LOAD segment. I don't know if flags-0 loads show up in real inputs. If they do, header.Flags == 0 || (header.Flags & flags) == flags in RequireLoaded would match what the output actually maps.
  2. The != R_X86_64_NONE skip at LinuxElfPatcher.cpp:378 never sees a NONE entry. extractRela drops NONE at RelinkerPipeline.cpp:351 before the tables are built, so none-outside already relinks on main. Same reasoning as leaving out the 32S case: the skip and that case could both go.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642615394 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10402#pullrequestreview-5642615394). Originally posted by [ahmadmuj](https://github.com/ahmadmuj) on 2026-10-25T09:56:11Z. **Review:** COMMENTED --- Checked head b15f3eac86, merged locally onto main 16025845e5 (clean merge). Relinker-only Release build, Ubuntu 24.04, GCC 13. What I ran: - Full relinker ctest on the merge: 266/266 pass. - Same merge with main's `LinuxElfPatcher.cpp`: `linux_load_bounds` and `unused_filter_indirect` fail, and all 15 rejected runs relink with rc 0. So the new test still guards the change on current main. - The 32S request from b10424a47b doesn't apply anymore. `ValidationPolicy.cpp:9-22` only accepts NONE, 64, PC32, GLOB_DAT, JUMP_SLOT, RELATIVE, 32, DTPMOD64, DTPOFF64 and TPOFF64, and `R_X86_64_32S` isn't in the tree. #10658 is merged, so COPY never reaches `Patch` either. `RelocationWidth` matches the psABI field size for every accepted type. - `RequireLoaded` can't wrap: `address - MappedAddress < MemorySize` is checked before the size subtraction. Two notes, neither blocking: 1. An entry in a PT_LOAD with `p_flags` 0 is now rejected, but the output maps that segment RWX (`_fixLoadFlags`, `ProgramHeaderLayoutBuilder.cpp:127`). The entry check reads the original flags instead. Repro: `fixture(linux=True)` from `test_jmprel_table_bounds.py` with the first PT_LOAD's `p_flags` set to 0. On main it relinks with rc 0 and `readelf -l` shows that segment as `RWE`. On this branch it fails with `e_entry 0x200 is not inside an executable PT_LOAD segment`. I don't know if flags-0 loads show up in real inputs. If they do, `header.Flags == 0 || (header.Flags & flags) == flags` in `RequireLoaded` would match what the output actually maps. 2. The `!= R_X86_64_NONE` skip at `LinuxElfPatcher.cpp:378` never sees a NONE entry. `extractRela` drops NONE at `RelinkerPipeline.cpp:351` before the tables are built, so `none-outside` already relinks on main. Same reasoning as leaving out the 32S case: the skip and that case could both go.
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!10402
No description provided.