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

Go to the source code of this file.

Classes

struct  platform
 

Macros

#define XCORE_CODE   "\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"
 

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

◆ XCORE_CODE

#define XCORE_CODE   "\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"

Function Documentation

◆ main()

int main ( void  )

Definition at line 135 of file test_xcore.c.

136 {
137  test();
138 
139  return 0;
140 }
static void test()
Definition: test_xcore.c:76

References test().

◆ print_insn_detail()

static void print_insn_detail ( cs_insn *  ins)
static

Definition at line 31 of file test_xcore.c.

32 {
33  cs_xcore *xcore;
34  int i;
35 
36  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37  if (ins->detail == NULL)
38  return;
39 
40  xcore = &(ins->detail->xcore);
41  if (xcore->op_count)
42  printf("\top_count: %u\n", xcore->op_count);
43 
44  for (i = 0; i < xcore->op_count; i++) {
45  cs_xcore_op *op = &(xcore->operands[i]);
46  switch((int)op->type) {
47  default:
48  break;
49  case XCORE_OP_REG:
50  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
51  break;
52  case XCORE_OP_IMM:
53  printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
54  break;
55  case XCORE_OP_MEM:
56  printf("\t\toperands[%u].type: MEM\n", i);
57  if (op->mem.base != XCORE_REG_INVALID)
58  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
59  i, cs_reg_name(handle, op->mem.base));
60  if (op->mem.index != XCORE_REG_INVALID)
61  printf("\t\t\toperands[%u].mem.index: REG = %s\n",
62  i, cs_reg_name(handle, op->mem.index));
63  if (op->mem.disp != 0)
64  printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
65  if (op->mem.direct != 1)
66  printf("\t\t\toperands[%u].mem.direct: -1\n", i);
67 
68 
69  break;
70  }
71  }
72 
73  printf("\n");
74 }
lzma_index ** i
Definition: index.h:629
@ 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
#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: xcore.h:75
Instruction structure.
Definition: xcore.h:85
static csh handle
Definition: test_xcore.c:17
Definition: dis.c:32

References cs_reg_name(), handle, i, NULL, printf(), XCORE_OP_IMM, XCORE_OP_MEM, XCORE_OP_REG, and XCORE_REG_INVALID.

Referenced by test().

◆ print_string_hex()

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

Definition at line 19 of file test_xcore.c.

20 {
21  unsigned char *c;
22 
23  printf("%s", comment);
24  for (c = str; c < str + len; c++) {
25  printf("0x%02x ", *c & 0xff);
26  }
27 
28  printf("\n");
29 }
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 76 of file test_xcore.c.

77 {
78 #define XCORE_CODE "\xfe\x0f\xfe\x17\x13\x17\xc6\xfe\xec\x17\x97\xf8\xec\x4f\x1f\xfd\xec\x37\x07\xf2\x45\x5b\xf9\xfa\x02\x06\x1b\x10\x09\xfd\xec\xa7"
79 
80  struct platform platforms[] = {
81  {
84  (unsigned char*)XCORE_CODE,
85  sizeof(XCORE_CODE) - 1,
86  "XCore",
87  },
88  };
89 
90  uint64_t address = 0x1000;
91  cs_insn *insn;
92  int i;
93  size_t count;
94 
95  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
96  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
97  if (err) {
98  printf("Failed on cs_open() with error returned: %u\n", err);
99  abort();
100  }
101 
103 
104  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
105  if (count) {
106  size_t j;
107 
108  printf("****************\n");
109  printf("Platform: %s\n", platforms[i].comment);
111  printf("Disasm:\n");
112 
113  for (j = 0; j < count; j++) {
114  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
115  print_insn_detail(&insn[j]);
116  }
117  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
118 
119  // free memory allocated by cs_disasm()
120  cs_free(insn, count);
121  } else {
122  printf("****************\n");
123  printf("Platform: %s\n", platforms[i].comment);
125  printf("ERROR: Failed to disasm given code!\n");
126  abort();
127  }
128 
129  printf("\n");
130 
131  cs_close(&handle);
132  }
133 }
static bool err
Definition: armass.c:435
@ CS_ARCH_XCORE
XCore architecture.
Definition: capstone.h:82
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ 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 PRIx64
Definition: sysdefs.h:94
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_xcore.c:19
#define XCORE_CODE
static void print_insn_detail(cs_insn *ins)
Definition: test_xcore.c:31
mnemonic
Definition: z80asm.h:48

References arch, platform::comment, count, CS_ARCH_XCORE, cs_close(), cs_disasm(), cs_free(), CS_MODE_BIG_ENDIAN, cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), err, handle, i, platforms, print_insn_detail(), print_string_hex(), printf(), PRIx64, and XCORE_CODE.

Referenced by main().

Variable Documentation

◆ handle

csh handle
static

Definition at line 17 of file test_xcore.c.

Referenced by print_insn_detail(), and test().