1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
67
68
69
70
77
78
79
80
87
88
89
90
91
92
93
94
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/log.h>
#include "target.h"
#include "a64_disassembler.h"
#if HAVE_CAPSTONE
#include <capstone.h>
static void print_opcode(struct command_invocation *cmd, const cs_insn *insn)
{
uint32_t opcode = 0;
memcpy(&opcode, insn->bytes, insn->size);
if (insn->size == 4) {
uint16_t opcode_high = opcode >> 16;
opcode = opcode & 0xffff;
command_print(cmd,
"0x%08" PRIx64" %04x %04x\t%s\t%s",
insn->address,
opcode,
opcode_high,
insn->mnemonic,
insn->op_str);
}if (insn->size == 4) { ... } else {
command_print(
cmd,
"0x%08" PRIx64" %04x\t%s\t%s",
insn->address,
opcode,
insn->mnemonic,
insn->op_str);
}else { ... }
}print_opcode (struct command_invocation *cmd, const cs_insn *insn) { ... }
int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
{
int ret;
int csret;
csh handle;
csret = cs_open(CS_ARCH_ARM64, CS_MODE_LITTLE_ENDIAN, &handle);
if (csret != CS_ERR_OK) {
LOG_ERROR("cs_open() failed: %s", cs_strerror(csret));
return ERROR_FAIL;
}if (csret != CS_ERR_OK) { ... }
csret = cs_option(handle, CS_OPT_SKIPDATA, CS_OPT_ON);
if (csret != CS_ERR_OK) {
LOG_ERROR("cs_option() failed: %s", cs_strerror(csret));
cs_close(&handle);
return ERROR_FAIL;
}if (csret != CS_ERR_OK) { ... }
cs_insn *insn = cs_malloc(handle);
if (csret != CS_ERR_OK) {
LOG_ERROR("cs_malloc() failed: %s", cs_strerror(csret));
cs_close(&handle);
return ERROR_FAIL;
}if (csret != CS_ERR_OK) { ... }
while (count > 0) {
uint8_t buffer[4];
ret = target_read_buffer(target, address, sizeof(buffer), buffer);
if (ret != ERROR_OK) {
cs_free(insn, 1);
cs_close(&handle);
return ret;
}if (ret != ERROR_OK) { ... }
size_t size = sizeof(buffer);
const uint8_t *tmp = buffer;
ret = cs_disasm_iter(handle, &tmp, &size, &address, insn);
if (!ret) {
LOG_ERROR("cs_disasm_iter() failed: %s", cs_strerror(cs_errno(handle)));
cs_free(insn, 1);
cs_close(&handle);
return ERROR_FAIL;
}if (!ret) { ... }
print_opcode(cmd, insn);
count--;
}while (count > 0) { ... }
cs_free(insn, 1);
cs_close(&handle);
return ERROR_OK;
}a64_disassemble (struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count) { ... }
/* ... */
#else
int a64_disassemble(struct command_invocation *cmd, struct target *target, target_addr_t address, size_t count)
{
command_print(cmd, "capstone disassembly framework required");
return ERROR_FAIL;
}{ ... }
/* ... */#endif