fix(relinker): decode the 64-bit address of MOV moffs instructions #1168

Merged
oguzhanozfe merged 1 commit from upstream-pr/1168 into main 2026-10-08 08:40:00 +00:00
oguzhanozfe commented 2026-10-07 16:01:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oguzhanozfe on 2026-10-07T16:01:45Z.


What

Fixes #1167.

A0-A3 (MOV AL/eAX/rAX to and from moffs) have no ModRM byte. In 64-bit mode they carry an 8-byte absolute address, or 4 bytes with a 0x67 prefix (Intel SDM Vol. 2A §2.2.1.4). The decoder added nothing for the address, so the linear scanner decoded the address bytes as instructions. The decoder now records the 0x67 prefix and adds the address size for these opcodes.

The decoderLengths test gains A0/A2 with no prefix, 48 A1, 64 48 A3, 67 A1 and 48 67 A3. In the last one, a REX before 0x67 is ignored and the address is 4 bytes.

This touches the same file as #1163, but the hunks are separate, and the two branches merge cleanly.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs x86_64-ucrt-posix-seh r7), configured like CI (-G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON). I built core/relinker/all and ran its ctest suite. On this branch 30/30 tests pass. With only the fix reverted, amd64_only_converter fails, so the new cases catch the bug.
  • Windows 11, MSVC 14.50: I compiled X64InstructionDecoder.cpp and InstructionScanner.cpp with a standalone program. It checks 11 A0-A3 forms and 5 controls (A8, A9, 67 8B 00, A4, 48 A5) through Decode and DecodeInstruction, and scans movabs rax, [0x8C0780F66]; ret.
    • Before the fix, all 11 forms are short. The scan finds an EXTRQ inside the address: [0,+2] [2,+6] [8,+2] [10,+1].
    • After the fix, everything passes and the scan gives [0,+10] [10,+1].
  • I built the relinker with MSVC from the add_executable(relinker ...) source list. These Python tests pass with it: dynamic_segment, elf_offsets, optional_plt, linux_load_alignment, string_table_bounds, executable_segments, elf_header, linux_entry_argv, input_magic, empty_tls and tls_function_coverage.

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:4775192265 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1168). Originally posted by [oguzhanozfe](https://github.com/oguzhanozfe) on 2026-10-07T16:01:45Z. --- ### What Fixes #1167. `A0`-`A3` (`MOV AL/eAX/rAX` to and from `moffs`) have no ModRM byte. In 64-bit mode they carry an 8-byte absolute address, or 4 bytes with a 0x67 prefix (Intel SDM Vol. 2A §2.2.1.4). The decoder added nothing for the address, so the linear scanner decoded the address bytes as instructions. The decoder now records the 0x67 prefix and adds the address size for these opcodes. The `decoderLengths` test gains `A0`/`A2` with no prefix, `48 A1`, `64 48 A3`, `67 A1` and `48 67 A3`. In the last one, a REX before 0x67 is ignored and the address is 4 bytes. This touches the same file as #1163, but the hunks are separate, and the two branches merge cleanly. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs `x86_64-ucrt-posix-seh` r7), configured like CI (`-G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON`). I built `core/relinker/all` and ran its ctest suite. On this branch 30/30 tests pass. With only the fix reverted, `amd64_only_converter` fails, so the new cases catch the bug. - Windows 11, MSVC 14.50: I compiled `X64InstructionDecoder.cpp` and `InstructionScanner.cpp` with a standalone program. It checks 11 `A0`-`A3` forms and 5 controls (`A8`, `A9`, `67 8B 00`, `A4`, `48 A5`) through `Decode` and `DecodeInstruction`, and scans `movabs rax, [0x8C0780F66]; ret`. - Before the fix, all 11 forms are short. The scan finds an EXTRQ inside the address: `[0,+2] [2,+6] [8,+2] [10,+1]`. - After the fix, everything passes and the scan gives `[0,+10] [10,+1]`. - I built the relinker with MSVC from the `add_executable(relinker ...)` source list. These Python tests pass with it: `dynamic_segment`, `elf_offsets`, `optional_plt`, `linux_load_alignment`, `string_table_bounds`, `executable_segments`, `elf_header`, `linux_entry_argv`, `input_magic`, `empty_tls` and `tls_function_coverage`. ### 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 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)
hikazey commented 2026-10-08 07:07:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-08T07:07:42Z.


Checked on Ubuntu 24.04, GCC 14.2:

build amd64_only_converter
this head (e8f73a55) passed
decoder and opcode constants from main, new test cases kept failed
this head merged with #1163 (same decoder file, merges without conflicts) passed

The semantics match the SDM (Vol. 2B, MOV, opcodes A0-A3): in 64-bit mode the moffs operand is 8 bytes, or 4 bytes with the 67 prefix. 66 and REX.W change only the accumulator size, not the moffs width, which is what the 0x48/0x64 0x48 cases cover. Moving 0x67 out of the skipped-prefix list into its own branch leaves prefix scanning unchanged, since it still continues the loop.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6054535595 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1168#issuecomment-6054535595). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-08T07:07:42Z. --- Checked on Ubuntu 24.04, GCC 14.2: | build | `amd64_only_converter` | | --- | --- | | this head (`e8f73a55`) | passed | | decoder and opcode constants from `main`, new test cases kept | failed | | this head merged with #1163 (same decoder file, merges without conflicts) | passed | The semantics match the SDM (Vol. 2B, MOV, opcodes A0-A3): in 64-bit mode the moffs operand is 8 bytes, or 4 bytes with the `67` prefix. `66` and REX.W change only the accumulator size, not the moffs width, which is what the `0x48`/`0x64 0x48` cases cover. Moving `0x67` out of the skipped-prefix list into its own branch leaves prefix scanning unchanged, since it still continues the loop. AI-assisted: yes (Claude Code).
adriadam10 commented 2026-10-08 08:39:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:39:56Z.


Checked: build + full ctest on this head merged onto current main (424/424), and amd64_only_converter fails with the decoder change reverted, so the new A0-A3 cases pin the fix. Prefix handling is right (0x67 only sets the flag, a REX before it is dropped, as the 48 67 A3 case pins). CI green on linux and windows; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056085650 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1168#issuecomment-6056085650). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:39:56Z. --- Checked: build + full ctest on this head merged onto current main (424/424), and `amd64_only_converter` fails with the decoder change reverted, so the new `A0`-`A3` cases pin the fix. Prefix handling is right (0x67 only sets the flag, a REX before it is dropped, as the `48 67 A3` case pins). CI green on linux and windows; merging.
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!1168
No description provided.