Rizin
unix-like reverse engineering framework and cli tools
asm_riscv_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 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 static csh cd = 0;
9 #include "cs_mnemonics.c"
10 
11 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
12  cs_insn *insn;
13  int mode = (a->bits == 64) ? CS_MODE_RISCV64 : CS_MODE_RISCV32;
14  op->size = 4;
15  if (cd != 0) {
16  cs_close(&cd);
17  }
18  int ret = cs_open(CS_ARCH_RISCV, mode, &cd);
19  if (ret) {
20  goto fin;
21  }
22 #if 0
23  if (a->syntax == RZ_ASM_SYNTAX_REGNUM) {
25  } else {
27  }
29 #endif
30  int n = cs_disasm(cd, (ut8 *)buf, len, a->pc, 1, &insn);
31  if (n < 1) {
32  rz_asm_op_set_asm(op, "invalid");
33  op->size = 2;
34  goto beach;
35  }
36  if (insn->size < 1) {
37  goto beach;
38  }
39  op->size = insn->size;
40  char *str = rz_str_newf("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str);
41  if (str) {
42  rz_str_replace_char(str, '$', 0);
43  // remove the '$'<registername> in the string
45  free(str);
46  }
47  cs_free(insn, n);
48 beach:
49  // cs_close (&cd);
50 fin:
51  return op->size;
52 }
53 
55  .name = "riscv.cs",
56  .desc = "Capstone RISCV disassembler",
57  .license = "BSD",
58  .arch = "riscv",
59  .cpus = "",
60  .bits = 32 | 64,
62  .disassemble = &disassemble,
63  .mnemonics = mnemonics,
64 };
65 
66 #ifndef RZ_PLUGIN_INCORE
69  .data = &rz_asm_plugin_riscv_cs,
71 };
72 #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
RzAsmPlugin rz_asm_plugin_riscv_cs
Definition: asm_riscv_cs.c:54
RZ_API RzLibStruct rizin_plugin
Definition: asm_riscv_cs.c:67
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_riscv_cs.c:11
static csh cd
Definition: asm_riscv_cs.c:8
@ 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
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
#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
static const char * mnemonics[]
Definition: z80asm.c:43