Rizin
unix-like reverse engineering framework and cli tools
asm_mips_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2013-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 RZ_IPI int mips_assemble(const char *str, ut64 pc, ut8 *out);
9 
10 static csh cd = 0;
11 #include "cs_mnemonics.c"
12 
13 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
14  cs_insn *insn;
15  int mode, n, ret = -1;
16  mode = (a->big_endian) ? CS_MODE_BIG_ENDIAN : CS_MODE_LITTLE_ENDIAN;
17  if (!op) {
18  return 0;
19  }
20  if (a->cpu && *a->cpu) {
21  if (!strcmp(a->cpu, "micro")) {
23  } else if (!strcmp(a->cpu, "r6")) {
25  } else if (!strcmp(a->cpu, "v3")) {
27  } else if (!strcmp(a->cpu, "v2")) {
28 #if CS_API_MAJOR > 3
30 #endif
31  }
32  }
33  mode |= (a->bits == 64) ? CS_MODE_MIPS64 : CS_MODE_MIPS32;
34  memset(op, 0, sizeof(RzAsmOp));
35  op->size = 4;
36  if (cd != 0) {
37  cs_close(&cd);
38  }
39  ret = cs_open(CS_ARCH_MIPS, mode, &cd);
40  if (ret) {
41  goto fin;
42  }
43  if (a->syntax == RZ_ASM_SYNTAX_REGNUM) {
45  } else {
47  }
49  n = cs_disasm(cd, (ut8 *)buf, len, a->pc, 1, &insn);
50  if (n < 1) {
51  rz_asm_op_set_asm(op, "invalid");
52  op->size = 4;
53  goto beach;
54  }
55  if (insn->size < 1) {
56  goto beach;
57  }
58  op->size = insn->size;
59  char *str = rz_str_newf("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str);
60  if (str) {
61  rz_str_replace_char(str, '$', 0);
62  // remove the '$'<registername> in the string
64  free(str);
65  }
66  cs_free(insn, n);
67 beach:
68  // cs_close (&cd);
69 fin:
70  return op->size;
71 }
72 
73 static int assemble(RzAsm *a, RzAsmOp *op, const char *str) {
74  ut8 *opbuf = (ut8 *)rz_strbuf_get(&op->buf);
75  int ret = mips_assemble(str, a->pc, opbuf);
76  if (a->big_endian) {
77  ut8 *buf = opbuf;
78  ut8 tmp = buf[0];
79  buf[0] = buf[3];
80  buf[3] = tmp;
81  tmp = buf[1];
82  buf[1] = buf[2];
83  buf[2] = tmp;
84  }
85  return ret;
86 }
87 
89  .name = "mips",
90  .desc = "Capstone MIPS disassembler",
91  .license = "BSD",
92  .arch = "mips",
93  .cpus = "mips32/64,micro,r6,v3,v2",
94  .bits = 16 | 32 | 64,
96  .disassemble = &disassemble,
97  .mnemonics = mnemonics,
98  .assemble = &assemble
99 };
100 
101 #ifndef RZ_PLUGIN_INCORE
104  .data = &rz_asm_plugin_mips_cs,
106 };
107 #endif
size_t len
Definition: 6502dis.c:15
#define RZ_IPI
Definition: analysis_wasm.c:11
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
RZ_IPI int mips_assemble(const char *str, ut64 pc, ut8 *out)
Definition: mipsasm.c:148
RZ_API RzLibStruct rizin_plugin
Definition: asm_mips_cs.c:102
RzAsmPlugin rz_asm_plugin_mips_cs
Definition: asm_mips_cs.c:88
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_mips_cs.c:13
static csh cd
Definition: asm_mips_cs.c:10
static int assemble(RzAsm *a, RzAsmOp *op, const char *str)
Definition: asm_mips_cs.c:73
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
@ CS_ARCH_MIPS
Mips architecture.
Definition: capstone.h:77
@ CS_MODE_MIPS64
Mips64 ISA (Mips)
Definition: capstone.h:125
@ CS_MODE_MICRO
MicroMips mode (MIPS)
Definition: capstone.h:111
@ CS_MODE_MIPS3
Mips III ISA.
Definition: capstone.h:112
@ CS_MODE_MIPS32
Mips32 ISA (Mips)
Definition: capstone.h:124
@ CS_MODE_MIPS32R6
Mips32r6 ISA.
Definition: capstone.h:113
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_MODE_LITTLE_ENDIAN
little-endian mode (default mode)
Definition: capstone.h:103
@ CS_MODE_MIPS2
Mips II ISA.
Definition: capstone.h:114
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
@ CS_OPT_SYNTAX
Assembly output syntax.
Definition: capstone.h:170
size_t csh
Definition: capstone.h:71
@ CS_OPT_SYNTAX_NOREGNAME
Prints register name with only number (CS_OPT_SYNTAX)
Definition: capstone.h:187
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
@ CS_OPT_SYNTAX_DEFAULT
Default asm syntax (CS_OPT_SYNTAX).
Definition: capstone.h:184
#define RZ_API
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 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
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
@ RZ_ASM_SYNTAX_REGNUM
Definition: rz_asm.h:53
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#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)()
static const char * mnemonics[]
Definition: z80asm.c:43