Rizin
unix-like reverse engineering framework and cli tools
asm_mips_cs.c File Reference
#include <rz_asm.h>
#include <rz_lib.h>
#include <capstone/capstone.h>
#include "cs_mnemonics.c"

Go to the source code of this file.

Functions

RZ_IPI int mips_assemble (const char *str, ut64 pc, ut8 *out)
 
static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 
static int assemble (RzAsm *a, RzAsmOp *op, const char *str)
 

Variables

static csh cd = 0
 
RzAsmPlugin rz_asm_plugin_mips_cs
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ assemble()

static int assemble ( RzAsm a,
RzAsmOp op,
const char *  str 
)
static

Definition at line 73 of file asm_mips_cs.c.

73  {
74  ut8 *opbuf = (ut8 *)rz_strbuf_get(&op->buf);
75  int ret = mips_assemble(str, a->pc, opbuf);
76  if (a->big_endian) {
77  ut8 *buf = opbuf;
78  ut8 tmp = buf[0];
79  buf[0] = buf[3];
80  buf[3] = tmp;
81  tmp = buf[1];
82  buf[1] = buf[2];
83  buf[2] = tmp;
84  }
85  return ret;
86 }
RZ_IPI int mips_assemble(const char *str, ut64 pc, ut8 *out)
Definition: mipsasm.c:148
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
#define a(i)
Definition: sha256.c:41
Definition: dis.c:32

References a, mips_assemble(), rz_strbuf_get(), cmd_descs_generate::str, and autogen_x86imm::tmp.

◆ disassemble()

static int disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 13 of file asm_mips_cs.c.

13  {
14  cs_insn *insn;
15  int mode, n, ret = -1;
16  mode = (a->big_endian) ? CS_MODE_BIG_ENDIAN : CS_MODE_LITTLE_ENDIAN;
17  if (!op) {
18  return 0;
19  }
20  if (a->cpu && *a->cpu) {
21  if (!strcmp(a->cpu, "micro")) {
23  } else if (!strcmp(a->cpu, "r6")) {
25  } else if (!strcmp(a->cpu, "v3")) {
27  } else if (!strcmp(a->cpu, "v2")) {
28 #if CS_API_MAJOR > 3
30 #endif
31  }
32  }
33  mode |= (a->bits == 64) ? CS_MODE_MIPS64 : CS_MODE_MIPS32;
34  memset(op, 0, sizeof(RzAsmOp));
35  op->size = 4;
36  if (cd != 0) {
37  cs_close(&cd);
38  }
39  ret = cs_open(CS_ARCH_MIPS, mode, &cd);
40  if (ret) {
41  goto fin;
42  }
43  if (a->syntax == RZ_ASM_SYNTAX_REGNUM) {
45  } else {
47  }
49  n = cs_disasm(cd, (ut8 *)buf, len, a->pc, 1, &insn);
50  if (n < 1) {
51  rz_asm_op_set_asm(op, "invalid");
52  op->size = 4;
53  goto beach;
54  }
55  if (insn->size < 1) {
56  goto beach;
57  }
58  op->size = insn->size;
59  char *str = rz_str_newf("%s%s%s", insn->mnemonic, insn->op_str[0] ? " " : "", insn->op_str);
60  if (str) {
61  rz_str_replace_char(str, '$', 0);
62  // remove the '$'<registername> in the string
64  free(str);
65  }
66  cs_free(insn, n);
67 beach:
68  // cs_close (&cd);
69 fin:
70  return op->size;
71 }
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
@ CS_ARCH_MIPS
Mips architecture.
Definition: capstone.h:77
@ CS_MODE_MIPS64
Mips64 ISA (Mips)
Definition: capstone.h:125
@ CS_MODE_MICRO
MicroMips mode (MIPS)
Definition: capstone.h:111
@ CS_MODE_MIPS3
Mips III ISA.
Definition: capstone.h:112
@ CS_MODE_MIPS32
Mips32 ISA (Mips)
Definition: capstone.h:124
@ CS_MODE_MIPS32R6
Mips32r6 ISA.
Definition: capstone.h:113
@ 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_MODE_MIPS2
Mips II ISA.
Definition: capstone.h:114
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
@ CS_OPT_SYNTAX
Assembly output syntax.
Definition: capstone.h:170
@ CS_OPT_SYNTAX_NOREGNAME
Prints register name with only number (CS_OPT_SYNTAX)
Definition: capstone.h:187
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
@ CS_OPT_SYNTAX_DEFAULT
Default asm syntax (CS_OPT_SYNTAX).
Definition: capstone.h:184
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 void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
return memset(p, 0, total)
int n
Definition: mipsasm.c:19
@ RZ_ASM_SYNTAX_REGNUM
Definition: rz_asm.h:53
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API int rz_str_replace_char(char *s, int a, int b)
Definition: str.c:169

