feat(relinker): relocate RIP-relative operands into --to-intel stubs #1218
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1218"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by hashaam1217 on 2026-10-07T18:03:46Z.
What
--to-intelfailed 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 ofLoweredBody,Amd64OnlyMatchandTrampolineSite, so existing positional initializers still compile.DecodeMemoryOperandaccepts mod=00 rm=101 and recordsRipRelativeandInstructionLength. The SHA decoders throw when a RIP-relative encoding does not end exactly at the instruction length, since the target depends on it.StubBodyBuildertracks the offset in the site.Loademitsmovdqu reg,[rip+0]with a relocation;Movecopies a following instruction and relocates its RIP-relative disp32;Advancesteps over an instruction.MatchandMatchSequence: emit, advance by the instruction length, then splittrailingwithX64InstructionDecoder. The lowering classes lose theirtrailingparameter: they do not know the instruction length, and copying RIP-relative bytes raw would be silently wrong.call [rip+x]andjmp [rip+x]still fail the relink.ApplyStubRelocations(inStubBodyBuilder.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 computessite + 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::GetRvais 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.Tested
Ubuntu 24.04.5, GCC 13.3.0, x86-64. Full build and
ctest: 399/399 passed (unchangedmain: 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:[rip+disp], the same with65 67prefixes (both kept on the reload), SHA1RNDS4 (immediate counted in the instruction end, negative displacement), a movedleaafter anop, 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.runStubBodymaps 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 againstsha256Reference, SHA1RNDS4 againstsha1Reference, and EXTRQ followed bymovdqu xmm3,[rip+x]andlea rax,[rip+x]. Also checks that no other xmm register, rcx or RFLAGS changes.kRipRelativeVectorLoads(VEX, VEX3, EVEX, 0F38, 0F3A with an immediate) move after a short EXTRQ with the right relocation;call [rip+x]andjmp [rip+x]still fail at their offset.guest_intel_trampolines: a new RIP-relative SHA256MSG1 site. For the Linux and--windowsguest modules the stub's reload resolves to the original operand address. With the relinker frommainthis case fails with "RIP-relative memory operand cannot move into a stub".tools/check_conventions.py --base origin/main: passed.amd64_only_windowsdid not run.WindowsTrampolineBuilderruns on Linux through the--windowsguest case above.git merge-treeagainst every open pull request. No open pull request implements this, and none conflicts with this branch unless it already conflicts withmain. #1128 (tree-wide clang-format) touches every file here.No title was run.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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.
main)docs/dev/TechnicalDebt.mdL276-277Imported 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.cppmerges cleanly in either order, so only that one doc line conflicts. If #1393 lands first, I'll resolve it here.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 inMatch/MatchSequence, the end-of-instruction check in the SHA decoders and the bounds inApplyStubRelocations.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.