Rizin
unix-like reverse engineering framework and cli tools
test_mips.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <capstone/platform.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Macros

#define MIPS_CODE   "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
 
#define MIPS_CODE2   "\x56\x34\x21\x34\xc2\x17\x01\x00"
 
#define MIPS_32R6M   "\x00\x07\x00\x07\x00\x11\x93\x7c\x01\x8c\x8b\x7c\x00\xc7\x48\xd0"
 
#define MIPS_32R6   "\xec\x80\x00\x19\x7c\x43\x22\xa0"
 
#define MIPS_64SD   "\x70\x00\xb2\xff"
 

Functions

static void print_string_hex (const char *comment, unsigned char *str, size_t len)
 
static void print_insn_detail (cs_insn *ins)
 
static void test ()
 
int main ()
 

Variables

static csh handle
 

Macro Definition Documentation

◆ MIPS_32R6

#define MIPS_32R6   "\xec\x80\x00\x19\x7c\x43\x22\xa0"

◆ MIPS_32R6M

#define MIPS_32R6M   "\x00\x07\x00\x07\x00\x11\x93\x7c\x01\x8c\x8b\x7c\x00\xc7\x48\xd0"

◆ MIPS_64SD

#define MIPS_64SD   "\x70\x00\xb2\xff"

◆ MIPS_CODE

#define MIPS_CODE   "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"

◆ MIPS_CODE2

#define MIPS_CODE2   "\x56\x34\x21\x34\xc2\x17\x01\x00"

Function Documentation

◆ main()

int main ( void  )

Definition at line 170 of file test_mips.c.

171 {
172  test();
173 
174  return 0;
175 }
static void test()
Definition: test_mips.c:72

References test().

◆ print_insn_detail()

static void print_insn_detail ( cs_insn *  ins)
static

Definition at line 32 of file test_mips.c.

33 {
34  int i;
35  cs_mips *mips;
36 
37  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
38  if (ins->detail == NULL)
39  return;
40 
41  mips = &(ins->detail->mips);
42  if (mips->op_count)
43  printf("\top_count: %u\n", mips->op_count);
44 
45  for (i = 0; i < mips->op_count; i++) {
46  cs_mips_op *op = &(mips->operands[i]);
47  switch((int)op->type) {
48  default:
49  break;
50  case MIPS_OP_REG:
51  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
52  break;
53  case MIPS_OP_IMM:
54  printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
55  break;
56  case MIPS_OP_MEM:
57  printf("\t\toperands[%u].type: MEM\n", i);
58  if (op->mem.base != MIPS_REG_INVALID)
59  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
60  i, cs_reg_name(handle, op->mem.base));
61  if (op->mem.disp != 0)
62  printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
63 
64  break;
65  }
66 
67  }
68 
69  printf("\n");
70 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
Instruction operand.
Definition: mips.h:240
Instruction structure.
Definition: mips.h:250
@ MIPS_OP_REG
= CS_OP_REG (Register operand).
Definition: mips.h:24
@ MIPS_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: mips.h:25
@ MIPS_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: mips.h:26
@ MIPS_REG_INVALID
Definition: mips.h:31
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_mips.c:18
Definition: dis.c:32

References cs_reg_name(), handle, i, MIPS_OP_IMM, MIPS_OP_MEM, MIPS_OP_REG, MIPS_REG_INVALID, NULL, printf(), and PRIx64.

Referenced by test().

◆ print_string_hex()

static void print_string_hex ( const char *  comment,
unsigned char *  str,
size_t  len 
)
static

Definition at line 20 of file test_mips.c.

21 {
22  unsigned char *c;
23 
24  printf("%s", comment);
25  for (c = str; c < str + len; c++) {
26  printf("0x%02x ", *c & 0xff);
27  }
28 
29  printf("\n");
30 }
size_t len
Definition: 6502dis.c:15
#define c(i)
Definition: sha256.c:43

References c, len, printf(), and cmd_descs_generate::str.

Referenced by test().

