1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2019 by Mete Balci *
5 * metebalci@gmail.com *
6 ***************************************************************************/
12 #include <helper/log.h>
14 #include "a64_disassembler.h"
20 static void print_opcode(struct command_invocation
*cmd
, const cs_insn
*insn
)
24 memcpy(&opcode
, insn
->bytes
, insn
->size
);
26 if (insn
->size
== 4) {
28 uint16_t opcode_high
= opcode
>> 16;
30 opcode
= opcode
& 0xffff;
33 "0x%08" PRIx64
" %04x %04x\t%s\t%s",
44 "0x%08" PRIx64
" %04x\t%s\t%s",
53 int a64_disassemble(struct command_invocation
*cmd
, struct target
*target
, target_addr_t address
, size_t count
)
59 csret
= cs_open(CS_ARCH_ARM64
, CS_MODE_LITTLE_ENDIAN
, &handle
);
61 if (csret
!= CS_ERR_OK
) {
63 LOG_ERROR("cs_open() failed: %s", cs_strerror(csret
));
68 csret
= cs_option(handle
, CS_OPT_SKIPDATA
, CS_OPT_ON
);
70 if (csret
!= CS_ERR_OK
) {
72 LOG_ERROR("cs_option() failed: %s", cs_strerror(csret
));
78 cs_insn
*insn
= cs_malloc(handle
);
80 if (csret
!= CS_ERR_OK
) {
82 LOG_ERROR("cs_malloc() failed: %s", cs_strerror(csret
));
92 ret
= target_read_buffer(target
, address
, sizeof(buffer
), buffer
);
94 if (ret
!= ERROR_OK
) {
100 size_t size
= sizeof(buffer
);
101 const uint8_t *tmp
= buffer
;
103 ret
= cs_disasm_iter(handle
, &tmp
, &size
, &address
, insn
);
107 LOG_ERROR("cs_disasm_iter() failed: %s", cs_strerror(cs_errno(handle
)));
114 print_opcode(cmd
, insn
);
127 int a64_disassemble(struct command_invocation
*cmd
, struct target
*target
, target_addr_t address
, size_t count
)
129 command_print(cmd
, "capstone disassembly framework required");