2020-11-22 23:07:50 +01:00
|
|
|
#include "views/view_disassembler.hpp"
|
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
|
|
|
#include <hex/helpers/utils.hpp>
|
2020-11-22 23:07:50 +01:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace std::literals::string_literals;
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2020-12-27 15:39:06 +01:00
|
|
|
ViewDisassembler::ViewDisassembler() : View("Disassembler") {
|
2021-01-21 10:53:12 +01:00
|
|
|
View::subscribeEvent(Events::DataChanged, [this](auto){
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
});
|
2020-11-28 16:15:12 +01:00
|
|
|
|
2021-01-21 10:53:12 +01:00
|
|
|
View::subscribeEvent(Events::RegionSelected, [this](auto userData) {
|
|
|
|
auto region = std::any_cast<Region>(userData);
|
2020-11-28 16:15:12 +01:00
|
|
|
|
|
|
|
if (this->m_shouldMatchSelection) {
|
|
|
|
this->m_codeRegion[0] = region.address;
|
|
|
|
this->m_codeRegion[1] = region.address + region.size - 1;
|
|
|
|
}
|
|
|
|
});
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewDisassembler::~ViewDisassembler() {
|
|
|
|
View::unsubscribeEvent(Events::DataChanged);
|
2020-11-28 16:15:12 +01:00
|
|
|
View::unsubscribeEvent(Events::RegionSelected);
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewDisassembler::drawContent() {
|
2020-11-22 23:07:50 +01:00
|
|
|
if (this->m_shouldInvalidate) {
|
|
|
|
this->m_disassembly.clear();
|
|
|
|
|
|
|
|
csh capstoneHandle;
|
|
|
|
cs_insn *instructions = nullptr;
|
|
|
|
|
|
|
|
cs_mode mode = cs_mode(this->m_modeBasicARM | this->m_modeExtraARM | this->m_modeBasicMIPS | this->m_modeBasicX86 | this->m_modeBasicPPC);
|
|
|
|
|
|
|
|
if (this->m_littleEndianMode)
|
|
|
|
mode = cs_mode(mode | CS_MODE_LITTLE_ENDIAN);
|
|
|
|
else
|
|
|
|
mode = cs_mode(mode | CS_MODE_BIG_ENDIAN);
|
|
|
|
|
|
|
|
if (this->m_micoMode)
|
|
|
|
mode = cs_mode(mode | CS_MODE_MICRO);
|
|
|
|
|
|
|
|
if (this->m_sparcV9Mode)
|
|
|
|
mode = cs_mode(mode | CS_MODE_V9);
|
|
|
|
|
2020-12-05 17:32:30 +01:00
|
|
|
if (cs_open(Disassembler::toCapstoneArchictecture(this->m_architecture), mode, &capstoneHandle) == CS_ERR_OK) {
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
auto provider = SharedData::currentProvider;
|
2020-11-22 23:07:50 +01:00
|
|
|
std::vector<u8> buffer(2048, 0x00);
|
2020-11-28 16:15:12 +01:00
|
|
|
for (u64 address = 0; address < (this->m_codeRegion[1] - this->m_codeRegion[0] + 1); address += 2048) {
|
|
|
|
size_t bufferSize = std::min(u64(2048), (this->m_codeRegion[1] - this->m_codeRegion[0] + 1) - address);
|
2020-12-27 15:39:06 +01:00
|
|
|
provider->read(this->m_codeRegion[0] + address, buffer.data(), bufferSize);
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2020-11-28 16:15:12 +01:00
|
|
|
size_t instructionCount = cs_disasm(capstoneHandle, buffer.data(), bufferSize, this->m_baseAddress + address, 0, &instructions);
|
2020-11-22 23:07:50 +01:00
|
|
|
|
|
|
|
if (instructionCount == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
u64 usedBytes = 0;
|
|
|
|
for (u32 instr = 0; instr < instructionCount; instr++) {
|
|
|
|
Disassembly disassembly = { 0 };
|
|
|
|
disassembly.address = instructions[instr].address;
|
2020-11-28 16:15:12 +01:00
|
|
|
disassembly.offset = this->m_codeRegion[0] + address + usedBytes;
|
2020-11-23 13:10:14 +01:00
|
|
|
disassembly.size = instructions[instr].size;
|
2020-11-23 15:22:26 +01:00
|
|
|
disassembly.mnemonic = instructions[instr].mnemonic;
|
|
|
|
disassembly.operators = instructions[instr].op_str;
|
2020-11-22 23:07:50 +01:00
|
|
|
|
|
|
|
for (u8 i = 0; i < instructions[instr].size; i++)
|
|
|
|
disassembly.bytes += hex::format("%02X ", instructions[instr].bytes[i]);
|
|
|
|
disassembly.bytes.pop_back();
|
|
|
|
|
|
|
|
this->m_disassembly.push_back(disassembly);
|
|
|
|
|
|
|
|
usedBytes += instructions[instr].size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instructionCount < bufferSize)
|
|
|
|
address -= (bufferSize - usedBytes);
|
|
|
|
|
|
|
|
cs_free(instructions, instructionCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
cs_close(&capstoneHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->m_shouldInvalidate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-23 23:57:19 +01:00
|
|
|
if (ImGui::Begin("Disassembler", &this->getWindowOpenState(), ImGuiWindowFlags_NoCollapse)) {
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2021-01-12 23:28:41 +01:00
|
|
|
auto provider = SharedData::currentProvider;
|
2020-12-27 15:39:06 +01:00
|
|
|
if (provider != nullptr && provider->isReadable()) {
|
2020-11-28 16:15:12 +01:00
|
|
|
ImGui::TextUnformatted("Position");
|
|
|
|
ImGui::Separator();
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2020-11-28 16:15:12 +01:00
|
|
|
ImGui::InputScalar("Base address", ImGuiDataType_U64, &this->m_baseAddress, nullptr, nullptr, "%08llX", ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
ImGui::InputScalarN("Code region", ImGuiDataType_U64, this->m_codeRegion, 2, nullptr, nullptr, "%08llX", ImGuiInputTextFlags_CharsHexadecimal);
|
|
|
|
ImGui::Checkbox("Match selection", &this->m_shouldMatchSelection);
|
2020-11-22 23:07:50 +01:00
|
|
|
|
|
|
|
ImGui::NewLine();
|
2020-11-28 16:15:12 +01:00
|
|
|
ImGui::TextUnformatted("Settings");
|
2020-11-22 23:07:50 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2020-12-05 17:32:30 +01:00
|
|
|
ImGui::Combo("Architecture", reinterpret_cast<int*>(&this->m_architecture), Disassembler::ArchitectureNames, Disassembler::getArchitectureSupportedCount());
|
2020-11-22 23:07:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
if (ImGui::BeginChild("modes", ImVec2(0, 100), true)) {
|
|
|
|
|
|
|
|
if (ImGui::RadioButton("Little Endian", this->m_littleEndianMode))
|
|
|
|
this->m_littleEndianMode = true;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("Big Endian", !this->m_littleEndianMode))
|
|
|
|
this->m_littleEndianMode = false;
|
|
|
|
|
|
|
|
ImGui::NewLine();
|
|
|
|
|
|
|
|
switch (this->m_architecture) {
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::ARM:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicMIPS = cs_mode(0);
|
|
|
|
this->m_modeBasicX86 = cs_mode(0);
|
|
|
|
this->m_modeBasicPPC = cs_mode(0);
|
|
|
|
this->m_micoMode = false;
|
|
|
|
this->m_sparcV9Mode = false;
|
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
if (this->m_modeBasicARM == cs_mode(0))
|
|
|
|
this->m_modeBasicARM = CS_MODE_ARM;
|
|
|
|
|
2020-11-22 23:07:50 +01:00
|
|
|
if (ImGui::RadioButton("ARM mode", this->m_modeBasicARM == CS_MODE_ARM))
|
|
|
|
this->m_modeBasicARM = CS_MODE_ARM;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("Thumb mode", this->m_modeBasicARM == CS_MODE_THUMB))
|
|
|
|
this->m_modeBasicARM = CS_MODE_THUMB;
|
|
|
|
|
|
|
|
if (ImGui::RadioButton("Default mode", (this->m_modeExtraARM & (CS_MODE_MCLASS | CS_MODE_V8)) == 0))
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("Cortex-M mode", (this->m_modeExtraARM & (CS_MODE_MCLASS | CS_MODE_V8)) == CS_MODE_MCLASS))
|
|
|
|
this->m_modeExtraARM = CS_MODE_MCLASS;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("ARMv8 mode", (this->m_modeExtraARM & (CS_MODE_MCLASS | CS_MODE_V8)) == CS_MODE_V8))
|
|
|
|
this->m_modeExtraARM = CS_MODE_V8;
|
|
|
|
break;
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::MIPS:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicARM = cs_mode(0);
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
this->m_modeBasicX86 = cs_mode(0);
|
|
|
|
this->m_modeBasicPPC = cs_mode(0);
|
|
|
|
this->m_sparcV9Mode = false;
|
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
if (this->m_modeBasicMIPS == cs_mode(0))
|
|
|
|
this->m_modeBasicMIPS = CS_MODE_MIPS32;
|
|
|
|
|
2020-11-22 23:07:50 +01:00
|
|
|
if (ImGui::RadioButton("MIPS32 mode", this->m_modeBasicMIPS == CS_MODE_MIPS32))
|
|
|
|
this->m_modeBasicMIPS = CS_MODE_MIPS32;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("MIPS64 mode", this->m_modeBasicMIPS == CS_MODE_MIPS64))
|
|
|
|
this->m_modeBasicMIPS = CS_MODE_MIPS64;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("MIPS32R6 mode", this->m_modeBasicMIPS == CS_MODE_MIPS32R6))
|
|
|
|
this->m_modeBasicMIPS = CS_MODE_MIPS32R6;
|
|
|
|
|
|
|
|
ImGui::Checkbox("Micro Mode", &this->m_micoMode);
|
|
|
|
break;
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::X86:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicARM = cs_mode(0);
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
this->m_modeBasicMIPS = cs_mode(0);
|
|
|
|
this->m_modeBasicPPC = cs_mode(0);
|
|
|
|
this->m_micoMode = false;
|
|
|
|
this->m_sparcV9Mode = false;
|
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
if (this->m_modeBasicX86 == cs_mode(0))
|
|
|
|
this->m_modeBasicX86 = CS_MODE_16;
|
|
|
|
|
2020-11-22 23:07:50 +01:00
|
|
|
if (ImGui::RadioButton("16-bit mode", this->m_modeBasicX86 == CS_MODE_16))
|
|
|
|
this->m_modeBasicX86 = CS_MODE_16;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("32-bit mode", this->m_modeBasicX86 == CS_MODE_32))
|
|
|
|
this->m_modeBasicX86 = CS_MODE_32;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("64-bit mode", this->m_modeBasicX86 == CS_MODE_64))
|
|
|
|
this->m_modeBasicX86 = CS_MODE_64;
|
|
|
|
break;
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::PPC:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicARM = cs_mode(0);
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
this->m_modeBasicMIPS = cs_mode(0);
|
|
|
|
this->m_modeBasicX86 = cs_mode(0);
|
|
|
|
this->m_micoMode = false;
|
|
|
|
this->m_sparcV9Mode = false;
|
|
|
|
|
2020-11-23 13:10:14 +01:00
|
|
|
if (m_modeBasicPPC == cs_mode(0))
|
|
|
|
this->m_modeBasicPPC = CS_MODE_32;
|
|
|
|
|
2020-11-22 23:07:50 +01:00
|
|
|
if (ImGui::RadioButton("32-bit mode", this->m_modeBasicPPC == CS_MODE_32))
|
|
|
|
this->m_modeBasicPPC = CS_MODE_32;
|
|
|
|
ImGui::SameLine();
|
|
|
|
if (ImGui::RadioButton("64-bit mode", this->m_modeBasicPPC == CS_MODE_64))
|
|
|
|
this->m_modeBasicPPC = CS_MODE_64;
|
|
|
|
break;
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::SPARC:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicARM = cs_mode(0);
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
this->m_modeBasicMIPS = cs_mode(0);
|
|
|
|
this->m_modeBasicX86 = cs_mode(0);
|
|
|
|
this->m_modeBasicPPC = cs_mode(0);
|
|
|
|
this->m_micoMode = false;
|
|
|
|
|
|
|
|
ImGui::Checkbox("Sparc V9 mode", &this->m_sparcV9Mode);
|
|
|
|
break;
|
2020-12-05 17:32:30 +01:00
|
|
|
case Architecture::ARM64:
|
|
|
|
case Architecture::SYSZ:
|
|
|
|
case Architecture::XCORE:
|
|
|
|
case Architecture::M68K:
|
|
|
|
case Architecture::TMS320C64X:
|
|
|
|
case Architecture::M680X:
|
|
|
|
case Architecture::EVM:
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_modeBasicARM = cs_mode(0);
|
|
|
|
this->m_modeExtraARM = cs_mode(0);
|
|
|
|
this->m_modeBasicMIPS = cs_mode(0);
|
|
|
|
this->m_modeBasicX86 = cs_mode(0);
|
|
|
|
this->m_modeBasicPPC = cs_mode(0);
|
|
|
|
this->m_micoMode = false;
|
|
|
|
this->m_sparcV9Mode = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-23 00:12:53 +01:00
|
|
|
ImGui::EndChild();
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
ImGui::SetCursorPosX((ImGui::GetContentRegionAvail().x - 300) / 2);
|
2020-11-28 16:15:12 +01:00
|
|
|
if (ImGui::Button("Disassemble", ImVec2(300, 20)))
|
2020-11-22 23:07:50 +01:00
|
|
|
this->m_shouldInvalidate = true;
|
|
|
|
ImGui::NewLine();
|
2020-11-28 16:15:12 +01:00
|
|
|
|
|
|
|
ImGui::TextUnformatted("Disassembly");
|
2020-11-22 23:07:50 +01:00
|
|
|
ImGui::Separator();
|
|
|
|
|
2020-11-23 15:22:26 +01:00
|
|
|
if (ImGui::BeginTable("##disassembly", 4, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_RowBg | ImGuiTableFlags_Reorderable)) {
|
|
|
|
ImGui::TableSetupScrollFreeze(0, 1);
|
|
|
|
ImGui::TableSetupColumn("Address");
|
|
|
|
ImGui::TableSetupColumn("Offset");
|
|
|
|
ImGui::TableSetupColumn("Bytes");
|
|
|
|
ImGui::TableSetupColumn("Disassembly");
|
|
|
|
|
|
|
|
ImGuiListClipper clipper;
|
|
|
|
clipper.Begin(this->m_disassembly.size());
|
|
|
|
|
|
|
|
ImGui::TableHeadersRow();
|
|
|
|
while (clipper.Step()) {
|
|
|
|
for (u64 i = clipper.DisplayStart; i < clipper.DisplayEnd; i++) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
if (ImGui::Selectable(("##DisassemblyLine"s + std::to_string(i)).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
|
|
|
|
Region selectRegion = { this->m_disassembly[i].offset, this->m_disassembly[i].size };
|
2021-01-21 10:53:12 +01:00
|
|
|
View::postEvent(Events::SelectionChangeRequest, selectRegion);
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
2020-11-23 15:22:26 +01:00
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::Text("0x%llx", this->m_disassembly[i].address);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::Text("0x%llx", this->m_disassembly[i].offset);
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(this->m_disassembly[i].bytes.c_str());
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextColored(ImColor(0xFFD69C56), "%s", this->m_disassembly[i].mnemonic.c_str());
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::TextUnformatted(this->m_disassembly[i].operators.c_str());
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
2020-11-23 15:22:26 +01:00
|
|
|
}
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2020-11-23 15:22:26 +01:00
|
|
|
clipper.End();
|
2020-11-22 23:07:50 +01:00
|
|
|
|
2020-11-23 15:22:26 +01:00
|
|
|
ImGui::EndTable();
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ImGui::End();
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void ViewDisassembler::drawMenu() {
|
2020-11-23 23:57:19 +01:00
|
|
|
|
2020-11-22 23:07:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|