Home | History | Annotate | Download | only in ocaml
      1 (* Capstone Disassembly Engine
      2 * By Guillaume Jeanne <guillaume.jeanne (a] ensimag.fr>, 2014> *)
      3 
      4 open Printf
      5 open Capstone
      6 open Sparc
      7 
      8 
      9 let 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 
     17 let _SPARC_CODE = "\x80\xa0\x40\x02\x85\xc2\x60\x08\x85\xe8\x20\x01\x81\xe8\x00\x00\x90\x10\x20\x01\xd5\xf6\x10\x16\x21\x00\x00\x0a\x86\x00\x40\x02\x01\x00\x00\x00\x12\xbf\xff\xff\x10\xbf\xff\xff\xa0\x02\x00\x09\x0d\xbf\xff\xff\xd4\x20\x60\x00\xd4\x4e\x00\x16\x2a\xc2\x80\x03";;
     18 let _SPARCV9_CODE = "\x81\xa8\x0a\x24\x89\xa0\x10\x20\x89\xa0\x1a\x60\x89\xa0\x00\xe0";;
     19 
     20 
     21 let all_tests = [
     22         (CS_ARCH_SPARC, [CS_MODE_BIG_ENDIAN], _SPARC_CODE, "Sparc");
     23         (CS_ARCH_SPARC, [CS_MODE_BIG_ENDIAN; CS_MODE_V9], _SPARCV9_CODE, "SparcV9");
     24 ];;
     25 
     26 let print_op handle i op =
     27 	( match op.value with
     28 	| SPARC_OP_INVALID _ -> ();	(* this would never happens *)
     29 	| SPARC_OP_REG reg -> printf "\t\top[%d]: REG = %s\n" i (cs_reg_name handle reg);
     30 	| SPARC_OP_IMM imm -> printf "\t\top[%d]: IMM = 0x%x\n" i imm;
     31 	| SPARC_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: 0x%x\n" i mem.index;
     36 		if mem.disp != 0 then
     37 			printf "\t\t\toperands[%u].mem.disp: 0x%x\n" i mem.disp;
     38 		);
     39 	);
     40 
     41 	();;
     42 
     43 
     44 let print_detail handle insn =
     45 	match insn.arch with
     46 	| CS_INFO_SPARC sparc -> (
     47 			(* print all operands info (type & value) *)
     48 			if (Array.length sparc.operands) > 0 then (
     49 				printf "\top_count: %d\n" (Array.length sparc.operands);
     50 				Array.iteri (print_op handle) sparc.operands;
     51 			);
     52 			printf "\n";
     53 		);
     54 	| _ -> ();
     55 	;;
     56 
     57 
     58 let print_insn handle insn =
     59 	printf "0x%x\t%s\t%s\n" insn.address insn.mnemonic insn.op_str;
     60 	print_detail handle insn
     61 
     62 
     63 let print_arch x =
     64 	let (arch, mode, code, comment) = x in
     65 		let handle = cs_open arch mode in
     66 		let err = cs_option handle CS_OPT_DETAIL _CS_OPT_ON in
     67 		match err with
     68 		| _ -> ();
     69 		let insns = cs_disasm handle code 0x1000L 0L in
     70 			printf "*************\n";
     71 			printf "Platform: %s\n" comment;
     72 			List.iter (print_insn handle) insns;
     73 		match cs_close handle with
     74 		| 0 -> ();
     75 		| _ -> printf "Failed to close handle";
     76 		;;
     77 
     78 
     79 List.iter print_arch all_tests;;
     80