fix(relinker): accept byte-congruent PT_LOAD overlaps by trimming the earlier segment #12591

Merged
tablefrogpillow merged 3 commits from upstream-pr/12591 into main 2026-10-30 06:41:26 +00:00
tablefrogpillow commented 2026-10-28 21:46:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tablefrogpillow on 2026-10-28T21:46:09Z.


What

Some homebrew eboots place a read-only PT_LOAD whose tail runs over the PT_GNU_RELRO start of the following read-write PT_LOAD, with byte-identical content across the overlap. The relinker refused every such title with Overlapping PT_LOAD memory ranges on both the Linux and Windows paths (#12183: PPSA99770; also PPSA99573, PPSA92013).

When an earlier PT_LOAD's tail overlaps a later one, the overlapping bytes are identical, and the overlap does not extend past the later segment, the earlier segment is trimmed to end where the later one starts, so the later segment owns the shared range. On Linux the preserved PT_GNU_RELRO is made read-only after relocation by ld.so, matching the earlier read-only mapping; the Windows image builder has no RELRO step, so the shared range stays writable (noted in docs/dev/debt/relinker.md, beside the existing uncovered-PT_SCE_RELRO note).

An overlap is still refused when the two segments share a start address, when it extends past the later segment, when the bytes differ (the #10564 case), and — because the Windows image ORs page permissions — when one segment is executable and the other writable and the later segment does not start on a page boundary, which would otherwise share a read-write-execute page. The rule is the same on both loaders, so their acceptance decision matches. The congruence check is bounded to the file-backed part of the overlap and compares spans rather than walking the implicit zero bytes of a large bss overlap.

Tested

Linux (Ubuntu 24.04) and Windows output, Photo Splat VR (PPSA99770, open-source homebrew): relinker --registry --skip-sce-module eboot.elf app.elf and --windows both succeed (previously FAIL: Overlapping PT_LOAD memory ranges (offset 0x4288000)). The output has no overlapping PT_LOADs and the earlier segment ends exactly at the later segment's start (0x4688000), with PT_GNU_RELRO preserved. The relinked title also runs (RADV, Mesa 25.2.8): guest runtime, threads and Vulkan device initialise, the first scene loads (288683 splats) and the VR eye buffers are created, and it stops at sceHmd2Initialize (no VR headset on the test box) — a hardware-absence gate, not a segment fault.

core/relinker/relinker/tests/test_executable_segments.py adds cases that run on every target ([], --lazy-binding, --linux-fixed-base, --windows, --macos): byte-congruent tail and bss overlaps are accepted with the earlier segment trimmed to the later start and no overlapping PT_LOADs in the output; a write-execute overlap at an unaligned boundary is refused while an aligned one is accepted with the Windows image carrying no write-execute section; and differing bytes, an overlap past the later end, a non-zero file byte over the later segment's bss, and a shared start address are refused. A no-overlap control keeps the old path.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #12183
  • Unblocks: PPSA99770 #12183 (also PPSA99573, PPSA92013)
  • GPUs tested: relinker change; the relinked PPSA99770 was run-checked on RADV (Mesa 25.2.8) to Vulkan init and scene load (stops at VR HMD init, no headset)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5058198971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12591). Originally posted by [tablefrogpillow](https://github.com/tablefrogpillow) on 2026-10-28T21:46:09Z. --- ### What Some homebrew eboots place a read-only `PT_LOAD` whose tail runs over the `PT_GNU_RELRO` start of the following read-write `PT_LOAD`, with byte-identical content across the overlap. The relinker refused every such title with `Overlapping PT_LOAD memory ranges` on both the Linux and Windows paths (#12183: PPSA99770; also PPSA99573, PPSA92013). When an earlier `PT_LOAD`'s tail overlaps a later one, the overlapping bytes are identical, and the overlap does not extend past the later segment, the earlier segment is trimmed to end where the later one starts, so the later segment owns the shared range. On Linux the preserved `PT_GNU_RELRO` is made read-only after relocation by `ld.so`, matching the earlier read-only mapping; the Windows image builder has no RELRO step, so the shared range stays writable (noted in `docs/dev/debt/relinker.md`, beside the existing uncovered-`PT_SCE_RELRO` note). An overlap is still refused when the two segments share a start address, when it extends past the later segment, when the bytes differ (the #10564 case), and — because the Windows image ORs page permissions — when one segment is executable and the other writable and the later segment does not start on a page boundary, which would otherwise share a read-write-execute page. The rule is the same on both loaders, so their acceptance decision matches. The congruence check is bounded to the file-backed part of the overlap and compares spans rather than walking the implicit zero bytes of a large bss overlap. ### Tested Linux (Ubuntu 24.04) and Windows output, Photo Splat VR (PPSA99770, open-source homebrew): `relinker --registry --skip-sce-module eboot.elf app.elf` and `--windows` both succeed (previously `FAIL: Overlapping PT_LOAD memory ranges (offset 0x4288000)`). The output has no overlapping `PT_LOAD`s and the earlier segment ends exactly at the later segment's start (`0x4688000`), with `PT_GNU_RELRO` preserved. The relinked title also runs (RADV, Mesa 25.2.8): guest runtime, threads and Vulkan device initialise, the first scene loads (288683 splats) and the VR eye buffers are created, and it stops at `sceHmd2Initialize` (no VR headset on the test box) — a hardware-absence gate, not a segment fault. `core/relinker/relinker/tests/test_executable_segments.py` adds cases that run on every target (`[]`, `--lazy-binding`, `--linux-fixed-base`, `--windows`, `--macos`): byte-congruent tail and bss overlaps are accepted with the earlier segment trimmed to the later start and no overlapping `PT_LOAD`s in the output; a write-execute overlap at an unaligned boundary is refused while an aligned one is accepted with the Windows image carrying no write-execute section; and differing bytes, an overlap past the later end, a non-zero file byte over the later segment's bss, and a shared start address are refused. A no-overlap control keeps the old path. ### 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](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 file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #12183 - [x] Unblocks: PPSA99770 #12183 (also PPSA99573, PPSA92013) - [x] GPUs tested: relinker change; the relinked PPSA99770 was run-checked on RADV (Mesa 25.2.8) to Vulkan init and scene load (stops at VR HMD init, no headset) - [x] AI-assisted: yes
rooklz commented 2026-10-28 22:09:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T22:09:28Z.

Review: COMMENTED


Thanks. I ran this on macOS arm64 (relinker-only build, --linux default / --windows / --macos) at efddf52138, which already includes current main 319621ba2e:

  • Relinker-only ctest passes 391/391, executable_segments + linux_gnu_segments are green, check_conventions --base upstream/main is clean, and it merges cleanly onto main. pr_overlap only reports the docs/dev/debt/relinker.md sharers it can't compare (#5021, #10861, #11192).
  • With main's sources and the new test, the run fails at the congruent-overlap case (Overlapping PT_LOAD memory ranges (offset 0x1800)), so the test does exercise the change.

1. The byte compare runs over zero-fill as well as file bytes. The loop goes to previousEnd one byte at a time, even past both FileSizes, where both sides are known to read 0. If an earlier segment's p_memsz overlaps a large .bss, that turns into billions of iterations. Same fixture each time, earlier segment p_filesz 0x602, both p_memsz raised:

overlap linux --windows --macos
1 GiB 0.54 s 0.51 s 0.59 s
64 GiB 0.00 s (rejected earlier) 33.9 s, then the 2 GiB range error 32.4 s
16 TiB rejected earlier >30 s timeout >30 s timeout

The same image without the overlap fails in 0.00 s. If the compare stops at the end of the file-backed bytes, every case is instant and nothing else changes (both suites green, 391/391):

-        for (auto address = current.MappedAddress; address < previousEnd; ++address) {
+        const auto backedEnd = std::min(previousEnd, std::max(previous.MappedAddress + previous.FileSize, current.MappedAddress + current.FileSize));
+        for (auto address = current.MappedAddress; address < backedEnd; ++address) {

(Same edit in WindowsLoadImage.cpp with segment/source.)

2. Mutants. I tried 12 single-edit mutants against the PR's test:

  • Killed: same-start check (both targets), Linux trim, Linux FileSize clamp, and "previous side reads zero" (both).
  • Survive: removing the extends-past check (both targets), removing the byte compare (both), removing the Windows trim, removing the Windows FileSize clamp.

The compare and extends-past survivors exist because the existing overlap-next / overlap-past-end cases are refused by the same-start/extends-past arms before the byte compare is reached, and nothing checks the Windows trim's effect. The test-only diff below kills all of them except the Windows clamp. That one looks equivalent to me: with an unclamped FileSize, copy_n writes the earlier segment's bytes into the later range, the bytes are congruent by construction, and the later segment copies over them anyway. It is green on head, and with main's sources it fails as before.

test diff (appended before the final print)
diff --git a/core/relinker/relinker/tests/test_executable_segments.py b/core/relinker/relinker/tests/test_executable_segments.py
index 7e6ea8af2c..d4e3442b9b 100644
--- a/core/relinker/relinker/tests/test_executable_segments.py
+++ b/core/relinker/relinker/tests/test_executable_segments.py
@@ -178,6 +178,40 @@ def main():
                                     capture_output=True, text=True, timeout=20)
             assert result.returncode == 2 and "Overlapping PT_LOAD memory ranges" in result.stderr \
                 and not output.exists(), (options, result.stdout, result.stderr)
+        for name, file_size, memory_size in (("congruent-past-end", 0x602, 0x700), ("differing-inside", 0x601, 0x602)):
+            image = fixture(b"\x90\xc3")
+            struct.pack_into("<Q", image, 176 + 32, file_size)
+            struct.pack_into("<Q", image, 176 + 40, memory_size)
+            source = work / (name + ".elf")
+            source.write_bytes(image)
+            for options in image_targets:
+                output = work / (name + ".out")
+                if output.exists():
+                    output.unlink()
+                result = subprocess.run([str(relinker), "--skip-sce-module", "--skip-syscall-check",
+                                         *options, str(source), str(output)],
+                                        capture_output=True, text=True, timeout=20)
+                assert result.returncode == 2 and "Overlapping PT_LOAD memory ranges" in result.stderr \
+                    and not output.exists(), (name, options, result.stdout, result.stderr)
+
+        writable = fixture(b"\x90\xc3")
+        struct.pack_into("<Q", writable, 176 + 32, 0x602)
+        struct.pack_into("<Q", writable, 176 + 40, 0x1e00)
+        struct.pack_into("<I", writable, 232 + 4, 6)
+        struct.pack_into("<Q", writable, 232 + 40, 0x4000)
+        source = work / "writable-overlap.elf"
+        source.write_bytes(writable)
+        output = work / "writable-overlap.exe"
+        result = subprocess.run([str(relinker), "--skip-sce-module", "--skip-syscall-check", "--windows",
+                                 str(source), str(output)], capture_output=True, text=True, timeout=20)
+        assert result.returncode == 0 and output.exists(), (result.stdout, result.stderr)
+        image = output.read_bytes()
+        header = struct.unpack_from("<I", image, 0x3c)[0]
+        count = struct.unpack_from("<H", image, header + 6)[0]
+        table = header + 24 + struct.unpack_from("<H", image, header + 20)[0]
+        sections = [struct.unpack_from("<IIIIIIHHI", image, table + index * 40 + 8) for index in range(count)]
+        executable = [(section[1], section[0]) for section in sections if section[8] & 0xa0000000 == 0xa0000000]
+        assert all(address + size <= 0x12000 for address, size in executable), executable
     print("Executable segment tests passed")
 
 

The last case is a congruent overlap into a writable segment. With the Windows trim removed, an executable+writable section runs past 0x12000 into the writable range, and the assertion fails. With the trim, it stops at 0x12000.

3. Nit on the protection wording. The body says the overlap "leaves no page executable-and-writable". That is true for PPSA99770 because its shared start (0x4688000) is page-aligned. If the later segment starts mid-page, pageFlags |= still ORs the boundary page. The fixture's .elf1@0x11000 page already comes out XW because its segments 0x1200/0x1800 share a page. Main does the same for sub-page adjacency without any overlap, so this isn't a regression; maybe say "page-aligned" in the body or the debt line.

Tested: macOS 26 arm64, AppleClang, relinker-only Release, Python 3.13.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690353541 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12591#pullrequestreview-5690353541). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T22:09:28Z. **Review:** COMMENTED --- Thanks. I ran this on macOS arm64 (relinker-only build, `--linux` default / `--windows` / `--macos`) at efddf52138, which already includes current main 319621ba2e: - Relinker-only ctest passes 391/391, `executable_segments` + `linux_gnu_segments` are green, `check_conventions --base upstream/main` is clean, and it merges cleanly onto main. `pr_overlap` only reports the `docs/dev/debt/relinker.md` sharers it can't compare (#5021, #10861, #11192). - With main's sources and the new test, the run fails at the congruent-overlap case (`Overlapping PT_LOAD memory ranges (offset 0x1800)`), so the test does exercise the change. **1. The byte compare runs over zero-fill as well as file bytes.** The loop goes to `previousEnd` one byte at a time, even past both `FileSize`s, where both sides are known to read 0. If an earlier segment's `p_memsz` overlaps a large `.bss`, that turns into billions of iterations. Same fixture each time, earlier segment `p_filesz 0x602`, both `p_memsz` raised: | overlap | linux | --windows | --macos | |---|---|---|---| | 1 GiB | 0.54 s | 0.51 s | 0.59 s | | 64 GiB | 0.00 s (rejected earlier) | **33.9 s**, then the 2 GiB range error | **32.4 s** | | 16 TiB | rejected earlier | >30 s timeout | >30 s timeout | The same image without the overlap fails in 0.00 s. If the compare stops at the end of the file-backed bytes, every case is instant and nothing else changes (both suites green, 391/391): ```diff - for (auto address = current.MappedAddress; address < previousEnd; ++address) { + const auto backedEnd = std::min(previousEnd, std::max(previous.MappedAddress + previous.FileSize, current.MappedAddress + current.FileSize)); + for (auto address = current.MappedAddress; address < backedEnd; ++address) { ``` (Same edit in `WindowsLoadImage.cpp` with `segment`/`source`.) **2. Mutants.** I tried 12 single-edit mutants against the PR's test: - Killed: same-start check (both targets), Linux trim, Linux `FileSize` clamp, and "previous side reads zero" (both). - **Survive:** removing the extends-past check (both targets), removing the byte compare (both), removing the Windows trim, removing the Windows `FileSize` clamp. The compare and extends-past survivors exist because the existing `overlap-next` / `overlap-past-end` cases are refused by the same-start/extends-past arms before the byte compare is reached, and nothing checks the Windows trim's effect. The test-only diff below kills all of them except the Windows clamp. That one looks equivalent to me: with an unclamped `FileSize`, `copy_n` writes the earlier segment's bytes into the later range, the bytes are congruent by construction, and the later segment copies over them anyway. It is green on head, and with main's sources it fails as before. <details><summary>test diff (appended before the final print)</summary> ```diff diff --git a/core/relinker/relinker/tests/test_executable_segments.py b/core/relinker/relinker/tests/test_executable_segments.py index 7e6ea8af2c..d4e3442b9b 100644 --- a/core/relinker/relinker/tests/test_executable_segments.py +++ b/core/relinker/relinker/tests/test_executable_segments.py @@ -178,6 +178,40 @@ def main(): capture_output=True, text=True, timeout=20) assert result.returncode == 2 and "Overlapping PT_LOAD memory ranges" in result.stderr \ and not output.exists(), (options, result.stdout, result.stderr) + for name, file_size, memory_size in (("congruent-past-end", 0x602, 0x700), ("differing-inside", 0x601, 0x602)): + image = fixture(b"\x90\xc3") + struct.pack_into("<Q", image, 176 + 32, file_size) + struct.pack_into("<Q", image, 176 + 40, memory_size) + source = work / (name + ".elf") + source.write_bytes(image) + for options in image_targets: + output = work / (name + ".out") + if output.exists(): + output.unlink() + result = subprocess.run([str(relinker), "--skip-sce-module", "--skip-syscall-check", + *options, str(source), str(output)], + capture_output=True, text=True, timeout=20) + assert result.returncode == 2 and "Overlapping PT_LOAD memory ranges" in result.stderr \ + and not output.exists(), (name, options, result.stdout, result.stderr) + + writable = fixture(b"\x90\xc3") + struct.pack_into("<Q", writable, 176 + 32, 0x602) + struct.pack_into("<Q", writable, 176 + 40, 0x1e00) + struct.pack_into("<I", writable, 232 + 4, 6) + struct.pack_into("<Q", writable, 232 + 40, 0x4000) + source = work / "writable-overlap.elf" + source.write_bytes(writable) + output = work / "writable-overlap.exe" + result = subprocess.run([str(relinker), "--skip-sce-module", "--skip-syscall-check", "--windows", + str(source), str(output)], capture_output=True, text=True, timeout=20) + assert result.returncode == 0 and output.exists(), (result.stdout, result.stderr) + image = output.read_bytes() + header = struct.unpack_from("<I", image, 0x3c)[0] + count = struct.unpack_from("<H", image, header + 6)[0] + table = header + 24 + struct.unpack_from("<H", image, header + 20)[0] + sections = [struct.unpack_from("<IIIIIIHHI", image, table + index * 40 + 8) for index in range(count)] + executable = [(section[1], section[0]) for section in sections if section[8] & 0xa0000000 == 0xa0000000] + assert all(address + size <= 0x12000 for address, size in executable), executable print("Executable segment tests passed") ``` </details> The last case is a congruent overlap into a writable segment. With the Windows trim removed, an executable+writable section runs past `0x12000` into the writable range, and the assertion fails. With the trim, it stops at `0x12000`. **3. Nit on the protection wording.** The body says the overlap "leaves no page executable-and-writable". That is true for PPSA99770 because its shared start (`0x4688000`) is page-aligned. If the later segment starts mid-page, `pageFlags |=` still ORs the boundary page. The fixture's `.elf1@0x11000` page already comes out `XW` because its segments `0x1200`/`0x1800` share a page. Main does the same for sub-page adjacency without any overlap, so this isn't a regression; maybe say "page-aligned" in the body or the debt line. Tested: macOS 26 arm64, AppleClang, relinker-only Release, Python 3.13.
adriadam10 commented 2026-10-28 22:22:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T22:22:23Z.

Review: CHANGES_REQUESTED


The approach (trim the earlier segment to the later one's start when the overlap is byte-identical and ends inside the later segment) reads correctly, and the same-start and extends-past refusals hold. What is still open, all from the review above:

  • W+X page on Windows. WindowsLoadImage.cpp ORs the page flags per page, so an R+X segment congruent with a following R+W one that starts mid-page yields an RWX page where main refused the image, and Linux gives that page the later segment's protection instead. Accept only layouts that cannot create a W+X page (refuse when one side has X and the other W unless the boundary is page-aligned), with output-permission tests for an aligned and an unaligned boundary, as @oneandonlydean asked.
  • Byte compare over bss. Both loops (LinuxElfPatcher.cpp:146, WindowsLoadImage.cpp:39) walk to previousEnd one byte at a time past both FileSizes, where both sides are zero. @rooklz measured 33.9 s for a 64 GiB overlap on --windows/--macos. Stop at the end of the file-backed bytes (their diff: backedEnd = min(previousEnd, max(previous.MappedAddress + previous.FileSize, current.MappedAddress + current.FileSize))), or compare in chunks.
  • Tests. The existing overlap-next/overlap-past-end cases are refused by the same-start/extends-past arms, so removing the extends-past check or the byte compare, or the Windows trim, still passes. Add the cases from the two reviews: differing bytes inside the overlap, an earlier segment past the later one's end, bss zero-against-zero accepted and non-zero file bytes against the later bss refused, exact trimmed sizes, a no-overlap control, and the aligned/unaligned permission cases. @rooklz's test diff covers several of these.
  • Debt entry (docs/dev/debt/relinker.md:31): keep only the PPSA ids, drop the title names.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5690465990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12591#pullrequestreview-5690465990). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-28T22:22:23Z. **Review:** CHANGES_REQUESTED --- The approach (trim the earlier segment to the later one's start when the overlap is byte-identical and ends inside the later segment) reads correctly, and the same-start and extends-past refusals hold. What is still open, all from the review above: - **W+X page on Windows.** `WindowsLoadImage.cpp` ORs the page flags per page, so an R+X segment congruent with a following R+W one that starts mid-page yields an RWX page where `main` refused the image, and Linux gives that page the later segment's protection instead. Accept only layouts that cannot create a W+X page (refuse when one side has X and the other W unless the boundary is page-aligned), with output-permission tests for an aligned and an unaligned boundary, as @oneandonlydean asked. - **Byte compare over bss.** Both loops (`LinuxElfPatcher.cpp:146`, `WindowsLoadImage.cpp:39`) walk to `previousEnd` one byte at a time past both `FileSize`s, where both sides are zero. @rooklz measured 33.9 s for a 64 GiB overlap on `--windows`/`--macos`. Stop at the end of the file-backed bytes (their diff: `backedEnd = min(previousEnd, max(previous.MappedAddress + previous.FileSize, current.MappedAddress + current.FileSize))`), or compare in chunks. - **Tests.** The existing `overlap-next`/`overlap-past-end` cases are refused by the same-start/extends-past arms, so removing the extends-past check or the byte compare, or the Windows trim, still passes. Add the cases from the two reviews: differing bytes inside the overlap, an earlier segment past the later one's end, bss zero-against-zero accepted and non-zero file bytes against the later bss refused, exact trimmed sizes, a no-overlap control, and the aligned/unaligned permission cases. @rooklz's test diff covers several of these. - **Debt entry** (`docs/dev/debt/relinker.md:31`): keep only the PPSA ids, drop the title names.
XairRrR commented 2026-10-29 15:16:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T15:16:50Z.

Review: APPROVED


Reviewed — approved

The overlap acceptance merges cleanly, builds, passes the new and existing relinker tests, and the new test fails on main's sources; the points from the earlier reviews are addressed. The two notes below don't block the merge: a refusal arm no test covers, and a debt entry that is narrower than what the code accepts.

What I ran

Head 324cbd20ebf47fbbff692ef27ae24beb6c638deb, merged with --no-ff onto main bb04a4f1ea0175e9e4f149f0359a764d62ff3296 in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF. Relinker-only change, so no GPU tests.

git fetch -q origin main pull/12591/head:refs/review/12591
git checkout -q --detach origin/main
git merge -q --no-ff --no-edit refs/review/12591
git diff --check origin/main...refs/review/12591
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target relinker
ctest --test-dir build -j4 --output-on-failure -R '^(executable_segments|linux_load_pages|linux_gnu_segments|dynamic_segment|overlapping_relocations|windows_fixed_base|windows_bss|windows_sparse_load|windows_address_space|windows_tls_zero_fill|macos_tls_overlap)$'
ctest --test-dir build/core/relinker -j4 --output-on-failure
# negative control
git checkout origin/main -- core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp
cmake --build build -j10 --target relinker
ctest --test-dir build -R '^executable_segments$' --output-on-failure
# 14 single-edit mutants of the two production files, one at a time, each rebuilt and run against executable_segments
# probe fixtures built from the test's fixture(): previous-segment bss over non-zero later file bytes, a 64 GiB bss overlap, an address wrap, and the aligned R+X/RW layout relinked for Linux

Results

Check Result
Merge onto main clean (docs/dev/debt/relinker.md auto-merged)
git diff --check clean
check_conventions.py --base origin/main All conventions checks passed.
Build relinker OK
11 focused segment/loader tests 11/11 passed, executable_segments 0.13 s
All tests under build/core/relinker 210/210 passed, 5.3 s
Negative control executable_segments fails on main's sources
Mutants 11 of 14 killed; 3 survive (see Findings)
64 GiB zero/zero bss overlap, --windows/--macos refused at once by the 2 GiB range check, after the overlap check (rooklz measured 33.9 s on the first head)
Address-wrapping earlier segment still refused on the default Linux, --lazy-binding, --windows and --macos targets (Load segment virtual range overflowed / PT_LOAD address overflow)
Mutant results
win-trim: Failed
win-filesize-clamp: Passed
win-wx: Failed
lin-wx: Failed
win-wx-aligned: Failed
lin-wx-aligned: Failed
win-equal: Failed
lin-equal: Failed
win-prevzero: Failed
lin-prevzero: Failed
win-curzero: Passed
lin-curzero: Passed
win-pastend: Failed
lin-pastend: Failed

Failed means the test caught the mutant. *-curzero replaces the last std::all_of (later segment's file bytes under the earlier segment's bss) with true. win-filesize-clamp removes the Windows FileSize clamp. rooklz already argued that one is equivalent, because the later segment's copy overwrites the congruent bytes.

Negative control output
1/1 Test #2797: executable_segments ..............***Failed    0.08 sec
AssertionError: ('congruent-tail-overlap', [], ... 'FAIL: Overlapping PT_LOAD memory ranges (offset 0x1800)\n...')
Aligned R+X over RW layout, Linux output (from the `overlap-write-execute-aligned` fixture)
input  phdr2 R+X 0x1200 memsz 0xe02 (ends 0x2002); phdr3 RW 0x2000 memsz 2
PT_LOAD flags=5 vaddr=0x401200 memsz=0xe00 end=0x402000
PT_LOAD flags=6 vaddr=0x402000 memsz=0x2 end=0x402002

Negative control

I restored LinuxElfPatcher.cpp and WindowsLoadImage.cpp from main, kept the new test and rebuilt. executable_segments fails at the first new case, congruent-tail-overlap, with Overlapping PT_LOAD memory ranges (offset 0x1800). With the PR files restored it passes.

Findings

Blocking

None.

Non-blocking

  1. One refusal arm has no test on either loader. Code: LinuxElfPatcher.cpp:167 and WindowsLoadImage.cpp:59. This is the case where the earlier segment's bss lies over file bytes of the later segment, and those bytes must be zero. If the check is replaced with true, executable_segments still passes (lin-curzero and win-curzero above). The code handles the case correctly: overlap_image(0x20, 0x602) (earlier bss over the later segment's 90 c3) is refused on all five targets. But overlap-nonzero-over-bss covers only the mirror case (earlier file bytes over the later bss). Suggested fix: add ("overlap-bss-over-nonzero-file", overlap_image(0x20, 0x602)) to the refused list.

  2. The debt entry is narrower than what the code accepts. The entry describes "a read-only PT_LOAD" over a PT_GNU_RELRO start. The code accepts any congruent overlap except an unaligned write-execute one, and the overlapped range then gets the later segment's permissions on both loaders. Two consequences are not in the entry:

    • An executable earlier segment loses execute permission on the overlapped bytes. In the overlap-write-execute-aligned fixture, 0x2000..0x2002 was in the R+X segment and comes out RW only (Linux output above).
    • On Linux, overlapped bytes outside PT_GNU_RELRO stay writable, as they do on Windows.

    Also, CONTRIBUTING asks that an entry state the gap first. This one starts with the implemented behaviour, and it does not say that the console's treatment of overlapping PT_LOADs (which segment's protection wins) is unchecked. Suggested wording: lead with "The protection the console gives a range mapped by two byte-identical overlapping PT_LOADs is not checked; the relinker gives it the later segment's protection (an earlier executable segment loses execute, an earlier read-only one becomes writable outside PT_GNU_RELRO, and on Windows inside it as well)". Then list the refused cases.

Not verified

  • No PS5 title was relinked or run here. The title results in the description, and in Noxy-HNR's Windows run, are the author's and that commenter's.
  • What the console loader does with overlapping PT_LOADs, which would settle whether the later segment's protection is the right one.
  • Windows and macOS hosts. Their output formats were exercised only through the relinker tests on Linux.
  • The full ctest suite, which is out of scope for a relinker-only change.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5700185695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12591#pullrequestreview-5700185695). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T15:16:50Z. **Review:** APPROVED --- # Reviewed — approved The overlap acceptance merges cleanly, builds, passes the new and existing relinker tests, and the new test fails on `main`'s sources; the points from the earlier reviews are addressed. The two notes below don't block the merge: a refusal arm no test covers, and a debt entry that is narrower than what the code accepts. ## What I ran Head `324cbd20ebf47fbbff692ef27ae24beb6c638deb`, merged with `--no-ff` onto `main` `bb04a4f1ea0175e9e4f149f0359a764d62ff3296` in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, Release, Ninja, Intel Core i9-14900KF. Relinker-only change, so no GPU tests. ```sh git fetch -q origin main pull/12591/head:refs/review/12591 git checkout -q --detach origin/main git merge -q --no-ff --no-edit refs/review/12591 git diff --check origin/main...refs/review/12591 python3 tools/check_conventions.py --base origin/main cmake --build build -j10 --target relinker ctest --test-dir build -j4 --output-on-failure -R '^(executable_segments|linux_load_pages|linux_gnu_segments|dynamic_segment|overlapping_relocations|windows_fixed_base|windows_bss|windows_sparse_load|windows_address_space|windows_tls_zero_fill|macos_tls_overlap)$' ctest --test-dir build/core/relinker -j4 --output-on-failure # negative control git checkout origin/main -- core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp cmake --build build -j10 --target relinker ctest --test-dir build -R '^executable_segments$' --output-on-failure # 14 single-edit mutants of the two production files, one at a time, each rebuilt and run against executable_segments # probe fixtures built from the test's fixture(): previous-segment bss over non-zero later file bytes, a 64 GiB bss overlap, an address wrap, and the aligned R+X/RW layout relinked for Linux ``` ## Results | Check | Result | |---|---| | Merge onto `main` | clean (`docs/dev/debt/relinker.md` auto-merged) | | `git diff --check` | clean | | `check_conventions.py --base origin/main` | `All conventions checks passed.` | | Build `relinker` | OK | | 11 focused segment/loader tests | 11/11 passed, `executable_segments` 0.13 s | | All tests under `build/core/relinker` | 210/210 passed, 5.3 s | | Negative control | `executable_segments` fails on `main`'s sources | | Mutants | 11 of 14 killed; 3 survive (see Findings) | | 64 GiB zero/zero bss overlap, `--windows`/`--macos` | refused at once by the 2 GiB range check, after the overlap check (rooklz measured 33.9 s on the first head) | | Address-wrapping earlier segment | still refused on the default Linux, `--lazy-binding`, `--windows` and `--macos` targets (`Load segment virtual range overflowed` / `PT_LOAD address overflow`) | <details><summary>Mutant results</summary> ``` win-trim: Failed win-filesize-clamp: Passed win-wx: Failed lin-wx: Failed win-wx-aligned: Failed lin-wx-aligned: Failed win-equal: Failed lin-equal: Failed win-prevzero: Failed lin-prevzero: Failed win-curzero: Passed lin-curzero: Passed win-pastend: Failed lin-pastend: Failed ``` `Failed` means the test caught the mutant. `*-curzero` replaces the last `std::all_of` (later segment's file bytes under the earlier segment's bss) with `true`. `win-filesize-clamp` removes the Windows `FileSize` clamp. rooklz already argued that one is equivalent, because the later segment's copy overwrites the congruent bytes. </details> <details><summary>Negative control output</summary> ``` 1/1 Test #2797: executable_segments ..............***Failed 0.08 sec AssertionError: ('congruent-tail-overlap', [], ... 'FAIL: Overlapping PT_LOAD memory ranges (offset 0x1800)\n...') ``` </details> <details><summary>Aligned R+X over RW layout, Linux output (from the `overlap-write-execute-aligned` fixture)</summary> ``` input phdr2 R+X 0x1200 memsz 0xe02 (ends 0x2002); phdr3 RW 0x2000 memsz 2 PT_LOAD flags=5 vaddr=0x401200 memsz=0xe00 end=0x402000 PT_LOAD flags=6 vaddr=0x402000 memsz=0x2 end=0x402002 ``` </details> ## Negative control I restored `LinuxElfPatcher.cpp` and `WindowsLoadImage.cpp` from `main`, kept the new test and rebuilt. `executable_segments` fails at the first new case, `congruent-tail-overlap`, with `Overlapping PT_LOAD memory ranges (offset 0x1800)`. With the PR files restored it passes. ## Findings ### Blocking None. ### Non-blocking 1. **One refusal arm has no test on either loader.** Code: [`LinuxElfPatcher.cpp:167`](https://github.com/boykopovar/AnyPS5/blob/324cbd20ebf47fbbff692ef27ae24beb6c638deb/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L163-L167) and [`WindowsLoadImage.cpp:59`](https://github.com/boykopovar/AnyPS5/blob/324cbd20ebf47fbbff692ef27ae24beb6c638deb/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp#L55-L59). This is the case where the earlier segment's bss lies over file bytes of the later segment, and those bytes must be zero. If the check is replaced with `true`, `executable_segments` still passes (`lin-curzero` and `win-curzero` above). The code handles the case correctly: `overlap_image(0x20, 0x602)` (earlier bss over the later segment's `90 c3`) is refused on all five targets. But `overlap-nonzero-over-bss` [covers only the mirror case](https://github.com/boykopovar/AnyPS5/blob/324cbd20ebf47fbbff692ef27ae24beb6c638deb/core/relinker/relinker/tests/test_executable_segments.py#L179-L184) (earlier file bytes over the later bss). Suggested fix: add `("overlap-bss-over-nonzero-file", overlap_image(0x20, 0x602))` to the refused list. 2. **The debt entry is narrower than what the code accepts.** The [entry](https://github.com/boykopovar/AnyPS5/blob/324cbd20ebf47fbbff692ef27ae24beb6c638deb/docs/dev/debt/relinker.md#L31) describes "a read-only `PT_LOAD`" over a `PT_GNU_RELRO` start. The [code](https://github.com/boykopovar/AnyPS5/blob/324cbd20ebf47fbbff692ef27ae24beb6c638deb/core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp#L146-L147) accepts any congruent overlap except an unaligned write-execute one, and the overlapped range then gets the later segment's permissions on both loaders. Two consequences are not in the entry: - An executable earlier segment loses execute permission on the overlapped bytes. In the `overlap-write-execute-aligned` fixture, `0x2000..0x2002` was in the R+X segment and comes out RW only (Linux output above). - On Linux, overlapped bytes outside `PT_GNU_RELRO` stay writable, as they do on Windows. Also, CONTRIBUTING asks that an entry state the gap first. This one starts with the implemented behaviour, and it does not say that the console's treatment of overlapping `PT_LOAD`s (which segment's protection wins) is unchecked. Suggested wording: lead with "The protection the console gives a range mapped by two byte-identical overlapping `PT_LOAD`s is not checked; the relinker gives it the later segment's protection (an earlier executable segment loses execute, an earlier read-only one becomes writable outside `PT_GNU_RELRO`, and on Windows inside it as well)". Then list the refused cases. ## Not verified - No PS5 title was relinked or run here. The title results in the description, and in Noxy-HNR's Windows run, are the author's and that commenter's. - What the console loader does with overlapping `PT_LOAD`s, which would settle whether the later segment's protection is the right one. - Windows and macOS hosts. Their output formats were exercised only through the relinker tests on Linux. - The full ctest suite, which is out of scope for a relinker-only change.
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!12591
No description provided.