References a, cd, CS_ARCH_MIPS, cs_close(), cs_disasm(), cs_free(), CS_MODE_BIG_ENDIAN, CS_MODE_LITTLE_ENDIAN, CS_MODE_MICRO, CS_MODE_MIPS2, CS_MODE_MIPS3, CS_MODE_MIPS32, CS_MODE_MIPS32R6, CS_MODE_MIPS64, cs_open(), CS_OPT_DETAIL, CS_OPT_OFF, CS_OPT_SYNTAX, CS_OPT_SYNTAX_DEFAULT, CS_OPT_SYNTAX_NOREGNAME, cs_option(), free(), len, memset(), n, rz_asm_op_set_asm(), RZ_ASM_SYNTAX_REGNUM, rz_str_newf(), rz_str_replace_char(), and cmd_descs_generate::str.

◆ mips_assemble()

RZ_IPI int mips_assemble ( const char *  str,
ut64  pc,
ut8 out 
)

Definition at line 148 of file mipsasm.c.

148  {
149  int i, hasp;
150  char w0[32], w1[32], w2[32], w3[32];
151  char *s = strdup(str);
152  if (!s) {
153  return -1;
154  }
155 
156  rz_str_replace_char(s, ',', ' ');
157  hasp = rz_str_replace_char(s, '(', ' ');
158  rz_str_replace_char(s, ')', ' ');
159 
160  *out = 0;
161  *w0 = 0;
162  *w1 = 0;
163  *w2 = 0;
164  *w3 = 0;
165 
166  if (!strncmp(s, "jalr", 4) && !strchr(s, ',')) {
167  char opstr[32];
168  const char *arg = strchr(s, ' ');
169  if (arg) {
170  snprintf(opstr, sizeof(opstr), "jalr ra ra %s", arg + 1);
171  free(s);
172  s = strdup(opstr);
173  if (!s) {
174  return -1;
175  }
176  }
177  }
178 
179  sscanf(s, "%31s", w0);
180  if (*w0) {
181  for (i = 0; ops[i].name; i++) {
182  if (!strcmp(ops[i].name, w0)) {
183  switch (ops[i].args) {
184  case 3: sscanf(s, "%31s %31s %31s %31s", w0, w1, w2, w3); break;
185  case -3: sscanf(s, "%31s %31s %31s %31s", w0, w1, w2, w3); break;
186  case 2: sscanf(s, "%31s %31s %31s", w0, w1, w2); break;
187  case -2: sscanf(s, "%31s %31s %31s", w0, w1, w2); break;
188  case 1: sscanf(s, "%31s %31s", w0, w1); break;
189  case -1: sscanf(s, "%31s %31s", w0, w1); break;
190  case 0: sscanf(s, "%31s", w0); break;
191  }
192  if (hasp) {
193  char tmp[32];
194  strcpy(tmp, w2);
195  strcpy(w2, w3);
196  strcpy(w3, tmp);
197  }
198  switch (ops[i].type) {
199  case 'R': {
200  // reg order diff per instruction 'group' - ordered to number of likelyhood to call (add > mfhi)
201  int op = 0, rs = 0, rt = 0, rd = 0, sa = 0, fn = 0;
202  bool invalid = false;
203  switch (ops[i].args) {
204  case 3:
205  rs = getreg(w2);
206  rt = getreg(w3);
207  rd = getreg(w1);
208  fn = ops[i].n;
209  break;
210  case -3:
211  if (ops[i].n > -1) {
212  rt = getreg(w2);
213  rd = getreg(w1);
214  sa = getreg(w3);
215  fn = ops[i].n;
216  } else {
217  rs = getreg(w3);
218  rt = getreg(w2);
219  rd = getreg(w1);
220  fn = (-1 * ops[i].n);
221  }
222  break;
223  case 2:
224  rs = getreg(w1);
225  rt = getreg(w2);
226  fn = ops[i].n;
227  break;
228  case 1:
229  rs = getreg(w1);
230  fn = ops[i].n;
231  break;
232  case -2:
233  rs = getreg(w2);
234  rd = getreg(w1);
235  fn = ops[i].n;
236  break;
237  case -1:
238  rd = getreg(w1);
239  fn = ops[i].n;
240  break;
241  case 0:
242  fn = ops[i].n;
243  break;
244  default:
245  invalid = true;
246  break;
247  }
248  if (!invalid) {
249  free(s);
250  return mips_r(out, op, rs, rt, rd, sa, fn);
251  }
252  break;
253  }
254  case 'I':
255  case 'B': {
256  bool invalid = false;
257  int op = 0, rs = 0, rt = 0, imm = 0, is_branch = ops[i].type == 'B';
258  switch (ops[i].args) {
259  case 2:
260  op = ops[i].n;
261  rt = getreg(w1);
262  imm = getreg(w2);
263  break;
264  case 3:
265  op = ops[i].n;
266  rs = getreg(w2);
267  rt = getreg(w1);
268  imm = getreg(w3);
269  break;
270  case -2:
271  if (ops[i].n > 0) {
272  op = ops[i].n;
273  rs = getreg(w1);
274  imm = getreg(w2);
275  } else {
276  op = (-1 * ops[i].n);
277  rs = getreg(w1);
278  rt = ops[i].x;
279  imm = getreg(w2);
280  }
281  break;
282  case -1:
283  if (ops[i].n > 0) {
284  op = ops[i].n;
285  imm = getreg(w1);
286  } else {
287  op = (-1 * ops[i].n);
288  rt = ops[i].x;
289  imm = getreg(w1);
290  }
291  break;
292  default:
293  invalid = true;
294  break;
295  }
296  if (!invalid) {
297  free(s);
298  return mips_i(out, op, rs, rt, imm, is_branch);
299  }
300  break;
301  }
302  case 'J':
303  if (ops[i].args == 1) {
304  free(s);
305  return mips_j(out, ops[i].n, getreg(w1));
306  }
307  break;
308  case 'N': // nop
309  memset(out, 0, 4);
310  free(s);
311  return 4;
312  }
313  free(s);
314  return -1;
315  }
316  }
317  }
318  free(s);
319  return -1;
320 }
#define rs()
#define rd()
#define imm
lzma_index ** i
Definition: index.h:629
static int opstr(RzAsm *a, ut8 *data, const Opcode *op)
Definition: asm_x86_nz.c:4054
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
snprintf
Definition: kernel.h:364
static const char struct stat static buf struct stat static buf static idle const char static path static fd const char static len const void static prot const char struct module static image struct kernel_sym static table unsigned char static buf static fsuid unsigned struct dirent unsigned static count const struct iovec static count static pid const void static len static flags const struct sched_param static p static pid static policy struct timespec static tp static suid unsigned fn
Definition: sflib.h:186
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
static int mips_r(ut8 *b, int op, int rs, int rt, int rd, int sa, int fun)
Definition: mipsasm.c:80
int args
Definition: mipsasm.c:18
static struct @104 ops[]
static int getreg(const char *p)
Definition: mipsasm.c:122
static int mips_j(ut8 *b, int op, int addr)
Definition: mipsasm.c:113
int type
Definition: mipsasm.c:17
static int mips_i(ut8 *b, int op, int rs, int rt, int imm, int is_branch)
Definition: mipsasm.c:94
def is_branch(insn)
static RzSocket * s
Definition: rtr.c:28
Definition: z80asm.h:102

Referenced by assemble().

Variable Documentation

◆ cd

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
}
RzAsmPlugin rz_asm_plugin_mips_cs
Definition: asm_mips_cs.c:88
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 102 of file asm_mips_cs.c.

◆ rz_asm_plugin_mips_cs

RzAsmPlugin rz_asm_plugin_mips_cs
Initial value:
= {
.name = "mips",
.desc = "Capstone MIPS disassembler",
.license = "BSD",
.arch = "mips",
.cpus = "mips32/64,micro,r6,v3,v2",
.bits = 16 | 32 | 64,
.disassemble = &disassemble,
.mnemonics = mnemonics,
.assemble = &assemble
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_mips_cs.c:13
static int assemble(RzAsm *a, RzAsmOp *op, const char *str)
Definition: asm_mips_cs.c:73
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
static const char * mnemonics[]
Definition: z80asm.c:43

Definition at line 88 of file asm_mips_cs.c.