blob: a39b6f824d851e30cfe3893b06fb71324fa67568 [file] [log] [blame]
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
Cr4sh19ee2d12015-03-29 18:29:06 +08005#include "../myinttypes.h"
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +08006
pancake9c10ace2015-02-24 04:55:55 +01007#include <capstone/capstone.h>
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +08008
9struct platform {
10 cs_arch arch;
11 cs_mode mode;
12 unsigned char *code;
13 size_t size;
14 char *comment;
15};
16
17static csh handle;
18
Mr. eXoDia9be1f932014-08-26 12:46:15 +020019static void print_string_hex(char *comment, unsigned char *str, size_t len)
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080020{
21 unsigned char *c;
22
23 printf("%s", comment);
24 for (c = str; c < str + len; c++) {
25 printf("0x%02x ", *c & 0xff);
26 }
27
28 printf("\n");
29}
30
kratolp5c0d9a42014-10-17 14:52:03 +020031static const char* get_bc_name(int bc)
32{
33 switch(bc) {
34 default:
35 case PPC_BC_INVALID:
36 return ("invalid");
37 case PPC_BC_LT:
38 return ("lt");
39 case PPC_BC_LE:
40 return ("le");
41 case PPC_BC_EQ:
42 return ("eq");
43 case PPC_BC_GE:
44 return ("ge");
45 case PPC_BC_GT:
46 return ("gt");
47 case PPC_BC_NE:
48 return ("ne");
49 case PPC_BC_UN:
50 return ("un");
51 case PPC_BC_NU:
52 return ("nu");
53 case PPC_BC_SO:
54 return ("so");
55 case PPC_BC_NS:
56 return ("ns");
57 }
58}
59
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080060static void print_insn_detail(cs_insn *ins)
61{
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080062 cs_ppc *ppc;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +080063 int i;
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080064
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080065 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
66 if (ins->detail == NULL)
67 return;
68
69 ppc = &(ins->detail->ppc);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080070 if (ppc->op_count)
71 printf("\top_count: %u\n", ppc->op_count);
72
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080073 for (i = 0; i < ppc->op_count; i++) {
74 cs_ppc_op *op = &(ppc->operands[i]);
75 switch((int)op->type) {
76 default:
77 break;
78 case PPC_OP_REG:
79 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
80 break;
81 case PPC_OP_IMM:
82 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
83 break;
84 case PPC_OP_MEM:
85 printf("\t\toperands[%u].type: MEM\n", i);
86 if (op->mem.base != X86_REG_INVALID)
87 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
88 i, cs_reg_name(handle, op->mem.base));
89 if (op->mem.disp != 0)
90 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
91
92 break;
kratolp5c0d9a42014-10-17 14:52:03 +020093 case PPC_OP_CRX:
94 printf("\t\toperands[%u].type: CRX\n", i);
95 printf("\t\t\toperands[%u].crx.scale: %d\n", i, op->crx.scale);
96 printf("\t\t\toperands[%u].crx.reg: %s\n", i, cs_reg_name(handle, op->crx.reg));
97 printf("\t\t\toperands[%u].crx.cond: %s\n", i, get_bc_name(op->crx.cond));
98 break;
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080099 }
100 }
101
102 if (ppc->bc != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +0800103 printf("\tBranch code: %u\n", ppc->bc);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800104
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +0800105 if (ppc->bh != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +0800106 printf("\tBranch hint: %u\n", ppc->bh);
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +0800107
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800108 if (ppc->update_cr0)
109 printf("\tUpdate-CR0: True\n");
110
111 printf("\n");
112}
113
114static void test()
115{
kratolp5c0d9a42014-10-17 14:52:03 +0200116#define PPC_CODE "\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
Nguyen Anh Quynhb8ffb862015-03-12 16:52:31 +0800117#define PPC_CODE2 "\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800118
119 struct platform platforms[] = {
120 {
Axel 0vercl0k Souchete116c512014-05-09 21:17:02 +0100121 CS_ARCH_PPC,
122 CS_MODE_BIG_ENDIAN,
123 (unsigned char*)PPC_CODE,
124 sizeof(PPC_CODE) - 1,
125 "PPC-64",
Nguyen Anh Quynhb8ffb862015-03-12 16:52:31 +0800126 },
127 {
128 CS_ARCH_PPC,
129 CS_MODE_BIG_ENDIAN + CS_MODE_QPX,
130 (unsigned char*)PPC_CODE2,
131 sizeof(PPC_CODE2) - 1,
132 "PPC-64 + QPX",
133 },
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800134 };
135
136 uint64_t address = 0x1000;
137 cs_insn *insn;
138 int i;
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800139 size_t count;
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800140
141 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800142 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
143 if (err) {
144 printf("Failed on cs_open() with error returned: %u\n", err);
Nguyen Anh Quynh49146912014-02-22 16:54:44 +0800145 continue;
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800146 }
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800147
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800148 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
149
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800150 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800151 if (count) {
Nguyen Anh Quynh5b556e52014-04-11 10:15:26 +0800152 size_t j;
153
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800154 printf("****************\n");
155 printf("Platform: %s\n", platforms[i].comment);
156 print_string_hex("Code:", platforms[i].code, platforms[i].size);
157 printf("Disasm:\n");
158
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800159 for (j = 0; j < count; j++) {
160 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
161 print_insn_detail(&insn[j]);
162 }
163 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
164
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800165 // free memory allocated by cs_disasm()
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800166 cs_free(insn, count);
167 } else {
168 printf("****************\n");
169 printf("Platform: %s\n", platforms[i].comment);
170 print_string_hex("Code:", platforms[i].code, platforms[i].size);
171 printf("ERROR: Failed to disasm given code!\n");
172 }
173
174 printf("\n");
175
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800176 cs_close(&handle);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800177 }
178}
179
180int main()
181{
182 test();
183
184 return 0;
185}