• 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 #include <stdlib.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 static csh handle;
11 
12 struct platform {
13 	cs_arch arch;
14 	cs_mode mode;
15 	unsigned char *code;
16 	size_t size;
17 	const char *comment;
18 };
19 
print_string_hex(const char * comment,unsigned char * str,size_t len)20 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
21 {
22 	unsigned char *c;
23 
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 
print_insn_detail(cs_insn * ins)32 static void print_insn_detail(cs_insn *ins)
33 {
34 	cs_arm64 *arm64;
35 	int i;
36 	cs_regs regs_read, regs_write;
37 	unsigned char regs_read_count, regs_write_count;
38 	unsigned char access;
39 
40 	// detail can be NULL if SKIPDATA option is turned ON
41 	if (ins->detail == NULL)
42 		return;
43 
44 	arm64 = &(ins->detail->arm64);
45 	if (arm64->op_count)
46 		printf("\top_count: %u\n", arm64->op_count);
47 
48 	for (i = 0; i < arm64->op_count; i++) {
49 		cs_arm64_op *op = &(arm64->operands[i]);
50 		switch(op->type) {
51 			default:
52 				break;
53 			case ARM64_OP_REG:
54 				printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
55 				break;
56 			case ARM64_OP_IMM:
57 				printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
58 				break;
59 			case ARM64_OP_FP:
60 #if defined(_KERNEL_MODE)
61 				// Issue #681: Windows kernel does not support formatting float point
62 				printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
63 #else
64 				printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
65 #endif
66 				break;
67 			case ARM64_OP_MEM:
68 				printf("\t\toperands[%u].type: MEM\n", i);
69 				if (op->mem.base != ARM64_REG_INVALID)
70 					printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
71 				if (op->mem.index != ARM64_REG_INVALID)
72 					printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
73 				if (op->mem.disp != 0)
74 					printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
75 
76 				break;
77 			case ARM64_OP_CIMM:
78 				printf("\t\toperands[%u].type: C-IMM = %u\n", i, (int)op->imm);
79 				break;
80 			case ARM64_OP_REG_MRS:
81 				printf("\t\toperands[%u].type: REG_MRS = 0x%x\n", i, op->reg);
82 				break;
83 			case ARM64_OP_REG_MSR:
84 				printf("\t\toperands[%u].type: REG_MSR = 0x%x\n", i, op->reg);
85 				break;
86 			case ARM64_OP_PSTATE:
87 				printf("\t\toperands[%u].type: PSTATE = 0x%x\n", i, op->pstate);
88 				break;
89 			case ARM64_OP_SYS:
90 				printf("\t\toperands[%u].type: SYS = 0x%x\n", i, op->sys);
91 				break;
92 			case ARM64_OP_PREFETCH:
93 				printf("\t\toperands[%u].type: PREFETCH = 0x%x\n", i, op->prefetch);
94 				break;
95 			case ARM64_OP_BARRIER:
96 				printf("\t\toperands[%u].type: BARRIER = 0x%x\n", i, op->barrier);
97 				break;
98 		}
99 
100 		access = op->access;
101 		switch(access) {
102 			default:
103 				break;
104 			case CS_AC_READ:
105 				printf("\t\toperands[%u].access: READ\n", i);
106 				break;
107 			case CS_AC_WRITE:
108 				printf("\t\toperands[%u].access: WRITE\n", i);
109 				break;
110 			case CS_AC_READ | CS_AC_WRITE:
111 				printf("\t\toperands[%u].access: READ | WRITE\n", i);
112 				break;
113 		}
114 
115 		if (op->shift.type != ARM64_SFT_INVALID &&
116 				op->shift.value)
117 			printf("\t\t\tShift: type = %u, value = %u\n",
118 					op->shift.type, op->shift.value);
119 
120 		if (op->ext != ARM64_EXT_INVALID)
121 			printf("\t\t\tExt: %u\n", op->ext);
122 
123 		if (op->vas != ARM64_VAS_INVALID)
124 			printf("\t\t\tVector Arrangement Specifier: 0x%x\n", op->vas);
125 
126 		if (op->vess != ARM64_VESS_INVALID)
127 			printf("\t\t\tVector Element Size Specifier: %u\n", op->vess);
128 
129 		if (op->vector_index != -1)
130 			printf("\t\t\tVector Index: %u\n", op->vector_index);
131 	}
132 
133 	if (arm64->update_flags)
134 		printf("\tUpdate-flags: True\n");
135 
136 	if (arm64->writeback)
137 		printf("\tWrite-back: True\n");
138 
139 	if (arm64->cc)
140 		printf("\tCode-condition: %u\n", arm64->cc);
141 
142 	// Print out all registers accessed by this instruction (either implicit or explicit)
143 	if (!cs_regs_access(handle, ins,
144 				regs_read, &regs_read_count,
145 				regs_write, &regs_write_count)) {
146 		if (regs_read_count) {
147 			printf("\tRegisters read:");
148 			for(i = 0; i < regs_read_count; i++) {
149 				printf(" %s", cs_reg_name(handle, regs_read[i]));
150 			}
151 			printf("\n");
152 		}
153 
154 		if (regs_write_count) {
155 			printf("\tRegisters modified:");
156 			for(i = 0; i < regs_write_count; i++) {
157 				printf(" %s", cs_reg_name(handle, regs_write[i]));
158 			}
159 			printf("\n");
160 		}
161 	}
162 
163 	printf("\n");
164 }
165 
test()166 static void test()
167 {
168 //#define ARM64_CODE "\xe1\x0b\x40\xb9"	// ldr		w1, [sp, #0x8]
169 //#define ARM64_CODE "\x21\x7c\x00\x53"	// lsr	w1, w1, #0x0
170 //#define ARM64_CODE "\x21\x7c\x02\x9b"
171 //#define ARM64_CODE "\x20\x04\x81\xda"	// csneg	x0, x1, x1, eq | cneg x0, x1, ne
172 //#define ARM64_CODE "\x20\x08\x02\x8b"		// add	x0, x1, x2, lsl #2
173 
174 //#define ARM64_CODE "\x20\xcc\x20\x8b"
175 //#define ARM64_CODE "\xe2\x8f\x40\xa9"	// ldp	x2, x3, [sp, #8]
176 //#define ARM64_CODE "\x20\x40\x60\x1e"	// fmov d0, d1
177 //#define ARM64_CODE "\x20\x7c\x7d\x93"	// sbfiz	x0, x1, #3, #32
178 
179 //#define ARM64_CODE "\x20\x88\x43\xb3"	// bfxil	x0, x1, #3, #32
180 //#define ARM64_CODE "\x01\x71\x08\xd5"	// sys	#0, c7, c1, #0, x1
181 //#define ARM64_CODE "\x00\x71\x28\xd5"	// sysl	x0, #0, c7, c1, #0
182 
183 //#define ARM64_CODE "\x20\xf4\x18\x9e"	// fcvtzs	x0, s1, #3
184 //#define ARM64_CODE "\x20\x74\x0b\xd5"	// dc	zva, x0: FIXME: handle as "sys" insn
185 //#define ARM64_CODE "\x00\x90\x24\x1e"	// fmov s0, ##10.00000000
186 //#define ARM64_CODE "\xe1\x0b\x40\xb9"	// ldr		w1, [sp, #0x8]
187 //#define ARM64_CODE "\x20\x78\x62\xf8"	// ldr x0, [x1, x2, lsl #3]
188 //#define ARM64_CODE "\x41\x14\x44\xb3"	// bfm	x1, x2, #4, #5
189 //#define ARM64_CODE "\x80\x23\x29\xd5"	// sysl	x0, #1, c2, c3, #4
190 //#define ARM64_CODE "\x20\x00\x24\x1e"	// fcvtas	w0, s1
191 //#define ARM64_CODE "\x41\x04\x40\xd2"	// eor	x1, x2, #0x3
192 //#define ARM64_CODE "\x9f\x33\x03\xd5"	// 	dsb	osh
193 //#define ARM64_CODE "\x41\x10\x23\x8a"	// bic	x1, x2, x3, lsl #4
194 //#define ARM64_CODE "\x16\x41\x3c\xd5"	// mrs	x22, sp_el1
195 //#define ARM64_CODE "\x41\x1c\x63\x0e"	// bic	v1.8b, v2.8b, v3.8b
196 //#define ARM64_CODE "\x41\xd4\xe3\x6e"	// fabd	v1.2d, v2.2d, v3.2d
197 //#define ARM64_CODE "\x20\x8c\x62\x2e"	// cmeq	v0.4h, v1.4h, v2.4h
198 //#define ARM64_CODE "\x20\x98\x20\x4e"	// cmeq	v0.16b, v1.16b, #0
199 //#define ARM64_CODE "\x20\x2c\x05\x4e"	// smov	x0, v1.b[2]
200 //#define ARM64_CODE "\x21\xe4\x00\x2f"	// movi d1, #0xff
201 //#define ARM64_CODE "\x60\x78\x08\xd5"	// at	s1e0w, x0	// FIXME: same problem with dc ZVA
202 //#define ARM64_CODE "\x20\x00\xa0\xf2"	// movk	x0, #1, lsl #16
203 //#define ARM64_CODE "\x20\x08\x00\xb1"	// adds	x0, x1, #0x2
204 //#define ARM64_CODE "\x41\x04\x00\x0f"	// movi v1.2s, #0x2
205 //#define ARM64_CODE "\x06\x00\x00\x14"	// b 0x44
206 //#define ARM64_CODE "\x00\x90\x24\x1e"	// fmov s0, ##10.00000000
207 //#define ARM64_CODE "\x5f\x3f\x03\xd5"	// clrex
208 //#define ARM64_CODE "\x5f\x3e\x03\xd5"	// clrex #14
209 //#define ARM64_CODE "\x20\x00\x02\xab"	// adds	 x0, x1, x2 (alias of adds x0, x1, x2, lsl #0)
210 //#define ARM64_CODE "\x20\xf4\x18\x9e"	// fcvtzs	x0, s1, #3
211 //#define ARM64_CODE "\x20\xfc\x02\x9b"	// mneg	x0, x1, x2
212 //#define ARM64_CODE "\xd0\xb6\x1e\xd5"	// msr	s3_6_c11_c6_6, x16
213 
214 //#define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x20\x04\x81\xda\x20\x08\x02\x8b"
215 
216 //#define ARM64_CODE "\x09\x00\x38\xd5"	// DBarrier
217 //#define ARM64_CODE "\x20\xe4\x3d\x0f\xa2\x00\xae\x9e"
218 //#define ARM64_CODE "\x9f\x37\x03\xd5\xbf\x33\x03\xd5\xdf\x3f\x03\xd5"	// DBarrier
219 //#define ARM64_CODE "\x10\x5b\xe8\x3c"
220 //#define ARM64_CODE "\x00\x18\xa0\x5f\xa2\x00\xae\x9e"
221 
222 #define ARM64_CODE "\x09\x00\x38\xd5\xbf\x40\x00\xd5\x0c\x05\x13\xd5\x20\x50\x02\x0e\x20\xe4\x3d\x0f\x00\x18\xa0\x5f\xa2\x00\xae\x9e\x9f\x37\x03\xd5\xbf\x33\x03\xd5\xdf\x3f\x03\xd5\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x20\x04\x81\xda\x20\x08\x02\x8b\x10\x5b\xe8\x3c"
223 
224 	struct platform platforms[] = {
225 		{
226 			CS_ARCH_ARM64,
227 			CS_MODE_ARM,
228 			(unsigned char *)ARM64_CODE,
229 			sizeof(ARM64_CODE) - 1,
230 			"ARM-64"
231 		},
232 	};
233 
234 	uint64_t address = 0x2c;
235 	cs_insn *insn;
236 	int i;
237 	size_t count;
238 
239 	for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
240 		cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
241 		if (err) {
242 			printf("Failed on cs_open() with error returned: %u\n", err);
243 			abort();
244 		}
245 
246 		cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
247 
248 		count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
249 		if (count) {
250 			size_t j;
251 
252 			printf("****************\n");
253 			printf("Platform: %s\n", platforms[i].comment);
254 			print_string_hex("Code: ", platforms[i].code, platforms[i].size);
255 			printf("Disasm:\n");
256 
257 			for (j = 0; j < count; j++) {
258 				printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
259 				print_insn_detail(&insn[j]);
260 			}
261 			printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
262 
263 			// free memory allocated by cs_disasm()
264 			cs_free(insn, count);
265 		} else {
266 			printf("****************\n");
267 			printf("Platform: %s\n", platforms[i].comment);
268 			print_string_hex("Code: ", platforms[i].code, platforms[i].size);
269 			printf("ERROR: Failed to disasm given code!\n");
270 			abort();
271 		}
272 
273 		printf("\n");
274 
275 		cs_close(&handle);
276 	}
277 }
278 
main()279 int main()
280 {
281 	test();
282 
283 	return 0;
284 }
285 
286