Rizin
unix-like reverse engineering framework and cli tools
analysis_xcore_cs.c File Reference
#include <rz_analysis.h>
#include <rz_lib.h>
#include <capstone/capstone.h>
#include <capstone/xcore.h>

Go to the source code of this file.

Macros

#define INSOP(n)   insn->detail->xcore.operands[n]
 

Functions

static void opex (RzStrBuf *buf, csh handle, cs_insn *insn)
 
static int analop (RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf, int len, RzAnalysisOpMask mask)
 

Variables

RzAnalysisPlugin rz_analysis_plugin_xcore_cs
 
RZ_API RzLibStruct rizin_plugin
 

Macro Definition Documentation

◆ INSOP

#define INSOP (   n)    insn->detail->xcore.operands[n]

Definition at line 13 of file analysis_xcore_cs.c.

Function Documentation

◆ analop()

static int analop ( RzAnalysis a,
RzAnalysisOp op,
ut64  addr,
const ut8 buf,
int  len,
RzAnalysisOpMask  mask 
)
static

Definition at line 57 of file analysis_xcore_cs.c.

57  {
58  static csh handle = 0;
59  static int omode = 0;
60  cs_insn *insn;
61  int mode, n, ret;
63  if (!strcmp(a->cpu, "v9")) {
64  mode |= CS_MODE_V9;
65  }
66  if (mode != omode) {
67  if (handle) {
68  cs_close(&handle);
69  handle = 0;
70  }
71  omode = mode;
72  }
73  if (handle == 0) {
74  ret = cs_open(CS_ARCH_XCORE, mode, &handle);
75  if (ret != CS_ERR_OK) {
76  return -1;
77  }
79  }
80  // capstone-next
81  n = cs_disasm(handle, (const ut8 *)buf, len, addr, 1, &insn);
82  if (n < 1) {
84  } else {
86  opex(&op->opex, handle, insn);
87  }
88  op->size = insn->size;
89  op->id = insn->id;
90  switch (insn->id) {
91  case XCORE_INS_DRET:
92  case XCORE_INS_KRET:
93  case XCORE_INS_RETSP:
95  break;
96  case XCORE_INS_DCALL:
97  case XCORE_INS_KCALL:
98  case XCORE_INS_ECALLF:
99  case XCORE_INS_ECALLT:
101  op->jump = INSOP(0).imm;
102  break;
103  /* ??? */
104  case XCORE_INS_BL:
105  case XCORE_INS_BLA:
106  case XCORE_INS_BLAT:
107  case XCORE_INS_BT:
108  case XCORE_INS_BF:
109  case XCORE_INS_BU:
110  case XCORE_INS_BRU:
112  op->jump = INSOP(0).imm;
113  break;
114  case XCORE_INS_SUB:
115  case XCORE_INS_LSUB:
116  op->type = RZ_ANALYSIS_OP_TYPE_SUB;
117  break;
118  case XCORE_INS_ADD:
119  case XCORE_INS_LADD:
120  op->type = RZ_ANALYSIS_OP_TYPE_ADD;
121  break;
122  }
123  cs_free(insn, n);
124  }
125  // cs_close (&handle);
126  return op->size;
127 }
size_t len
Definition: 6502dis.c:15
#define mask()
#define INSOP(n)
static void opex(RzStrBuf *buf, csh handle, cs_insn *insn)
static mcore_handle handle
Definition: asm_mcore.c:8
@ CS_ARCH_XCORE
XCore architecture.
Definition: capstone.h:82
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_MODE_V9
SparcV9 mode (Sparc)
Definition: capstone.h:115
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
@ XCORE_INS_BL
Definition: xcore.h:104
@ XCORE_INS_LSUB
Definition: xcore.h:165
@ XCORE_INS_SUB
Definition: xcore.h:204
@ XCORE_INS_BLA
Definition: xcore.h:102
@ XCORE_INS_BT
Definition: xcore.h:106
@ XCORE_INS_KCALL
Definition: xcore.h:150
@ XCORE_INS_BRU
Definition: xcore.h:108
@ XCORE_INS_BLAT
Definition: xcore.h:103
@ XCORE_INS_ECALLF
Definition: xcore.h:124
@ XCORE_INS_RETSP
Definition: xcore.h:184
@ XCORE_INS_ECALLT
Definition: xcore.h:125
@ XCORE_INS_DCALL
Definition: xcore.h:117
@ XCORE_INS_ADD
Definition: xcore.h:96
@ XCORE_INS_BU
Definition: xcore.h:107
@ XCORE_INS_DRET
Definition: xcore.h:123
@ XCORE_INS_BF
Definition: xcore.h:105
@ XCORE_INS_LADD
Definition: xcore.h:154
@ XCORE_INS_KRET
Definition: xcore.h:153
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
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
int CS_ERR_OK
Definition: __init__.py:235
@ RZ_ANALYSIS_OP_MASK_OPEX
Definition: rz_analysis.h:444
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_CALL
Definition: rz_analysis.h:378
@ RZ_ANALYSIS_OP_TYPE_ADD
Definition: rz_analysis.h:401
@ RZ_ANALYSIS_OP_TYPE_ILL
Definition: rz_analysis.h:387
@ RZ_ANALYSIS_OP_TYPE_RET
Definition: rz_analysis.h:385
#define a(i)
Definition: sha256.c:41
Definition: dis.c:32
static int addr
Definition: z80asm.c:58

