feat(relinker): relocate RIP-relative operands into --to-intel stubs #1218

Merged
hashaam1217 merged 2 commits from upstream-pr/1218 into main 2026-10-08 09:40:18 +00:00
hashaam1217 commented 2026-10-07 18:03:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-07T18:03:46Z.


What

--to-intel failed the relink when a SHA-1 or SHA-256 instruction had a [rip+disp32] operand, and when a site shorter than the 5-byte jump had to absorb a following instruction with one ("followed by an instruction that cannot move"; VRSQRTPS/VRCPPS sites were kept native instead). Both now move into the stub, and the stub's displacement is resolved once the stub address is known.

  • StubRelocation {DisplacementOffset, InstructionEnd, SiteTarget}: a disp32 in the stub body and the operand address relative to the site address. Appended as the last field of LoweredBody, Amd64OnlyMatch and TrampolineSite, so existing positional initializers still compile.
  • DecodeMemoryOperand accepts mod=00 rm=101 and records RipRelative and InstructionLength. The SHA decoders throw when a RIP-relative encoding does not end exactly at the instruction length, since the target depends on it.
  • StubBodyBuilder tracks the offset in the site. Load emits movdqu reg,[rip+0] with a relocation; Move copies a following instruction and relocates its RIP-relative disp32; Advance steps over an instruction.
  • The matcher builds every out-of-line body itself, in Match and MatchSequence: emit, advance by the instruction length, then split trailing with X64InstructionDecoder. The lowering classes lose their trailing parameter: they do not know the instruction length, and copying RIP-relative bytes raw would be silently wrong.
  • The converter no longer rejects a following RIP-relative instruction. Non-sequential flow and branch targets are still rejected, so call [rip+x] and jmp [rip+x] still fail the relink.
  • ApplyStubRelocations (in StubBodyBuilder.cpp, no new source file, no CMake change) resolves the relocations in the Linux ELF patcher, the Linux guest module writer and the Windows trampoline builder. It computes site + SiteTarget - (stub + InstructionEnd) modulo 2^64, as the CPU does, and throws when the result does not fit in rel32 or the relocation is not inside the body before the return jump. Windows works on RVAs: WindowsLoadImage::GetRva is linear, so the site-to-target distance is the one in the ELF.
  • 0x67 (EIP-relative) needs no special case: prefixes are kept on the reload and the 64-bit sum is preserved, so the truncated address is the same. This is covered by a golden check of the reload bytes, not by execution.
  • TechnicalDebt: removed the sentence about SHA RIP-relative operands. The VRSQRTPS/VRCPPS entry still says 5 sites in PPSA21564 are kept native; some of them may now lower if the following instruction was RIP-relative. I don't have the title to recount, so the number is unchanged.

Tested

Ubuntu 24.04.5, GCC 13.3.0, x86-64. Full build and ctest: 399/399 passed (unchanged main: 399/399).

  • amd64_only_converter, flipped: the 7 assertions that expected the rejection (SHA-256/SHA-1/SHA1RNDS4 operand decode, the RIP-relative follower, the converter SHA-256/SHA-1 RIP-relative forms, VRSQRTPS with a RIP-relative follower) now check the decoded operand or the recorded relocation.
  • amd64_only_converter, new:
    • Golden stub bytes and relocation for SHA256MSG1 [rip+disp], the same with 65 67 prefixes (both kept on the reload), SHA1RNDS4 (immediate counted in the instruction end, negative displacement), a moved lea after a nop, and the sequence EXTRQ + SHA256MSG1 (target relative to the site start).
    • ApplyStubRelocations: stub below and above the site, out of rel32 range, outside the body, narrower than a disp32.
    • Execution on Linux: runStubBody maps the stub, a fictitious site page and a data page, and applies the relocations against the site page. SHA256RNDS2/MSG1/MSG2 with a RIP-relative source against sha256Reference, SHA1RNDS4 against sha1Reference, and EXTRQ followed by movdqu xmm3,[rip+x] and lea rax,[rip+x]. Also checks that no other xmm register, rcx or RFLAGS changes.
    • Converter: the five loads in kRipRelativeVectorLoads (VEX, VEX3, EVEX, 0F38, 0F3A with an immediate) move after a short EXTRQ with the right relocation; call [rip+x] and jmp [rip+x] still fail at their offset.
    • Linux ELF placement: the patched disp32 in the stub resolves to the original operand address.
    • The new tests fail when the instruction end is left out of the displacement, and when the site offset never advances (checked by breaking each on purpose).
  • guest_intel_trampolines: a new RIP-relative SHA256MSG1 site. For the Linux and --windows guest modules the stub's reload resolves to the original operand address. With the relinker from main this case fails with "RIP-relative memory operand cannot move into a stub".
  • tools/check_conventions.py --base origin/main: passed.
  • Windows (MinGW) was not built or run, so amd64_only_windows did not run. WindowsTrampolineBuilder runs on Linux through the --windows guest case above.
  • Overlap: checked with git merge-tree against every open pull request. No open pull request implements this, and none conflicts with this branch unless it already conflicts with main. #1128 (tree-wide clang-format) touches every file here.

