Rizin
unix-like reverse engineering framework and cli tools
asm_tms320.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014 Ilya V. Matveychikov <i.matveychikov@milabs.ru>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <stdio.h>
5 #include <rz_types.h>
6 #include <rz_lib.h>
7 #include <rz_asm.h>
8 #include <capstone/capstone.h>
9 
10 #ifdef CAPSTONE_TMS320C64X_H
11 #define CAPSTONE_HAS_TMS320C64X 1
12 //#include "cs_mnemonics.c"
13 #else
14 #define CAPSTONE_HAS_TMS320C64X 0
15 #warning Cannot find capstone-tms320c64x support
16 #endif
17 
18 #include "../arch/tms320/tms320_dasm.h"
19 
20 typedef struct tms_cs_context_t {
21 #if CAPSTONE_HAS_TMS320C64X
22  csh cd;
23 #endif
26 
27 #if CAPSTONE_HAS_TMS320C64X
28 
29 static int tms320c64x_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
30  TmsContext *ctx = (TmsContext *)a->plugin_data;
31 
32  cs_insn *insn;
33  int n = -1, ret = -1;
34  int mode = 0;
35  if (op) {
36  memset(op, 0, sizeof(RzAsmOp));
37  op->size = 4;
38  }
39  if (ctx->cd != 0) {
40  cs_close(&ctx->cd);
41  }
42  ret = cs_open(CS_ARCH_TMS320C64X, mode, &ctx->cd);
43  if (ret) {
44  goto fin;
45  }
47  if (!op) {
48  return 0;
49  }
50  n = cs_disasm(ctx->cd, buf, len, a->pc, 1, &insn);
51  if (n < 1) {
52  rz_asm_op_set_asm(op, "invalid");
53  op->size = 4;
54  ret = -1;
55  goto beach;
56  } else {
57  ret = 4;
58  }
59  if (insn->size < 1) {
60  goto beach;
61  }
62  op->size = insn->size;
63  char *buf_asm = sdb_fmt("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str);
64  rz_str_replace_char(buf_asm, '%', 0);
65  rz_str_case(buf_asm, false);
66  rz_asm_op_set_asm(op, buf_asm);
67  cs_free(insn, n);
68 beach:
69 // cs_close (&ctx->cd);
70 fin:
71  return ret;
72 }
73 #endif
74 
75 static int tms320_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
76  TmsContext *ctx = (TmsContext *)a->plugin_data;
77  if (a->cpu && rz_str_casecmp(a->cpu, "c54x") == 0) {
79  } else if (a->cpu && rz_str_casecmp(a->cpu, "c55x+") == 0) {
81  } else if (a->cpu && rz_str_casecmp(a->cpu, "c55x") == 0) {
83  } else {
84 #if CAPSTONE_HAS_TMS320C64X
85  if (a->cpu && !rz_str_casecmp(a->cpu, "c64x")) {
86  return tms320c64x_disassemble(a, op, buf, len);
87  }
88 #endif
89  rz_asm_op_set_asm(op, "unknown asm.cpu");
90  return op->size = -1;
91  }
92  op->size = tms320_dasm(&ctx->engine, buf, len);
93  rz_asm_op_set_asm(op, ctx->engine.syntax);
94  return op->size;
95 }
96 
97 static bool tms320_init(void **user) {
99  if (!ctx) {
100  return false;
101  }
102  tms320_dasm_init(&ctx->engine);
103  *user = ctx;
104  return true;
105 }
106 
107 static bool tms320_fini(void *user) {
108  rz_return_val_if_fail(user, false);
109  TmsContext *ctx = (TmsContext *)user;
110 #if CAPSTONE_HAS_TMS320C64X
111  cs_close(&ctx->cd);
112 #endif
113  tms320_dasm_fini(&ctx->engine);
114  free(ctx);
115  return true;
116 }
117 
119  .name = "tms320",
120  .arch = "tms320",
121 #if CAPSTONE_HAS_TMS320C64X
122  .cpus = "c54x,c55x,c55x+,c64x",
123  .desc = "TMS320 DSP family (c54x,c55x,c55x+,c64x)",
124 #else
125  .cpus = "c54x,c55x,c55x+",
126  .desc = "TMS320 DSP family (c54x,c55x,c55x+)",
127 #endif
128  .license = "LGPLv3",
129  .bits = 32,
131  .init = tms320_init,
132  .fini = tms320_fini,
133  .disassemble = &tms320_disassemble,
134 };
135 
136 #ifndef RZ_PLUGIN_INCORE
139  .data = &rz_asm_plugin_tms320,
141 };
142 #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 csh cd
Definition: asm_mips_cs.c:10
static bool tms320_fini(void *user)
Definition: asm_tms320.c:107
static bool tms320_init(void **user)
Definition: asm_tms320.c:97
RZ_API RzLibStruct rizin_plugin
Definition: asm_tms320.c:137
struct tms_cs_context_t TmsContext
RzAsmPlugin rz_asm_plugin_tms320
Definition: asm_tms320.c:118
static int tms320_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_tms320.c:75
@ 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_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
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
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API int rz_str_casecmp(const char *dst, const char *orig)
Definition: str.c:121
RZ_API void rz_str_case(char *str, bool up)
Definition: str.c:341
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#define RZ_NEW0(x)
Definition: rz_types.h:284
#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
tms320_dasm_t engine
Definition: asm_tms320.c:24
int tms320_dasm_fini(tms320_dasm_t *dasm)
Definition: tms320_dasm.c:1202
int tms320_dasm_init(tms320_dasm_t *dasm)
Definition: tms320_dasm.c:1181
int tms320_dasm(tms320_dasm_t *dasm, const ut8 *stream, int len)
Definition: tms320_dasm.c:1154
#define tms320_f_set_cpu(d, v)
Definition: tms320_dasm.h:215
#define TMS320_F_CPU_C55X_PLUS
Definition: tms320_dasm.h:211
#define TMS320_F_CPU_C54X
Definition: tms320_dasm.h:209
#define TMS320_F_CPU_C55X
Definition: tms320_dasm.h:210
Definition: dis.c:32
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4