Rizin
unix-like reverse engineering framework and cli tools
analysis_xcore_cs.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_analysis.h>
5 #include <rz_lib.h>
6 #include <capstone/capstone.h>
7 #include <capstone/xcore.h>
8 
9 #if CS_API_MAJOR < 2
10 #error Old Capstone not supported
11 #endif
12 
13 #define INSOP(n) insn->detail->xcore.operands[n]
14 
15 static void opex(RzStrBuf *buf, csh handle, cs_insn *insn) {
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 }
56 
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 }
128 
130  .name = "xcore",
131  .desc = "Capstone XCORE analysis",
132  .license = "BSD",
133  .esil = false,
134  .arch = "xcore",
135  .bits = 32,
136  .op = &analop,
137  //.set_reg_profile = &set_reg_profile,
138 };
139 
140 #ifndef RZ_PLUGIN_INCORE
145 };
146 #endif
size_t len
Definition: 6502dis.c:15
#define mask()
RzAnalysisPlugin rz_analysis_plugin_xcore_cs
#define INSOP(n)
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)
RZ_API RzLibStruct rizin_plugin
lzma_index ** i
Definition: index.h:629
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_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
@ 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
#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 const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
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 x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
int CS_ERR_OK
Definition: __init__.py:235
RzAnalysisOpMask
Definition: rz_analysis.h:439
@ 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
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
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
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
Instruction operand.
Definition: xcore.h:75
Instruction structure.
Definition: xcore.h:85
Definition: rz_pj.h:12
const char * version
Definition: rz_analysis.h:1239
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58