Debian Codegraph @

tests/test_tricore.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <[email protected]>, 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 char *comment;
15 };
16
17 static csh handle;
18
19 static void print_string_hex(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
31 static void print_insn_detail(cs_insn *ins)
32 {
33 cs_tricore *tricore;
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 tricore = &(ins->detail->tricore);
41 if (tricore->op_count)
42 printf("\top_count: %u\n", tricore->op_count);
43
44 for (i = 0; i < tricore->op_count; i++) {
45 cs_tricore_op *op = &(tricore->operands[i]);
46 switch ((int)op->type) {
47 default:
48 break;
49 case TRICORE_OP_REG:
50 printf("\t\toperands[%u].type: REG = %s\n", i,
51 cs_reg_name(handle, op->reg));
52 break;
53 case TRICORE_OP_IMM:
54 printf("\t\toperands[%u].type: IMM = 0x%x\n", i,
55 op->imm);
56 break;
57 case TRICORE_OP_MEM:
58 printf("\t\toperands[%u].type: MEM\n", i);
59 if (op->mem.base != TRICORE_REG_INVALID)
60 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
61 i, cs_reg_name(handle, op->mem.base));
62 if (op->mem.disp != 0)
63 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i,
64 op->mem.disp);
65
66 break;
67 }
68 }
69
70 printf("\n");
71 }
72
73 static void test()
74 {
75 //#define TRICORE_CODE "\x16\x01\x20\x01\x1d\x00\x02\x00\x8f\x70\x00\x11\x40\xae\x89\xee\x04\x09\x42\xf2\xe2\xf2\xc2\x11\x19\xff\xc0\x70\x19\xff\x20\x10"
76 #define TRICORE_CODE \
77 "\x09\xcf\xbc\xf5\x09\xf4\x01\x00\x89\xfb\x8f\x74\x89\xfe\x48\x01\x29\x00\x19\x25\x29\x03\x09\xf4\x85\xf9\x68\x0f\x16\x01"
78
79 struct platform platforms[] = {
80 {
81 CS_ARCH_TRICORE,
82 CS_MODE_TRICORE_162,
83 (unsigned char *)TRICORE_CODE,
84 sizeof(TRICORE_CODE) - 1,
85 "TriCore",
86 },
87 };
88
89 uint64_t address = 0x1000;
90 cs_insn *insn;
91 int i;
92 size_t count;
93
94 for (i = 0; i < sizeof(platforms) / sizeof(platforms[0]); i++) {
95 cs_err err =
96 cs_open(platforms[i].arch, platforms[i].mode, &handle);
97 if (err) {
98 printf("Failed on cs_open() with error returned: %u\n",
99 err);
100 continue;
101 }
102
103 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
104
105 count = cs_disasm(handle, platforms[i].code, platforms[i].size,
106 address, 0, &insn);
107 if (count) {
108 size_t j;
109
110 printf("****************\n");
111 printf("Platform: %s\n", platforms[i].comment);
112 print_string_hex("Code:", platforms[i].code,
113 platforms[i].size);
114 printf("Disasm:\n");
115
116 for (j = 0; j < count; j++) {
117 printf("0x%" PRIx64 ":\t%s\t%s\n",
118 insn[j].address, insn[j].mnemonic,
119 insn[j].op_str);
120 print_insn_detail(&insn[j]);
121 }
122 printf("0x%" PRIx64 ":\n",
123 insn[j - 1].address + insn[j - 1].size);
124
125 // free memory allocated by cs_disasm()
126 cs_free(insn, count);
127 } else {
128 printf("****************\n");
129 printf("Platform: %s\n", platforms[i].comment);
130 print_string_hex("Code:", platforms[i].code,
131 platforms[i].size);
132 printf("ERROR: Failed to disasm given code!\n");
133 }
134
135 printf("\n");
136
137 cs_close(&handle);
138 }
139 }
140
141 int main()
142 {
143 test();
144
145 return 0;
146 }