• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1(* Capstone Disassembly Engine
2* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 *)
3
4open Printf
5open Capstone
6open X86
7open X86_const
8
9
10let print_string_hex comment str =
11	printf "%s" comment;
12	for i = 0 to (Array.length str - 1) do
13		printf "0x%02x " str.(i)
14	done;
15	printf "\n"
16
17
18let _X86_CODE16 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00";;
19let _X86_CODE32 = "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00";;
20let _X86_CODE64 = "\x55\x48\x8b\x05\xb8\x13\x00\x00";;
21
22
23let all_tests = [
24	(CS_ARCH_X86, [CS_MODE_16], _X86_CODE16, "X86 16bit (Intel syntax)", 0L);
25	(CS_ARCH_X86, [CS_MODE_32], _X86_CODE32, "X86 32bit (ATT syntax)", _CS_OPT_SYNTAX_ATT);
26	(CS_ARCH_X86, [CS_MODE_32], _X86_CODE32, "X86 32 (Intel syntax)", 0L);
27	(CS_ARCH_X86, [CS_MODE_64], _X86_CODE64, "X86 64 (Intel syntax)", 0L);
28];;
29
30let print_op handle i op =
31	( match op.value with
32	| X86_OP_INVALID _ -> ();	(* this would never happens *)
33	| X86_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg);
34	| X86_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm;
35	| X86_OP_FP fp -> printf "\t\top[%d]: FP = %f\n" i fp;
36	| X86_OP_MEM mem -> ( printf "\t\top[%d]: MEM\n" i;
37		if mem.base != 0 then
38			printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle  mem.base);
39		if mem.index != 0 then
40			printf "\t\t\toperands[%u].mem.index: REG = %s\n" i (cs_reg_name handle mem.index);
41		if mem.scale != 1 then
42			printf "\t\t\toperands[%u].mem.scale: %d\n" i mem.scale;
43		if mem.disp != 0 then
44			printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp;
45		);
46	);
47	();;
48
49
50let print_detail handle mode insn =
51	match insn.arch with
52	| CS_INFO_X86 x86 -> (
53			print_string_hex "\tPrefix: " x86.prefix;
54
55			(* print instruction's opcode *)
56			print_string_hex "\tOpcode: " x86.opcode;
57
58			(* print operand's size, address size, displacement size & immediate size *)
59			printf "\taddr_size: %u\n" x86.addr_size;
60
61			(* print modRM byte *)
62			printf "\tmodrm: 0x%x\n" x86.modrm;
63
64			(* print displacement value *)
65			if x86.disp != 0 then
66			printf "\tdisp: 0x%x\n" x86.disp;
67
68			(* SIB is invalid in 16-bit mode *)
69			if not (List.mem CS_MODE_16 mode) then (
70				(* print SIB byte *)
71				printf "\tsib: 0x%x\n" x86.sib;
72
73				(* print sib index/scale/base (if applicable) *)
74				if x86.sib_index != _X86_REG_INVALID then
75				printf "\tsib_index: %s, sib_scale: %u, sib_base: %s\n"
76				(cs_reg_name handle x86.sib_index)
77				x86.sib_scale
78				(cs_reg_name handle x86.sib_base);
79			);
80
81			(* print all operands info (type & value) *)
82			if (Array.length x86.operands) > 0 then (
83				printf "\top_count: %d\n" (Array.length x86.operands);
84				Array.iteri (print_op handle) x86.operands;
85			);
86			printf "\n";
87	);
88	| _ -> ();
89	;;
90
91
92let print_insn handle mode insn =
93	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
94	print_detail handle mode insn
95
96
97let print_arch x =
98	let (arch, mode, code, comment, syntax) = x in
99	let handle = cs_open arch mode in (
100		if syntax != 0L then (
101			let err = cs_option handle CS_OPT_SYNTAX syntax in
102			match err with
103			| _ -> ();
104		);
105		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
106		match err with
107		| _ -> ();
108		let insns = cs_disasm handle code 0x1000L 0L in (
109			printf "*************\n";
110			printf "Platform: %s\n" comment;
111			List.iter (print_insn handle mode) insns;
112		);
113		match cs_close handle with
114		| 0 -> ();
115		| _ -> printf "Failed to close handle";
116	);;
117
118List.iter print_arch all_tests;;
119