References a, addr, CS_ARCH_XCORE, cs_close(), cs_disasm(), capstone::CS_ERR_OK, cs_free(), CS_MODE_BIG_ENDIAN, CS_MODE_V9, cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), handle, INSOP, len, mask, n, opex(), RZ_ANALYSIS_OP_MASK_OPEX, RZ_ANALYSIS_OP_TYPE_ADD, RZ_ANALYSIS_OP_TYPE_CALL, RZ_ANALYSIS_OP_TYPE_ILL, RZ_ANALYSIS_OP_TYPE_RET, RZ_ANALYSIS_OP_TYPE_SUB, XCORE_INS_ADD, XCORE_INS_BF, XCORE_INS_BL, XCORE_INS_BLA, XCORE_INS_BLAT, XCORE_INS_BRU, XCORE_INS_BT, XCORE_INS_BU, XCORE_INS_DCALL, XCORE_INS_DRET, XCORE_INS_ECALLF, XCORE_INS_ECALLT, XCORE_INS_KCALL, XCORE_INS_KRET, XCORE_INS_LADD, XCORE_INS_LSUB, XCORE_INS_RETSP, and XCORE_INS_SUB.

◆ opex()

static void opex ( RzStrBuf buf,
csh  handle,
cs_insn *  insn 
)
static

Definition at line 15 of file analysis_xcore_cs.c.

15  {
16  int i;
17  PJ *pj = pj_new();
18  if (!pj) {
19  return;
20  }
21  pj_o(pj);
22  pj_ka(pj, "operands");
23  cs_xcore *x = &insn->detail->xcore;
24  for (i = 0; i < x->op_count; i++) {
25  cs_xcore_op *op = x->operands + i;
26  pj_o(pj);
27  switch (op->type) {
28  case XCORE_OP_REG:
29  pj_ks(pj, "type", "reg");
30  pj_ks(pj, "value", cs_reg_name(handle, op->reg));
31  break;
32  case XCORE_OP_IMM:
33  pj_ks(pj, "type", "imm");
34  pj_ki(pj, "value", op->imm);
35  break;
36  case XCORE_OP_MEM:
37  pj_ks(pj, "type", "mem");
38  if (op->mem.base != XCORE_REG_INVALID) {
39  pj_ks(pj, "base", cs_reg_name(handle, op->mem.base));
40  }
41  pj_ki(pj, "disp", op->mem.disp);
42  break;
43  default:
44  pj_ks(pj, "type", "invalid");
45  break;
46  }
47  pj_end(pj); /* o operand */
48  }
49  pj_end(pj); /* a operands */
50  pj_end(pj);
51 
54  pj_free(pj);
55 }
lzma_index ** i
Definition: index.h:629
@ XCORE_REG_INVALID
Definition: xcore.h:27
@ XCORE_OP_REG
= CS_OP_REG (Register operand).
Definition: xcore.h:20
@ XCORE_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: xcore.h:21
@ XCORE_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: xcore.h:22
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
int x
Definition: mipsasm.c:20
RZ_API PJ * pj_ka(PJ *j, const char *k)
Definition: pj.c:163
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_ki(PJ *j, const char *k, int d)
Definition: pj.c:149
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
Instruction operand.
Definition: xcore.h:75
Instruction structure.
Definition: xcore.h:85
Definition: rz_pj.h:12

References cs_reg_name(), handle, i, pj_end(), pj_free(), pj_ka(), pj_ki(), pj_ks(), pj_new(), pj_o(), pj_string(), rz_strbuf_append(), rz_strbuf_init(), x, XCORE_OP_IMM, XCORE_OP_MEM, XCORE_OP_REG, and XCORE_REG_INVALID.

Referenced by analop().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
}
RzAnalysisPlugin rz_analysis_plugin_xcore_cs
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_analysis.h:1239

Definition at line 141 of file analysis_xcore_cs.c.

◆ rz_analysis_plugin_xcore_cs

RzAnalysisPlugin rz_analysis_plugin_xcore_cs
Initial value:
= {
.name = "xcore",
.desc = "Capstone XCORE analysis",
.license = "BSD",
.esil = false,
.arch = "xcore",
.bits = 32,
.op = &analop,
}
static int analop(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf, int len, RzAnalysisOpMask mask)

Definition at line 129 of file analysis_xcore_cs.c.