Skip to content

Commit

Permalink
makes radare2 plugin respect the paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ivg committed Jun 29, 2020
1 parent ceaaace commit 7866818
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions plugins/radare2/radare2_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ let agent =
KB.Agent.register ~package:"bap.std" "radare2-symbolizer"
~desc:"extracts symbols radare2"

let provide_roots funcs =
let provide_roots file funcs =
let promise_property slot =
KB.promise slot @@ fun label ->
KB.collect Theory.Label.addr label >>| function
| None -> None
| Some addr ->
KB.collect Theory.Label.addr label >>=? fun addr ->
KB.collect Theory.Label.path label >>|? fun path ->
if String.equal path file then
let addr = Bitvec.to_bigint addr in
Option.some_if (Hashtbl.mem funcs addr) true in
Option.some_if (Hashtbl.mem funcs addr) true
else None in
promise_property Theory.Label.is_valid;
promise_property Theory.Label.is_subroutine

let extract_name (json : Yojson.t) =
let extract_name (json : Yojson.t) =
match json with
| `Assoc list ->
| `Assoc list ->
(match List.find list ~f:(fun (key, _) -> String.equal key "name") with
| Some (_, v) -> (match v with
| `String str -> Some str
| _ -> None)
| _ -> None)
| _ -> None

let extract_addr (json : Yojson.t) =
let extract_addr (json : Yojson.t) =
match json with
| `Assoc list ->
| `Assoc list ->
(match List.find list ~f:(fun (key, _) -> String.equal key "vaddr") with
| Some (_, v) -> (match v with
| `Int i -> Some (Z.of_int i)
Expand All @@ -42,12 +43,12 @@ let extract_addr (json : Yojson.t) =
| _ -> None)
| _ -> None

let strip str =
let strip str =
match String.chop_prefix str ~prefix:"sym.imp." with
| Some str -> str
| None -> str

let provide_radare2 file =
let provide_radare2 file =
let funcs = Hashtbl.create (module struct
type t = Z.t
let compare = Z.compare and hash = Z.hash
Expand All @@ -57,8 +58,9 @@ let provide_radare2 file =
let symbol_list = match R2.with_command_j "isj" file with
| `List list -> Some list
| s -> warning "unexpected radare2 output: %a" Yojson.pp s; None
| exception _ -> warning "failed to get symbols - radare2 command failed"; None in
Option.iter symbol_list
| exception Invalid_argument msg ->
warning "radare2 command failed with: %s" msg; None in
Option.iter symbol_list
~f:(List.iter ~f:(fun s -> match extract_name s, extract_addr s with
| Some name, Some addr -> accept (strip name) addr
| _ -> debug "skipping json item %a" Yojson.pp s));
Expand All @@ -67,8 +69,9 @@ let provide_radare2 file =
let symbolizer = Symbolizer.create @@ fun addr ->
Hashtbl.find funcs @@
Bitvec.to_bigint (Word.to_bitvec addr) in
let symbolizer = Symbolizer.set_path symbolizer file in
Symbolizer.provide agent symbolizer;
provide_roots funcs
provide_roots file funcs



Expand All @@ -86,4 +89,4 @@ let () =
`S "SEE ALSO";
`P "$(b,bap-plugin-objdump)(1)"
];
Config.when_ready (fun {get=_} -> main ())
Config.when_ready (fun {get=_} -> main ())

0 comments on commit 7866818

Please sign in to comment.