Rizin
unix-like reverse engineering framework and cli tools
asm_m680x_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018-2019 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 #if CS_API_MAJOR >= 4 && CS_API_MINOR >= 0
9 #define CAPSTONE_HAS_M680X 1
10 #else
11 #define CAPSTONE_HAS_M680X 0
12 #endif
13 
14 #if CAPSTONE_HAS_M680X
15 
16 static csh cd = 0;
17 
18 static int m680xmode(const char *str) {
19  if (!str) {
20  return CS_MODE_M680X_6800;
21  }
22  // replace this with the asm.features?
23  if (strstr(str, "6800")) {
24  return CS_MODE_M680X_6800;
25  }
26  if (strstr(str, "6801")) {
27  return CS_MODE_M680X_6801;
28  } else if (strstr(str, "6805")) {
29  return CS_MODE_M680X_6805;
30  } else if (strstr(str, "6808")) {
31  return CS_MODE_M680X_6808;
32  } else if (strstr(str, "6809")) {
33  return CS_MODE_M680X_6809;
34  } else if (strstr(str, "6811")) {
35  return CS_MODE_M680X_6811;
36  } else if (strstr(str, "cpu12")) {
37  return CS_MODE_M680X_CPU12;
38  } else if (strstr(str, "6301")) {
39  return CS_MODE_M680X_6301;
40  }
41  if (strstr(str, "6309")) {
42  return CS_MODE_M680X_6309;
43  }
44  if (strstr(str, "hcs08")) {
45  return CS_MODE_M680X_HCS08;
46  }
47  return CS_MODE_M680X_6800;
48 }
49 
50 static bool the_end(void *p) {
51  if (cd) {
52  cs_close(&cd);
53  cd = 0;
54  }
55  return true;
56 }
57 
58 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
59  static int omode = 0;
60  int mode, n, ret;
61  ut64 off = a->pc;
62  cs_insn *insn = NULL;
63  mode = m680xmode(a->cpu);
64  if (cd && mode != omode) {
65  cs_close(&cd);
66  cd = 0;
67  }
68  op->size = 0;
69  omode = mode;
70  if (cd == 0) {
71  ret = cs_open(CS_ARCH_M680X, mode, &cd);
72  if (ret) {
73  return 0;
74  }
76  }
77  n = cs_disasm(cd, (const ut8 *)buf, len, off, 1, &insn);
78  if (n > 0) {
79  if (insn->size > 0) {
80  op->size = insn->size;
81  char *buf_asm = sdb_fmt("%s%s%s",
82  insn->mnemonic, insn->op_str[0] ? " " : "",
83  insn->op_str);
84  char *ptrstr = strstr(buf_asm, "ptr ");
85  if (ptrstr) {
86  memmove(ptrstr, ptrstr + 4, strlen(ptrstr + 4) + 1);
87  }
88  rz_asm_op_set_asm(op, buf_asm);
89  }
90  cs_free(insn, n);
91  }
92  return op->size;
93 }
94 
96  .name = "m680x",
97  .cpus = "6800,6801,6805,6808,6809,6811,cpu12,6301,6309,hcs08",
98  .desc = "Capstone M680X Disassembler",
99  .license = "BSD",
100  .arch = "m680x",
101  .bits = 8 | 32,
102  .endian = RZ_SYS_ENDIAN_LITTLE,
103  .fini = the_end,
104  .disassemble = &disassemble,
105 };
106 
107 #else
109  .name = "m680x",
110  .desc = "Capstone M680X Disassembler (Not supported)",
111  .license = "BSD",
112  .arch = "m680x",
113  .bits = 8 | 32,
114 };
115 #endif
116 
117 #ifndef RZ_PLUGIN_INCORE
120  .data = &rz_asm_plugin_m680x_cs,
122 };
123 #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
RZ_API RzLibStruct rizin_plugin
Definition: asm_m680x_cs.c:118
RzAsmPlugin rz_asm_plugin_m680x_cs
Definition: asm_m680x_cs.c:108
static csh cd
Definition: asm_mips_cs.c:10
static bool the_end(void *p)
Definition: asm_ppc_cs.c:12
@ CS_ARCH_M680X
680X architecture
Definition: capstone.h:85
@ CS_MODE_M680X_6811
M680X Motorola/Freescale/NXP 68HC11 mode.
Definition: capstone.h:133
@ CS_MODE_M680X_6805
M680X Motorola/Freescale 6805 mode.
Definition: capstone.h:130
@ CS_MODE_M680X_HCS08
M680X Freescale/NXP HCS08 mode.
Definition: capstone.h:136
@ CS_MODE_M680X_6309
M680X Hitachi 6309 mode.
Definition: capstone.h:127
@ CS_MODE_M680X_CPU12
used on M68HC12/HCS12
Definition: capstone.h:134
@ CS_MODE_M680X_6301
M680X Hitachi 6301,6303 mode.
Definition: capstone.h:126
@ CS_MODE_M680X_6801
M680X Motorola 6801,6803 mode.
Definition: capstone.h:129
@ CS_MODE_M680X_6800
M680X Motorola 6800,6802 mode.
Definition: capstone.h:128
@ CS_MODE_M680X_6808
M680X Motorola/Freescale/NXP 68HC08 mode.
Definition: capstone.h:131
@ CS_MODE_M680X_6809
M680X Motorola 6809 mode.
Definition: capstone.h:132
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ 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
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
int n
Definition: mipsasm.c:19
int off
Definition: pal.c:13
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#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
ut64(WINAPI *w32_GetEnabledXStateFeatures)()