blob: dc502cd45c733511923d758a47c94b280589a56e [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;
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +080018 cs_opt_type opt_type;
19 cs_opt_value opt_value;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020};
21
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080022static void print_string_hex(char *comment, unsigned char *str, int len)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080023{
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080024 unsigned char *c;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025
26 printf("%s", comment);
27 for (c = str; c < str + len; c++) {
28 printf("0x%02x ", *c & 0xff);
29 }
30
31 printf("\n");
32}
33
34static void print_insn_detail(csh ud, cs_mode mode, cs_insn *ins)
35{
36 int i;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080037 cs_x86 *x86 = &(ins->detail->x86);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080039 print_string_hex("\tPrefix:", x86->prefix, 5);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080040
41 if (x86->segment != X86_REG_INVALID)
42 printf("\tSegment override: %s\n", cs_reg_name(handle, x86->segment));
43
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080044 print_string_hex("\tOpcode:", x86->opcode, 3);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080045 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);
46 printf("\tmodrm: 0x%x\n", x86->modrm);
47 printf("\tdisp: 0x%x\n", x86->disp);
48
49 // SIB is not available in 16-bit mode
50 if ((mode & CS_MODE_16) == 0) {
51 printf("\tsib: 0x%x\n", x86->sib);
52 if (x86->sib_index != X86_REG_INVALID)
53 printf("\tsib_index: %s, sib_scale: %u, sib_base: %s\n",
54 cs_reg_name(handle, x86->sib_index),
55 x86->sib_scale,
56 cs_reg_name(handle, x86->sib_base));
57 }
58
59 int count = cs_op_count(ud, ins, X86_OP_IMM);
60 if (count) {
61 printf("\timm_count: %u\n", count);
62 for (i = 1; i < count + 1; i++) {
63 int index = cs_op_index(ud, ins, X86_OP_IMM, i);
64 printf("\t\timms[%u]: 0x%"PRIx64 "\n", i, x86->operands[index].imm);
65 }
66 }
67
68 if (x86->op_count)
69 printf("\top_count: %u\n", x86->op_count);
70 for (i = 0; i < x86->op_count; i++) {
71 cs_x86_op *op = &(x86->operands[i]);
72
73 switch((int)op->type) {
74 case X86_OP_REG:
75 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
76 break;
77 case X86_OP_IMM:
78 printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm);
79 break;
80 case X86_OP_FP:
81 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
82 break;
83 case X86_OP_MEM:
84 printf("\t\toperands[%u].type: MEM\n", i);
85 if (op->mem.base != 0)
86 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
87 if (op->mem.index != 0)
88 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
89 if (op->mem.scale != 1)
90 printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
91 if (op->mem.disp != 0)
92 printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
93 break;
94 default:
95 break;
96 }
97 }
98
99 printf("\n");
100}
101
102static void test()
103{
104//#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00"
105//#define X86_CODE32 "\x05\x78\x56\x00\x00"
106//#define X86_CODE32 "\x01\xd8"
107//#define X86_CODE32 "\x05\x23\x01\x00\x00"
108//#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00"
109//#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00"
110//#define X86_CODE32 "\xb4\xc6" // mov ah, 0x6c
111//#define X86_CODE32 "\x77\x04" // ja +6
112#define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00"
Nguyen Anh Quynh6d552e52013-12-03 18:23:53 +0800113//#define X86_CODE64 "\xe9\x79\xff\xff\xff" // jmp 0xf7e
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800114
115#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"
116//#define X86_CODE16 "\x67\x00\x18"
117#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"
Nguyen Anh Quynhb4ce3832013-12-06 08:06:21 +0800118//#define X86_CODE32 "\x0f\xa7\xc0" // xstorerng
Nguyen Anh Quynhb9b3d292013-12-02 16:21:55 +0800119//#define X86_CODE32 "\x64\xa1\x18\x00\x00\x00" // mov eax, dword ptr fs:[18]
120//#define X86_CODE32 "\x64\xa3\x00\x00\x00\x00" // mov [fs:0x0], eax
Nguyen Anh Quynh9c950c12013-12-02 23:20:07 +0800121//#define X86_CODE32 "\xd1\xe1" // shl ecx, 1
122//#define X86_CODE32 "\xd1\xc8" // ror eax, 1
Nguyen Anh Quynhd57fa4f2013-12-03 00:45:39 +0800123//#define X86_CODE32 "\x83\xC0\x80" // add eax, -x80
Nguyen Anh Quynh641b7832013-12-04 16:56:40 +0800124//#define X86_CODE32 "\xe8\x26\xfe\xff\xff" // call 0xe2b
125//#define X86_CODE32 "\xcd\x80" // int 0x80
Nguyen Anh Quynhd57fa4f2013-12-03 00:45:39 +0800126//#define X86_CODE32 "\x24\xb8" // and $0xb8,%al
Nguyen Anh Quynhbdaf3b52013-12-09 00:55:49 +0800127//#define X86_CODE32 "\xf0\x01\xd8" // lock add eax,ebx
128//#define X86_CODE32 "\xf3\xaa" // rep stosb
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800129
130 struct platform platforms[] = {
131 {
132 .arch = CS_ARCH_X86,
133 .mode = CS_MODE_16,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800134 .code = (unsigned char *)X86_CODE16,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800135 .size = sizeof(X86_CODE16) - 1,
136 .comment = "X86 16bit (Intel syntax)"
137 },
138 {
139 .arch = CS_ARCH_X86,
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800140 .mode = CS_MODE_32,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800141 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800142 .size = sizeof(X86_CODE32) - 1,
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800143 .comment = "X86 32 (AT&T syntax)",
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800144 .opt_type = CS_OPT_SYNTAX,
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +0800145 .opt_value = CS_OPT_SYNTAX_ATT,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800146 },
147 {
148 .arch = CS_ARCH_X86,
149 .mode = CS_MODE_32,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800150 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800151 .size = sizeof(X86_CODE32) - 1,
152 .comment = "X86 32 (Intel syntax)"
153 },
154 {
155 .arch = CS_ARCH_X86,
156 .mode = CS_MODE_64,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800157 .code = (unsigned char *)X86_CODE64,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800158 .size = sizeof(X86_CODE64) - 1,
159 .comment = "X86 64 (Intel syntax)"
160 },
161 };
162
Nguyen Anh Quynh5df9e4b2013-12-03 15:02:12 +0800163 uint64_t address = 0x1000;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164 cs_insn *insn;
165 int i;
166
167 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
168 if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
169 return;
170
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800171 if (platforms[i].opt_type)
172 cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800173
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800174 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800175 if (count) {
176 printf("****************\n");
177 printf("Platform: %s\n", platforms[i].comment);
178 print_string_hex("Code:", platforms[i].code, platforms[i].size);
179 printf("Disasm:\n");
180
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800181 size_t j;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800182 for (j = 0; j < count; j++) {
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800183 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800184 print_insn_detail(handle, platforms[i].mode, &insn[j]);
185 }
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800186 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800188 // free memory allocated by cs_disasm_ex()
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800189 cs_free(insn, count);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190 } else {
191 printf("****************\n");
192 printf("Platform: %s\n", platforms[i].comment);
193 print_string_hex("Code:", platforms[i].code, platforms[i].size);
194 printf("ERROR: Failed to disasm given code!\n");
195 }
196
197 printf("\n");
198
199 cs_close(handle);
200 }
201}
202
203int main()
204{
205 test();
206
207 return 0;
208}