relinker: opt-in, byte-checked game patches at conversion time #6206

Closed
opened 2026-10-15 02:54:59 +00:00 by eliyarson · 2 comments
eliyarson commented 2026-10-15 02:54:59 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-15T02:54:59Z.


Proposal. An opt-in relinker feature: a per-title patch file that the relinker applies to the title's executable (and bundled modules) at conversion time. Each entry gives a virtual address, the bytes expected there and the bytes to write. The relinker refuses the conversion on any mismatch and writes a record of what it changed. With no patch file the output is byte-identical to today's. Nothing in code or docs names a title.

PR 1 (core patcher) is open: #6314. PRs 2–4 follow only if you want the feature.

Why. Titles hard-code budgets tuned for one machine, and PCs differ in core count, page size and per-access cost. The relinker already makes up for host differences at conversion time (--to-intel, --to-rosetta, --linux-fixed-base); a checked, recorded change of a game constant is the same kind of step, and is shareable and reproducible.

Why not a hex editor. The relinker checks the expected bytes against the original image, so a patch for another content version fails instead of corrupting the file. It refuses a patch that its own rewrites would replace: the ELF header and program-header table, Intel rewrites and their trampolines, syscall traps, relinker patches, and relocation targets, which the loader overwrites.

Design (PR 1).

  • relinker-patches.toml next to the input is loaded automatically; --patches <file> overrides it, --no-patches disables it.
  • Entries have target, va, expected, value, an optional type, an optional name, and optional min/max/align checks on the value.
  • The record is <out>.patches.toml. --patches with it reproduces the conversion.
  • --verify-patches re-checks a patch file against an input, or against an output, and writes nothing.

Follow-ups. A sha256 version gate (PR 2); the web converter (PR 3); --set name=value (KiB/MiB/GiB suffixes) and --settings <profile> for shareable per-machine settings (PR 4). Settings apply at conversion, not at launch.

What it does not do. It buys headroom for the title's own pipeline; it does not make the emulator cheaper. I know of no emulator-side fix. Two experiments (huge pages on direct memory; lazy write-watch re-arm) were not separable from base at 10 runs per arm (unpinned; base 1/10, huge pages 2/10, lazy re-arm 0/10).

Motivating case: Hades II (PPSA36082)

#1965 lists the 512 MiB upload-pool overflow, and its notes say the author played "with the pool patched bigger in the executable". The pool is a constant in the game's heap descriptor. I changed one byte of app.elf (the block size, 0x20 to 0x30 at file offset 0x290414): 768 MiB instead of 512 MiB. The arms differ only in that byte.

Pinned to 2 cores (taskset -c 0-1), 10 interleaved runs per arm:

arm crashes min free over the run, MiB peak used, MiB (max)
base (512 MiB) 7/10 0.3 to 104.9 512
768 MiB 0/10 118.2 to 360.9 650

One-sided Fisher p = 0.0015 (two-sided 0.0031). It also holds on rounds 1–8 alone (5/8 against 0/8, p = 0.0128). Unpinned, neither arm crashed in 10 runs: no regression, no improvement.

Crash. The pool runs out and a memcpy writes to NULL from app.elf+0x28fbae (the type-2 heap returning NULL is an inference, consistent with under 16 MiB free at every crash). gdb shows this in 6 of the 6 cores I could still read; the seventh has only a saved stack. #1965's blocker table lists "memcpy to null at eboot 0x28fba9" when the first flip replaces the device, fixed by #1367 (merged 2026-10-09). 0x28fba9 is the call and 0x28fbae its return address, so I believe it is the same site. My base build includes #1367, so that path is gone; the pool still runs out at the same site during loading, rarely unpinned and in 7 of 10 runs pinned to 2 cores.

Caveat. The game ran at nice 16–19, not 0 (inferred, not recorded per run); base crashed and the patched arm did not at both.

Provenance. main 73efd9407 with #6000; Linux (CachyOS, 7.2.2), Ryzen 9 5950X, RTX 3080 Ti, about 79 GiB available; 2026-10-14.

Patch file and verify mode in detail

Grammar. A strict subset of TOML, line-oriented UTF-8. Any other syntax is an error that quotes the line.

  • Blank lines and # comments are allowed. [[patch]] starts an entry; no other tables.
  • key = value, keys [A-Za-z0-9_]+. Integers are decimal or 0x hex (no _, no size suffixes). Strings are double-quoted, no escapes.
  • Rejected: arrays, inline tables, single quotes, floats, booleans, dotted keys, duplicate or unknown keys, multiline strings.
  • Top level: format = 1 is required. The record keys relinker_version, relinker_commit, config, config_sha256 and main_target are accepted and ignored on input.
