blob: 02f8d88f836ad195379ea9899de89fe8fbfacee9 [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;
16};
17
18static csh handle;
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
Nguyen Anh Quynh397d0de2013-12-16 23:37:08 +080032static void print_insn_detail(cs_insn *ins)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080033{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080034 cs_mips *mips = &(ins->detail->mips);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035
36 if (mips->op_count)
37 printf("\top_count: %u\n", mips->op_count);
38
39 int i;
40 for (i = 0; i < mips->op_count; i++) {
41 cs_mips_op *op = &(mips->operands[i]);
42 switch((int)op->type) {
43 default:
44 break;
45 case MIPS_OP_REG:
46 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
47 break;
48 case MIPS_OP_IMM:
49 printf("\t\toperands[%u].type: IMM = 0x%"PRIx64 "\n", i, op->imm);
50 break;
51 case MIPS_OP_MEM:
52 printf("\t\toperands[%u].type: MEM\n", i);
53 if (op->mem.base != X86_REG_INVALID)
54 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
55 i, cs_reg_name(handle, op->mem.base));
56 if (op->mem.disp != 0)
57 printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
58
59 break;
60 }
61
62 }
63
64 printf("\n");
65}
66
67static void test()
68{
69//#define MIPS_CODE "\x8f\xa2\x00\x00"
70//#define MIPS_CODE "\x00\x00\xa7\xac\x10\x00\xa2\x8f"
71//#define MIPS_CODE "\x21\x30\xe6\x70" // clo $6, $7
72//#define MIPS_CODE "\x00\x00\x00\x00" // nop
73//#define MIPS_CODE "\xc6\x23\xe9\xe4" // swc1 $f9, 0x23c6($7)
74//#define MIPS_CODE "\x21\x38\x00\x01" // move $7, $8
75#define MIPS_CODE "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
Nguyen Anh Quynh9f523d12013-12-06 13:49:23 +080076//#define MIPS_CODE "\x04\x11\x00\x01" // bal 0x8
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077#define MIPS_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00"
78
79 struct platform platforms[] = {
80 {
81 .arch = CS_ARCH_MIPS,
82 .mode = CS_MODE_32 + CS_MODE_BIG_ENDIAN,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080083 .code = (unsigned char *)MIPS_CODE,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080084 .size = sizeof(MIPS_CODE) - 1,
85 .comment = "MIPS-32 (Big-endian)"
86 },
87 {
88 .arch = CS_ARCH_MIPS,
89 .mode = CS_MODE_64+ CS_MODE_LITTLE_ENDIAN,
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +080090 .code = (unsigned char *)MIPS_CODE2,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080091 .size = sizeof(MIPS_CODE2) - 1,
92 .comment = "MIPS-64-EL (Little-endian)"
93 },
94 };
95
Nguyen Anh Quynh5df9e4b2013-12-03 15:02:12 +080096 uint64_t address = 0x1000;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080097 cs_insn *insn;
98 int i;
99
100 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800101 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
102 if (err) {
103 printf("Failed on cs_open() with error returned: %u\n", err);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800104 return;
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800105 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800106
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800107 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
108
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800109 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800110 if (count) {
111 printf("****************\n");
112 printf("Platform: %s\n", platforms[i].comment);
113 print_string_hex("Code:", platforms[i].code, platforms[i].size);
114 printf("Disasm:\n");
115
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800116 size_t j;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800117 for (j = 0; j < count; j++) {
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800118 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
Nguyen Anh Quynh397d0de2013-12-16 23:37:08 +0800119 print_insn_detail(&insn[j]);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800120 }
Nguyen Anh Quynh7b7b40c2013-12-03 12:24:06 +0800121 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800122
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800123 // free memory allocated by cs_disasm_ex()
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800124 cs_free(insn, count);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125 } else {
126 printf("****************\n");
127 printf("Platform: %s\n", platforms[i].comment);
128 print_string_hex("Code:", platforms[i].code, platforms[i].size);
129 printf("ERROR: Failed to disasm given code!\n");
130 }
131
132 printf("\n");
133
134 cs_close(handle);
135 }
136}
137
138int main()
139{
140 test();
141
142 return 0;
143}