fix(relinker): preserve EIP-relative jump-table destinations #15639

Merged
PolkadotDom merged 1 commit from upstream-pr/15639 into main 2026-11-12 20:35:04 +00:00
PolkadotDom commented 2026-11-03 22:47:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PolkadotDom on 2026-11-03T22:47:51Z.


What

The AMD-only converter misses relative jump tables addressed by 67 LEA when the effective address wraps to 32 bits. It then treats a table destination as an instruction it can move into a conversion stub. In the regression, Rosetta conversion of a four-byte vector store consumes the following instruction even though the jump table enters it.

For example, an eight-byte LEA at 0x1000 with displacement bits 0x80000008 forms 0x80001010. Main looks for the table at 0xffffffff80001010 instead. The table's second entry targets 0x1019, which main moves into the store's stub. With this fix the converter takes the preceding instruction into the stub and leaves the destination at 0x1019 intact.

Truncate the computed table address to 32 bits when the decoder reports the address-size override. Normal RIP-relative table addresses retain their existing behavior. The architecture rule is Intel SDM Volume 2A, §2.2.1.6, reproduced in the Intel documentation changes PDF: the address-size prefix truncates and zero-extends the effective address.

Tested

Base 1c8c0efc8c; candidate 8a0ce96cba.

  • Windows 11 x86-64, GCC 12.2, Release relinker-only configuration: built relinker, amd64_only_converter_tests, amd64_only_windows_tests, relinker_conversion_tests and relinker_game_patch_tests.
  • Seven focused CTests passed: amd64_only_converter, amd64_only_windows, relinker_conversion, relinker_game_patch, relinker_executables, mulx_conversion, and to_rosetta.
  • Fresh Ubuntu/WSL build with GCC 11.4: amd64_only_converter_tests built and its CTest passed.
  • The added converter matrix checks 12 combinations: RIP/EIP addressing, normal/wrapping displacement, and no segment prefix/FS/GS. It checks the placement and the unchanged destination bytes. The RIP controls prevent incorrectly truncating all table addresses. The new regression fails on unchanged main with A jump table's address size did not protect the instruction after a 256-bit store.
  • Independent native x86-64 Linux probe: an EIP-relative LEA, signed table-entry load, add and indirect jump reached both destinations (returning 41 and 42), with no segment prefix, FS and GS. Six executions passed. Source and command below.
  • Conventions and whitespace checks passed; clean merge preview against current main.
  • The initial overlap scan covered 1,802 open PRs. Moved the new test/function call to avoid #11525's insertion points, then reran against 1,804 open PRs after verifying 1,799 cached head hashes and fetching five changed/missing heads. That November 3 scan had no line/export collision. Its exit status is 2 because #11192 and #13486 already conflict with main; their shared-file diffs were reviewed manually. #11192 changes reciprocal tests, and #13486's converter changes concern entry protection around padding; neither implements the EIP table-address fix or conflicts with the relocated test. The combined merge preview with #11525 is clean. No dependency is needed.

Synthetic code/conversion validation. No PS5 title, Apple/Rosetta execution, GPU testing or full product build was performed.

Native x86-64 jump-table probe

Compile with g++ -std=c++20 -O2 probe.cpp -o probe, then run ./probe on Linux x86-64. The mappings use MAP_FIXED_NOREPLACE and are private to the probe. It verifies the input instruction/addressing semantics; it does not execute the converter's Rosetta output.

#include <sys/mman.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>

