blob: 3963db6a1cd38a00c799816002d9a662296a9f8e [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone 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
10struct platform {
11 cs_arch arch;
12 cs_mode mode;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080013 unsigned char *code;
14 size_t size;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015 char *comment;
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +080016 cs_opt_type opt_type;
17 cs_opt_value opt_value;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018};
19
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080020static void print_string_hex(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("Code: ");
25 for (c = str; c < str + len; c++) {
26 printf("0x%02x ", *c & 0xff);
27 }
28 printf("\n");
29}
30
31static void test()
32{
33#define X86_CODE16 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00"
34#define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00"
35#define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00"
36//#define ARM_CODE "\x04\xe0\x2d\xe5"
37#define ARM_CODE "\xED\xFF\xFF\xEB\x04\xe0\x2d\xe5\x00\x00\x00\x00\xe0\x83\x22\xe5\xf1\x02\x03\x0e\x00\x00\xa0\xe3\x02\x30\xc1\xe7\x00\x00\x53\xe3"
38#define ARM_CODE2 "\x10\xf1\x10\xe7\x11\xf2\x31\xe7\xdc\xa1\x2e\xf3\xe8\x4e\x62\xf3"
39#define THUMB_CODE "\x70\x47\xeb\x46\x83\xb0\xc9\x68"
40#define THUMB_CODE2 "\x4f\xf0\x00\x01\xbd\xe8\x00\x88\xd1\xe8\x00\xf0"
41#define MIPS_CODE "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
42//#define MIPS_CODE "\x21\x38\x00\x01"
43//#define MIPS_CODE "\x21\x30\xe6\x70"
44#define MIPS_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00"
45//#define ARM64_CODE "\xe1\x0b\x40\xb9" // ldr w1, [sp, #0x8]
46//#define ARM64_CODE "\x00\x40\x21\x4b" // sub w0, w0, w1, uxtw
47//#define ARM64_CODE "\x21\x7c\x02\x9b" // mul x1, x1, x2
48//#define ARM64_CODE "\x20\x74\x0b\xd5" // dc zva, x0
Nguyen Anh Quynhad61c492013-11-30 16:23:31 +080049//#define ARM64_CODE "\x20\xfc\x02\x9b" // mneg x0, x1, x2
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050#define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x10\x20\x21\x1e"
Nguyen Anh Quynhf1c2eee2013-12-02 12:29:07 +080051//#define THUMB_CODE "\x0a\xbf" // itet eq
Nguyen Anh Quynhec0ed8e2013-12-02 13:55:38 +080052//#define X86_CODE32 "\x77\x04" // ja +6
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080053
54 struct platform platforms[] = {
55 {
56 .arch = CS_ARCH_X86,
57 .mode = CS_MODE_16,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080058 .code = (unsigned char *)X86_CODE16,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059 .size = sizeof(X86_CODE32) - 1,
60 .comment = "X86 16bit (Intel syntax)"
61 },
62 {
63 .arch = CS_ARCH_X86,
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080064 .mode = CS_MODE_32,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080065 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080066 .size = sizeof(X86_CODE32) - 1,
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +080067 .comment = "X86 32bit (ATT syntax)",
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +080068 .opt_type = CS_OPT_SYNTAX,
Nguyen Anh Quynhc618db42013-12-04 00:05:04 +080069 .opt_value = CS_OPT_SYNTAX_ATT,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070 },
71 {
72 .arch = CS_ARCH_X86,
73 .mode = CS_MODE_32,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080074 .code = (unsigned char *)X86_CODE32,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075 .size = sizeof(X86_CODE32) - 1,
76 .comment = "X86 32 (Intel syntax)"
77 },
78 {
79 .arch = CS_ARCH_X86,
80 .mode = CS_MODE_64,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080081 .code = (unsigned char *)X86_CODE64,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080082 .size = sizeof(X86_CODE64) - 1,
83 .comment = "X86 64 (Intel syntax)"
84 },
85 {
86 .arch = CS_ARCH_ARM,
87 .mode = CS_MODE_ARM,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080088 .code = (unsigned char *)ARM_CODE,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089 .size = sizeof(ARM_CODE) - 1,
90 .comment = "ARM"
91 },
92 {
93 .arch = CS_ARCH_ARM,
94 .mode = CS_MODE_THUMB,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080095 .code = (unsigned char *)THUMB_CODE2,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080096 .size = sizeof(THUMB_CODE2) - 1,
97 .comment = "THUMB-2"
98 },
99 {
100 .arch = CS_ARCH_ARM,
101 .mode = CS_MODE_ARM,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800102 .code = (unsigned char *)ARM_CODE2,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800103 .size = sizeof(ARM_CODE2) - 1,
104 .comment = "ARM: Cortex-A15 + NEON"
105 },
106 {
107 .arch = CS_ARCH_ARM,
108 .mode = CS_MODE_THUMB,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800109 .code = (unsigned char *)THUMB_CODE,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800110 .size = sizeof(THUMB_CODE) - 1,
111 .comment = "THUMB"
112 },
113 {
114 .arch = CS_ARCH_MIPS,
115 .mode = CS_MODE_32 + CS_MODE_BIG_ENDIAN,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800116 .code = (unsigned char *)MIPS_CODE,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800117 .size = sizeof(MIPS_CODE) - 1,
118 .comment = "MIPS-32 (Big-endian)"
119 },
120 {
121 .arch = CS_ARCH_MIPS,
122 .mode = CS_MODE_64+ CS_MODE_LITTLE_ENDIAN,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800123 .code = (unsigned char *)MIPS_CODE2,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800124 .size = sizeof(MIPS_CODE2) - 1,
125 .comment = "MIPS-64-EL (Little-endian)"
126 },
127 {
128 .arch = CS_ARCH_ARM64,
129 .mode = CS_MODE_ARM,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800130 .code = (unsigned char *)ARM64_CODE,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800131 .size = sizeof(ARM64_CODE) - 1,
132 .comment = "ARM-64"
133 },
134 };
135
136 csh handle;
Nguyen Anh Quynh5df9e4b2013-12-03 15:02:12 +0800137 uint64_t address = 0x1000;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800138 //cs_insn all_insn[16];
139 cs_insn *all_insn;
140 int i;
141
142 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
143 if (cs_open(platforms[i].arch, platforms[i].mode, &handle))
144 return;
145
Nguyen Anh Quynhb8ce68e2013-12-03 23:45:08 +0800146 if (platforms[i].opt_type)
147 cs_option(handle, platforms[i].opt_type, platforms[i].opt_value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800148
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800149 //size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, all_insn);
150 size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800151 if (count) {
152 printf("****************\n");
153 printf("Platform: %s\n", platforms[i].comment);
154 print_string_hex(platforms[i].code, platforms[i].size);
155 printf("Disasm:\n");
156
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800157 size_t j;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800158 int n;
159 for (j = 0; j < count; j++) {
160 cs_insn *i = &(all_insn[j]);
Nguyen Anh Quynh723687e2013-11-29 22:36:45 +0800161 printf("0x%"PRIx64":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n",
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800162 i->address, i->mnemonic, i->op_str,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800163 i->id, cs_insn_name(handle, i->id));
164
165 // print implicit registers used by this instruction
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800166 if (i->regs_read_count > 0) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800167 printf("\tImplicit registers read: ");
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800168 for (n = 0; n < i->regs_read_count; n++) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800169 printf("%s ", cs_reg_name(handle, i->regs_read[n]));
170 }
171 printf("\n");
172 }
173
174 // print implicit registers modified by this instruction
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800175 if (i->regs_write_count > 0) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176 printf("\tImplicit registers modified: ");
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800177 for (n = 0; n < i->regs_write_count; n++) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800178 printf("%s ", cs_reg_name(handle, i->regs_write[n]));
179 }
180 printf("\n");
181 }
182
183 // print the groups this instruction belong to
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800184 if (i->groups_count > 0) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800185 printf("\tThis instruction belongs to groups: ");
Nguyen Anh Quynhf35e2ad2013-12-03 11:10:26 +0800186 for (n = 0; n < i->groups_count; n++) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187 printf("%u ", i->groups[n]);
188 }
189 printf("\n");
190 }
191 }
192
193 // print out the next offset, after the last insn
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800194 printf("0x%"PRIx64":\n", all_insn[j-1].address + all_insn[j-1].size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195
196 // free memory allocated by cs_disasm_dyn()
197 cs_free(all_insn);
198 } else {
199 printf("****************\n");
200 printf("Platform: %s\n", platforms[i].comment);
201 print_string_hex(platforms[i].code, platforms[i].size);
202 printf("ERROR: Failed to disasm given code!\n");
203 }
204
205 printf("\n");
206
207 cs_close(handle);
208 }
209}
210
211int main()
212{
213 test();
214
215 return 0;
216}