feat(relinker): add conversion-time game patches #6314

Merged
eliyarson merged 4 commits from upstream-pr/6314 into main 2026-10-18 22:37:52 +00:00
eliyarson commented 2026-10-15 11:06:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-15T11:06:56Z.


What

Adds an opt-in per-title patch file, relinker-patches.toml, that the relinker applies to the executable and its modules at conversion time. Proposal and design: #6206, questions: #6207.

The file is found next to the input automatically. --patches <file> selects another file. --no-patches ignores it. The file is a strict subset of TOML. Each [[patch]] entry has target, va, expected and value, and optionally name, type, min, max, align and description.

The relinker refuses the conversion when:

  • the bytes at va differ from expected;
  • the range is not inside one PT_LOAD;
  • two entries overlap;
  • the target matches no file, or more than one file;
  • an entry overlaps one of the relinker's own in-place writes: Intel lowering sites (including trampolines), relinker patches, module syscall traps, the ELF header and program-header table, or relocation targets.

Every check runs before any output is written. On error nothing is written. With no patch file the output is byte-identical to the output of main.

The relinker writes a manifest, <stem>.patches.toml, built before the outputs. It lists every applied entry. --patches <manifest> replays it.

--verify-patches <file> <path> [--verify-output] checks a patch file against an input, or against a converted output with --verify-output. It writes nothing.