entry key notes
target required. The main executable by file name; --executable targets and modules by path relative to the module root. An ambiguous or unmatched name is an error
va required, the virtual address as a disassembly shows it
type optional: u8 u16 u32 u64 i8 i16 i32 i64. Typed entries take integers, range-checked, little-endian; otherwise expected and value are hex-byte strings
name optional, [a-z0-9_]+, unique in the file; shown in progress lines, the record and the verify report
expected, value required, same width
min, max, align optional, typed entries only, checked on the value
description optional, reported and copied to the record

x86 mov r/m64, imm32 sign-extends its immediate; patch a 64-bit field through an imm32 with type = "i32" and a max.

Fatal before any output is written. An expected-byte mismatch; a range outside one PT_LOAD's file bytes; overlapping entries; an unmatched or ambiguous target; an overlap with another in-place writer (Intel reports and trampoline sites, window and island sites, relinker patches, module syscall traps, the ELF header and program-header table), or with a relocation target.

Record. <out>.patches.toml starts with format = 1 and main_target (the main executable's input name), then lists every applied entry with the effective value, the old expected bytes, name and description.

Verify. --verify-patches <file> <path> [--verify-output]. Input mode checks the input executable and its modules. Output mode needs an ET_DYN Linux ELF; main_target picks the entry checked against that file, --executable entries are checked against their derived outputs, other entries report not-checked, and a file without main_target is refused. Each entry is applicable, applied, not-checked or mismatch; exit code 0 means no mismatch.

format = 1
[[patch]]
target      = "eboot.elf"
name        = "upload_pool"
va          = 0x28c411
type        = "i32"
expected    = 0x20000000
value       = 0x30000000
min         = 0x20000000
max         = 0x7fe00000
align       = 0x200000
description = "allocator types 2..4 heap block size"

Related. #1965 (Hades II bring-up); #5485 (Hades II hub performance). Questions for maintainers: #6207.

AI assistance: the analysis, measurements and this text were produced with LLM assistance; I reviewed them and take responsibility for them.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5859258318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6206). Originally posted by [eliyarson](https://github.com/eliyarson) on 2026-10-15T02:54:59Z. --- **Proposal.** An opt-in relinker feature: a per-title patch file that the relinker applies to the title's executable (and bundled modules) at conversion time. Each entry gives a virtual address, the bytes expected there and the bytes to write. The relinker refuses the conversion on any mismatch and writes a record of what it changed. With no patch file the output is byte-identical to today's. Nothing in code or docs names a title. PR 1 (core patcher) is open: #6314. PRs 2–4 follow only if you want the feature. **Why.** Titles hard-code budgets tuned for one machine, and PCs differ in core count, page size and per-access cost. The relinker already makes up for host differences at conversion time (`--to-intel`, `--to-rosetta`, `--linux-fixed-base`); a checked, recorded change of a game constant is the same kind of step, and is shareable and reproducible. **Why not a hex editor.** The relinker checks the expected bytes against the original image, so a patch for another content version fails instead of corrupting the file. It refuses a patch that its own rewrites would replace: the ELF header and program-header table, Intel rewrites and their trampolines, syscall traps, relinker patches, and relocation targets, which the loader overwrites. **Design (PR 1).** - `relinker-patches.toml` next to the input is loaded automatically; `--patches <file>` overrides it, `--no-patches` disables it. - Entries have `target`, `va`, `expected`, `value`, an optional `type`, an optional `name`, and optional `min`/`max`/`align` checks on the value. - The record is `<out>.patches.toml`. `--patches` with it reproduces the conversion. - `--verify-patches` re-checks a patch file against an input, or against an output, and writes nothing. **Follow-ups.** A `sha256` version gate (PR 2); the web converter (PR 3); `--set name=value` (KiB/MiB/GiB suffixes) and `--settings <profile>` for shareable per-machine settings (PR 4). Settings apply at conversion, not at launch. **What it does not do.** It buys headroom for the title's own pipeline; it does not make the emulator cheaper. I know of no emulator-side fix. Two experiments (huge pages on direct memory; lazy write-watch re-arm) were not separable from base at 10 runs per arm (unpinned; base 1/10, huge pages 2/10, lazy re-arm 0/10). ### Motivating case: Hades II (PPSA36082) [#1965](https://github.com/boykopovar/AnyPS5/discussions/1965) lists the 512 MiB upload-pool overflow, and its notes say the author played "with the pool patched bigger in the executable". The pool is a constant in the game's heap descriptor. I changed one byte of `app.elf` (the block size, `0x20` to `0x30` at file offset `0x290414`): 768 MiB instead of 512 MiB. The arms differ only in that byte. Pinned to 2 cores (`taskset -c 0-1`), 10 interleaved runs per arm: | arm | crashes | min free over the run, MiB | peak used, MiB (max) | |---|---|---|---| | base (512 MiB) | **7/10** | 0.3 to 104.9 | 512 | | 768 MiB | **0/10** | 118.2 to 360.9 | 650 | One-sided Fisher p = 0.0015 (two-sided 0.0031). It also holds on rounds 1–8 alone (5/8 against 0/8, p = 0.0128). Unpinned, neither arm crashed in 10 runs: no regression, no improvement. **Crash.** The pool runs out and a `memcpy` writes to NULL from `app.elf+0x28fbae` (the type-2 heap returning NULL is an inference, consistent with under 16 MiB free at every crash). gdb shows this in 6 of the 6 cores I could still read; the seventh has only a saved stack. #1965's blocker table lists "memcpy to null at eboot `0x28fba9`" when the first flip replaces the device, fixed by #1367 (merged 2026-10-09). `0x28fba9` is the `call` and `0x28fbae` its return address, so I believe it is the same site. My base build includes #1367, so that path is gone; the pool still runs out at the same site during loading, rarely unpinned and in 7 of 10 runs pinned to 2 cores. **Caveat.** The game ran at nice 16–19, not 0 (inferred, not recorded per run); base crashed and the patched arm did not at both. **Provenance.** `main` 73efd9407 with #6000; Linux (CachyOS, 7.2.2), Ryzen 9 5950X, RTX 3080 Ti, about 79 GiB available; 2026-10-14. <details> <summary>Patch file and verify mode in detail</summary> **Grammar.** A strict subset of TOML, line-oriented UTF-8. Any other syntax is an error that quotes the line. - Blank lines and `#` comments are allowed. `[[patch]]` starts an entry; no other tables. - `key = value`, keys `[A-Za-z0-9_]+`. Integers are decimal or `0x` hex (no `_`, no size suffixes). Strings are double-quoted, no escapes. - Rejected: arrays, inline tables, single quotes, floats, booleans, dotted keys, duplicate or unknown keys, multiline strings. - Top level: `format = 1` is required. The record keys `relinker_version`, `relinker_commit`, `config`, `config_sha256` and `main_target` are accepted and ignored on input. | entry key | notes | |---|---| | `target` | required. The main executable by file name; `--executable` targets and modules by path relative to the module root. An ambiguous or unmatched name is an error | | `va` | required, the virtual address as a disassembly shows it | | `type` | optional: `u8 u16 u32 u64 i8 i16 i32 i64`. Typed entries take integers, range-checked, little-endian; otherwise `expected` and `value` are hex-byte strings | | `name` | optional, `[a-z0-9_]+`, unique in the file; shown in progress lines, the record and the verify report | | `expected`, `value` | required, same width | | `min`, `max`, `align` | optional, typed entries only, checked on the value | | `description` | optional, reported and copied to the record | x86 `mov r/m64, imm32` sign-extends its immediate; patch a 64-bit field through an imm32 with `type = "i32"` and a `max`. **Fatal before any output is written.** An expected-byte mismatch; a range outside one `PT_LOAD`'s file bytes; overlapping entries; an unmatched or ambiguous target; an overlap with another in-place writer (Intel reports and trampoline sites, window and island sites, relinker patches, module syscall traps, the ELF header and program-header table), or with a relocation target. **Record.** `<out>.patches.toml` starts with `format = 1` and `main_target` (the main executable's input name), then lists every applied entry with the effective `value`, the old `expected` bytes, `name` and `description`. **Verify.** `--verify-patches <file> <path> [--verify-output]`. Input mode checks the input executable and its modules. Output mode needs an `ET_DYN` Linux ELF; `main_target` picks the entry checked against that file, `--executable` entries are checked against their derived outputs, other entries report `not-checked`, and a file without `main_target` is refused. Each entry is `applicable`, `applied`, `not-checked` or `mismatch`; exit code 0 means no `mismatch`. ```toml format = 1 [[patch]] target = "eboot.elf" name = "upload_pool" va = 0x28c411 type = "i32" expected = 0x20000000 value = 0x30000000 min = 0x20000000 max = 0x7fe00000 align = 0x200000 description = "allocator types 2..4 heap block size" ``` </details> **Related.** [#1965](https://github.com/boykopovar/AnyPS5/discussions/1965) (Hades II bring-up); #5485 (Hades II hub performance). Questions for maintainers: #6207. AI assistance: the analysis, measurements and this text were produced with LLM assistance; I reviewed them and take responsibility for them.
SleglJan commented 2026-10-15 11:28:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-15T11:28:43Z.


A second case from #6172 (007 First Light, PPSA11386), with the same shape. The title waits for the GPU to write a feedback buffer for at most 2 s of TSC time and skips the wait once the job is two frames old; on our stack the GPU side takes longer than that at a level load, the title reads the buffer stale and crashes. The two limits are instruction immediates in the executable: an imm32 in the compare behind the timeout and an imm8 in the compare behind the frame delta. With both widened, 8 runs of 8 were clean against 10 crashes in 24 unpatched runs the same day. Locally we patch the converted output with a script that checks the old bytes first.

With this proposal it would be two typed entries (u32, and i8 since the imm8 is sign-extended; expected checked) applied at conversion and recorded, so a run's log can carry the record and --verify-patches tells another content version apart.

For us the patch is a diagnosis, not a fix: the emulator-side direction (a GPU-paced guest clock, or holding the title within two frames of the GPU) is in #6172.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190985441 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6206#issuecomment-6190985441). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-15T11:28:43Z. --- A second case from #6172 (007 First Light, PPSA11386), with the same shape. The title waits for the GPU to write a feedback buffer for at most 2 s of TSC time and skips the wait once the job is two frames old; on our stack the GPU side takes longer than that at a level load, the title reads the buffer stale and crashes. The two limits are instruction immediates in the executable: an imm32 in the compare behind the timeout and an imm8 in the compare behind the frame delta. With both widened, 8 runs of 8 were clean against 10 crashes in 24 unpatched runs the same day. Locally we patch the converted output with a script that checks the old bytes first. With this proposal it would be two typed entries (`u32`, and `i8` since the imm8 is sign-extended; `expected` checked) applied at conversion and recorded, so a run's log can carry the record and `--verify-patches` tells another content version apart. For us the patch is a diagnosis, not a fix: the emulator-side direction (a GPU-paced guest clock, or holding the title within two frames of the GPU) is in #6172. AI-assisted: yes (Claude Code)
eliyarson commented 2026-10-18 18:06:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-18T18:06:40Z.


Your 007 First Light case (PPSA11386, from #6172): an imm32 behind the timeout compare, an imm8 behind the frame-delta compare. With #6314 both are expressible as entries (u32, or i32 for a 64-bit compare, and i8) with expected, applied at conversion and recorded in the manifest.

The record/verify part is in #6314: <output-stem>.patches.toml beside the output, --verify-patches <file> <path> checking an input executable, and --verify-patches <file> --verify-output <path> checking a converted output; either mode reports another content version as mismatch.

x86 mov/add/sub/cmp r/m64, imm32 sign-extend the immediate, so a 64-bit field reached through an imm32 takes type = "i32" plus a min to keep negative values out. For the imm8, type = "i8" with min/max keeps the value in range.

PR #6314 (core patcher) is rebased onto current main (862503857) at head 62d881cfa, mergeable; CI has not passed yet — ubuntu, macOS and ASan are green, the Windows relinker job is still running. I have not run it on 007 First Light.

Agreed: a patch is a diagnosis, not the fix; the emulator-side direction belongs in #6172.

LLM assistance used.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253311673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6206#issuecomment-6253311673). Originally posted by [eliyarson](https://github.com/eliyarson) on 2026-10-18T18:06:40Z. --- Your 007 First Light case (PPSA11386, from #6172): an imm32 behind the timeout compare, an imm8 behind the frame-delta compare. With #6314 both are expressible as entries (`u32`, or `i32` for a 64-bit compare, and `i8`) with `expected`, applied at conversion and recorded in the manifest. The record/verify part is in #6314: `<output-stem>.patches.toml` beside the output, `--verify-patches <file> <path>` checking an input executable, and `--verify-patches <file> --verify-output <path>` checking a converted output; either mode reports another content version as `mismatch`. x86 `mov`/`add`/`sub`/`cmp r/m64, imm32` sign-extend the immediate, so a 64-bit field reached through an imm32 takes `type = "i32"` plus a `min` to keep negative values out. For the imm8, `type = "i8"` with `min`/`max` keeps the value in range. PR #6314 (core patcher) is rebased onto current main (862503857) at head 62d881cfa, mergeable; CI has not passed yet — ubuntu, macOS and ASan are green, the Windows relinker job is still running. I have not run it on 007 First Light. Agreed: a patch is a diagnosis, not the fix; the emulator-side direction belongs in #6172. LLM assistance used.
Sign in to join this conversation.
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#6206
No description provided.