int main() {
    constexpr std::uintptr_t codeAddress = 0x10000000;
    constexpr std::uintptr_t dataAddress = 0x90000000;
    constexpr std::uintptr_t tableAddress = dataAddress + 0x10;
    auto* code = static_cast<std::uint8_t*>(mmap(reinterpret_cast<void*>(codeAddress), 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0));
    auto* data = static_cast<std::uint8_t*>(mmap(reinterpret_cast<void*>(dataAddress), 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0));
    if (code == MAP_FAILED || data == MAP_FAILED) {
        std::perror("mmap");
        return 1;
    }
    const auto put32 = [](std::uint8_t* at, std::uint32_t value) { std::memcpy(at, &value, 4); };
    for (const std::uint8_t prefix : {0x00, 0x64, 0x65}) {
        if (mprotect(code, 4096, PROT_READ | PROT_WRITE)) return 2;
        std::vector<std::uint8_t> bytes{0x89, 0xf8};
        if (prefix) bytes.push_back(prefix);
        bytes.insert(bytes.end(), {0x67, 0x48, 0x8d, 0x0d, 0, 0, 0, 0});
        put32(bytes.data() + bytes.size() - 4, static_cast<std::uint32_t>(tableAddress - (codeAddress + bytes.size())));
        bytes.insert(bytes.end(), {0x48, 0x63, 0x04, 0x81, 0x48, 0x01, 0xc8, 0xff, 0xe0});
        std::memcpy(code, bytes.data(), bytes.size());
        for (unsigned index = 0; index < 2; ++index) {
            const auto offset = 0x20 + index * 0x10;
            code[offset] = 0xb8;
            put32(code + offset + 1, 41 + index);
            code[offset + 5] = 0xc3;
            put32(data + 0x10 + 4 * index, static_cast<std::uint32_t>(codeAddress + offset - tableAddress));
        }
        if (mprotect(code, 4096, PROT_READ | PROT_EXEC)) return 3;
        auto function = reinterpret_cast<int (*)(int)>(code);
        for (int index = 0; index < 2; ++index) {
            const auto result = function(index);
            std::printf("prefix=%02x index=%d result=%d\n", prefix, index, result);
            if (result != 41 + index) return 4;
        }
    }
    munmap(code, 4096);
    munmap(data, 4096);
}
prefix=00 index=0 result=41
prefix=00 index=1 result=42
prefix=64 index=0 result=41
prefix=64 index=1 result=42
prefix=65 index=0 result=41
prefix=65 index=1 result=42

November 9 overlap follow-up

The later #15903 (d1166c4c8e) now changes the same table-address expression. A combined git merge-tree preview reports a content conflict in Amd64OnlyConverter.cpp. These patches are not interchangeable: its unconditional negative-displacement guard skips a valid EIP-relative table before the address can wrap to 32 bits.

Verified on Windows x86-64 with GCC 12.2, using this PR's unchanged head 8a0ce96cba: built and ran amd64_only_converter successfully; temporarily inserted exactly the two displacement guards from #15903 before this PR's table calculation; rebuilt and the existing regression failed with A jump table's address size did not protect the instruction after a 256-bit store. Restored the source byte-for-byte, rebuilt, and the test passed again. This is a focused interaction test, not a build or review of all of #15903.

