• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1(* Capstone Disassembly Engine
2* By Guillaume Jeanne <guillaume.jeanne@ensimag.fr>, 2014> *)
3
4open Printf
5open Capstone
6open Ppc
7
8
9let print_string_hex comment str =
10	printf "%s" comment;
11	for i = 0 to (Array.length str - 1) do
12		printf "0x%02x " str.(i)
13	done;
14	printf "\n"
15
16
17let _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";;
18
19let all_tests = [
20	(CS_ARCH_PPC, [CS_MODE_64; CS_MODE_BIG_ENDIAN], _PPC_CODE, "PPC-64");
21];;
22
23let print_op handle i op =
24	( match op.value with
25	| PPC_OP_INVALID _ -> ();	(* this would never happens *)
26	| PPC_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg);
27	| PPC_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm;
28	| PPC_OP_MEM mem -> ( printf "\t\top[%d]: MEM\n" i;
29		if mem.base != 0 then
30			printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle mem.base);
31		if mem.disp != 0 then
32			printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp;
33		);
34	| PPC_OP_CRX crx -> ( printf "\t\top[%d]: CRX\n" i;
35		if crx.scale != 0 then
36			printf "\t\t\toperands[%u].crx.scale = %u\n" i crx.scale;
37		if crx.reg != 0 then
38			printf "\t\t\toperands[%u].crx.reg = %s\n" i (cs_reg_name handle crx.reg);
39		if crx.cond != 0 then
40			printf "\t\t\toperands[%u].crx.cond = 0x%x\n" i crx.cond;
41		);
42	);
43	();;
44
45
46let print_detail handle insn =
47	match insn.arch with
48	| CS_INFO_PPC ppc -> (
49			(* print all operands info (type & value) *)
50			if (Array.length ppc.operands) > 0 then (
51				printf "\top_count: %d\n" (Array.length ppc.operands);
52				Array.iteri (print_op handle) ppc.operands;
53			);
54			printf "\n";
55		);
56	| _ -> ();
57	;;
58
59
60let print_insn handle insn =
61	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
62	print_detail handle insn
63
64
65let print_arch x =
66	let (arch, mode, code, comment) = x in
67		let handle = cs_open arch mode in
68		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
69		match err with
70		| _ -> ();
71		let insns = cs_disasm handle code 0x1000L 0L in
72			printf "*************\n";
73			printf "Platform: %s\n" comment;
74			List.iter (print_insn handle) insns;
75		match cs_close handle with
76		| 0 -> ();
77		| _ -> printf "Failed to close handle";
78		;;
79
80
81List.iter print_arch all_tests;;
82