Rizin
unix-like reverse engineering framework and cli tools
analysis_tms320c64x.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 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 
8 #ifdef CAPSTONE_TMS320C64X_H
9 #define CAPSTONE_HAS_TMS320C64X 1
10 #else
11 #define CAPSTONE_HAS_TMS320C64X 0
12 #warning Cannot find capstone-tms320c64x support
13 #endif
14 
15 #if CS_API_MAJOR < 2
16 #undef CAPSONT_HAS_TMS320C64X
17 #define CAPSTONE_HAS_TMS320C64X 0
18 #endif
19 
20 #if CAPSTONE_HAS_TMS320C64X
21 
22 #define INSOP(n) insn->detail->tms320c64x.operands[n]
23 #define INSCC insn->detail->tms320c64x.cc
24 
25 static void opex(RzStrBuf *buf, csh handle, cs_insn *insn) {
26  int i;
27  PJ *pj = pj_new();
28  if (!pj) {
29  return;
30  }
31  pj_o(pj);
32  pj_ka(pj, "operands");
33  cs_tms320c64x *x = &insn->detail->tms320c64x;
34  for (i = 0; i < x->op_count; i++) {
35  cs_tms320c64x_op *op = x->operands + i;
36  pj_o(pj);
37  switch (op->type) {
38  case TMS320C64X_OP_REG:
39  pj_ks(pj, "type", "reg");
40  pj_ks(pj, "value", cs_reg_name(handle, op->reg));
41  break;
42  case TMS320C64X_OP_IMM:
43  pj_ks(pj, "type", "imm");
44  pj_ki(pj, "value", op->imm);
45  break;
46  case TMS320C64X_OP_MEM:
47  pj_ks(pj, "type", "mem");
48  if (op->mem.base != SPARC_REG_INVALID) {
49  pj_ks(pj, "base", cs_reg_name(handle, op->mem.base));
50  }
51  pj_kN(pj, "disp", (st64)op->mem.disp);
52  break;
53  default:
54  pj_ks(pj, "type", "invalid");
55  break;
56  }
57  pj_end(pj); /* o operand */
58  }
59  pj_end(pj); /* a operands */
60  pj_end(pj);
61 
64  pj_free(pj);
65 }
66 
67 static int tms320c64x_analop(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf, int len, RzAnalysisOpMask mask) {
68  static csh handle = 0;
69  static int omode;
70  cs_insn *insn;
71  int mode = 0, n, ret;
72 
73  if (mode != omode) {
74  cs_close(&handle);
75  handle = 0;
76  omode = mode;
77  }
78  if (handle == 0) {
80  if (ret != CS_ERR_OK) {
81  return -1;
82  }
84  }
85  // capstone-next
86  n = cs_disasm(handle, (const ut8 *)buf, len, addr, 1, &insn);
87  if (n < 1) {
89  } else {
91  opex(&op->opex, handle, insn);
92  }
93  op->size = insn->size;
94  op->id = insn->id;
95  switch (insn->id) {
98  break;
99  case TMS320C64X_INS_AND:
100  case TMS320C64X_INS_ANDN:
101  op->type = RZ_ANALYSIS_OP_TYPE_AND;
102  break;
103  case TMS320C64X_INS_NOT:
104  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
105  break;
106  case TMS320C64X_INS_NEG:
107  op->type = RZ_ANALYSIS_OP_TYPE_NOT;
108  break;
111  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
112  op->type = RZ_ANALYSIS_OP_TYPE_MOV;
113  break;
114  case TMS320C64X_INS_BNOP:
115  case TMS320C64X_INS_NOP:
116  op->type = RZ_ANALYSIS_OP_TYPE_NOP;
117  break;
126  op->type = RZ_ANALYSIS_OP_TYPE_CMP;
127  break;
128  case TMS320C64X_INS_B:
129  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
130  // higher 32bits of the 64bit address is lost, lets clone
131  op->jump = INSOP(0).imm + (addr & 0xFFFFFFFF00000000);
132  break;
133  case TMS320C64X_INS_LDB:
134  case TMS320C64X_INS_LDBU:
135  case TMS320C64X_INS_LDDW:
136  case TMS320C64X_INS_LDH:
137  case TMS320C64X_INS_LDHU:
139  case TMS320C64X_INS_LDNW:
140  case TMS320C64X_INS_LDW:
141  case TMS320C64X_INS_LMBD:
143  break;
144  case TMS320C64X_INS_STB:
145  case TMS320C64X_INS_STDW:
146  case TMS320C64X_INS_STH:
148  case TMS320C64X_INS_STNW:
149  case TMS320C64X_INS_STW:
151  break;
152  case TMS320C64X_INS_OR:
153  op->type = RZ_ANALYSIS_OP_TYPE_OR;
154  break;
155  case TMS320C64X_INS_SSUB:
156  case TMS320C64X_INS_SUB:
157  case TMS320C64X_INS_SUB2:
158  case TMS320C64X_INS_SUB4:
163  case TMS320C64X_INS_SUBC:
164  case TMS320C64X_INS_SUBU:
165  op->type = RZ_ANALYSIS_OP_TYPE_SUB;
166  break;
167  case TMS320C64X_INS_ADD:
168  case TMS320C64X_INS_ADD2:
169  case TMS320C64X_INS_ADD4:
174  case TMS320C64X_INS_ADDK:
176  case TMS320C64X_INS_ADDU:
177  case TMS320C64X_INS_SADD:
181  op->type = RZ_ANALYSIS_OP_TYPE_ADD;
182  break;
183  }
184  cs_free(insn, n);
185  }
186  return op->size;
187 }
188 #endif
size_t len
Definition: 6502dis.c:15
static void opex(RzStrBuf *buf, csh handle, cs_insn *insn)
#define mask()
lzma_index ** i
Definition: index.h:629
#define INSOP(x)
static mcore_handle handle
Definition: asm_mcore.c:8
@ CS_ARCH_TMS320C64X
TMS320C64x architecture.
Definition: capstone.h:84
@ 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
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_CMP
Definition: rz_analysis.h:399
@ RZ_ANALYSIS_OP_TYPE_SUB
Definition: rz_analysis.h:402
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ RZ_ANALYSIS_OP_TYPE_JMP
Definition: rz_analysis.h:368
@ RZ_ANALYSIS_OP_TYPE_AND
Definition: rz_analysis.h:411
@ RZ_ANALYSIS_OP_TYPE_ADD
Definition: rz_analysis.h:401
@ RZ_ANALYSIS_OP_TYPE_OR
Definition: rz_analysis.h:410
@ RZ_ANALYSIS_OP_TYPE_STORE
Definition: rz_analysis.h:415
@ RZ_ANALYSIS_OP_TYPE_MOV
Definition: rz_analysis.h:390
@ RZ_ANALYSIS_OP_TYPE_ILL
Definition: rz_analysis.h:387
@ RZ_ANALYSIS_OP_TYPE_NOT
Definition: rz_analysis.h:414
@ RZ_ANALYSIS_OP_TYPE_NOP
Definition: rz_analysis.h:389
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 PJ * pj_kN(PJ *j, const char *k, st64 n)
Definition: pj.c:128
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 st64
Definition: rz_types_base.h:10
#define a(i)
Definition: sha256.c:41
Definition: rz_pj.h:12
@ SPARC_REG_INVALID
Definition: sparc.h:79
@ TMS320C64X_INS_ADDAH
Definition: tms320c64x.h:189
@ TMS320C64X_INS_SUBAH
Definition: tms320c64x.h:310
@ TMS320C64X_INS_LDHU
Definition: tms320c64x.h:227
@ TMS320C64X_INS_SUB4
Definition: tms320c64x.h:307
@ TMS320C64X_INS_CMPLTU
Definition: tms320c64x.h:211
@ TMS320C64X_INS_ADDAD
Definition: tms320c64x.h:188
@ TMS320C64X_INS_LDBU
Definition: tms320c64x.h:224
@ TMS320C64X_INS_INVALID
Definition: tms320c64x.h:180
@ TMS320C64X_INS_STNW
Definition: tms320c64x.h:303
@ TMS320C64X_INS_SADD2
Definition: tms320c64x.h:275
@ TMS320C64X_INS_NEG
Definition: tms320c64x.h:323
@ TMS320C64X_INS_ADD2
Definition: tms320c64x.h:185
@ TMS320C64X_INS_SADD
Definition: tms320c64x.h:274
@ TMS320C64X_INS_SUB
Definition: tms320c64x.h:305
@ TMS320C64X_INS_ADDKPC
Definition: tms320c64x.h:192
@ TMS320C64X_INS_SADDU4
Definition: tms320c64x.h:276
@ TMS320C64X_INS_LDDW
Definition: tms320c64x.h:225
@ TMS320C64X_INS_SUBAB
Definition: tms320c64x.h:308
@ TMS320C64X_INS_ADDU
Definition: tms320c64x.h:193
@ TMS320C64X_INS_CMPEQ4
Definition: tms320c64x.h:206
@ TMS320C64X_INS_ANDN
Definition: tms320c64x.h:195
@ TMS320C64X_INS_SWAP2
Definition: tms320c64x.h:325
@ TMS320C64X_INS_ADD
Definition: tms320c64x.h:184
@ TMS320C64X_INS_LDH
Definition: tms320c64x.h:226
@ TMS320C64X_INS_SUBU
Definition: tms320c64x.h:313
@ TMS320C64X_INS_ADDAW
Definition: tms320c64x.h:190
@ TMS320C64X_INS_CMPEQ2
Definition: tms320c64x.h:205
@ TMS320C64X_INS_CMPGT
Definition: tms320c64x.h:207
@ TMS320C64X_INS_SUB2
Definition: tms320c64x.h:306
@ TMS320C64X_INS_OR
Definition: tms320c64x.h:266
@ TMS320C64X_INS_LDNW
Definition: tms320c64x.h:229
@ TMS320C64X_INS_BNOP
Definition: tms320c64x.h:201
@ TMS320C64X_INS_B
Definition: tms320c64x.h:198
@ TMS320C64X_INS_SUBABS4
Definition: tms320c64x.h:309
@ TMS320C64X_INS_SSUB
Definition: tms320c64x.h:298
@ TMS320C64X_INS_STB
Definition: tms320c64x.h:299
@ TMS320C64X_INS_STH
Definition: tms320c64x.h:301
@ TMS320C64X_INS_LDB
Definition: tms320c64x.h:223
@ TMS320C64X_INS_AND
Definition: tms320c64x.h:194
@ TMS320C64X_INS_STDW
Definition: tms320c64x.h:300
@ TMS320C64X_INS_CMPGTU4
Definition: tms320c64x.h:209
@ TMS320C64X_INS_CMPLT
Definition: tms320c64x.h:210
@ TMS320C64X_INS_SADDUS2
Definition: tms320c64x.h:277
@ TMS320C64X_INS_SUBAW
Definition: tms320c64x.h:311
@ TMS320C64X_INS_LDW
Definition: tms320c64x.h:230
@ TMS320C64X_INS_ADDAB
Definition: tms320c64x.h:187
@ TMS320C64X_INS_CMPGT2
Definition: tms320c64x.h:208
@ TMS320C64X_INS_NOT
Definition: tms320c64x.h:324
@ TMS320C64X_INS_LDNDW
Definition: tms320c64x.h:228
@ TMS320C64X_INS_STNDW
Definition: tms320c64x.h:302
@ TMS320C64X_INS_CMPEQ
Definition: tms320c64x.h:204
@ TMS320C64X_INS_SWAP4
Definition: tms320c64x.h:314
@ TMS320C64X_INS_NOP
Definition: tms320c64x.h:264
@ TMS320C64X_INS_LMBD
Definition: tms320c64x.h:231
@ TMS320C64X_INS_ADDK
Definition: tms320c64x.h:191
@ TMS320C64X_INS_ADD4
Definition: tms320c64x.h:186
@ TMS320C64X_INS_STW
Definition: tms320c64x.h:304
@ TMS320C64X_INS_SUBC
Definition: tms320c64x.h:312
@ TMS320C64X_OP_REG
= CS_OP_REG (Register operand).
Definition: tms320c64x.h:20
@ TMS320C64X_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: tms320c64x.h:21
@ TMS320C64X_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: tms320c64x.h:22
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58