Rizin
unix-like reverse engineering framework and cli tools
asm_xcore_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-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 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
9  csh handle;
10  cs_insn *insn;
11  int mode, n, ret = -1;
13  memset(op, 0, sizeof(RzAsmOp));
14  op->size = 4;
15  ret = cs_open(CS_ARCH_XCORE, mode, &handle);
16  if (ret) {
17  goto fin;
18  }
20  n = cs_disasm(handle, (ut8 *)buf, len, a->pc, 1, &insn);
21  if (n < 1) {
22  rz_asm_op_set_asm(op, "invalid");
23  op->size = 4;
24  ret = -1;
25  goto beach;
26  }
27  ret = 4;
28  if (insn->size < 1) {
29  goto beach;
30  }
31  op->size = insn->size;
32  rz_asm_op_set_asm(op, sdb_fmt("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str));
33 // TODO: remove the '$'<registername> in the string
34 beach:
35  cs_free(insn, n);
36  cs_close(&handle);
37 fin:
38  return ret;
39 }
40 
42  .name = "xcore",
43  .desc = "Capstone XCore disassembler",
44  .license = "BSD",
45  .author = "pancake",
46  .arch = "xcore",
47  .bits = 32,
49  .disassemble = &disassemble,
50 };
51 
52 #ifndef RZ_PLUGIN_INCORE
55  .data = &rz_asm_plugin_xcore_cs,
57 };
58 #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 mcore_handle handle
Definition: asm_mcore.c:8
RzAsmPlugin rz_asm_plugin_xcore_cs
Definition: asm_xcore_cs.c:41
RZ_API RzLibStruct rizin_plugin
Definition: asm_xcore_cs.c:53
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_xcore_cs.c:8
@ CS_ARCH_XCORE
XCore architecture.
Definition: capstone.h:82
@ 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_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
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
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#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