No title was run.

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:4776525286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1218). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-07T18:03:46Z. --- ### What `--to-intel` failed the relink when a SHA-1 or SHA-256 instruction had a `[rip+disp32]` operand, and when a site shorter than the 5-byte jump had to absorb a following instruction with one ("followed by an instruction that cannot move"; VRSQRTPS/VRCPPS sites were kept native instead). Both now move into the stub, and the stub's displacement is resolved once the stub address is known. - `StubRelocation {DisplacementOffset, InstructionEnd, SiteTarget}`: a disp32 in the stub body and the operand address relative to the site address. Appended as the last field of `LoweredBody`, `Amd64OnlyMatch` and `TrampolineSite`, so existing positional initializers still compile. - `DecodeMemoryOperand` accepts mod=00 rm=101 and records `RipRelative` and `InstructionLength`. The SHA decoders throw when a RIP-relative encoding does not end exactly at the instruction length, since the target depends on it. - `StubBodyBuilder` tracks the offset in the site. `Load` emits `movdqu reg,[rip+0]` with a relocation; `Move` copies a following instruction and relocates its RIP-relative disp32; `Advance` steps over an instruction. - The matcher builds every out-of-line body itself, in `Match` and `MatchSequence`: emit, advance by the instruction length, then split `trailing` with `X64InstructionDecoder`. The lowering classes lose their `trailing` parameter: they do not know the instruction length, and copying RIP-relative bytes raw would be silently wrong. - The converter no longer rejects a following RIP-relative instruction. Non-sequential flow and branch targets are still rejected, so `call [rip+x]` and `jmp [rip+x]` still fail the relink. - `ApplyStubRelocations` (in `StubBodyBuilder.cpp`, no new source file, no CMake change) resolves the relocations in the Linux ELF patcher, the Linux guest module writer and the Windows trampoline builder. It computes `site + SiteTarget - (stub + InstructionEnd)` modulo 2^64, as the CPU does, and throws when the result does not fit in rel32 or the relocation is not inside the body before the return jump. Windows works on RVAs: `WindowsLoadImage::GetRva` is linear, so the site-to-target distance is the one in the ELF. - `0x67` (EIP-relative) needs no special case: prefixes are kept on the reload and the 64-bit sum is preserved, so the truncated address is the same. This is covered by a golden check of the reload bytes, not by execution. - TechnicalDebt: removed the sentence about SHA RIP-relative operands. The VRSQRTPS/VRCPPS entry still says 5 sites in PPSA21564 are kept native; some of them may now lower if the following instruction was RIP-relative. I don't have the title to recount, so the number is unchanged. ### Tested Ubuntu 24.04.5, GCC 13.3.0, x86-64. Full build and `ctest`: 399/399 passed (unchanged `main`: 399/399). - `amd64_only_converter`, flipped: the 7 assertions that expected the rejection (SHA-256/SHA-1/SHA1RNDS4 operand decode, the RIP-relative follower, the converter SHA-256/SHA-1 RIP-relative forms, VRSQRTPS with a RIP-relative follower) now check the decoded operand or the recorded relocation. - `amd64_only_converter`, new: - Golden stub bytes and relocation for SHA256MSG1 `[rip+disp]`, the same with `65 67` prefixes (both kept on the reload), SHA1RNDS4 (immediate counted in the instruction end, negative displacement), a moved `lea` after a `nop`, and the sequence EXTRQ + SHA256MSG1 (target relative to the site start). - `ApplyStubRelocations`: stub below and above the site, out of rel32 range, outside the body, narrower than a disp32. - Execution on Linux: `runStubBody` maps the stub, a fictitious site page and a data page, and applies the relocations against the site page. SHA256RNDS2/MSG1/MSG2 with a RIP-relative source against `sha256Reference`, SHA1RNDS4 against `sha1Reference`, and EXTRQ followed by `movdqu xmm3,[rip+x]` and `lea rax,[rip+x]`. Also checks that no other xmm register, rcx or RFLAGS changes. - Converter: the five loads in `kRipRelativeVectorLoads` (VEX, VEX3, EVEX, 0F38, 0F3A with an immediate) move after a short EXTRQ with the right relocation; `call [rip+x]` and `jmp [rip+x]` still fail at their offset. - Linux ELF placement: the patched disp32 in the stub resolves to the original operand address. - The new tests fail when the instruction end is left out of the displacement, and when the site offset never advances (checked by breaking each on purpose). - `guest_intel_trampolines`: a new RIP-relative SHA256MSG1 site. For the Linux and `--windows` guest modules the stub's reload resolves to the original operand address. With the relinker from `main` this case fails with "RIP-relative memory operand cannot move into a stub". - `tools/check_conventions.py --base origin/main`: passed. - Windows (MinGW) was not built or run, so `amd64_only_windows` did not run. `WindowsTrampolineBuilder` runs on Linux through the `--windows` guest case above. - Overlap: checked with `git merge-tree` against every open pull request. No open pull request implements this, and none conflicts with this branch unless it already conflicts with `main`. #1128 (tree-wide clang-format) touches every file here. No title was run. ### 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)
github-actions[bot] commented 2026-10-08 04:34:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T04:34:45Z.


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
#1393 docs/dev/TechnicalDebt.md L276-277
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052320117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1218#issuecomment-6052320117). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T04:34:45Z. --- <!-- 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 | |---|---|---|---| | #1393 | `docs/dev/TechnicalDebt.md` [L276-277](https://github.com/boykopovar/AnyPS5/blob/b36718214cf78c353a97eba1a3ff4815e4e124e9/docs/dev/TechnicalDebt.md#L276-L277) | | |
hashaam1217 commented 2026-10-08 04:45:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-08T04:45:24Z.


