Rizin
unix-like reverse engineering framework and cli tools
asm_m68k_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2015-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_asm.h>
5 #include <rz_lib.h>
6 #include <capstone/capstone.h>
7 
8 #ifdef CAPSTONE_M68K_H
9 #define CAPSTONE_HAS_M68K 1
10 #else
11 #define CAPSTONE_HAS_M68K 0
12 #ifdef _MSC_VER
13 #pragma message("Cannot find capstone-m68k support")
14 #else
15 #warning Cannot find capstone-m68k support
16 #endif
17 #endif
18 
19 #if CAPSTONE_HAS_M68K
20 
21 // Size of the longest instruction in bytes
22 #define M68K_LONGEST_INSTRUCTION 10
23 
24 static bool check_features(RzAsm *a, cs_insn *insn);
25 static csh cd = 0;
26 #include "cs_mnemonics.c"
27 
28 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
29  const char *buf_asm = NULL;
30  static int omode = -1;
31  static int obits = 32;
32  cs_insn *insn = NULL;
33  int ret = 0, n = 0;
35  if (mode != omode || a->bits != obits) {
36  cs_close(&cd);
37  cd = 0; // unnecessary
38  omode = mode;
39  obits = a->bits;
40  }
41 
42  // replace this with the asm.features?
43  if (a->cpu && strstr(a->cpu, "68000")) {
45  }
46  if (a->cpu && strstr(a->cpu, "68010")) {
48  }
49  if (a->cpu && strstr(a->cpu, "68020")) {
51  }
52  if (a->cpu && strstr(a->cpu, "68030")) {
54  }
55  if (a->cpu && strstr(a->cpu, "68040")) {
57  }
58  if (a->cpu && strstr(a->cpu, "68060")) {
60  }
61  if (op) {
62  op->size = 4;
63  }
64  if (cd == 0) {
65  ret = cs_open(CS_ARCH_M68K, mode, &cd);
66  if (ret) {
67  ret = -1;
68  goto beach;
69  }
70  }
71  if (a->features && *a->features) {
73  } else {
75  }
76  if (!buf) {
77  goto beach;
78  }
79 
80  ut8 mybuf[M68K_LONGEST_INSTRUCTION] = { 0 };
81  int mylen = RZ_MIN(M68K_LONGEST_INSTRUCTION, len);
82  memcpy(mybuf, buf, mylen);
83 
84  n = cs_disasm(cd, mybuf, mylen, a->pc, 1, &insn);
85  if (n < 1) {
86  ret = -1;
87  goto beach;
88  }
89  if (op) {
90  op->size = 0;
91  }
92  if (insn->size < 1) {
93  ret = -1;
94  goto beach;
95  }
96  if (a->features && *a->features) {
97  if (!check_features(a, insn)) {
98  if (op) {
99  op->size = insn->size;
100  buf_asm = "illegal";
101  }
102  }
103  }
104  if (op && !op->size) {
105  op->size = insn->size;
106  buf_asm = sdb_fmt("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str);
107  }
108  if (op && buf_asm) {
109  char *p = rz_str_replace(strdup(buf_asm), "$", "0x", true);
110  if (p) {
111  rz_str_replace_char(p, '#', 0);
113  free(p);
114  }
115  }
116  cs_free(insn, n);
117 beach:
118  // cs_close (&cd);
119  if (op && buf_asm) {
120  if (!strncmp(buf_asm, "dc.w", 4)) {
121  rz_asm_op_set_asm(op, "invalid");
122  }
123  return op->size;
124  }
125  return ret;
126 }
127 
129  .name = "m68k",
130  .desc = "Capstone M68K disassembler",
131  .cpus = "68000,68010,68020,68030,68040,68060",
132  .license = "BSD",
133  .arch = "m68k",
134  .bits = 32,
135  .endian = RZ_SYS_ENDIAN_BIG,
136  .disassemble = &disassemble,
137  .mnemonics = &mnemonics,
138 };
139 
140 static bool check_features(RzAsm *a, cs_insn *insn) {
141  /* TODO: Implement support for m68k */
142  return true;
143 }
144 
145 #ifndef RZ_PLUGIN_INCORE
148  .data = &rz_asm_plugin_m68k_cs,
150 };
151 #endif
152 
153 #else
155  .name = "m68k.cs (unsupported)",
156  .desc = "Capstone M68K disassembler (unsupported)",
157  .license = "BSD",
158  .author = "pancake",
159  .arch = "m68k",
160  .bits = 32,
161  .endian = RZ_SYS_ENDIAN_BIG,
162 };
163 
164 #ifndef RZ_PLUGIN_INCORE
167  .data = &rz_asm_plugin_m68k_cs,
169 };
170 #endif
171 
172 #endif
size_t len
Definition: 6502dis.c:15
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_6502.c:12
static bool check_features(RzAsm *a, cs_insn *insn)
Definition: asm_arm_cs.c:21
RZ_API RzLibStruct rizin_plugin
Definition: asm_m68k_cs.c:165
RzAsmPlugin rz_asm_plugin_m68k_cs
Definition: asm_m68k_cs.c:154
static csh cd
Definition: asm_mips_cs.c:10
@ CS_ARCH_M68K
68K architecture
Definition: capstone.h:83
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_M68K_040
M68K 68040 mode.
Definition: capstone.h:121
@ CS_MODE_M68K_000
M68K 68000 mode.
Definition: capstone.h:117
@ CS_MODE_M68K_060
M68K 68060 mode.
Definition: capstone.h:122
@ CS_MODE_M68K_010
M68K 68010 mode.
Definition: capstone.h:118
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_MODE_M68K_020
M68K 68020 mode.
Definition: capstone.h:119
@ CS_MODE_M68K_030
M68K 68030 mode.
Definition: capstone.h:120
@ CS_MODE_LITTLE_ENDIAN
little-endian mode (default mode)
Definition: capstone.h:103
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
#define RZ_API
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
int n
Definition: mipsasm.c:19
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API char * rz_str_replace(char *str, const char *key, const char *val, int g)
Definition: str.c:1110
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_MIN(x, y)
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
const char * name
Definition: rz_asm.h:130
const char * version
Definition: rz_asm.h:133
Definition: dis.c:32
static const char * mnemonics[]
Definition: z80asm.c:43