For the documented example, next = 0x1008 and signed displacement -0x7ffffff8 form the valid 32-bit address 0x80001010; rejecting the intermediate negative 64-bit sum loses that table. The overlap requires an explicit resolution preserving address-size wrapping and this regression. This PR does not depend on #15903 and remains independently applicable; no merge order has been agreed. No production code changed in this follow-up.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5145704058 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15639). Originally posted by [PolkadotDom](https://github.com/PolkadotDom) on 2026-11-03T22:47:51Z. --- ### What The AMD-only converter misses relative jump tables addressed by `67 LEA` when the effective address wraps to 32 bits. It then treats a table destination as an instruction it can move into a conversion stub. In the regression, Rosetta conversion of a four-byte vector store consumes the following instruction even though the jump table enters it. For example, an eight-byte LEA at `0x1000` with displacement bits `0x80000008` forms `0x80001010`. Main looks for the table at `0xffffffff80001010` instead. The table's second entry targets `0x1019`, which main moves into the store's stub. With this fix the converter takes the preceding instruction into the stub and leaves the destination at `0x1019` intact. Truncate the computed table address to 32 bits when the decoder reports the address-size override. Normal RIP-relative table addresses retain their existing behavior. The architecture rule is Intel SDM Volume 2A, §2.2.1.6, reproduced in the [Intel documentation changes PDF](https://cdrdv2-public.intel.com/851055/252046-079-sdm-change-document.pdf): the address-size prefix truncates and zero-extends the effective address. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Base `1c8c0efc8c`; candidate `8a0ce96cba`. - Windows 11 x86-64, GCC 12.2, Release relinker-only configuration: built `relinker`, `amd64_only_converter_tests`, `amd64_only_windows_tests`, `relinker_conversion_tests` and `relinker_game_patch_tests`. - Seven focused CTests passed: `amd64_only_converter`, `amd64_only_windows`, `relinker_conversion`, `relinker_game_patch`, `relinker_executables`, `mulx_conversion`, and `to_rosetta`. - Fresh Ubuntu/WSL build with GCC 11.4: `amd64_only_converter_tests` built and its CTest passed. - The added converter matrix checks 12 combinations: RIP/EIP addressing, normal/wrapping displacement, and no segment prefix/FS/GS. It checks the placement and the unchanged destination bytes. The RIP controls prevent incorrectly truncating all table addresses. The new regression fails on unchanged main with `A jump table's address size did not protect the instruction after a 256-bit store`. - Independent native x86-64 Linux probe: an EIP-relative LEA, signed table-entry load, add and indirect jump reached both destinations (returning 41 and 42), with no segment prefix, FS and GS. Six executions passed. Source and command below. - Conventions and whitespace checks passed; clean merge preview against current main. - The initial overlap scan covered 1,802 open PRs. Moved the new test/function call to avoid #11525's insertion points, then reran against 1,804 open PRs after verifying 1,799 cached head hashes and fetching five changed/missing heads. That November 3 scan had no line/export collision. Its exit status is 2 because #11192 and #13486 already conflict with main; their shared-file diffs were reviewed manually. #11192 changes reciprocal tests, and #13486's converter changes concern entry protection around padding; neither implements the EIP table-address fix or conflicts with the relocated test. The combined merge preview with #11525 is clean. No dependency is needed. Synthetic code/conversion validation. No PS5 title, Apple/Rosetta execution, GPU testing or full product build was performed. <details> <summary>Native x86-64 jump-table probe</summary> Compile with `g++ -std=c++20 -O2 probe.cpp -o probe`, then run `./probe` on Linux x86-64. The mappings use `MAP_FIXED_NOREPLACE` and are private to the probe. It verifies the input instruction/addressing semantics; it does not execute the converter's Rosetta output. ```cpp #include <sys/mman.h> #include <cstdint> #include <cstdio> #include <cstring> #include <vector> int main() { constexpr std::uintptr_t codeAddress = 0x10000000; constexpr std::uintptr_t dataAddress = 0x90000000; constexpr std::uintptr_t tableAddress = dataAddress + 0x10; auto* code = static_cast<std::uint8_t*>(mmap(reinterpret_cast<void*>(codeAddress), 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0)); auto* data = static_cast<std::uint8_t*>(mmap(reinterpret_cast<void*>(dataAddress), 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0)); if (code == MAP_FAILED || data == MAP_FAILED) { std::perror("mmap"); return 1; } const auto put32 = [](std::uint8_t* at, std::uint32_t value) { std::memcpy(at, &value, 4); }; for (const std::uint8_t prefix : {0x00, 0x64, 0x65}) { if (mprotect(code, 4096, PROT_READ | PROT_WRITE)) return 2; std::vector<std::uint8_t> bytes{0x89, 0xf8}; if (prefix) bytes.push_back(prefix); bytes.insert(bytes.end(), {0x67, 0x48, 0x8d, 0x0d, 0, 0, 0, 0}); put32(bytes.data() + bytes.size() - 4, static_cast<std::uint32_t>(tableAddress - (codeAddress + bytes.size()))); bytes.insert(bytes.end(), {0x48, 0x63, 0x04, 0x81, 0x48, 0x01, 0xc8, 0xff, 0xe0}); std::memcpy(code, bytes.data(), bytes.size()); for (unsigned index = 0; index < 2; ++index) { const auto offset = 0x20 + index * 0x10; code[offset] = 0xb8; put32(code + offset + 1, 41 + index); code[offset + 5] = 0xc3; put32(data + 0x10 + 4 * index, static_cast<std::uint32_t>(codeAddress + offset - tableAddress)); } if (mprotect(code, 4096, PROT_READ | PROT_EXEC)) return 3; auto function = reinterpret_cast<int (*)(int)>(code); for (int index = 0; index < 2; ++index) { const auto result = function(index); std::printf("prefix=%02x index=%d result=%d\n", prefix, index, result); if (result != 41 + index) return 4; } } munmap(code, 4096); munmap(data, 4096); } ``` ```text prefix=00 index=0 result=41 prefix=00 index=1 result=42 prefix=64 index=0 result=41 prefix=64 index=1 result=42 prefix=65 index=0 result=41 prefix=65 index=1 result=42 ``` </details> ### November 9 overlap follow-up The later #15903 (`d1166c4c8e`) now changes the same table-address expression. A combined `git merge-tree` preview reports a content conflict in `Amd64OnlyConverter.cpp`. These patches are not interchangeable: its unconditional negative-displacement guard skips a valid EIP-relative table before the address can wrap to 32 bits. Verified on Windows x86-64 with GCC 12.2, using this PR's unchanged head `8a0ce96cba`: built and ran `amd64_only_converter` successfully; temporarily inserted exactly the two displacement guards from #15903 before this PR's table calculation; rebuilt and the existing regression failed with `A jump table's address size did not protect the instruction after a 256-bit store`. Restored the source byte-for-byte, rebuilt, and the test passed again. This is a focused interaction test, not a build or review of all of #15903. For the documented example, `next = 0x1008` and signed displacement `-0x7ffffff8` form the valid 32-bit address `0x80001010`; rejecting the intermediate negative 64-bit sum loses that table. The overlap requires an explicit resolution preserving address-size wrapping and this regression. This PR does not depend on #15903 and remains independently applicable; no merge order has been agreed. No production code changed in this follow-up. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
BunnyFl commented 2026-11-04 01:53:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-04T01:53:22Z.