Motivating case: Hades II. One byte raises its upload pool from 512 to 768 MiB. Pinned to 2 cores, 7 of 10 runs crashed before and 0 of 10 after (details in #6206). The code and docs do not name a title.

Follow-ups: not in this PR; see #6206.

Tested

CachyOS, Linux 7.2.2, Ryzen 9 5950X, RTX 3080 Ti.

Results are for the squashed commit cde39e8e5.

  • Relinker tests only: ctest passes 174 of 174.
  • Full ctest on the branch: 1236 tests, 4 failed, 2 skipped. Plain main 8856f180c with the same configuration: 1234 tests, the same 4 failed and the same 2 skipped. The branch adds two tests. Nothing here is caused by this PR.
    • guest_allocator_failures fails on this host because of memory overcommit. #6310 (open) fixes the test.
    • video_out_flip_eop, video_out_flip_eop_main and video_out_flip_eop_aftergpu also fail on plain main 8856f180c; they passed here on 760ffa683.
    • agc_driver_stencil_ref_export is skipped: the GPU lacks VK_EXT_shader_stencil_export.
    • agc_import_watch_other_family is skipped.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Hades II conversion without a patch file: app.elf and all 7 modules are byte-identical to the output of main. The same holds with a patch file present and --no-patches.
  • Hades II conversion with the patch file: exactly one byte of app.elf differs (offset 0x290414, 0x20 to 0x30). The modules are unchanged.
  • --verify-patches on the input reports applicable. On the converted app.elf with --verify-output it reports applied. Both exit 0.
  • Converting again with --patches on the manifest gives identical outputs; the new manifest differs only in config.

Not tested: Windows; macOS; the web converter.

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
  • Unblocks: none (the patch file is a local change)
  • GPUs tested: n/a (relinker only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4872261826 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6314). Originally posted by [eliyarson](https://github.com/eliyarson) on 2026-10-15T11:06:56Z. --- ### What Adds an opt-in per-title patch file, `relinker-patches.toml`, that the relinker applies to the executable and its modules at conversion time. Proposal and design: #6206, questions: #6207. The file is found next to the input automatically. `--patches <file>` selects another file. `--no-patches` ignores it. The file is a strict subset of TOML. Each `[[patch]]` entry has `target`, `va`, `expected` and `value`, and optionally `name`, `type`, `min`, `max`, `align` and `description`. The relinker refuses the conversion when: - the bytes at `va` differ from `expected`; - the range is not inside one `PT_LOAD`; - two entries overlap; - the target matches no file, or more than one file; - an entry overlaps one of the relinker's own in-place writes: Intel lowering sites (including trampolines), relinker patches, module syscall traps, the ELF header and program-header table, or relocation targets. Every check runs before any output is written. On error nothing is written. With no patch file the output is byte-identical to the output of `main`. The relinker writes a manifest, `<stem>.patches.toml`, built before the outputs. It lists every applied entry. `--patches <manifest>` replays it. `--verify-patches <file> <path> [--verify-output]` checks a patch file against an input, or against a converted output with `--verify-output`. It writes nothing. Motivating case: Hades II. One byte raises its upload pool from 512 to 768 MiB. Pinned to 2 cores, 7 of 10 runs crashed before and 0 of 10 after (details in #6206). The code and docs do not name a title. Follow-ups: not in this PR; see #6206. ### Tested CachyOS, Linux 7.2.2, Ryzen 9 5950X, RTX 3080 Ti. Results are for the squashed commit `cde39e8e5`. - Relinker tests only: `ctest` passes 174 of 174. - Full `ctest` on the branch: 1236 tests, 4 failed, 2 skipped. Plain `main` 8856f180c with the same configuration: 1234 tests, the same 4 failed and the same 2 skipped. The branch adds two tests. Nothing here is caused by this PR. - `guest_allocator_failures` fails on this host because of memory overcommit. #6310 (open) fixes the test. - `video_out_flip_eop`, `video_out_flip_eop_main` and `video_out_flip_eop_aftergpu` also fail on plain `main` 8856f180c; they passed here on 760ffa683. - `agc_driver_stencil_ref_export` is skipped: the GPU lacks `VK_EXT_shader_stencil_export`. - `agc_import_watch_other_family` is skipped. - `python3 tools/check_conventions.py --base origin/main` passes. - Hades II conversion without a patch file: `app.elf` and all 7 modules are byte-identical to the output of `main`. The same holds with a patch file present and `--no-patches`. - Hades II conversion with the patch file: exactly one byte of `app.elf` differs (offset 0x290414, 0x20 to 0x30). The modules are unchanged. - `--verify-patches` on the input reports `applicable`. On the converted `app.elf` with `--verify-output` it reports `applied`. Both exit 0. - Converting again with `--patches` on the manifest gives identical outputs; the new manifest differs only in `config`. Not tested: Windows; macOS; the web converter. ### 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: none - [x] Unblocks: none (the patch file is a local change) - [x] GPUs tested: n/a (relinker only) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-15 11:13:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T11:13:57Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6802 core/relinker/cli/src/CliArgs.cpp L14, L17-19
core/relinker/cli/tests/CliArgsTests.cpp L310
PR 7450 core/relinker/main.cpp L65
core/relinker/relinker/include/relinker/pipeline/Conversion.hpp L26
docs/user/USAGE.md L78

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 2780 core/relinker/CMakeLists.txt
core/relinker/relinker/src/guest/GuestModuleBuilder.cpp
PR 3998 docs/user/USAGE.md
PR 4891 docs/user/USAGE.md
PR 5021 core/relinker/CMakeLists.txt
docs/user/USAGE.md
PR 5925 docs/dev/ARCHITECTURE.md
docs/user/USAGE.md
PR 6044 core/relinker/CMakeLists.txt
docs/dev/ARCHITECTURE.md
PR 6412 docs/dev/ARCHITECTURE.md
PR 6454 docs/dev/ARCHITECTURE.md
PR 6503 docs/dev/ARCHITECTURE.md
PR 6763 docs/dev/ARCHITECTURE.md
PR 6788 docs/user/USAGE.md
PR 6879 core/relinker/CMakeLists.txt
PR 6927 core/relinker/main.cpp
PR 6931 docs/dev/ARCHITECTURE.md
PR 6946 docs/dev/ARCHITECTURE.md
docs/user/USAGE.md
PR 6947 docs/dev/ARCHITECTURE.md
docs/user/USAGE.md
PR 6977 core/relinker/CMakeLists.txt
core/relinker/relinker/include/relinker/guest/GuestImage.hpp
core/relinker/relinker/src/guest/GuestModuleBuilder.cpp
PR 7173 core/relinker/CMakeLists.txt
PR 7332 core/relinker/CMakeLists.txt
core/relinker/cli/include/Cli.hpp
core/relinker/cli/src/CliArgs.cpp
core/relinker/cli/tests/CliArgsTests.cpp
core/relinker/main.cpp
core/relinker/relinker/include/relinker/pipeline/Conversion.hpp
core/relinker/relinker/src/pipeline/Conversion.cpp
docs/user/USAGE.md
PR 7531 core/relinker/CMakeLists.txt
PR 7642 docs/dev/ARCHITECTURE.md
docs/user/USAGE.md
PR 7644 docs/dev/ARCHITECTURE.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190725175 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6314#issuecomment-6190725175). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T11:13:57Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6802](https://www.github.com/boykopovar/AnyPS5/pull/6802) | `core/relinker/cli/src/CliArgs.cpp` [L14](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/core/relinker/cli/src/CliArgs.cpp#L14), [L17-19](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/core/relinker/cli/src/CliArgs.cpp#L17-L19)<br>`core/relinker/cli/tests/CliArgsTests.cpp` [L310](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/core/relinker/cli/tests/CliArgsTests.cpp#L310) | | | | [PR 7450](https://www.github.com/boykopovar/AnyPS5/pull/7450) | `core/relinker/main.cpp` [L65](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/core/relinker/main.cpp#L65)<br>`core/relinker/relinker/include/relinker/pipeline/Conversion.hpp` [L26](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/core/relinker/relinker/include/relinker/pipeline/Conversion.hpp#L26)<br>`docs/user/USAGE.md` [L78](https://github.com/boykopovar/AnyPS5/blob/c820102bd03cc5ec4f3ebd35ccc20570e5d50f5d/docs/user/USAGE.md#L78) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 2780](https://www.github.com/boykopovar/AnyPS5/pull/2780) | `core/relinker/CMakeLists.txt`<br>`core/relinker/relinker/src/guest/GuestModuleBuilder.cpp` | | [PR 3998](https://www.github.com/boykopovar/AnyPS5/pull/3998) | `docs/user/USAGE.md` | | [PR 4891](https://www.github.com/boykopovar/AnyPS5/pull/4891) | `docs/user/USAGE.md` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/relinker/CMakeLists.txt`<br>`docs/user/USAGE.md` | | [PR 5925](https://www.github.com/boykopovar/AnyPS5/pull/5925) | `docs/dev/ARCHITECTURE.md`<br>`docs/user/USAGE.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/relinker/CMakeLists.txt`<br>`docs/dev/ARCHITECTURE.md` | | [PR 6412](https://www.github.com/boykopovar/AnyPS5/pull/6412) | `docs/dev/ARCHITECTURE.md` | | [PR 6454](https://www.github.com/boykopovar/AnyPS5/pull/6454) | `docs/dev/ARCHITECTURE.md` | | [PR 6503](https://www.github.com/boykopovar/AnyPS5/pull/6503) | `docs/dev/ARCHITECTURE.md` | | [PR 6763](https://www.github.com/boykopovar/AnyPS5/pull/6763) | `docs/dev/ARCHITECTURE.md` | | [PR 6788](https://www.github.com/boykopovar/AnyPS5/pull/6788) | `docs/user/USAGE.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/relinker/CMakeLists.txt` | | [PR 6927](https://www.github.com/boykopovar/AnyPS5/pull/6927) | `core/relinker/main.cpp` | | [PR 6931](https://www.github.com/boykopovar/AnyPS5/pull/6931) | `docs/dev/ARCHITECTURE.md` | | [PR 6946](https://www.github.com/boykopovar/AnyPS5/pull/6946) | `docs/dev/ARCHITECTURE.md`<br>`docs/user/USAGE.md` | | [PR 6947](https://www.github.com/boykopovar/AnyPS5/pull/6947) | `docs/dev/ARCHITECTURE.md`<br>`docs/user/USAGE.md` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/relinker/CMakeLists.txt`<br>`core/relinker/relinker/include/relinker/guest/GuestImage.hpp`<br>`core/relinker/relinker/src/guest/GuestModuleBuilder.cpp` | | [PR 7173](https://www.github.com/boykopovar/AnyPS5/pull/7173) | `core/relinker/CMakeLists.txt` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/relinker/CMakeLists.txt`<br>`core/relinker/cli/include/Cli.hpp`<br>`core/relinker/cli/src/CliArgs.cpp`<br>`core/relinker/cli/tests/CliArgsTests.cpp`<br>`core/relinker/main.cpp`<br>`core/relinker/relinker/include/relinker/pipeline/Conversion.hpp`<br>`core/relinker/relinker/src/pipeline/Conversion.cpp`<br>`docs/user/USAGE.md` | | [PR 7531](https://www.github.com/boykopovar/AnyPS5/pull/7531) | `core/relinker/CMakeLists.txt` | | [PR 7642](https://www.github.com/boykopovar/AnyPS5/pull/7642) | `docs/dev/ARCHITECTURE.md`<br>`docs/user/USAGE.md` | | [PR 7644](https://www.github.com/boykopovar/AnyPS5/pull/7644) | `docs/dev/ARCHITECTURE.md` |
MarcZX10 commented 2026-10-15 11:25:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T11:25:29Z.


The latest Windows relinker job is failing two tests: relinker_game_patch rejects a config path containing dir\\p.toml as unholdable, and the game_patches Python test asserts at line 69 that the generated manifest contains format = 1. Linux and macOS pass. Could you reproduce on Windows and check the UTF-8/native path conversion plus manifest generation? This looks Windows-specific; it prevents merging even though the focused Linux suite is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190927521 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6314#issuecomment-6190927521). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T11:25:29Z. --- The latest Windows relinker job is failing two tests: `relinker_game_patch` rejects a config path containing `dir\\p.toml` as unholdable, and the `game_patches` Python test asserts at line 69 that the generated manifest contains `format = 1`. Linux and macOS pass. Could you reproduce on Windows and check the UTF-8/native path conversion plus manifest generation? This looks Windows-specific; it prevents merging even though the focused Linux suite is green.
MarcZX10 commented 2026-10-15 11:26:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T11:26:58Z.


I narrowed these failures to Windows-specific test assumptions. In GamePatchTests.cpp, the dir\\p.toml case expects rejection, but on Windows that is a normal path separator and the fallback filename is p.toml; the same input is a literal backslash filename on Linux. The end-to-end Python assertion compares the manifest’s generic forward-slash path against str(Path), which uses backslashes on Windows. The implementation intentionally serializes generic paths. I suggest making the C++ case use a platform-independent invalid character (e.g. control byte) and comparing source.parent.as_posix() in the Python test. These appear to be test portability fixes rather than a Windows product bug.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190954310 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6314#issuecomment-6190954310). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T11:26:58Z. --- I narrowed these failures to Windows-specific test assumptions. In `GamePatchTests.cpp`, the `dir\\p.toml` case expects rejection, but on Windows that is a normal path separator and the fallback filename is `p.toml`; the same input is a literal backslash filename on Linux. The end-to-end Python assertion compares the manifest’s generic forward-slash path against `str(Path)`, which uses backslashes on Windows. The implementation intentionally serializes generic paths. I suggest making the C++ case use a platform-independent invalid character (e.g. control byte) and comparing `source.parent.as_posix()` in the Python test. These appear to be test portability fixes rather than a Windows product bug.
eliyarson commented 2026-10-15 23:14:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-15T23:14:45Z.


Thanks, you were right: both were test bugs, now fixed. I also rebased onto main, and CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6204242522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6314#issuecomment-6204242522). Originally posted by [eliyarson](https://github.com/eliyarson) on 2026-10-15T23:14:45Z. --- Thanks, you were right: both were test bugs, now fixed. I also rebased onto `main`, and CI is green.
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!6314
No description provided.