1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
3
4 #include <stdio.h>
5
6 #include <capstone/platform.h>
7 #include <capstone/capstone.h>
8
9 struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 unsigned char *code;
13 size_t size;
14 const char *comment;
15 };
16
17 static csh handle;
18
print_string_hex(const char * comment,unsigned char * str,size_t len)19 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
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 }
30
print_insn_detail(cs_insn * ins)31 static void print_insn_detail(cs_insn *ins)
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 }
75
test()76 static void test()
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 {
82 CS_ARCH_XCORE,
83 CS_MODE_BIG_ENDIAN,
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
102 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
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);
110 print_string_hex("Code:", platforms[i].code, platforms[i].size);
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);
124 print_string_hex("Code:", platforms[i].code, platforms[i].size);
125 printf("ERROR: Failed to disasm given code!\n");
126 abort();
127 }
128
129 printf("\n");
130
131 cs_close(&handle);
132 }
133 }
134
main()135 int main()
136 {
137 test();
138
139 return 0;
140 }
141