Review: APPROVED


@PolkadotDom thanks, the fix looks right to me. With a 67 prefix in 64-bit mode, the RIP-relative form becomes EIP-relative: next-instruction address plus disp32, truncated to 32 bits (Intel SDM Vol. 2A, §2.2.1.6). Casting the sum to std::uint32_t (Amd64OnlyConverter.cpp:327) does that and leaves the plain RIP case unchanged. lea computes only the offset and ignores the segment (SDM Vol. 2A, LEA), and the FS and GS rows in the new test (Amd64OnlyConverterTests.cpp:117) expect the same placement as the unprefixed ones. The 0x3000 rows check that RIP-relative tables still resolve, and the 0x80001010 row without 67 checks that wrapping isn't applied to them.

What I ran: head 8a0ce96cba merged onto main 1c8c0efc8c, Windows 11, WinLibs GCC, relinker-only Release build:

  • amd64_only_converter, amd64_only_windows, relinker_conversion, mulx_conversion and to_rosetta pass (5 of 5);
  • negative control, Amd64OnlyConverter.cpp taken from main with the new test kept: amd64_only_converter fails with "A jump table's address size did not protect the instruction after a 256-bit store";
  • tools/check_conventions.py --base upstream/main on the head: all checks passed;
  • tools/pr_overlap.py --branch: no shared lines or exports. The tool reported #11192 and #13486 as conflicting with main and left them uncompared. #13486's diff changes no lines of src/Amd64OnlyConverter.cpp, and #11192 touches only the test file, as you found.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5762113498 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15639#pullrequestreview-5762113498). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-04T01:53:22Z. **Review:** APPROVED --- @PolkadotDom thanks, the fix looks right to me. With a `67` prefix in 64-bit mode, the RIP-relative form becomes EIP-relative: next-instruction address plus `disp32`, truncated to 32 bits (Intel SDM Vol. 2A, §2.2.1.6). Casting the sum to `std::uint32_t` (`Amd64OnlyConverter.cpp:327`) does that and leaves the plain RIP case unchanged. `lea` computes only the offset and ignores the segment (SDM Vol. 2A, LEA), and the FS and GS rows in the new test (`Amd64OnlyConverterTests.cpp:117`) expect the same placement as the unprefixed ones. The `0x3000` rows check that RIP-relative tables still resolve, and the `0x80001010` row without `67` checks that wrapping isn't applied to them. **What I ran:** head `8a0ce96cba` merged onto `main` `1c8c0efc8c`, Windows 11, WinLibs GCC, relinker-only Release build: - `amd64_only_converter`, `amd64_only_windows`, `relinker_conversion`, `mulx_conversion` and `to_rosetta` pass (5 of 5); - negative control, `Amd64OnlyConverter.cpp` taken from `main` with the new test kept: `amd64_only_converter` fails with "A jump table's address size did not protect the instruction after a 256-bit store"; - `tools/check_conventions.py --base upstream/main` on the head: all checks passed; - `tools/pr_overlap.py --branch`: no shared lines or exports. The tool reported #11192 and #13486 as conflicting with `main` and left them uncompared. #13486's diff changes no lines of `src/Amd64OnlyConverter.cpp`, and #11192 touches only the test file, as you found. Approving.
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!15639
No description provided.