blob: 7f8c408671be16fd53314e0504528272336bdb09 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Second-Best Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <inttypes.h>
7
8#include <capstone.h>
9
10static csh handle;
11
12struct platform {
13 cs_arch arch;
14 cs_mode mode;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080015 unsigned char *code;
16 size_t size;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017 char *comment;
18};
19
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080020static void print_string_hex(char *comment, unsigned char *str, int len)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021{
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080022 unsigned char *c;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080023
24 printf("%s", comment);
25 for (c = str; c < str + len; c++) {
26 printf("0x%02x ", *c & 0xff);
27 }
28
29 printf("\n");
30}
31
32static void print_insn_detail(csh ud, cs_mode mode, cs_insn *ins)
33{
34 int i;
35 cs_x86 *x86 = &(ins->x86);
36
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080037 print_string_hex("\tPrefix:", x86->prefix, 5);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038
39 if (x86->segment != X86_REG_INVALID)
40 printf("\tSegment override: %s\n", cs_reg_name(handle, x86->segment));
41
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080042 print_string_hex("\tOpcode:", x86->opcode, 3);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043 printf("\top_size: %u, addr_size: %u, disp_size: %u, imm_size: %u\n", x86->op_size, x86->addr_size, x86->disp_size, x86->imm_size);
44 printf("\tmodrm: 0x%x\n", x86->modrm);
45 printf("\tdisp: 0x%x\n", x86->disp);
46
47 // SIB is not available in 16-bit mode
48 if ((mode & CS_MODE_16) == 0) {
49 printf("\tsib: 0x%x\n", x86->sib);
50 if (x86->sib_index != X86_REG_INVALID)
51 printf("\tsib_index: %s, sib_scale: %u, sib_base: %s\n",
52 cs_reg_name(handle, x86->sib_index),
53 x86->sib_scale,
54 cs_reg_name(handle, x86->sib_base));
55 }
56
57 int count = cs_op_count(ud, ins, X86_OP_IMM);
58 if (count) {
59 printf("\timm_count: %u\n", count);
60 for (i = 1; i < count + 1; i++) {
61 int index = cs_op_index(ud, ins, X86_OP_IMM, i);
62 printf("\t\timms[%u]: 0x%"PRIx64 "\n", i, x86->operands[index].imm);
63 }
64 }
65
66 if (x86->op_count)
67 printf("\top_count: %u\n", x86->op_count);
68 for (i = 0; i < x86->op_count; i++) {
69 cs_x86_op *op = &(x86->operands[i]);
70
71 switch((int)op->type) {
72 case X86_OP_REG:
73 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
74 break;
75 case X86_OP_IMM:
76 printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm);
77 break;
78 case X86_OP_FP:
79 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
80 break;
81 case X86_OP_MEM:
82 printf("\t\toperands[%u].type: MEM\n", i);
83 if (op->mem.base != 0)
84 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
85 if (op->mem.index != 0)
86 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
87 if (op->mem.scale != 1)
88 printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
89 if (op->mem.disp != 0)
90 printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
91 break;
92 default:
93 break;
94 }
95 }
96
97 printf("\n");
98}
99
100static void test()
101{
102//#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00"
103//#define X86_CODE32 "\x05\x78\x56\x00\x00"
104//#define X86_CODE32 "\x01\xd8"
105//#define X86_CODE32 "\x05\x23\x01\x00\x00"
106//#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00"
107//#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00"
108//#define X86_CODE32 "\xb4\xc6" // mov ah, 0x6c
109//#define X86_CODE32 "\x77\x04" // ja +6
110#define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00"
111
112#define X86_CODE16 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6"
113//#define X86_CODE16 "\x67\x00\x18"
114#define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x23\x01\x00\x00\x36\x8b\x84\x91\x23\x01\x00\x00\x41\x8d\x84\x39\x89\x67\x00\x00\x8d\x87\x89\x67\x00\x00\xb4\xc6"
115
116 struct platform platforms[] = {
117 {
118 .arch = CS_ARCH_X86,
119 .mode = CS_MODE_16,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800120 .code = (unsigned char *)X86_CODE16,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800121 .size = sizeof(X86_CODE16) - 1,
122 .comment = "X86 16bit (Intel syntax)"
123 },
124 {
125 .arch = CS_ARCH_X86,
126 .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800127 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800128 .size = sizeof(X86_CODE32) - 1,
129 .comment = "X86 32 (AT&T syntax)"
130 },
131 {
132 .arch = CS_ARCH_X86,
133 .mode = CS_MODE_32,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800134 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800135 .size = sizeof(X86_CODE32) - 1,
136 .comment = "X86 32 (Intel syntax)"
137 },
138 {
139 .arch = CS_ARCH_X86,
140 .mode = CS_MODE_64,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800141 .code = (unsigned char *)X86_CODE64,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800142 .size = sizeof(X86_CODE64) - 1,
143 .comment = "X86 64 (Intel syntax)"
144 },
145 };
146
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800147 size_t address = 0x1000;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148 //cs_insn insn[16];
149 cs_insn *insn;
150 int i;
151
152 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
153 if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
154 return;
155
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800156 //size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn);
157 size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800158 if (count) {
159 printf("****************\n");
160 printf("Platform: %s\n", platforms[i].comment);
161 print_string_hex("Code:", platforms[i].code, platforms[i].size);
162 printf("Disasm:\n");
163
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800164 size_t j;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800165 for (j = 0; j < count; j++) {
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800166 printf("0x%zu:\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800167 print_insn_detail(handle, platforms[i].mode, &insn[j]);
168 }
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800169 printf("0x%zu:\n", insn[j-1].address + insn[j-1].size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
171 // free memory allocated by cs_disasm_dyn()
172 cs_free(insn);
173 } else {
174 printf("****************\n");
175 printf("Platform: %s\n", platforms[i].comment);
176 print_string_hex("Code:", platforms[i].code, platforms[i].size);
177 printf("ERROR: Failed to disasm given code!\n");
178 }
179
180 printf("\n");
181
182 cs_close(handle);
183 }
184}
185
186int main()
187{
188 test();
189
190 return 0;
191}