From ad9b9c5165fd018b2d253aa314b592fc4a45a04f Mon Sep 17 00:00:00 2001 From: Kirill Zhumarin Date: Mon, 7 Sep 2026 22:44:56 +0300 Subject: [PATCH] feat(xref): expand ARM and C166 references --- JS.md | 3 +- README.md | 11 ++- lib/src/Arch.cpp | 218 ++++++++++++++++++++++++++++++++------------ lib/src/Arch.h | 7 +- lib/src/Pattern.cpp | 28 ++++-- package.json | 2 +- src/tests.cpp | 81 ++++++++++++++++ test/index.test.ts | 26 ++++++ 8 files changed, 304 insertions(+), 72 deletions(-) diff --git a/JS.md b/JS.md index 2cd23b7..1d680a4 100644 --- a/JS.md +++ b/JS.md @@ -99,7 +99,8 @@ findXRefs(address: number, limit?: number): Ptr89XRef[]; ``` Finds branches, decoded references and stored pointers to a 32-bit address. -The default limit is `100`; a limit of `0` disables it: +C166 code and data pointers are both supported. The default limit is `100`; a +limit of `0` disables it: ```ts type Ptr89XRefType = "pointer" | "reference" | "branch"; diff --git a/README.md b/README.md index 0ecfe80..a4ad7dd 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ Found 3 xrefs: Search done in 1612 ms ``` +ARM x-refs include ARM/THUMB branches, `LDR`/`ADR` references, and 32-bit pointers. C166 x-refs include branches, large-model `SOF`/`SEG` and `POF`/`PAG` register pairs, and stored code or data pointers. + ### Find patterns ```bash $ ptr89 -f EL71v45.bin -p "F0B5061C0C1C151C85B068461122??49??????????E0207869466A460009085C307021780134" @@ -261,7 +263,7 @@ Steps: 3. Result is `0xA8D837B8 + 0x2 = 0xA8D837BA` ## Decode as reference -Emulating ARM/THUMB `LDR Rd, [PC, #offset]` instruction found by the pattern. +Emulating ARM/THUMB `LDR Rd, [PC, #offset]` or `ADR Rd, address` instruction found by the pattern. The `&(...)` operator remains ARM-specific. C166 register-pair references are supported by the `LDR{...}` nested form described below. @@ -413,10 +415,10 @@ Follow the reference and checking it for a pattern. Syntax: ```bash -# ARM LDR or the first MOV of a C166 pointer pair (4-byte instruction) +# ARM LDR/ADR or the first MOV of a C166 pointer pair (4-byte instruction) LDR{ subPattern } -# THUMB LDR (2-byte instruction; not supported for C166) +# THUMB LDR/ADR or the first 2-byte MOV of a C166 pointer pair LDR[ subPattern ] ``` @@ -424,6 +426,7 @@ Supported instructions: ``` # ARM/THUMB LDR Rd, [PC, #offset] +ADR Rd, address # C166 large model: huge/code pointer MOV Rn, #SOF(target) @@ -434,7 +437,7 @@ MOV Rn, #POF(target) MOV Rn+1, #PAG(target) ``` -The two C166 `MOV` instructions must be adjacent and `Rn` must be even. `LDR{...}` occupies the first four-byte `MOV`; the following `SEG`/`PAG` load is inspected to reconstruct the linear address. When the raw operands are valid both as a far data pointer and as a huge/code pointer, the nested pattern is checked at both addresses. +The two C166 `MOV` instructions must be adjacent and `Rn` must be even. Use `LDR{...}` for a four-byte first `MOV` and `LDR[...]` for a two-byte first `MOV`. The following `SEG`/`PAG` load is inspected to reconstruct the linear address. When the operands are valid both as a far data pointer and as a huge/code pointer, the nested pattern is checked at both addresses. ARM: ```bash diff --git a/lib/src/Arch.cpp b/lib/src/Arch.cpp index b45777e..122e42c 100644 --- a/lib/src/Arch.cpp +++ b/lib/src/Arch.cpp @@ -1,5 +1,6 @@ #include "Arch.h" +#include #include #include @@ -92,9 +93,12 @@ static std::pair decodeThumbB(uint32_t address, const uint8_t *b return { true, target }; } if ((instruction & 0xF000) == 0xD000) { + uint32_t condition = (instruction & 0x0F00) >> 8; + if (condition >= 0xE) + return { false, 0 }; + int32_t displacement = static_cast(signExtend(instruction & 0xFF, 8, 32) << 1); uint32_t target = address + 4 + displacement; - uint32_t condition = (instruction & 0x0F00) >> 8; if (spdlog::should_log(spdlog::level::debug)) { spdlog::debug("{:08X}: {:02X} {:02X} B{} #0x{:08X}", address, bytes[0], bytes[1], MNEMONICS[condition], target); @@ -119,12 +123,28 @@ static std::pair decodeThumbLDR(uint32_t address, const uint8_t return { true, target }; } +static std::pair decodeThumbADR(uint32_t address, const uint8_t *bytes) { + uint16_t instruction = (bytes[1] << 8) | bytes[0]; + if ((address % 2) != 0 || (instruction & 0xF800) != 0xA000) + return { false, 0 }; + + uint32_t displacement = (instruction & 0xFF) << 2; + uint32_t reg = (instruction & 0x700) >> 8; + uint32_t target = ((address + 4) & ~3U) + displacement; + if (spdlog::should_log(spdlog::level::debug)) { + spdlog::debug("{:08X}: {:02X} {:02X} ADR {}, #0x{:X} ; 0x{:08X}", + address, bytes[0], bytes[1], REGNAMES[reg], displacement, target); + } + return { true, target }; +} + static std::tuple decodeArmLDR(uint32_t address, const uint8_t *bytes) { uint32_t instruction = static_cast(bytes[0]) | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | (static_cast(bytes[3]) << 24); - if ((address % 4) != 0 || (instruction & 0xE0F0000) != 0x40F0000) + if ((address % 4) != 0 || (instruction & 0x0F7F0000) != 0x051F0000 || + (instruction & 0xF0000000) == 0xF0000000) return { false, 0, false }; bool add = (instruction & (1U << 23)) != 0; @@ -140,6 +160,68 @@ static std::tuple decodeArmLDR(uint32_t address, const uin return { true, target, reg == 0xF }; } +static std::pair decodeArmADR(uint32_t address, const uint8_t *bytes) { + uint32_t instruction = static_cast(bytes[0]) | + (static_cast(bytes[1]) << 8) | + (static_cast(bytes[2]) << 16) | + (static_cast(bytes[3]) << 24); + uint32_t opcode = instruction & 0x0FF00000; + if ((address % 4) != 0 || (opcode != 0x02800000 && opcode != 0x02400000) || + (instruction & 0x000F0000) != 0x000F0000 || (instruction & 0x0000F000) == 0x0000F000 || + (instruction & 0xF0000000) == 0xF0000000) + return { false, 0 }; + + uint32_t immediate = std::rotr(instruction & 0xFF, ((instruction >> 8) & 0xF) * 2); + bool add = opcode == 0x02800000; + uint32_t target = add ? address + 8 + immediate : address + 8 - immediate; + uint32_t reg = (instruction & 0xF000) >> 12; + uint32_t condition = instruction >> 28; + if (spdlog::should_log(spdlog::level::debug)) { + spdlog::debug("{:08X}: {:02X} {:02X} {:02X} {:02X} {}{} {}, PC, #0x{:X} ; 0x{:08X}", address, + bytes[0], bytes[1], bytes[2], bytes[3], add ? "ADD" : "SUB", MNEMONICS[condition], + REGNAMES[reg], immediate, target); + } + return { true, target }; +} + +static std::tuple decodeC166Mov(const uint8_t *bytes, size_t size) { + if (size >= 4 && bytes[0] == 0xE6 && bytes[1] >= 0xF0) + return { true, bytes[1], bytes[2] | (static_cast(bytes[3]) << 8), 4 }; + if (size >= 2 && bytes[0] == 0xE0) + return { true, static_cast(0xF0 | (bytes[1] & 0x0F)), bytes[1] >> 4, 2 }; + return { false, 0, 0, 0 }; +} + +static std::vector decodeC166Pair(uint32_t address, const uint8_t *bytes, + const Pattern::Memory &memory, size_t firstSize) { + if ((address & 1) != 0 || !Pattern::inMemory(memory, address, firstSize + 2)) + return { }; + + size_t available = memory.base + memory.size - address; + auto [hasLow, lowReg, low, lowSize] = decodeC166Mov(bytes, available); + if (!hasLow || lowSize != firstSize || (lowReg & 1) != 0) + return { }; + + bool hasHigh; + uint8_t highReg; + uint32_t high; + std::tie(hasHigh, highReg, high, std::ignore) = decodeC166Mov(bytes + lowSize, available - lowSize); + if (!hasHigh || highReg != lowReg + 1) + return { }; + + std::vector references; + // Huge/code pointer: 16-bit segment offset and 8-bit segment number. + if (high < 0x100) + references.push_back((high << 16) | low); + // Far data pointer: 14-bit page offset and 10-bit page number. + if (low < 0x4000 && high < 0x400) { + uint32_t target = (high << 14) | low; + if (references.empty() || references.front() != target) + references.push_back(target); + } + return references; +} + class ArmArch final: public Arch { public: std::pair decodeBranch2(uint32_t address, const uint8_t *bytes, const Pattern::Memory &) const override { @@ -167,42 +249,45 @@ class ArmArch final: public Arch { std::vector decodeReferences2(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const override { auto [isLdr, pointerAddress] = decodeThumbLDR(address, bytes); - if (!isLdr) - return { }; - auto [success, pointer] = decodePointer(pointerAddress, memory); - return success ? std::vector{ pointer } : std::vector{ }; + if (isLdr) { + auto [success, pointer] = decodePointer(pointerAddress, memory); + return success ? std::vector{ pointer } : std::vector{ }; + } + + auto [isAdr, target] = decodeThumbADR(address, bytes); + return isAdr ? std::vector{ target } : std::vector{ }; } std::vector decodeReferences4(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const override { auto ldr = decodeArmLDR(address, bytes); - if (!std::get<0>(ldr)) - return { }; - auto [success, pointer] = decodePointer(std::get<1>(ldr), memory); - return success ? std::vector{ pointer } : std::vector{ }; + if (std::get<0>(ldr)) { + auto [success, pointer] = decodePointer(std::get<1>(ldr), memory); + return success ? std::vector{ pointer } : std::vector{ }; + } + + auto [isAdr, target] = decodeArmADR(address, bytes); + return isAdr ? std::vector{ target } : std::vector{ }; } std::tuple decodeReference(uint32_t offset, const Pattern::Memory &memory) const override { - offset &= ~1U; - if (offset + 2 > memory.size) - return { false, 0, 0 }; - - if (offset + 4 <= memory.size) { - auto references = decodeReferences4(memory.base + offset, memory.data + offset, memory); - if (!references.empty()) - return { true, references.front(), 4 }; - } - - auto references = decodeReferences2(memory.base + offset, memory.data + offset, memory); + auto [references, size] = decodeReferences(offset, memory); return references.empty() ? - std::make_tuple(false, 0U, 0U) : - std::make_tuple(true, references.front(), 2U); + std::make_tuple(false, 0U, size_t{ 0 }) : + std::make_tuple(true, references.front(), size); } std::tuple decodeBranchReference(uint32_t offset, const Pattern::Memory &memory) const override { - if (offset + 4 > memory.size) + if (offset + 2 > memory.size) return { false, 0, 0 }; uint32_t address = memory.base + offset; + auto [isThumbB, thumbBAddress] = decodeThumbB(address, memory.data + offset); + if (isThumbB && Pattern::inMemory(memory, thumbBAddress, 2)) + return { true, resolveThunks(thumbBAddress, memory) | 1U, 2 }; + + if (offset + 4 > memory.size) + return { false, 0, 0 }; + auto [isThumb, thumbAddress, isThumbBLX] = decodeThumbBL(address, memory.data + offset); if (isThumb && Pattern::inMemory(memory, thumbAddress, 4)) return { true, resolveThunks(thumbAddress, memory) | (!isThumbBLX ? 1U : 0U), 4 }; @@ -221,16 +306,17 @@ class ArmArch final: public Arch { return { false, 0, 0 }; } - std::pair decodePointer(uint32_t address, const Pattern::Memory &memory) const override { + size_t decodePointers(uint32_t address, const Pattern::Memory &memory, + std::array &pointers) const override { if (!Pattern::inMemory(memory, address, 4)) - return { false, 0 }; + return 0; const uint8_t *bytes = memory.data + (address - memory.base); - uint32_t value = bytes[0] | + pointers[0] = bytes[0] | (static_cast(bytes[1]) << 8) | (static_cast(bytes[2]) << 16) | (static_cast(bytes[3]) << 24); - return { true, value }; + return 1; } uint32_t resolveThunks(uint32_t address, const Pattern::Memory &memory) const override { @@ -261,6 +347,10 @@ class ArmArch final: public Arch { return address; } + bool sameAddress(uint32_t first, uint32_t second) const override { + return (first & ~1U) == (second & ~1U); + } + int reference4Align() const override { return 4; } @@ -320,36 +410,13 @@ class C166Arch final: public Arch { return { true, target }; } - std::vector decodeReferences2(uint32_t, const uint8_t *, const Pattern::Memory &) const override { - return { }; + std::vector decodeReferences2(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const override { + return decodeC166Pair(address, bytes, memory, 2); } std::vector decodeReferences4(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const override { - if ((address & 1) != 0 || !Pattern::inMemory(memory, address, 8)) - return { }; - // TASKING large model: MOV Rn, #POF/SOF(target); MOV Rn+1, #PAG/SEG(target). - uint8_t reg = bytes[1]; - if (bytes[0] != 0xE6 || reg < 0xF0 || reg >= 0xFF || (reg & 1) != 0 || - bytes[4] != 0xE6 || bytes[5] != reg + 1) - return { }; - - uint32_t low = bytes[2] | (static_cast(bytes[3]) << 8); - uint32_t high = bytes[6] | (static_cast(bytes[7]) << 8); - std::vector references; - - // Huge/code pointer: 16-bit segment offset and 8-bit segment number. - if (high < 0x100) - references.push_back((high << 16) | low); - - // Far data pointer: 14-bit page offset and 10-bit page number. - if (low < 0x4000 && high < 0x400) { - uint32_t target = (high << 14) | low; - if (references.empty() || references.front() != target) - references.push_back(target); - } - - return references; + return decodeC166Pair(address, bytes, memory, 4); } std::tuple decodeReference(uint32_t, const Pattern::Memory &) const override { @@ -374,14 +441,24 @@ class C166Arch final: public Arch { return { false, 0, 0 }; } - std::pair decodePointer(uint32_t address, const Pattern::Memory &memory) const override { + size_t decodePointers(uint32_t address, const Pattern::Memory &memory, + std::array &pointers) const override { if (!Pattern::inMemory(memory, address, 4)) - return { false, 0 }; + return 0; const uint8_t *bytes = memory.data + (address - memory.base); uint32_t segmentOffset = bytes[0] | (static_cast(bytes[1]) << 8); uint32_t segment = bytes[2]; - return { true, (segment << 16) | segmentOffset }; + pointers[0] = (segment << 16) | segmentOffset; + size_t count = 1; + + uint32_t page = bytes[2] | (static_cast(bytes[3]) << 8); + if (segmentOffset < 0x4000 && page < 0x400) { + uint32_t pointer = (page << 14) | segmentOffset; + if (pointers[0] != pointer) + pointers[count++] = pointer; + } + return count; } uint32_t resolveThunks(uint32_t address, const Pattern::Memory &memory) const override { @@ -410,6 +487,10 @@ class C166Arch final: public Arch { return address; } + bool sameAddress(uint32_t first, uint32_t second) const override { + return first == second; + } + int reference4Align() const override { return 2; } @@ -417,6 +498,29 @@ class C166Arch final: public Arch { }; // namespace +std::pair, size_t> Arch::decodeReferences(uint32_t offset, const Pattern::Memory &memory) const { + if (offset + 4 <= memory.size) { + auto references = decodeReferences4(memory.base + offset, memory.data + offset, memory); + if (!references.empty()) + return { references, 4 }; + } + + if (offset + 2 <= memory.size) { + auto references = decodeReferences2(memory.base + offset, memory.data + offset, memory); + if (!references.empty()) + return { references, 2 }; + } + return { { }, 0 }; +} + +std::pair Arch::decodePointer(uint32_t address, const Pattern::Memory &memory) const { + std::array pointers; + size_t count = decodePointers(address, memory, pointers); + return count == 0 ? + std::make_pair(false, 0U) : + std::make_pair(true, pointers[0]); +} + const Arch &getArch(Architecture architecture) { static const ArmArch arm; static const C166Arch c166; diff --git a/lib/src/Arch.h b/lib/src/Arch.h index fac4454..2740468 100644 --- a/lib/src/Arch.h +++ b/lib/src/Arch.h @@ -1,6 +1,7 @@ #pragma once #include "Pattern.h" +#include namespace Ptr89 { @@ -12,11 +13,15 @@ class Arch { virtual std::pair decodeBranch4(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const = 0; virtual std::vector decodeReferences2(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const = 0; virtual std::vector decodeReferences4(uint32_t address, const uint8_t *bytes, const Pattern::Memory &memory) const = 0; + std::pair, size_t> decodeReferences(uint32_t offset, const Pattern::Memory &memory) const; virtual std::tuple decodeReference(uint32_t offset, const Pattern::Memory &memory) const = 0; virtual std::tuple decodeBranchReference(uint32_t offset, const Pattern::Memory &memory) const = 0; - virtual std::pair decodePointer(uint32_t address, const Pattern::Memory &memory) const = 0; + virtual size_t decodePointers(uint32_t address, const Pattern::Memory &memory, + std::array &pointers) const = 0; + std::pair decodePointer(uint32_t address, const Pattern::Memory &memory) const; virtual uint32_t resolveThunks(uint32_t address, const Pattern::Memory &memory) const = 0; virtual uint32_t offsetValue(uint32_t address, uint32_t offset, const Pattern::Memory &memory) const = 0; + virtual bool sameAddress(uint32_t first, uint32_t second) const = 0; virtual int reference4Align() const = 0; }; diff --git a/lib/src/Pattern.cpp b/lib/src/Pattern.cpp index 2fbeae9..96875a8 100644 --- a/lib/src/Pattern.cpp +++ b/lib/src/Pattern.cpp @@ -429,23 +429,35 @@ std::vector Pattern::finXRefs(uint32_t addr, const Memory &memory if (debug) spdlog::debug("Searching XRef's for {:08X}", addr); std::vector searchResults; + const Arch &arch = getArch(memory.arch); + auto hasAddress = [&arch, addr](const uint32_t *addresses, size_t count) { + for (size_t i = 0; i < count; i++) { + if (arch.sameAddress(addresses[i], addr)) + return true; + } + return false; + }; for (size_t i = 0; i < memory.size; i += 2) { - bool isReference; - uint32_t refAddr; - std::tie(isReference, refAddr, std::ignore) = decodeReference(i, memory); + auto decodedReferences = arch.decodeReferences(i, memory); + const auto &references = decodedReferences.first; bool isBranchReference; uint32_t branchAddr; - std::tie(isBranchReference, branchAddr, std::ignore) = decodeBranchReference(i, memory); - auto [isPointer, ptrAddr] = decodePointer(i + memory.base, memory); - if (isBranchReference && (branchAddr & ~1) == (addr & ~1)) { + size_t branchSize; + std::tie(isBranchReference, branchAddr, branchSize) = decodeBranchReference(i, memory); + std::array pointers; + size_t pointerCount = arch.decodePointers(i + memory.base, memory, pointers); + bool matchesBranch = branchSize == 2 ? + branchAddr == addr : + arch.sameAddress(branchAddr, addr); + if (isBranchReference && matchesBranch) { if (debug) spdlog::debug("FOUND: branch at {:08X}", i + memory.base); searchResults.push_back({ RESULT_TYPE_BRANCH, static_cast(memory.base + i), static_cast(i) }); - } else if (isReference && (refAddr & ~1) == (addr & ~1)) { + } else if (hasAddress(references.data(), references.size())) { if (debug) spdlog::debug("FOUND: reference at {:08X}", i + memory.base); searchResults.push_back({ RESULT_TYPE_REFERENCE, static_cast(memory.base + i), static_cast(i) }); - } else if (isPointer && (ptrAddr & ~1) == (addr & ~1)) { + } else if (hasAddress(pointers.data(), pointerCount)) { if (debug) spdlog::debug("FOUND: pointer at {:08X}", i + memory.base); searchResults.push_back({ RESULT_TYPE_POINTER, static_cast(memory.base + i), static_cast(i) }); diff --git a/package.json b/package.json index cb2f7e1..b97cf36 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sie-js/ptr89", - "version": "3.0.2", + "version": "3.0.3", "description": "ARM/THUMB/C166 binary pattern finder", "type": "module", "main": "dist/index.js", diff --git a/src/tests.cpp b/src/tests.cpp index 5eaf54a..987328f 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -81,6 +81,8 @@ static void testArmDecoder() { // 00 01 1F E5: LDR R0, [PC, #-0x100] -> 0xA0000008 assert(arch.decodeReferences4(0xA0000100, I({ 0x00, 0x01, 0x1F, 0xE5 }), memory) == std::vector{ pointer }); + // 00 00 8F E5: STR R0, [PC], not a reference load. + assert(arch.decodeReferences4(0xA0000000, I({ 0x00, 0x00, 0x8F, 0xE5 }), memory).empty()); // 00 F1 9F E5: LDR PC, [PC, #0x100] -> 0xA0000108 assert(arch.decodeBranch4(0xA0000000, I({ 0x00, 0xF1, 0x9F, 0xE5 }), memory) == std::make_pair(true, pointer)); @@ -94,6 +96,43 @@ static void testArmDecoder() { // 00 F0 FE EF: BLX 0xA0001000; ARM function pointer is 0xA0001000. data[0] = 0x00; data[1] = 0xF0; data[2] = 0xFE; data[3] = 0xEF; assert(arch.decodeBranchReference(0, memory) == std::make_tuple(true, 0xA0001000U, 4U)); + + std::vector xrefBytes(0x50); + uint32_t xrefTarget = 0xA0000040; + xrefBytes[0x00] = 0x0E; xrefBytes[0x01] = 0x00; xrefBytes[0x02] = 0x00; xrefBytes[0x03] = 0xEB; // BL 0xA0000040 + xrefBytes[0x04] = 0x41; xrefBytes[0x05] = 0x00; xrefBytes[0x06] = 0x00; xrefBytes[0x07] = 0xA0; // Pointer to 0xA0000041 + xrefBytes[0x08] = 0x10; xrefBytes[0x09] = 0x00; xrefBytes[0x0A] = 0x9F; xrefBytes[0x0B] = 0xE5; // LDR R0, [PC, #0x10] -> 0xA0000020 + xrefBytes[0x0C] = 0x2C; xrefBytes[0x0D] = 0x00; xrefBytes[0x0E] = 0x8F; xrefBytes[0x0F] = 0xE2; // ADR R0, 0xA0000040 + xrefBytes[0x10] = 0x16; xrefBytes[0x11] = 0xE0; // B 0xA0000040 + xrefBytes[0x12] = 0x0B; xrefBytes[0x13] = 0xA0; // ADR R0, 0xA0000040 + xrefBytes[0x14] = 0x03; xrefBytes[0x15] = 0x48; // LDR R0, [PC, #0xC] -> 0xA0000024 + xrefBytes[0x20] = 0x41; xrefBytes[0x21] = 0x00; xrefBytes[0x22] = 0x00; xrefBytes[0x23] = 0xA0; // Pointer to 0xA0000041 + xrefBytes[0x24] = 0x41; xrefBytes[0x25] = 0x00; xrefBytes[0x26] = 0x00; xrefBytes[0x27] = 0xA0; // Pointer to 0xA0000041 + Pattern::Memory xrefMemory = { 0xA0000000, xrefBytes.data(), xrefBytes.size(), 1, ARCH_ARM }; + auto xrefs = Pattern::finXRefs(xrefTarget, xrefMemory); + auto hasXRef = [&xrefs](ResultType type, uint32_t address) { + for (const auto &xref: xrefs) { + if (xref.type == type && xref.address == address) + return true; + } + return false; + }; + assert(hasXRef(RESULT_TYPE_BRANCH, 0xA0000000)); + assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000004)); + assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000008)); + assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA000000C)); + assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000012)); + assert(hasXRef(RESULT_TYPE_REFERENCE, 0xA0000014)); + assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000020)); + assert(hasXRef(RESULT_TYPE_POINTER, 0xA0000024)); + auto thumbXRefs = Pattern::finXRefs(xrefTarget | 1U, xrefMemory); + assert(!hasXRef(RESULT_TYPE_BRANCH, 0xA0000010)); + bool hasThumbBranch = false; + for (const auto &xref: thumbXRefs) { + if (xref.type == RESULT_TYPE_BRANCH && xref.address == 0xA0000010) + hasThumbBranch = true; + } + assert(hasThumbBranch); } static void testC166Decoder() { @@ -204,6 +243,48 @@ static void testC166Decoder() { auto c166Results = Pattern::find(pattern, c166Memory, 1); assert(c166Results.size() == 1 && c166Results[0].address == 0x200000 && c166Results[0].size == 2); + std::vector xrefBytes(0x40, 0xCC); + uint32_t codeTarget = 0x250020; + uint32_t dataTarget = 0x5FBDA0; + xrefBytes[0x00] = 0xDA; xrefBytes[0x01] = 0x25; xrefBytes[0x02] = 0x20; xrefBytes[0x03] = 0x00; // CALLS 0x25, 0x0020 + xrefBytes[0x04] = 0x20; xrefBytes[0x05] = 0x00; xrefBytes[0x06] = 0x25; xrefBytes[0x07] = 0x00; // Code pointer 0x25:0x0020 + xrefBytes[0x08] = 0xE6; xrefBytes[0x09] = 0xF4; xrefBytes[0x0A] = 0x20; xrefBytes[0x0B] = 0x00; // MOV R4, #SOF(0x250020) + xrefBytes[0x0C] = 0xE6; xrefBytes[0x0D] = 0xF5; xrefBytes[0x0E] = 0x25; xrefBytes[0x0F] = 0x00; // MOV R5, #SEG(0x250020) + xrefBytes[0x10] = 0xA0; xrefBytes[0x11] = 0x3D; xrefBytes[0x12] = 0x7E; xrefBytes[0x13] = 0x01; // Data pointer 0x017E:0x3DA0 + xrefBytes[0x14] = 0xE6; xrefBytes[0x15] = 0xF4; xrefBytes[0x16] = 0xA0; xrefBytes[0x17] = 0x3D; // MOV R4, #POF(0x5FBDA0) + xrefBytes[0x18] = 0xE6; xrefBytes[0x19] = 0xF5; xrefBytes[0x1A] = 0x7E; xrefBytes[0x1B] = 0x01; // MOV R5, #PAG(0x5FBDA0) + Pattern::Memory xrefMemory = { 0x250000, xrefBytes.data(), xrefBytes.size(), 1, ARCH_C166 }; + auto codeXRefs = Pattern::finXRefs(codeTarget, xrefMemory); + auto dataXRefs = Pattern::finXRefs(dataTarget, xrefMemory); + auto hasXRef = [](const std::vector &xrefs, ResultType type, uint32_t address) { + for (const auto &xref: xrefs) { + if (xref.type == type && xref.address == address) + return true; + } + return false; + }; + assert(hasXRef(codeXRefs, RESULT_TYPE_BRANCH, 0x250000)); + assert(hasXRef(codeXRefs, RESULT_TYPE_POINTER, 0x250004)); + assert(hasXRef(codeXRefs, RESULT_TYPE_REFERENCE, 0x250008)); + assert(hasXRef(dataXRefs, RESULT_TYPE_POINTER, 0x250010)); + assert(hasXRef(dataXRefs, RESULT_TYPE_REFERENCE, 0x250014)); + assert(Pattern::finXRefs(codeTarget | 1U, xrefMemory).empty()); + std::array pointers; + assert(arch.decodePointers(0x250010, xrefMemory, pointers) == 2); + assert(pointers[0] == 0x7E3DA0 && pointers[1] == dataTarget); + + std::vector shortMovBytes(8); + Pattern::Memory shortMovMemory = { 0x50000, shortMovBytes.data(), shortMovBytes.size(), 1, ARCH_C166 }; + // E0 A4: MOV R4, #0xA; E0 55: MOV R5, #0x5. + assert(arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE0, 0x55 }), shortMovMemory) == + std::vector({ 0x5000A, 0x1400A })); + // E0 A4: MOV R4, #0xA; E6 F5 05 00: MOV R5, #0x5. + assert(arch.decodeReferences2(0x50000, I({ 0xE0, 0xA4, 0xE6, 0xF5, 0x05, 0x00 }), shortMovMemory) == + std::vector({ 0x5000A, 0x1400A })); + // E6 F4 0A 00: MOV R4, #0xA; E0 55: MOV R5, #0x5. + assert(arch.decodeReferences4(0x50000, I({ 0xE6, 0xF4, 0x0A, 0x00, 0xE0, 0x55 }), shortMovMemory) == + std::vector({ 0x5000A, 0x1400A })); + } static void testArmPattern() { diff --git a/test/index.test.ts b/test/index.test.ts index c2e22c9..b2ee50c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -109,6 +109,32 @@ describe("Ptr89 WASM", () => { ptr89.close(); }); + it("finds C166 code and data x-refs", async () => { + const memory = new Uint8Array(0x40).fill(0xCC); + memory.set([0xDA, 0xFF, 0xE0, 0xFF], 0x00); // CALLS 0xFF, 0xFFE0 + memory.set([0xE0, 0xFF, 0xFF, 0x00], 0x04); // Code pointer 0xFF:0xFFE0 + memory.set([0xE6, 0xF4, 0xE0, 0xFF], 0x08); // MOV R4, #SOF(0xFFFFE0) + memory.set([0xE6, 0xF5, 0xFF, 0x00], 0x0C); // MOV R5, #SEG(0xFFFFE0) + memory.set([0xA0, 0x3D, 0x7E, 0x01], 0x10); // Data pointer 0x017E:0x3DA0 + memory.set([0xE6, 0xF4, 0xA0, 0x3D], 0x14); // MOV R4, #POF(0x5FBDA0) + memory.set([0xE6, 0xF5, 0x7E, 0x01], 0x18); // MOV R5, #PAG(0x5FBDA0) + const ptr89 = new Ptr89(); + await ptr89.open(memory, { arch: "c166" }); + + expect(ptr89.findXRefs(0xFFFFE0, 10)).toEqual(expect.arrayContaining([ + { type: "branch", xref: 0xFFFFC0, offset: 0x00 }, + { type: "pointer", xref: 0xFFFFC4, offset: 0x04 }, + { type: "reference", xref: 0xFFFFC8, offset: 0x08 }, + ])); + expect(ptr89.findXRefs(0x5FBDA0, 10)).toEqual(expect.arrayContaining([ + { type: "pointer", xref: 0xFFFFD0, offset: 0x10 }, + { type: "reference", xref: 0xFFFFD4, offset: 0x14 }, + ])); + expect(ptr89.findXRefs(0xFFFFE1, 10)).toEqual([]); + + ptr89.close(); + }); + it("reopens memory", async () => { const ptr89 = new Ptr89(); await ptr89.open(Uint8Array.from([