• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3 
4 #include <stdio.h>
5 
6 #include <platform.h>
7 #include <capstone.h>
8 
9 struct platform {
10 	cs_arch arch;
11 	cs_mode mode;
12 	unsigned char *code;
13 	size_t size;
14 	char *comment;
15 };
16 
17 static csh handle;
18 
print_string_hex(char * comment,unsigned char * str,size_t len)19 static void print_string_hex(char *comment, unsigned char *str, size_t 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 
get_bc_name(int bc)31 static 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 
print_insn_detail(cs_insn * ins)60 static void print_insn_detail(cs_insn *ins)
61 {
62 	cs_ppc *ppc;
63 	int i;
64 
65 	// 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);
70 	if (ppc->op_count)
71 		printf("\top_count: %u\n", ppc->op_count);
72 
73 	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 != PPC_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;
93 			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;
99 		}
100 	}
101 
102 	if (ppc->bc != 0)
103 		printf("\tBranch code: %u\n", ppc->bc);
104 
105 	if (ppc->bh != 0)
106 		printf("\tBranch hint: %u\n", ppc->bh);
107 
108 	if (ppc->update_cr0)
109 		printf("\tUpdate-CR0: True\n");
110 
111 	printf("\n");
112 }
113 
test()114 static void test()
115 {
116 #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"
117 
118 	struct platform platforms[] = {
119 		{
120 			CS_ARCH_PPC,
121 			CS_MODE_BIG_ENDIAN,
122 			(unsigned char*)PPC_CODE,
123 			sizeof(PPC_CODE) - 1,
124 			"PPC-64",
125 		}
126 	};
127 
128 	uint64_t address = 0x1000;
129 	cs_insn *insn;
130 	int i;
131 	size_t count;
132 
133 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
134 		cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
135 		if (err) {
136 			printf("Failed on cs_open() with error returned: %u\n", err);
137 			continue;
138 		}
139 
140 		cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
141 
142 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
143 		if (count) {
144 			size_t j;
145 
146 			printf("****************\n");
147 			printf("Platform: %s\n", platforms[i].comment);
148 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
149 			printf("Disasm:\n");
150 
151 			for (j = 0; j < count; j++) {
152 				printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
153 				print_insn_detail(&insn[j]);
154 			}
155 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
156 
157 			// free memory allocated by cs_disasm()
158 			cs_free(insn, count);
159 		} else {
160 			printf("****************\n");
161 			printf("Platform: %s\n", platforms[i].comment);
162 			print_string_hex("Code:", platforms[i].code, platforms[i].size);
163 			printf("ERROR: Failed to disasm given code!\n");
164 		}
165 
166 		printf("\n");
167 
168 		cs_close(&handle);
169 	}
170 }
171 
main()172 int main()
173 {
174 	test();
175 
176 	return 0;
177 }
178