diff --git a/lib/bap/bap.mli b/lib/bap/bap.mli index 1fb4d31d2..6f8f25b42 100644 --- a/lib/bap/bap.mli +++ b/lib/bap/bap.mli @@ -7845,6 +7845,13 @@ module Std : sig (** [span fn] returns a memory map of a region occupied by a function [fn] *) val span : fn -> unit memmap + + (** [explicit_callee symtab address] returns a callee which is + called from a block with the given [address]. + + @since 2.5.0 + *) + val callee : t -> addr -> string option end type lifter = mem -> Disasm_expert.Basic.full_insn -> bil Or_error.t diff --git a/lib/bap_disasm/bap_disasm_symtab.ml b/lib/bap_disasm/bap_disasm_symtab.ml index 17ad332eb..53b79d5d3 100644 --- a/lib/bap_disasm/bap_disasm_symtab.ml +++ b/lib/bap_disasm/bap_disasm_symtab.ml @@ -120,6 +120,10 @@ let insert_call ?(implicit=false) symtab block data = let explicit_callee {ecalls} = Map.find ecalls let implicit_callee {icalls} = Map.find icalls +let callee tab src = match explicit_callee tab src with + | Some dst -> Some dst + | None -> implicit_callee tab src + let (<--) = fun g f -> match g with diff --git a/lib/bap_disasm/bap_disasm_symtab.mli b/lib/bap_disasm/bap_disasm_symtab.mli index b194ef648..94ee836df 100644 --- a/lib/bap_disasm/bap_disasm_symtab.mli +++ b/lib/bap_disasm/bap_disasm_symtab.mli @@ -61,3 +61,5 @@ val explicit_callee : t -> addr -> string option (** [implicit_callee symtab address] returns a callee which is implicitly called from a block with the given [address]. *) val implicit_callee : t -> addr -> string option + +val callee : t -> addr -> string option diff --git a/plugins/print/print_main.ml b/plugins/print/print_main.ml index 468dd5d82..5d2ecc001 100644 --- a/plugins/print/print_main.ml +++ b/plugins/print/print_main.ml @@ -369,7 +369,6 @@ let section_name memory start = function Some name -> name | None -> Format.asprintf ".section@%a" Addr.pp start - let print_disasm pp_insn patterns ppf proj = let memory = Project.memory proj in let syms = Project.symbols proj in @@ -389,19 +388,40 @@ let print_disasm pp_insn patterns ppf proj = sorted_blocks (Graphs.Cfg.nodes cfg) |> Seq.iter ~f:(fun blk -> let mem = Block.memory blk in fprintf ppf "%a:@\n" pp_addr (Memory.min_addr mem); - Block.insns blk |> List.iter ~f:(pp_insn ppf)))); + Block.insns blk |> List.iter ~f:(pp_insn syms blk ppf)))); pp_close_tbox ppf () -let pp_bil fmt ppf (mem,insn) = +let pp_bil fmt _ _ ppf (mem,insn) = let pp_bil ppf = Bil.Io.print ~fmt ppf in let addr = Memory.min_addr mem in fprintf ppf "%a: %s@\n%a@\n" pp_addr addr (Insn.asm insn) pp_bil (Insn.bil insn) -let pp_insn fmt ppf (mem,insn) = + +let jmp_dst insn = + let rec find = List.find_map ~f:(function + | Bil.Jmp (Int dst) -> Some dst + | Bil.If (_,yay,nay) -> + Option.first_some (find yay) (find nay) + | _ -> None) in + find (Insn.bil insn) + +let print_jmp_dst tab blk ppf insn = + match jmp_dst insn, Symtab.callee tab (Block.addr blk) with + | Some dst, Some name -> + Format.fprintf ppf " # %s <%s>" (Addr.string_of_value dst) name + | Some dst, None -> + Format.fprintf ppf " # %s" (Addr.string_of_value dst) + | None, Some name -> + Format.fprintf ppf " # <%s>" name + | None, None -> () + +let pp_insn fmt tab blk ppf (mem,insn) = Memory.pp ppf mem; pp_print_tab ppf () [@ocaml.warning "-3"]; Insn.Io.print ~fmt ppf insn; + if phys_equal insn (Block.terminator blk) + then print_jmp_dst tab blk ppf insn; fprintf ppf "@\n" let pp_knowledge ppf _ =