The overlap with #1393 is two adjacent lines in docs/dev/TechnicalDebt.md. This PR removes the SHA sentence from the RDPRU/MCOMMIT entry, and #1393 rewords the next entry, about the instruction rewriter. The code changes don't overlap: Amd64OnlyConverterTests.cpp merges cleanly in either order, so only that one doc line conflicts. If #1393 lands first, I'll resolve it here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052449930 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1218#issuecomment-6052449930). Originally posted by [hashaam1217](https://github.com/hashaam1217) on 2026-10-08T04:45:24Z. --- The overlap with #1393 is two adjacent lines in `docs/dev/TechnicalDebt.md`. This PR removes the SHA sentence from the RDPRU/MCOMMIT entry, and #1393 rewords the next entry, about the instruction rewriter. The code changes don't overlap: `Amd64OnlyConverterTests.cpp` merges cleanly in either order, so only that one doc line conflicts. If #1393 lands first, I'll resolve it here.
adriadam10 commented 2026-10-08 09:13:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T09:13:16Z.


Checked: build + full ctest on current main (430/430), new tests don't build without the change, CI green. Reviewed the relocation math (site + SiteTarget - (stub + InstructionEnd)), site-offset tracking in Match/MatchSequence, the end-of-instruction check in the SHA decoders and the bounds in ApplyStubRelocations.

Added one commit on your branch (274b070ac): the VRSQRTPS/VRCPPS TechnicalDebt entry still stated "5 sites in PPSA21564" as a fact, which this PR can change; it now says the count predates RIP-relative followers and was not recounted.

Note: the line you edit in TechnicalDebt is adjacent to the one #1393 rewords; whichever lands second rebases.

Merging once CI is green on the rebased head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056635422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1218#issuecomment-6056635422). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T09:13:16Z. --- Checked: build + full ctest on current `main` (430/430), new tests don't build without the change, CI green. Reviewed the relocation math (`site + SiteTarget - (stub + InstructionEnd)`), site-offset tracking in `Match`/`MatchSequence`, the end-of-instruction check in the SHA decoders and the bounds in `ApplyStubRelocations`. Added one commit on your branch (274b070ac): the VRSQRTPS/VRCPPS TechnicalDebt entry still stated "5 sites in PPSA21564" as a fact, which this PR can change; it now says the count predates RIP-relative followers and was not recounted. Note: the line you edit in TechnicalDebt is adjacent to the one #1393 rewords; whichever lands second rebases. Merging once CI is green on the rebased head.
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!1218
No description provided.