From 22ebf5e60895d9d0db2549f6889d46e72522db2a Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Thu, 8 Oct 2026 03:00:47 +0800 Subject: [PATCH 1/2] fix(relinker): classify rewrite reference sites with the instruction decoder ClassifyInstruction assumed a ModRM byte right after every opcode that was not a near branch. A length-changing rewrite therefore adjusted immediates such as MOVABS imm64 that looked like a RIP-relative ModRM, and missed RIP-relative operands of VEX and 0F 38 / 0F 3A instructions and the rel8 targets of LOOP and JRCXZ. Take the RIP-relative displacement and branch target from DecodeInstruction instead. --- .../codegen/x86/X64OpcodeConstants.hpp | 1 - .../src/x86/X64InstructionRewriter.cpp | 74 ++----------------- .../codegen/tests/Amd64OnlyConverterTests.cpp | 14 ++++ 3 files changed, 22 insertions(+), 67 deletions(-) diff --git a/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp b/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp index ba63d071c..969a1fd30 100644 --- a/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp +++ b/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp @@ -215,7 +215,6 @@ inline constexpr std::uint8_t SibBaseDisp32 = 0x05; inline constexpr std::size_t Disp8Size = 1; inline constexpr std::size_t Disp32Size = 4; -inline constexpr std::size_t Rel32InstructionLength = 5; inline constexpr std::size_t Vex2PrefixLength = 2; inline constexpr std::size_t Vex3PrefixLength = 3; diff --git a/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp b/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp index 0d012bea4..a3899c3dd 100644 --- a/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp +++ b/core/relinker/codegen/src/x86/X64InstructionRewriter.cpp @@ -14,78 +14,20 @@ X64InstructionRewriter::ReferenceSite X64InstructionRewriter::ClassifyInstructio const std::uint8_t* data, std::size_t length ) { - std::size_t pos = 0; + const auto info = X64InstructionDecoder{}.DecodeInstruction(data, length); - while (pos < length) { - const std::uint8_t b = data[pos]; - if ((b >= RexMin && b <= RexMax) || - b == PrefixLock || b == PrefixRepne || b == PrefixRep || - b == PrefixSegCs || b == PrefixSegSs || b == PrefixSegDs || - b == PrefixSegEs || b == PrefixSegFs || b == PrefixSegGs || - b == PrefixOperandSize || b == PrefixAddressSize) { - pos += 1; - continue; - } - break; + if (info.HasRipRelativeDisp) { + return {ReferenceKind::RipRelativeDisp32, info.RipRelativeDispOffset}; } - if (pos >= length) { + if (!info.HasBranchTarget) { return {ReferenceKind::None, 0}; } - std::uint8_t opcode = data[pos]; - const std::size_t opcodeOffset = pos; - pos += 1; - bool twoByteOpcode = false; - - if (opcode == TwoByteOpcodeEscape) { - if (pos >= length) { - return {ReferenceKind::None, 0}; - } - twoByteOpcode = true; - opcode = data[pos]; - pos += 1; - } - - if (!twoByteOpcode) { - if (opcode == OneByteCallRel32 || opcode == OneByteJmpRel32) { - if (length >= opcodeOffset + Rel32InstructionLength) { - return {ReferenceKind::Rel32Branch, pos}; - } - return {ReferenceKind::None, 0}; - } - - if (opcode == OneByteJmpRel8 || (opcode >= OneByteJccRel8Min && opcode <= OneByteJccRel8Max)) { - if (length >= pos + Disp8Size) { - return {ReferenceKind::Rel8Branch, pos}; - } - return {ReferenceKind::None, 0}; - } - } else { - if (opcode >= TwoByteJccRel32Min && opcode <= TwoByteJccRel32Max) { - if (length >= pos + Disp32Size) { - return {ReferenceKind::Rel32Branch, pos}; - } - return {ReferenceKind::None, 0}; - } - } - - if (pos >= length) { - return {ReferenceKind::None, 0}; - } - - const std::uint8_t modrm = data[pos]; - const std::uint8_t mod = static_cast((modrm >> ModRmModShift) & ModRmModMask); - const std::uint8_t rm = static_cast(modrm & ModRmRmMask); - - if (mod == ModRmModIndirect && rm == ModRmRmRipRelative) { - const std::size_t dispOffset = pos + 1; - if (length >= dispOffset + Disp32Size) { - return {ReferenceKind::RipRelativeDisp32, dispOffset}; - } - } - - return {ReferenceKind::None, 0}; + const bool rel32 = info.IsTwoByteOpcode || info.Opcode == OneByteCallRel32 || info.Opcode == OneByteJmpRel32; + return rel32 + ? ReferenceSite{ReferenceKind::Rel32Branch, info.Length - Disp32Size} + : ReferenceSite{ReferenceKind::Rel8Branch, info.Length - Disp8Size}; } std::int32_t X64InstructionRewriter::ReadInt32(const std::uint8_t* data) { diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index f94034c19..69ec74251 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -723,6 +723,19 @@ void rewriterStrayRex() { require(rewritten.Bytes == expected, "Branch with a stray REX was not adjusted by a length-changing rewrite"); } +void rewriterReferenceSites() { + const std::vector> cases = { + {{0x90, 0x48, 0xB8, 0x05, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xC3}, 0, + {0x66, 0x90, 0x48, 0xB8, 0x05, 0xF0, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xC3}}, + {{0xC5, 0xF9, 0x6F, 0x05, 0x01, 0x00, 0x00, 0x00, 0x90, 0xC3}, 8, + {0xC5, 0xF9, 0x6F, 0x05, 0x02, 0x00, 0x00, 0x00, 0x66, 0x90, 0xC3}}, + {{0x66, 0x0F, 0x38, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x90, 0xC3}, 9, + {0x66, 0x0F, 0x38, 0x00, 0x05, 0x02, 0x00, 0x00, 0x00, 0x66, 0x90, 0xC3}}, + {{0xE2, 0x01, 0x90, 0xC3}, 2, {0xE2, 0x02, 0x66, 0x90, 0xC3}}}; + for (const auto& [code, offset, expected] : cases) + require(Codegen::X64InstructionRewriter{}.Rewrite(code, {offset, {0x66, 0x90}}).Bytes == expected, "Length-changing rewrite adjusted an immediate or missed a VEX, three-byte or LOOP reference"); +} + void converterFailureOffsets() { const auto converter = Codegen::MakeAmd64OnlyConverter(); const auto file = segmentFixture(); @@ -1323,6 +1336,7 @@ int main() { converterReciprocal(); converterStrayRex(); rewriterStrayRex(); + rewriterReferenceSites(); converterFailureOffsets(); linuxPlacement(); linuxRipRelativePlacement(); From 70b3ae082b7781e61a91c22e644cd9b3077305c8 Mon Sep 17 00:00:00 2001 From: xsvm <2479667226@qq.com> Date: Thu, 8 Oct 2026 10:21:42 +0800 Subject: [PATCH 2/2] docs(relinker): drop the VEX/0F38/0F3A gap from the rewriter debt entry --- docs/dev/TechnicalDebt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 42df79944..590fe2dbf 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -276,7 +276,7 @@ Throughout the project, every function at every stage either **does exactly what - [Per-stage descriptor count](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp) (libSceAgcDriver) - a pixel shader's colour attachments are not counted against `maxPerStageResources`, so its buffers and images can fit the limit while the pipeline exceeds it - [v_rcp, v_rsq, v_sqrt, v_log, v_exp, v_sin and v_cos](../../core/shader/recompiler/Translation/src/FloatInstructions.cpp) (f32 and f16) use the driver's approximation for finite results, not the console's: the low bits can differ. Only NaN, invalid, zero and infinite results and the exact sin/cos zeros are chosen on the bits. v_sin_f32/v_cos_f32 with inputs beyond ±256 cycles were not compared with the console. - `--to-intel` does not lower RDPRU/MCOMMIT; the [matcher](../../core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp) fails the relink instead. -- The length-changing path of the [instruction rewriter](../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust VEX/0F38/0F3A RIP-relative operands, data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length. +- The length-changing path of the [instruction rewriter](../../core/relinker/codegen/src/x86/X64InstructionRewriter.cpp) is not used by the [converter](../../core/relinker/codegen/src/Amd64OnlyConverter.cpp): it does not adjust data-to-code references (relocations, FDEs, jump tables) or segment sizes, so every substitution keeps the instruction length. - [DB_SHADER_CONTROL.CONSERVATIVE_Z_EXPORT](../../core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp) (libSceAgcDriver) becomes `DepthLess`/`DepthGreater`. For a pixel shader that breaks the promise, the host driver decides whether its interpolated Z is depth-tested before shading; what the console does then was not measured. - [DB_SHADER_CONTROL.PRIMITIVE_ORDERED_PIXEL_SHADER](../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - the host pixel interlock spans the whole shader, not only up to `s_sendmsg` ORDERED_PS_DONE. - [POPS collision SGPRs](../../core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp) (shader recompiler) - the collision wave ID and intrawave collision mask read 0, so a title skips its POPS wait and runs its per-collision loop once.