blob: 4d31074c8d421677f0849f5778782228fd747463 [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>
Yegor Derevenetsced9d242014-09-21 17:27:11 +02006#include "../inttypes.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007
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
Mr. eXoDia9be1f932014-08-26 12:46:15 +020022static void print_string_hex(char *comment, unsigned char *str, size_t 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{
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +080036 int count, i;
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080037 cs_x86 *x86;
38
39 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
40 if (ins->detail == NULL)
41 return;
42
43 x86 = &(ins->detail->x86);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080044
Nguyen Anh Quynhd948dd42014-06-20 13:55:24 +080045 print_string_hex("\tPrefix:", x86->prefix, 4);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080046
Nguyen Anh Quynhbb6440c2014-06-24 21:46:54 +080047 print_string_hex("\tOpcode:", x86->opcode, 4);
Nguyen Anh Quynh9f6ed712014-07-01 10:13:28 +080048
49 printf("\trex: 0x%x\n", x86->rex);
50
Nguyen Anh Quynh10850732014-06-18 12:16:24 +080051 printf("\taddr_size: %u\n", x86->addr_size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080052 printf("\tmodrm: 0x%x\n", x86->modrm);
53 printf("\tdisp: 0x%x\n", x86->disp);
54
55 // SIB is not available in 16-bit mode
56 if ((mode & CS_MODE_16) == 0) {
57 printf("\tsib: 0x%x\n", x86->sib);
Nguyen Anh Quynhcb2c4f92014-05-17 13:12:29 +080058 if (x86->sib_base != X86_REG_INVALID)
59 printf("\t\tsib_base: %s\n", cs_reg_name(handle, x86->sib_base));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060 if (x86->sib_index != X86_REG_INVALID)
Nguyen Anh Quynhcb2c4f92014-05-17 13:12:29 +080061 printf("\t\tsib_index: %s\n", cs_reg_name(handle, x86->sib_index));
62 if (x86->sib_scale != 0)
63 printf("\t\tsib_scale: %d\n", x86->sib_scale);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080064 }
65
Nguyen Anh Quynh1a66fec2014-06-26 12:09:15 +080066 // SSE code condition
Nguyen Anh Quynh4c5eabc2014-06-24 23:50:41 +080067 if (x86->sse_cc != X86_SSE_CC_INVALID) {
68 printf("\tsse_cc: %u\n", x86->sse_cc);
69 }
70
Nguyen Anh Quynh1a66fec2014-06-26 12:09:15 +080071 // AVX code condition
Nguyen Anh Quynh4c5eabc2014-06-24 23:50:41 +080072 if (x86->avx_cc != X86_AVX_CC_INVALID) {
73 printf("\tavx_cc: %u\n", x86->avx_cc);
74 }
75
Nguyen Anh Quynh1a66fec2014-06-26 12:09:15 +080076 // AVX Suppress All Exception
77 if (x86->avx_sae) {
78 printf("\tavx_sae: %u\n", x86->avx_sae);
79 }
80
81 // AVX Rounding Mode
82 if (x86->avx_rm != X86_AVX_RM_INVALID) {
83 printf("\tavx_rm: %u\n", x86->avx_rm);
84 }
85
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +080086 count = cs_op_count(ud, ins, X86_OP_IMM);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087 if (count) {
88 printf("\timm_count: %u\n", count);
89 for (i = 1; i < count + 1; i++) {
90 int index = cs_op_index(ud, ins, X86_OP_IMM, i);
91 printf("\t\timms[%u]: 0x%"PRIx64 "\n", i, x86->operands[index].imm);
92 }
93 }
94
95 if (x86->op_count)
96 printf("\top_count: %u\n", x86->op_count);
97 for (i = 0; i < x86->op_count; i++) {
98 cs_x86_op *op = &(x86->operands[i]);
99
100 switch((int)op->type) {
101 case X86_OP_REG:
102 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
103 break;
104 case X86_OP_IMM:
105 printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm);
106 break;
107 case X86_OP_FP:
108 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
109 break;
110 case X86_OP_MEM:
111 printf("\t\toperands[%u].type: MEM\n", i);
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800112 if (op->mem.segment != X86_REG_INVALID)
113 printf("\t\t\toperands[%u].mem.segment: REG = %s\n", i, cs_reg_name(handle, op->mem.segment));
114 if (op->mem.base != X86_REG_INVALID)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800115 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
Nguyen Anh Quynh14ba46b2014-06-24 14:32:01 +0800116 if (op->mem.index != X86_REG_INVALID)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800117 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
118 if (op->mem.scale != 1)
119 printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
120 if (op->mem.disp != 0)
121 printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
122 break;
123 default:
124 break;
125 }
Nguyen Anh Quynh10850732014-06-18 12:16:24 +0800126
Nguyen Anh Quynh92a3d4c2014-06-25 23:10:39 +0800127 // AVX broadcast type
Nguyen Anh Quynh0d716452014-06-24 22:51:56 +0800128 if (op->avx_bcast != X86_AVX_BCAST_INVALID)
129 printf("\t\toperands[%u].avx_bcast: %u\n", i, op->avx_bcast);
130
Nguyen Anh Quynh92a3d4c2014-06-25 23:10:39 +0800131 // AVX zero opmask {z}
Nguyen Anh Quynh1a66fec2014-06-26 12:09:15 +0800132 if (op->avx_zero_opmask != false)
133 printf("\t\toperands[%u].avx_zero_opmask: TRUE\n", i);
Nguyen Anh Quynh92a3d4c2014-06-25 23:10:39 +0800134
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +0800135 printf("\t\toperands[%u].size: %u\n", i, op->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136 }
137
138 printf("\n");
139}
140
141static void test()
142{
143//#define X86_CODE32 "\x01\xd8\x81\xc6\x34\x12\x00\x00\x05\x78\x56\x00\x00"
144//#define X86_CODE32 "\x05\x78\x56\x00\x00"
145//#define X86_CODE32 "\x01\xd8"
146//#define X86_CODE32 "\x05\x23\x01\x00\x00"
147//#define X86_CODE32 "\x8d\x87\x89\x67\x00\x00"
148//#define X86_CODE32 "\xa1\x13\x48\x6d\x3a\x8b\x81\x23\x01\x00\x00\x8b\x84\x39\x23\x01\x00\x00"
149//#define X86_CODE32 "\xb4\xc6" // mov ah, 0x6c
150//#define X86_CODE32 "\x77\x04" // ja +6
151#define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00"
Nguyen Anh Quynh6d552e52013-12-03 18:23:53 +0800152//#define X86_CODE64 "\xe9\x79\xff\xff\xff" // jmp 0xf7e
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800153
154#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"
155//#define X86_CODE16 "\x67\x00\x18"
156#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 +0800157//#define X86_CODE32 "\x0f\xa7\xc0" // xstorerng
Nguyen Anh Quynhb9b3d292013-12-02 16:21:55 +0800158//#define X86_CODE32 "\x64\xa1\x18\x00\x00\x00" // mov eax, dword ptr fs:[18]
159//#define X86_CODE32 "\x64\xa3\x00\x00\x00\x00" // mov [fs:0x0], eax
Nguyen Anh Quynh9c950c12013-12-02 23:20:07 +0800160//#define X86_CODE32 "\xd1\xe1" // shl ecx, 1
161//#define X86_CODE32 "\xd1\xc8" // ror eax, 1
Nguyen Anh Quynhd57fa4f2013-12-03 00:45:39 +0800162//#define X86_CODE32 "\x83\xC0\x80" // add eax, -x80
Nguyen Anh Quynh641b7832013-12-04 16:56:40 +0800163//#define X86_CODE32 "\xe8\x26\xfe\xff\xff" // call 0xe2b
164//#define X86_CODE32 "\xcd\x80" // int 0x80
Nguyen Anh Quynhd57fa4f2013-12-03 00:45:39 +0800165//#define X86_CODE32 "\x24\xb8" // and $0xb8,%al
Nguyen Anh Quynhbdaf3b52013-12-09 00:55:49 +0800166//#define X86_CODE32 "\xf0\x01\xd8" // lock add eax,ebx
167//#define X86_CODE32 "\xf3\xaa" // rep stosb
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168
169 struct platform platforms[] = {
170 {
Axel 0vercl0k Souchete47b8122014-05-09 21:03:29 +0100171 CS_ARCH_X86,
172 CS_MODE_16,
173 (unsigned char *)X86_CODE16,
174 sizeof(X86_CODE16) - 1,
175 "X86 16bit (Intel syntax)"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176 },
177 {
Axel 0vercl0k Souchete47b8122014-05-09 21:03:29 +0100178 CS_ARCH_X86,
179 CS_MODE_32,
180 (unsigned char *)X86_CODE32,
181 sizeof(X86_CODE32) - 1,
182 "X86 32 (AT&T syntax)",
183 CS_OPT_SYNTAX,
184 CS_OPT_SYNTAX_ATT,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800185 },
186 {
Axel 0vercl0k Souchete47b8122014-05-09 21:03:29 +0100187 CS_ARCH_X86,
188 CS_MODE_32,
189 (unsigned char *)X86_CODE32,
190 sizeof(X86_CODE32) - 1,
191 "X86 32 (Intel syntax)"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800192 },
193 {
Axel 0vercl0k Souchete47b8122014-05-09 21:03:29 +0100194 CS_ARCH_X86,
195 CS_MODE_64,
196 (unsigned char *)X86_CODE64,
197 sizeof(X86_CODE64) - 1,
198 "X86 64 (Intel syntax)"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199 },
200 };
201
Nguyen Anh Quynh5df9e4b2013-12-03 15:02:12 +0800202 uint64_t address = 0x1000;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203 cs_insn *insn;
204 int i;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800205 size_t count;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800206
207 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800208 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
209 if (err) {
210 printf("Failed on cs_open() with error returned: %u\n", err);
Nguyen Anh Quynh49146912014-02-22 16:54:44 +0800211 continue;
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800212 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800213
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800214 if (platforms[i].opt_type)
215 cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800216
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800217 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
218
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800219 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220 if (count) {
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800221 size_t j;
222
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223 printf("****************\n");
224 printf("Platform: %s\n", platforms[i].comment);
225 print_string_hex("Code:", platforms[i].code, platforms[i].size);
226 printf("Disasm:\n");
227
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228 for (j = 0; j < count; j++) {
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800229 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 +0800230 print_insn_detail(handle, platforms[i].mode, &insn[j]);
231 }
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800232 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800233
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800234 // free memory allocated by cs_disasm()
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800235 cs_free(insn, count);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800236 } else {
237 printf("****************\n");
238 printf("Platform: %s\n", platforms[i].comment);
239 print_string_hex("Code:", platforms[i].code, platforms[i].size);
240 printf("ERROR: Failed to disasm given code!\n");
241 }
242
243 printf("\n");
244
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800245 cs_close(&handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800246 }
247}
248
249int main()
250{
251 test();
252
253 return 0;
254}