parent
010025cbfa
commit
0454a369e5
@ -104,21 +104,23 @@ namespace hex::plugin::disasm {
|
|||||||
std::vector<u8> data(std::min<u64>(32, evaluator->getSectionSize(sectionId) - address));
|
std::vector<u8> data(std::min<u64>(32, evaluator->getSectionSize(sectionId) - address));
|
||||||
evaluator->readData(address, data.data(), data.size(), sectionId);
|
evaluator->readData(address, data.data(), data.size(), sectionId);
|
||||||
|
|
||||||
cs_insn instruction;
|
auto *instruction = cs_malloc(capstone);
|
||||||
|
ON_SCOPE_EXIT { cs_free(instruction, 1); };
|
||||||
|
|
||||||
const u8 *code = data.data();
|
const u8 *code = data.data();
|
||||||
size_t dataSize = data.size();
|
size_t dataSize = data.size();
|
||||||
if (!cs_disasm_iter(capstone, &code, &dataSize, &instructionLoadAddress, &instruction)) {
|
if (!cs_disasm_iter(capstone, &code, &dataSize, &instructionLoadAddress, instruction)) {
|
||||||
err::E0012.throwError("Failed to disassemble instruction");
|
err::E0012.throwError("Failed to disassemble instruction");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = std::make_unique<PatternInstruction>(evaluator, address, instruction.size, 0);
|
auto result = std::make_unique<PatternInstruction>(evaluator, address, instruction->size, 0);
|
||||||
|
|
||||||
std::string instructionString;
|
std::string instructionString;
|
||||||
if (instruction.mnemonic[0] != '\x00')
|
if (instruction->mnemonic[0] != '\x00')
|
||||||
instructionString += instruction.mnemonic;
|
instructionString += instruction->mnemonic;
|
||||||
if (instruction.op_str[0] != '\x00') {
|
if (instruction->op_str[0] != '\x00') {
|
||||||
instructionString += ' ';
|
instructionString += ' ';
|
||||||
instructionString += instruction.op_str;
|
instructionString += instruction->op_str;
|
||||||
}
|
}
|
||||||
result->setInstructionString(instructionString);
|
result->setInstructionString(instructionString);
|
||||||
|
|
||||||
|
@ -48,12 +48,14 @@ namespace hex::plugin::disasm {
|
|||||||
|
|
||||||
m_disassemblerTask = TaskManager::createTask("hex.disassembler.view.disassembler.disassembling"_lang, m_regionToDisassemble.getSize(), [this](auto &task) {
|
m_disassemblerTask = TaskManager::createTask("hex.disassembler.view.disassembler.disassembling"_lang, m_regionToDisassemble.getSize(), [this](auto &task) {
|
||||||
csh capstoneHandle;
|
csh capstoneHandle;
|
||||||
cs_insn instruction;
|
|
||||||
|
|
||||||
cs_mode mode = m_mode;
|
cs_mode mode = m_mode;
|
||||||
|
|
||||||
// Create a capstone disassembler instance
|
// Create a capstone disassembler instance
|
||||||
if (cs_open(Disassembler::toCapstoneArchitecture(m_architecture), mode, &capstoneHandle) == CS_ERR_OK) {
|
if (cs_open(Disassembler::toCapstoneArchitecture(m_architecture), mode, &capstoneHandle) == CS_ERR_OK) {
|
||||||
|
auto *instruction = cs_malloc(capstoneHandle);
|
||||||
|
ON_SCOPE_EXIT { cs_free(instruction, 1); };
|
||||||
|
|
||||||
|
|
||||||
// Tell capstone to skip data bytes
|
// Tell capstone to skip data bytes
|
||||||
cs_option(capstoneHandle, CS_OPT_SKIPDATA, CS_OPT_ON);
|
cs_option(capstoneHandle, CS_OPT_SKIPDATA, CS_OPT_ON);
|
||||||
@ -75,24 +77,24 @@ namespace hex::plugin::disasm {
|
|||||||
|
|
||||||
// Ask capstone to disassemble the data
|
// Ask capstone to disassemble the data
|
||||||
const u8 *code = buffer.data();
|
const u8 *code = buffer.data();
|
||||||
while (cs_disasm_iter(capstoneHandle, &code, &bufferSize, &instructionLoadAddress, &instruction)) {
|
while (cs_disasm_iter(capstoneHandle, &code, &bufferSize, &instructionLoadAddress, instruction)) {
|
||||||
task.update(instructionDataAddress);
|
task.update(instructionDataAddress);
|
||||||
|
|
||||||
// Convert the capstone instructions to our disassembly format
|
// Convert the capstone instructions to our disassembly format
|
||||||
Disassembly disassembly = { };
|
Disassembly disassembly = { };
|
||||||
disassembly.address = instruction.address;
|
disassembly.address = instruction->address;
|
||||||
disassembly.offset = instructionDataAddress - m_imageBaseAddress;
|
disassembly.offset = instructionDataAddress - m_imageBaseAddress;
|
||||||
disassembly.size = instruction.size;
|
disassembly.size = instruction->size;
|
||||||
disassembly.mnemonic = instruction.mnemonic;
|
disassembly.mnemonic = instruction->mnemonic;
|
||||||
disassembly.operators = instruction.op_str;
|
disassembly.operators = instruction->op_str;
|
||||||
|
|
||||||
for (u16 j = 0; j < instruction.size; j++)
|
for (u16 j = 0; j < instruction->size; j++)
|
||||||
disassembly.bytes += hex::format("{0:02X} ", instruction.bytes[j]);
|
disassembly.bytes += hex::format("{0:02X} ", instruction->bytes[j]);
|
||||||
disassembly.bytes.pop_back();
|
disassembly.bytes.pop_back();
|
||||||
|
|
||||||
m_disassembly.push_back(disassembly);
|
m_disassembly.push_back(disassembly);
|
||||||
|
|
||||||
instructionDataAddress += instruction.size;
|
instructionDataAddress += instruction->size;
|
||||||
hadError = false;
|
hadError = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user