Rizin
unix-like reverse engineering framework and cli tools
asm_riscv_cs.c File Reference
#include <rz_asm.h>
#include <rz_lib.h>
#include <capstone/capstone.h>
#include "cs_mnemonics.c"

Go to the source code of this file.

Functions

static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 

Variables

static csh cd = 0
 
RzAsmPlugin rz_asm_plugin_riscv_cs
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ disassemble()

static int disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 11 of file asm_riscv_cs.c.

11  {
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 }
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 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
@ 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
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_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 a(i)
Definition: sha256.c:41
Definition: dis.c:32

References a, cd, cs_close(), cs_disasm(), cs_free(), cs_open(), CS_OPT_DETAIL, CS_OPT_OFF, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT, CS_OPT_SYNTAX_NOREGNAME, cs_option(), free(), len, n, rz_asm_op_set_asm(), RZ_ASM_SYNTAX_REGNUM, rz_str_newf(), rz_str_replace_char(), and cmd_descs_generate::str.

Variable Documentation

◆ cd

csh cd = 0
static

Definition at line 8 of file asm_riscv_cs.c.

Referenced by disassemble().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
}
RzAsmPlugin rz_asm_plugin_riscv_cs
Definition: asm_riscv_cs.c:54
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 67 of file asm_riscv_cs.c.

◆ rz_asm_plugin_riscv_cs

RzAsmPlugin rz_asm_plugin_riscv_cs
Initial value:
= {
.name = "riscv.cs",
.desc = "Capstone RISCV disassembler",
.license = "BSD",
.arch = "riscv",
.cpus = "",
.bits = 32 | 64,
.disassemble = &disassemble,
.mnemonics = mnemonics,
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_riscv_cs.c:11
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
static const char * mnemonics[]
Definition: z80asm.c:43

Definition at line 54 of file asm_riscv_cs.c.