/
asm_m68k_cs.c
115 lines (105 loc) · 2.53 KB
/
asm_m68k_cs.c
1
2
3
4
5
6
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* radare2 - LGPL - Copyright 2015 - pancake */
#include <r_asm.h>
#include <r_lib.h>
#include <capstone/capstone.h>
#if CS_API_MAJOR>=4 && CS_NEXT_VERSION>=3
#define CAPSTONE_HAS_M68K 1
#else
#define CAPSTONE_HAS_M68K 0
#warning Cannot find capstone-m68k support
#endif
#if CAPSTONE_HAS_M68K
static bool check_features(RAsm *a, cs_insn *insn);
static csh cd;
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
cs_insn* insn = NULL;
cs_mode mode = 0;
int ret, n = 0;
mode |= (a->big_endian)? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;
// replace this with the asm.features?
if (a->cpu && strstr (a->cpu, "mclass"))
mode |= CS_MODE_MCLASS;
if (a->cpu && strstr (a->cpu, "v8"))
mode |= CS_MODE_V8;
op->size = 4;
op->buf_asm[0] = 0;
ret = cs_open (CS_ARCH_M68K, mode, &cd);
if (ret) {
ret = -1;
goto beach;
}
if (a->syntax == R_ASM_SYNTAX_REGNUM) {
cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME);
} else cs_option (cd, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT);
if (a->features && *a->features) {
cs_option (cd, CS_OPT_DETAIL, CS_OPT_ON);
} else {
cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
}
n = cs_disasm (cd, buf, R_MIN (4, len),
a->pc, 1, &insn);
if (n<1) {
ret = -1;
goto beach;
}
op->size = 0;
if (insn->size<1) {
ret = -1;
goto beach;
}
if (a->features && *a->features) {
if (!check_features (a, insn)) {
op->size = insn->size;
strcpy (op->buf_asm, "illegal");
}
}
if (!op->size) {
op->size = insn->size;
snprintf (op->buf_asm, R_ASM_BUFSIZE, "%s%s%s",
insn->mnemonic,
insn->op_str[0]?" ":"",
insn->op_str);
r_str_rmch (op->buf_asm, '#');
}
cs_free (insn, n);
beach:
cs_close (&cd);
if (!op->buf_asm[0])
strcpy (op->buf_asm, "invalid");
return op->size;
}
RAsmPlugin r_asm_plugin_m68k_cs = {
.name = "m68k.cs",
.desc = "Capstone M68K disassembler",
.cpus = "68000,68010,68020,68030,68040,68060",
.license = "BSD",
.arch = "m68k",
.bits = 32,
.disassemble = &disassemble,
.features = NULL
};
static bool check_features(RAsm *a, cs_insn *insn) {
/* TODO: Implement support for m68k */
return true;
}
#ifndef CORELIB
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_m68k_cs,
.version = R2_VERSION
};
#endif
#else
RAsmPlugin r_asm_plugin_m68k_cs = {
.name = "m68k.cs (unsupported)",
.desc = "Capstone M68K disassembler (unsupported)",
.license = "BSD",
.arch = "m68k",
.bits = 32,
};
struct r_lib_struct_t radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_m68k_cs,
.version = R2_VERSION
};
#endif