Skip to content

Commit 7514371

Browse files
authored
prints the jump destination address and name in the assembly output (#1469)
This is a simple quality of life update that should be done many years ago. This change adds printing the jump destination address and name, if present, in the assembly output, much like objdump does, e.g., ``` c203: e8 70 7c ff ff callq -0x8390 # 3e78 <malloc> ``` This commit also publishes the `Symtab.callee` function that gives this information.
1 parent b865c4a commit 7514371

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

lib/bap/bap.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7845,6 +7845,13 @@ module Std : sig
78457845
(** [span fn] returns a memory map of a region occupied by a
78467846
function [fn] *)
78477847
val span : fn -> unit memmap
7848+
7849+
(** [explicit_callee symtab address] returns a callee which is
7850+
called from a block with the given [address].
7851+
7852+
@since 2.5.0
7853+
*)
7854+
val callee : t -> addr -> string option
78487855
end
78497856

78507857
type lifter = mem -> Disasm_expert.Basic.full_insn -> bil Or_error.t

lib/bap_disasm/bap_disasm_symtab.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ let insert_call ?(implicit=false) symtab block data =
120120

121121
let explicit_callee {ecalls} = Map.find ecalls
122122
let implicit_callee {icalls} = Map.find icalls
123+
let callee tab src = match explicit_callee tab src with
124+
| Some dst -> Some dst
125+
| None -> implicit_callee tab src
126+
123127

124128

125129
let (<--) = fun g f -> match g with

lib/bap_disasm/bap_disasm_symtab.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ val explicit_callee : t -> addr -> string option
6161
(** [implicit_callee symtab address] returns a callee which is
6262
implicitly called from a block with the given [address]. *)
6363
val implicit_callee : t -> addr -> string option
64+
65+
val callee : t -> addr -> string option

plugins/print/print_main.ml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ let section_name memory start =
369369
function Some name -> name
370370
| None -> Format.asprintf ".section@%a" Addr.pp start
371371

372-
373372
let print_disasm pp_insn patterns ppf proj =
374373
let memory = Project.memory proj in
375374
let syms = Project.symbols proj in
@@ -389,19 +388,40 @@ let print_disasm pp_insn patterns ppf proj =
389388
sorted_blocks (Graphs.Cfg.nodes cfg) |> Seq.iter ~f:(fun blk ->
390389
let mem = Block.memory blk in
391390
fprintf ppf "%a:@\n" pp_addr (Memory.min_addr mem);
392-
Block.insns blk |> List.iter ~f:(pp_insn ppf))));
391+
Block.insns blk |> List.iter ~f:(pp_insn syms blk ppf))));
393392
pp_close_tbox ppf ()
394393

395-
let pp_bil fmt ppf (mem,insn) =
394+
let pp_bil fmt _ _ ppf (mem,insn) =
396395
let pp_bil ppf = Bil.Io.print ~fmt ppf in
397396
let addr = Memory.min_addr mem in
398397
fprintf ppf "%a: %s@\n%a@\n" pp_addr addr (Insn.asm insn)
399398
pp_bil (Insn.bil insn)
400399

401-
let pp_insn fmt ppf (mem,insn) =
400+
401+
let jmp_dst insn =
402+
let rec find = List.find_map ~f:(function
403+
| Bil.Jmp (Int dst) -> Some dst
404+
| Bil.If (_,yay,nay) ->
405+
Option.first_some (find yay) (find nay)
406+
| _ -> None) in
407+
find (Insn.bil insn)
408+
409+
let print_jmp_dst tab blk ppf insn =
410+
match jmp_dst insn, Symtab.callee tab (Block.addr blk) with
411+
| Some dst, Some name ->
412+
Format.fprintf ppf " # %s <%s>" (Addr.string_of_value dst) name
413+
| Some dst, None ->
414+
Format.fprintf ppf " # %s" (Addr.string_of_value dst)
415+
| None, Some name ->
416+
Format.fprintf ppf " # <%s>" name
417+
| None, None -> ()
418+
419+
let pp_insn fmt tab blk ppf (mem,insn) =
402420
Memory.pp ppf mem;
403421
pp_print_tab ppf () [@ocaml.warning "-3"];
404422
Insn.Io.print ~fmt ppf insn;
423+
if phys_equal insn (Block.terminator blk)
424+
then print_jmp_dst tab blk ppf insn;
405425
fprintf ppf "@\n"
406426

407427
let pp_knowledge ppf _ =

0 commit comments

Comments
 (0)