◆ test()

static void test ( )
static

Definition at line 72 of file test_mips.c.

73 {
74 #define MIPS_CODE "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
75 #define MIPS_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00"
76 #define MIPS_32R6M "\x00\x07\x00\x07\x00\x11\x93\x7c\x01\x8c\x8b\x7c\x00\xc7\x48\xd0"
77 #define MIPS_32R6 "\xec\x80\x00\x19\x7c\x43\x22\xa0"
78 #define MIPS_64SD "\x70\x00\xb2\xff"
79 
80  struct platform platforms[] = {
81  {
84  (unsigned char *)MIPS_CODE,
85  sizeof(MIPS_CODE) - 1,
86  "MIPS-32 (Big-endian)"
87  },
88  {
91  (unsigned char *)MIPS_CODE2,
92  sizeof(MIPS_CODE2) - 1,
93  "MIPS-64-EL (Little-endian)"
94  },
95  {
98  (unsigned char*)MIPS_32R6M,
99  sizeof(MIPS_32R6M) - 1,
100  "MIPS-32R6 | Micro (Big-endian)"
101  },
102  {
103  CS_ARCH_MIPS,
105  (unsigned char*)MIPS_32R6,
106  sizeof(MIPS_32R6) - 1,
107  "MIPS-32R6 (Big-endian)"
108  },
109  {
110  CS_ARCH_MIPS,
112  (unsigned char *)MIPS_64SD,
113  sizeof(MIPS_64SD) - 1,
114  "MIPS-64-EL + Mips II (Little-endian)"
115  },
116  {
117  CS_ARCH_MIPS,
119  (unsigned char *)MIPS_64SD,
120  sizeof(MIPS_64SD) - 1,
121  "MIPS-64-EL (Little-endian)"
122  },
123  };
124 
125  uint64_t address = 0x1000;
126  cs_insn *insn;
127  int i;
128  size_t count;
129 
130  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
131  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
132  if (err) {
133  printf("Failed on cs_open() with error returned: %u\n", err);
134  abort();
135  }
136 
138 
139  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
140  if (count) {
141  size_t j;
142 
143  printf("****************\n");
144  printf("Platform: %s\n", platforms[i].comment);
146  printf("Disasm:\n");
147 
148  for (j = 0; j < count; j++) {
149  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
150  print_insn_detail(&insn[j]);
151  }
152  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
153 
154  // free memory allocated by cs_disasm()
155  cs_free(insn, count);
156  } else {
157  printf("****************\n");
158  printf("Platform: %s\n", platforms[i].comment);
160  printf("ERROR: Failed to disasm given code!\n");
161  abort();
162  }
163 
164  printf("\n");
165 
166  cs_close(&handle);
167  }
168 }
static bool err
Definition: armass.c:435
@ CS_ARCH_MIPS
Mips architecture.
Definition: capstone.h:77
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_MIPS64
Mips64 ISA (Mips)
Definition: capstone.h:125
@ CS_MODE_MICRO
MicroMips mode (MIPS)
Definition: capstone.h:111
@ 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_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 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
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
unsigned long uint64_t
Definition: sftypes.h:28
Definition: inftree9.h:24
#define MIPS_CODE2
#define MIPS_32R6
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_mips.c:20
#define MIPS_CODE
#define MIPS_64SD
static void print_insn_detail(cs_insn *ins)
Definition: test_mips.c:32
#define MIPS_32R6M
mnemonic
Definition: z80asm.h:48

References arch, platform::comment, count, 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_MIPS32, CS_MODE_MIPS32R6, CS_MODE_MIPS64, cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), err, handle, i, MIPS_32R6, MIPS_32R6M, MIPS_64SD, MIPS_CODE, MIPS_CODE2, platforms, print_insn_detail(), print_string_hex(), printf(), and PRIx64.

Referenced by main().

Variable Documentation

◆ handle

csh handle
static

Definition at line 18 of file test_mips.c.

Referenced by print_insn_detail(), and test().