mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-06-03 12:27:21 +12:00
Integrate Capstone disassembler
This commit is contained in:
parent
2eaaccd96b
commit
eab1a12b07
5 changed files with 101 additions and 10 deletions
53
include/capstone.hpp
Normal file
53
include/capstone.hpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#pragma once
|
||||
#include <capstone/capstone.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <span>
|
||||
#include <string>
|
||||
|
||||
#include "helpers.hpp"
|
||||
|
||||
namespace Common {
|
||||
class CapstoneDisassembler {
|
||||
csh handle; // Handle to our disassembler object
|
||||
cs_insn* instructions = nullptr; // Pointer to instruction object
|
||||
bool initialized = false;
|
||||
|
||||
public:
|
||||
void init(cs_arch arch, cs_mode mode) { initialized = (cs_open(arch, mode, &handle) == CS_ERR_OK); }
|
||||
|
||||
CapstoneDisassembler() {}
|
||||
CapstoneDisassembler(cs_arch arch, cs_mode mode) { init(arch, mode); }
|
||||
|
||||
// Returns the number of instructions successfully disassembled
|
||||
// pc: program counter of the instruction to disassemble
|
||||
// bytes: Byte representation of instruction
|
||||
// buffer: text buffer to output the disassembly too
|
||||
usize disassemble(std::string& buffer, u32 pc, std::span<u8> bytes, u64 offset = 0) {
|
||||
if (!initialized) {
|
||||
buffer = "Capstone was not properly initialized";
|
||||
return 0;
|
||||
}
|
||||
|
||||
usize count = cs_disasm(handle, bytes.data(), bytes.size(), pc, offset, &instructions);
|
||||
if (count == 0) {
|
||||
// Error in disassembly, quit early and return empty string
|
||||
buffer = "Error disassembling instructions with Capstone";
|
||||
return 0;
|
||||
}
|
||||
|
||||
buffer = "";
|
||||
for (usize i = 0; i < count; i++) {
|
||||
buffer += std::string(instructions[i].mnemonic) + " " + std::string(instructions[i].op_str);
|
||||
|
||||
if (i < count - 1) {
|
||||
// Append newlines between instructions, sans the final instruction
|
||||
buffer += "\n";
|
||||
}
|
||||
}
|
||||
|
||||
cs_free(instructions, count);
|
||||
return count;
|
||||
}
|
||||
};
|
||||
} // namespace Common
|
Loading…
Add table
Add a link
Reference in a new issue