blob: 7620a112576c338b1cb2e6c0798409800f95618a [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>
5#include <inttypes.h>
6
7#include <capstone.h>
8
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
19static void print_string_hex(char *comment, unsigned char *str, int len)
20{
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
31static void print_insn_detail(cs_insn *ins)
32{
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080033 cs_ppc *ppc;
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080034
Nguyen Anh Quynh54015f42014-04-10 00:02:04 +080035 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
36 if (ins->detail == NULL)
37 return;
38
39 ppc = &(ins->detail->ppc);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080040 if (ppc->op_count)
41 printf("\top_count: %u\n", ppc->op_count);
42
43 int i;
44 for (i = 0; i < ppc->op_count; i++) {
45 cs_ppc_op *op = &(ppc->operands[i]);
46 switch((int)op->type) {
47 default:
48 break;
49 case PPC_OP_REG:
50 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
51 break;
52 case PPC_OP_IMM:
53 printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
54 break;
55 case PPC_OP_MEM:
56 printf("\t\toperands[%u].type: MEM\n", i);
57 if (op->mem.base != X86_REG_INVALID)
58 printf("\t\t\toperands[%u].mem.base: REG = %s\n",
59 i, cs_reg_name(handle, op->mem.base));
60 if (op->mem.disp != 0)
61 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
62
63 break;
64 }
65 }
66
67 if (ppc->bc != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +080068 printf("\tBranch code: %u\n", ppc->bc);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080069
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +080070 if (ppc->bh != 0)
Nguyen Anh Quynhf122ae02014-01-05 21:45:30 +080071 printf("\tBranch hint: %u\n", ppc->bh);
Nguyen Anh Quynh91e532d2014-01-05 09:15:42 +080072
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +080073 if (ppc->update_cr0)
74 printf("\tUpdate-CR0: True\n");
75
76 printf("\n");
77}
78
79static void test()
80{
81#define PPC_CODE "\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"
82
83 struct platform platforms[] = {
84 {
85 .arch = CS_ARCH_PPC,
86 .mode = CS_MODE_BIG_ENDIAN,
87 .code = (unsigned char*)PPC_CODE,
88 .size = sizeof(PPC_CODE) - 1,
89 .comment = "PPC-64",
90 }
91 };
92
93 uint64_t address = 0x1000;
94 cs_insn *insn;
95 int i;
96
97 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +080098 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
99 if (err) {
100 printf("Failed on cs_open() with error returned: %u\n", err);
Nguyen Anh Quynh49146912014-02-22 16:54:44 +0800101 continue;
Nguyen Anh Quynhceae16d2014-01-19 16:04:23 +0800102 }
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800103
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800104 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
105
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800106 size_t count = cs_disasm_ex(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
107 if (count) {
108 printf("****************\n");
109 printf("Platform: %s\n", platforms[i].comment);
110 print_string_hex("Code:", platforms[i].code, platforms[i].size);
111 printf("Disasm:\n");
112
113 size_t j;
114 for (j = 0; j < count; j++) {
115 printf("0x%"PRIx64":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
116 print_insn_detail(&insn[j]);
117 }
118 printf("0x%"PRIx64":\n", insn[j-1].address + insn[j-1].size);
119
120 // free memory allocated by cs_disasm_ex()
121 cs_free(insn, count);
122 } else {
123 printf("****************\n");
124 printf("Platform: %s\n", platforms[i].comment);
125 print_string_hex("Code:", platforms[i].code, platforms[i].size);
126 printf("ERROR: Failed to disasm given code!\n");
127 }
128
129 printf("\n");
130
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800131 cs_close(&handle);
Nguyen Anh Quynhf1d489b2014-01-05 00:00:05 +0800132 }
133}
134
135int main()
136{
137 test();
138
139 return 0;
140}