fix(relinker): reject a Linux entry stub whose call cannot reach the entry #2097

Merged
treuenten merged 1 commit from upstream-pr/2097 into main 2026-10-10 06:49:35 +00:00
treuenten commented 2026-10-09 08:27:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by treuenten on 2026-10-09T08:27:38Z.


What

EntryStubBuilder::BuildEntryStub encodes the Linux entry stub's call with static_cast<std::int32_t>(realEntryVaddr - callNextVaddr) and no range check. The stub sits just above the highest PT_LOAD end, so when an input has a PT_LOAD at or above 2 GiB the displacement silently truncates. With an extra bss PT_LOAD at 0x80000000 and e_entry = 0x10, main exits 0 and writes a binary whose entry stub calls 0x100000010, which is unmapped, so it crashes at startup.

The builder now computes the displacement as unsigned, converts it to int64_t, and throws RelinkerException("Entry stub call exceeds rel32 range") when it does not fit in rel32. Doing the subtraction unsigned first keeps layouts that wrap past 2^64 working, because RIP-relative math is modular. The other rel32 encodings in the Linux path (LinuxElfPatcher::_appendTrampoline and the guest initializer call in LinuxGuestModuleWriter) already check their range.

test_linux_entry_argv.py (ctest linux_entry_argv) relinks the argv fixture with a sixth PT_LOAD:

  • at 0x7FF00000 it must exit 0, and the decoded stub call must land on the entry;
  • at 0x80000000 it must exit 2 with the new message and write no output.

The test finds the stub's call by its opcode instead of reading it at a fixed offset, so it works with both the current stub and #2339's. The far-load case adds one program header slot to the shared fixture() instead of setting a fixed count, so it also holds when the synthetic header count changes.

EntryStubBuilder.cpp now includes <domain/Types.hpp> for RelinkerException, as the other elfpatcher sources do. The entry_stub_tests target from #2339 compiles that file, so core/relinker/CMakeLists.txt adds ${CMAKE_CURRENT_SOURCE_DIR}/domain/include to it. Without that line, the target fails with 'domain/Types.hpp' file not found.

#2339 (now on main) put a frame record before the stub's call, which moved it from entry + 10 to entry + 17. The test finds the call wherever it is.

One open pull request interacts with this one:

  • #2274 raises the synthetic program header count and widens fixture() in test_linux_load_alignment.py. The far-load case sizes its header table from fixture(), so it passes with or without #2274.

No other open pull request touches these files.

Tested

macOS arm64 (Apple M3), AppleClang, relinker-only Release build on main b667d1e3, which includes #2339:

  • ctest 52/52, including entry_stub and linux_entry_argv with the call at entry + 17.
  • Without the CMakeLists.txt line, entry_stub_tests fails to compile ('domain/Types.hpp' file not found).
  • The new case fails on main (exit 0, stub calls 0x100000010) and passes with the fix.
  • Merged with #2274 (b25c767b) before this rebase: ctest 44/44. The previous far-load fixture fails there (Not enough program header slots: need 7, available 6). #2274 now conflicts with main in test_linux_eh_frame_header.py, a file this PR doesn't touch. With it merged on top of this branch, entry_stub and linux_entry_argv still pass.
  • With main's EntryStubBuilder.cpp, linux_entry_argv fails: the 0x80000000 case exits 0.
  • python3 tools/check_conventions.py --base origin/main passes.

Full CI on my fork, using this repository's build.yml unchanged:

  • Run 38027307997, this head merged into main b667d1e3. Every job passes: the Linux full build (ctest 598/598, including the lavapipe Vulkan tests), the Windows full build (MinGW, 606/606), and the relinker-only build on Linux (59/59), Windows (55/55) and macOS (52/52). entry_stub and linux_entry_argv pass in all five.
  • Run 38011567988, this PR merged into main b993145d. The relinker-only build passes on Linux (52/52), Windows (47/47) and macOS (45/45), including linux_entry_argv, which on Linux also runs the relinked binary. The full Linux and Windows jobs stop at check_test_registration.py (agc_driver_recorder_tests is run by ctest now; remove it from KNOWN_UNRUN), exactly as they do on main b993145d alone (run 38011548052).
  • Run 38009295433, this PR merged into main 1f0c9fef. The relinker-only build passes (50/50, 46/46, 44/44). Linux full build: ctest 567/568, and linux_entry_argv passes. Windows full build: 557/559. guest_video_out_resolution_status fails in both, as it does on main 1f0c9fef in this repository's own CI; #2514 has since fixed it. Windows also failed guest_video_out_wqhd_detection_no_vulkan with a heap corruption. That test links only libSceVideoOut, libSceAgcDriver, libkernel and libc, so none of the relinker.

