1 /***************************************************************************
2 * Copyright (C) 2019 by Mete Balci *
3 * metebalci@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
23 #include <helper/log.h>
25 #include "a64_disassembler.h"
29 #include <capstone/capstone.h>
31 static void print_opcode(struct command_invocation *cmd, const cs_insn *insn)
35 memcpy(&opcode, insn->bytes, insn->size);
37 if (insn->size == 4) {
39 uint16_t opcode_high = opcode >> 16;
41 opcode = opcode & 0xffff;
44 "0x%08" PRIx64" %04x %04x\t%s\t%s",
55 "0x%08" PRIx64" %04x\t%s\t%s",
64 int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
70 csret = cs_open(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN, &handle);
72 if (csret != CS_ERR_OK) {
74 LOG_ERROR("cs_open() failed: %s", cs_strerror(csret));
79 csret = cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
81 if (csret != CS_ERR_OK) {
83 LOG_ERROR("cs_option() failed: %s", cs_strerror(csret));
89 cs_insn *insn = cs_malloc(handle);
91 if (csret != CS_ERR_OK) {
93 LOG_ERROR("cs_malloc() failed: %s", cs_strerror(csret));
103 ret = target_read_buffer(target, address, sizeof(buffer), buffer);
105 if (ret != ERROR_OK) {
111 size_t size = sizeof(buffer);
112 const uint8_t *tmp = buffer;
114 ret = cs_disasm_iter(handle, &tmp, &size, &address, insn);
118 LOG_ERROR("cs_disasm_iter() failed: %s", cs_strerror(cs_errno(handle)));
125 print_opcode(cmd, insn);
138 int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
140 command_print(cmd, "capstone disassembly framework required");