From ed0a94659e9851f8f7b85ae40baafcfdb7cbd5bb Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 14 Dec 2024 21:52:19 +0100 Subject: [PATCH] impr: Added load and base address to instruction pl types --- .../source/content/pl_builtin_types.cpp | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/plugins/disassembler/source/content/pl_builtin_types.cpp b/plugins/disassembler/source/content/pl_builtin_types.cpp index 8cb6bbe60..5f37f3b26 100644 --- a/plugins/disassembler/source/content/pl_builtin_types.cpp +++ b/plugins/disassembler/source/content/pl_builtin_types.cpp @@ -64,7 +64,7 @@ namespace hex::plugin::disasm { const pl::api::Namespace nsHexDec = { "builtin", "hex", "dec" }; /* Json */ - ContentRegistry::PatternLanguage::addType(nsHexDec, "Instruction", FunctionParameterCount::exactly(3), [](Evaluator *evaluator, auto params) -> std::unique_ptr { + ContentRegistry::PatternLanguage::addType(nsHexDec, "Instruction", FunctionParameterCount::exactly(4), [](Evaluator *evaluator, auto params) -> std::unique_ptr { cs_arch arch; cs_mode mode; @@ -74,10 +74,15 @@ namespace hex::plugin::disasm { err::E0012.throwError(e.what()); } const auto syntaxString = params[1].toString(); - const auto relocatedAddress = params[2].toUnsigned(); + const auto imageBaseAddress = params[2].toUnsigned(); + const auto imageLoadAddress = params[3].toUnsigned(); const auto address = evaluator->getReadOffset(); + const auto codeOffset = address - imageBaseAddress; + + u64 instructionLoadAddress = imageLoadAddress + codeOffset; + csh capstone; if (cs_open(arch, mode, &capstone) == CS_ERR_OK) { cs_opt_value syntax; @@ -99,24 +104,24 @@ namespace hex::plugin::disasm { std::vector data(std::min(32, evaluator->getSectionSize(sectionId) - address)); evaluator->readData(address, data.data(), data.size(), sectionId); - cs_insn *instruction = nullptr; - size_t instructionCount = cs_disasm(capstone, data.data(), data.size(), relocatedAddress, 1, &instruction); - if (instructionCount != 1) { + cs_insn instruction; + const u8 *code = data.data(); + size_t dataSize = data.size(); + if (!cs_disasm_iter(capstone, &code, &dataSize, &instructionLoadAddress, &instruction)) { err::E0012.throwError("Failed to disassemble instruction"); } - auto result = std::make_unique(evaluator, address, instruction->size, 0); + auto result = std::make_unique(evaluator, address, instruction.size, 0); std::string instructionString; - if (instruction->mnemonic[0] != '\x00') - instructionString += instruction->mnemonic; - if (instruction->op_str[0] != '\x00') { + if (instruction.mnemonic[0] != '\x00') + instructionString += instruction.mnemonic; + if (instruction.op_str[0] != '\x00') { instructionString += ' '; - instructionString += instruction->op_str; + instructionString += instruction.op_str; } result->setInstructionString(instructionString); - cs_free(instruction, instructionCount); cs_close(&capstone); return result;