Home | History | Annotate | Download | only in ocaml
      1 (* Capstone Disassembly Engine
      2 * By Nguyen Anh Quynh <aquynh (a] gmail.com>, 2013-2014 *)
      3 
      4 open Printf
      5 open Capstone
      6 open Arm64
      7 open Arm64_const
      8 
      9 
     10 let 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 
     18 let _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";;
     19 
     20 let all_tests = [
     21         (CS_ARCH_ARM64, [CS_MODE_ARM], _ARM64_CODE, "ARM-64");
     22 ];;
     23 
     24 let print_op handle i op =
     25 	( match op.value with
     26 	| ARM64_OP_INVALID _ -> ();	(* this would never happens *)
     27 	| ARM64_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg);
     28 	| ARM64_OP_CIMM imm -> printf "\t\top[%d]: C-IMM = %u\n" i imm;
     29 	| ARM64_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm;
     30 	| ARM64_OP_FP fp -> printf "\t\top[%d]: FP = %f\n" i fp;
     31 	| ARM64_OP_MEM mem -> ( printf "\t\top[%d]: MEM\n" i;
     32 		if mem.base != 0 then
     33 			printf "\t\t\toperands[%u].mem.base: REG = %s\n" i (cs_reg_name handle mem.base);
     34 		if mem.index != 0 then
     35 			printf "\t\t\toperands[%u].mem.index: REG = %s\n" i (cs_reg_name handle mem.index);
     36 		if mem.disp != 0 then
     37 			printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp;
     38 		);
     39 	| ARM64_OP_REG_MRS reg -> printf "\t\top[%d]: REG_MRS = %u\n" i reg;
     40 	| ARM64_OP_REG_MSR reg -> printf "\t\top[%d]: REG_MSR = %u\n" i reg;
     41 	| ARM64_OP_PSTATE v -> printf "\t\top[%d]: PSTATE = %u\n" i v;
     42 	| ARM64_OP_SYS v -> printf "\t\top[%d]: SYS = %u\n" i v;
     43 	| ARM64_OP_PREFETCH v -> printf "\t\top[%d]: PREFETCH = %u\n" i v;
     44 	| ARM64_OP_BARRIER v -> printf "\t\top[%d]: BARRIER = %u\n" i v;
     45 	);
     46 
     47 	if op.shift.shift_type != _ARM64_SFT_INVALID && op.shift.shift_value > 0 then
     48 		printf "\t\t\tShift: type = %u, value = %u\n"
     49                 op.shift.shift_type op.shift.shift_value;
     50 	if op.ext != _ARM64_EXT_INVALID then
     51 		printf "\t\t\tExt: %u\n" op.ext;
     52 
     53 	();;
     54 
     55 
     56 let print_detail handle insn =
     57 	match insn.arch with
     58 	| CS_INFO_ARM64 arm64 -> (
     59 			if arm64.cc != _ARM64_CC_AL && arm64.cc != _ARM64_CC_INVALID then
     60 			printf "\tCode condition: %u\n" arm64.cc;
     61 
     62 			if arm64.update_flags then
     63 			printf "\tUpdate-flags: True\n";
     64 
     65 			if arm64.writeback then
     66 			printf "\tWriteback: True\n";
     67 
     68 			(* print all operands info (type & value) *)
     69 			if (Array.length arm64.operands) > 0 then (
     70 				printf "\top_count: %d\n" (Array.length arm64.operands);
     71 				Array.iteri (print_op handle) arm64.operands;
     72 			);
     73 			printf "\n";
     74 		)
     75 	| _ -> ();
     76 	;;
     77 
     78 
     79 let print_insn handle insn =
     80 	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
     81 	print_detail handle insn
     82 
     83 
     84 let print_arch x =
     85 	let (arch, mode, code, comment) = x in
     86 		let handle = cs_open arch mode in
     87 		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
     88 		match err with
     89 		| _ -> ();
     90 		let insns = cs_disasm handle code 0x1000L 0L in
     91 			printf "*************\n";
     92 			printf "Platform: %s\n" comment;
     93 			List.iter (print_insn handle) insns;
     94 		match cs_close handle with
     95 		| 0 -> ();
     96 		| _ -> printf "Failed to close handle";
     97 		;;
     98 
     99 
    100 
    101 List.iter print_arch all_tests;;
    102