Negesin ran the relinker-only build on Windows with the previous head c04fb011 (36/36).

Not run: sanitizer build, any title.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4798000859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097). Originally posted by [treuenten](https://github.com/treuenten) on 2026-10-09T08:27:38Z. --- ### What `EntryStubBuilder::BuildEntryStub` encodes the Linux entry stub's `call` with `static_cast<std::int32_t>(realEntryVaddr - callNextVaddr)` and no range check. The stub sits just above the highest PT_LOAD end, so when an input has a PT_LOAD at or above 2 GiB the displacement silently truncates. With an extra bss PT_LOAD at `0x80000000` and `e_entry = 0x10`, main exits 0 and writes a binary whose entry stub calls `0x100000010`, which is unmapped, so it crashes at startup. The builder now computes the displacement as unsigned, converts it to `int64_t`, and throws `RelinkerException("Entry stub call exceeds rel32 range")` when it does not fit in rel32. Doing the subtraction unsigned first keeps layouts that wrap past 2^64 working, because RIP-relative math is modular. The other rel32 encodings in the Linux path (`LinuxElfPatcher::_appendTrampoline` and the guest initializer call in `LinuxGuestModuleWriter`) already check their range. `test_linux_entry_argv.py` (ctest `linux_entry_argv`) relinks the argv fixture with a sixth PT_LOAD: - at `0x7FF00000` it must exit 0, and the decoded stub `call` must land on the entry; - at `0x80000000` it must exit 2 with the new message and write no output. The test finds the stub's `call` by its opcode instead of reading it at a fixed offset, so it works with both the current stub and #2339's. The far-load case adds one program header slot to the shared `fixture()` instead of setting a fixed count, so it also holds when the synthetic header count changes. `EntryStubBuilder.cpp` now includes `<domain/Types.hpp>` for `RelinkerException`, as the other elfpatcher sources do. The `entry_stub_tests` target from #2339 compiles that file, so `core/relinker/CMakeLists.txt` adds `${CMAKE_CURRENT_SOURCE_DIR}/domain/include` to it. Without that line, the target fails with `'domain/Types.hpp' file not found`. #2339 (now on `main`) put a frame record before the stub's `call`, which moved it from entry + 10 to entry + 17. The test finds the `call` wherever it is. One open pull request interacts with this one: - #2274 raises the synthetic program header count and widens `fixture()` in `test_linux_load_alignment.py`. The far-load case sizes its header table from `fixture()`, so it passes with or without #2274. No other open pull request touches these files. ### Tested macOS arm64 (Apple M3), AppleClang, relinker-only Release build on `main` b667d1e3, which includes #2339: - `ctest` 52/52, including `entry_stub` and `linux_entry_argv` with the `call` at entry + 17. - Without the `CMakeLists.txt` line, `entry_stub_tests` fails to compile (`'domain/Types.hpp' file not found`). - The new case fails on main (exit 0, stub calls `0x100000010`) and passes with the fix. - Merged with #2274 (b25c767b) before this rebase: `ctest` 44/44. The previous far-load fixture fails there (`Not enough program header slots: need 7, available 6`). #2274 now conflicts with `main` in `test_linux_eh_frame_header.py`, a file this PR doesn't touch. With it merged on top of this branch, `entry_stub` and `linux_entry_argv` still pass. - With main's `EntryStubBuilder.cpp`, `linux_entry_argv` fails: the `0x80000000` case exits 0. - `python3 tools/check_conventions.py --base origin/main` passes. Full CI on my fork, using this repository's `build.yml` unchanged: - [Run 38027307997](https://github.com/treuenten/AnyPS5/actions/runs/38027307997), this head merged into `main` b667d1e3. Every job passes: the Linux full build (ctest 598/598, including the lavapipe Vulkan tests), the Windows full build (MinGW, 606/606), and the relinker-only build on Linux (59/59), Windows (55/55) and macOS (52/52). `entry_stub` and `linux_entry_argv` pass in all five. - [Run 38011567988](https://github.com/treuenten/AnyPS5/actions/runs/38011567988), this PR merged into `main` b993145d. The relinker-only build passes on Linux (52/52), Windows (47/47) and macOS (45/45), including `linux_entry_argv`, which on Linux also runs the relinked binary. The full Linux and Windows jobs stop at `check_test_registration.py` (`agc_driver_recorder_tests is run by ctest now; remove it from KNOWN_UNRUN`), exactly as they do on `main` b993145d alone ([run 38011548052](https://github.com/treuenten/AnyPS5/actions/runs/38011548052)). - [Run 38009295433](https://github.com/treuenten/AnyPS5/actions/runs/38009295433), this PR merged into `main` 1f0c9fef. The relinker-only build passes (50/50, 46/46, 44/44). Linux full build: ctest 567/568, and `linux_entry_argv` passes. Windows full build: 557/559. `guest_video_out_resolution_status` fails in both, as it does on `main` 1f0c9fef in this repository's own CI; #2514 has since fixed it. Windows also failed `guest_video_out_wqhd_detection_no_vulkan` with a heap corruption. That test links only libSceVideoOut, libSceAgcDriver, libkernel and libc, so none of the relinker. Negesin ran the relinker-only build on Windows with the previous head c04fb011 (36/36). Not run: sanitizer build, any title. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Negesin commented 2026-10-09 08:58:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Negesin on 2026-10-09T08:58:48Z.


Tested the current PR head on Windows 11 with WinLibs MinGW-w64 GCC 15.2.0.

Commit tested: c04fb011

  • Release build with ANYPS5_RELINKER_ONLY=ON and BUILD_TESTING=ON
  • Build completed successfully
  • ctest -R "^linux_entry_argv$" --output-on-failure: 1/1 passed
  • Full relinker-only CTest suite: 36/36 passed in 23.62 s

I did not test a full AnyPS5 build or any title/game execution.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6077765936 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6077765936). Originally posted by [Negesin](https://github.com/Negesin) on 2026-10-09T08:58:48Z. --- Tested the current PR head on Windows 11 with WinLibs MinGW-w64 GCC 15.2.0. Commit tested: `c04fb011` - Release build with `ANYPS5_RELINKER_ONLY=ON` and `BUILD_TESTING=ON` - Build completed successfully - `ctest -R "^linux_entry_argv$" --output-on-failure`: 1/1 passed - Full relinker-only CTest suite: 36/36 passed in 23.62 s I did not test a full AnyPS5 build or any title/game execution.
johnmaia commented 2026-10-09 15:51:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by johnmaia on 2026-10-09T15:51:42Z.


Heads-up: #2339 changes the same Linux entry stub (it pushes a zero frame record and sets rbp before the call), so the call moves from entry + 10 to entry + 17. entry_call_target in this PR's test reads it at + 10; whichever of the two lands second needs that offset updated. The range check here and that change don't otherwise interact.

Written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084357638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6084357638). Originally posted by [johnmaia](https://github.com/johnmaia) on 2026-10-09T15:51:42Z. --- Heads-up: #2339 changes the same Linux entry stub (it pushes a zero frame record and sets `rbp` before the call), so the `call` moves from `entry + 10` to `entry + 17`. `entry_call_target` in this PR's test reads it at `+ 10`; whichever of the two lands second needs that offset updated. The range check here and that change don't otherwise interact. *Written by Claude (AI assistant) on a contributor's machine.*
treuenten commented 2026-10-10 01:34:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by treuenten on 2026-10-10T01:34:08Z.


@johnmaia thanks for the heads-up, you're right: the test read the call at a fixed entry + 10. entry_call_target now finds the call by its opcode instead, so the test works with both the current stub and yours. With this PR merged with #2339, linux_entry_argv passes with the call at entry + 17, and the old version of the test fails there, so the test no longer needs changing whichever lands first.

There's one more interaction, in the build. This PR makes EntryStubBuilder.cpp include <domain/Types.hpp> (for RelinkerException, as the other elfpatcher sources do). #2339's new entry_stub_tests target only adds elfpatcher/include, so with both merged that target fails to compile because it can't find domain/Types.hpp. Adding ${CMAKE_CURRENT_SOURCE_DIR}/domain/include to its include directories, as windows_icon_resource_tests does, fixes it. With that line the merged tree passes 45/45. The line does nothing on current main, so adding it to #2339 now covers both orders. If #2339 lands first without it, I'll add it here.

While checking, I also found that #2274 would break this test's far-load case (it adds a synthetic program header), so that case now sizes its header table from the shared fixture. I've rebased onto main a291ae0f and squashed it into 607fde62. Fork CI: the relinker-only build passes on Linux, Windows and macOS (run 38011567988). The full builds fail only where main itself currently fails; the description has the details.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6092229952 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6092229952). Originally posted by [treuenten](https://github.com/treuenten) on 2026-10-10T01:34:08Z. --- @johnmaia thanks for the heads-up, you're right: the test read the `call` at a fixed `entry + 10`. `entry_call_target` now finds the `call` by its opcode instead, so the test works with both the current stub and yours. With this PR merged with #2339, `linux_entry_argv` passes with the call at `entry + 17`, and the old version of the test fails there, so the test no longer needs changing whichever lands first. There's one more interaction, in the build. This PR makes `EntryStubBuilder.cpp` include `<domain/Types.hpp>` (for `RelinkerException`, as the other elfpatcher sources do). #2339's new `entry_stub_tests` target only adds `elfpatcher/include`, so with both merged that target fails to compile because it can't find `domain/Types.hpp`. Adding `${CMAKE_CURRENT_SOURCE_DIR}/domain/include` to its include directories, as `windows_icon_resource_tests` does, fixes it. With that line the merged tree passes 45/45. The line does nothing on current `main`, so adding it to #2339 now covers both orders. If #2339 lands first without it, I'll add it here. While checking, I also found that #2274 would break this test's far-load case (it adds a synthetic program header), so that case now sizes its header table from the shared fixture. I've rebased onto `main` a291ae0f and squashed it into 607fde62. Fork CI: the relinker-only build passes on Linux, Windows and macOS ([run 38011567988](https://github.com/treuenten/AnyPS5/actions/runs/38011567988)). The full builds fail only where `main` itself currently fails; the description has the details. AI-assisted: yes
oneandonlydean commented 2026-10-10 05:17:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T05:17:47Z.


#2339 is on main now, and its entry_stub_tests target builds EntryStubBuilder.cpp without domain/include, so with this PR merged <domain/Types.hpp> isn't found. Please rebase and add ${CMAKE_CURRENT_SOURCE_DIR}/domain/include to target_include_directories(entry_stub_tests ...) in core/relinker/CMakeLists.txt:303, as you suggested on #2339.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094137289 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6094137289). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T05:17:47Z. --- #2339 is on main now, and its `entry_stub_tests` target builds `EntryStubBuilder.cpp` without `domain/include`, so with this PR merged `<domain/Types.hpp>` isn't found. Please rebase and add `${CMAKE_CURRENT_SOURCE_DIR}/domain/include` to `target_include_directories(entry_stub_tests ...)` in `core/relinker/CMakeLists.txt:303`, as you suggested on #2339.
treuenten commented 2026-10-10 05:38:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by treuenten on 2026-10-10T05:38:00Z.


@oneandonlydean done: rebased onto main b667d1e3, and ${CMAKE_CURRENT_SOURCE_DIR}/domain/include is now in target_include_directories(entry_stub_tests ...), squashed into ded18e50. Without that line entry_stub_tests fails with 'domain/Types.hpp' file not found. With it, the relinker-only ctest passes 52/52 here, including entry_stub and linux_entry_argv.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094279291 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6094279291). Originally posted by [treuenten](https://github.com/treuenten) on 2026-10-10T05:38:00Z. --- @oneandonlydean done: rebased onto `main` b667d1e3, and `${CMAKE_CURRENT_SOURCE_DIR}/domain/include` is now in `target_include_directories(entry_stub_tests ...)`, squashed into ded18e50. Without that line `entry_stub_tests` fails with `'domain/Types.hpp' file not found`. With it, the relinker-only ctest passes 52/52 here, including `entry_stub` and `linux_entry_argv`. AI-assisted: yes
adriadam10 commented 2026-10-10 06:49:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:49:32Z.


Checked the rebase on current main: domain/include is now in entry_stub_tests (core/relinker/CMakeLists.txt:322), as Dean asked. Relinker build and ctest pass (55/55, including entry_stub and linux_entry_argv), relinker CI is green on the head. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094816886 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2097#issuecomment-6094816886). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T06:49:32Z. --- Checked the rebase on current `main`: `domain/include` is now in `entry_stub_tests` (`core/relinker/CMakeLists.txt:322`), as Dean asked. Relinker build and ctest pass (55/55, including `entry_stub` and `linux_entry_argv`), relinker CI is green on the head. Merging.
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!2097
No description provided.