Skip to content

Commit 06b700b

Browse files
committed
CHB:ARM: add disassembly-level callgraph
1 parent 42f9bbd commit 06b700b

9 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎CodeHawk/CHB/bchanalyze/bCHFileIO.ml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,19 @@ let save_arm_functions_list () =
187187
end
188188

189189

190+
let save_arm_callgraph () =
191+
let filename = get_callgraph_filename () in
192+
let doc = xmlDocument () in
193+
let root = get_bch_root "callgraph" in
194+
let cNode = xmlElement "callgraph" in
195+
let callgraph = BCHARMAssemblyFunctions.arm_assembly_functions#get_callgraph in
196+
begin
197+
callgraph#write_xml cNode;
198+
doc#setNode root;
199+
root#appendChildren [cNode];
200+
file_output#saveFile filename doc#toPretty
201+
end
202+
190203

191204
let save_global_state () =
192205
let filename = get_global_state_filename () in

‎CodeHawk/CHB/bchanalyze/bCHFileIO.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ val save_functions_list: unit -> unit
4141

4242
(** save function cfg info for arm functions *)
4343
val save_arm_functions_list: unit -> unit
44+
val save_arm_callgraph: unit -> unit
4445

4546
val save_global_state: unit -> unit
4647
val save_global_memory_map: unit -> unit

‎CodeHawk/CHB/bchcmdline/bCHXBinaryAnalyzer.ml‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ let add_print_datasection (s: string) =
116116
print_datasections := s :: !print_datasections
117117

118118
let save_asm_cfg_info = ref false (* save functions list with cfg info in xml *)
119+
let save_asm_callgraph = ref false (* save callgraph edges based on disassembly *)
119120
let set_datablocks = ref false (* only supported for arm *)
120121
let construct_all_functions = ref false
121122

@@ -226,6 +227,8 @@ let speclist =
226227
"save assembly listing in the analysis directory");
227228
("-save_asm_cfg_info", Arg.Unit (fun () -> save_asm_cfg_info := true),
228229
"save list of functions with cfg info to xml file (may be slow)");
230+
("-save_asm_callgraph", Arg.Unit (fun () -> save_asm_callgraph := true),
231+
"save list of callgraph edges based on disassembly only in xml");
229232
("-print_datasection", Arg.String (fun s -> add_print_datasection s),
230233
"print the data sections as part of the assembly listing");
231234
("-construct_all_functions",
@@ -618,7 +621,12 @@ let main () =
618621
(if !save_asm_cfg_info then
619622
begin
620623
save_arm_functions_list ();
621-
pr_timing [STR "function cfg info saved"]
624+
pr_timing [STR "function cfg info saved"];
625+
end);
626+
(if !save_asm_callgraph then
627+
begin
628+
save_arm_callgraph ();
629+
pr_timing [STR "callgraph saved"]
622630
end);
623631
save_system_info ();
624632
pr_timing [STR "system_info saved"];

‎CodeHawk/CHB/bchlib/bCHCallgraph.ml‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ object ('a)
151151
method get_constraint : xpr_t
152152
method get_stack_arguments : (int * xpr_t) list
153153
method get_register_arguments: (variable_t * xpr_t) list
154+
method write_xml : xml_element_int -> unit
154155

155156
(* printing *)
156157
method toPretty : pretty_t
@@ -223,7 +224,7 @@ class callgraph_edge_t
223224
(callsite:ctxt_iaddress_t)
224225
(_argExprs:(int * string * xpr_t) list)
225226
:callgraph_edge_int =
226-
object (_:'a)
227+
object (self:'a)
227228

228229
method compare (other:'a) =
229230
let l0 = src#compare other#get_source in
@@ -246,6 +247,18 @@ object (_:'a)
246247

247248
method get_register_arguments = []
248249

250+
method write_xml (node: xml_element_int) =
251+
let set = node#setAttribute in
252+
let seta t a = set t a#to_hex_string in
253+
let tgtinfo = new callgraph_node_info_t self#get_target in
254+
let tgtnode = xmlElement "tgt" in
255+
begin
256+
seta "src" self#get_source;
257+
set "cs" self#get_callsite;
258+
tgtinfo#write_xml tgtnode;
259+
node#appendChildren [tgtnode]
260+
end
261+
249262
method toPretty =
250263
LBLOCK [ src#toPretty ; STR " -> " ;
251264
callgraph_node_to_pretty tgt ; STR " @ " ;
@@ -349,7 +362,14 @@ object (self)
349362
let eNode = xmlElement "edges" in
350363
let edges = ref [] in
351364
let _ = out_n#iter (fun _ s -> edges := s#toList @ !edges) in
352-
node#appendChildren [ eNode ]
365+
let _ =
366+
List.iter (fun e ->
367+
let edgenode = xmlElement "edge" in
368+
begin
369+
e#write_xml edgenode;
370+
eNode#appendChildren [edgenode]
371+
end) !edges in
372+
node#appendChildren [eNode]
353373

354374
end
355375

‎CodeHawk/CHB/bchlib/bCHPreFileIO.ml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ let get_functions_filename () =
328328
let _ = create_directory fdir in
329329
Filename.concat fdir (exename ^ "_functions.xml")
330330

331+
332+
let get_callgraph_filename () =
333+
let exename = get_filename () in
334+
let fdir = get_analysis_dir () in
335+
let _ = create_directory fdir in
336+
Filename.concat fdir (exename ^ "_callgraph.xml")
337+
338+
331339
let get_function_filename (fname:string) (ext:string) =
332340
let exename = get_filename () in
333341
let fdir = get_analysis_dir () in

‎CodeHawk/CHB/bchlib/bCHPreFileIO.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ val get_pwr_dictionary_filename: unit -> string
5555
val get_pwr_assembly_instructions_filename: unit -> string
5656

5757
val get_functions_filename: unit -> string
58+
val get_callgraph_filename: unit -> string
5859
val get_global_state_filename: unit -> string
5960
val get_global_memory_map_filename: unit -> string
6061
val get_system_info_filename: unit -> string

‎CodeHawk/CHB/bchlib/bCHVersion.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ end
9595

9696

9797
let version = new version_info_t
98-
~version:"0.6.0_20260909"
99-
~date:"2026-0909"
98+
~version:"0.6.0_20260930"
99+
~date:"2026-09-30"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

‎CodeHawk/CHB/bchlibarm32/bCHARMAssemblyFunction.ml‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,28 @@ object (self)
229229
List.iter (fun (b:arm_assembly_block_int) ->
230230
b#itera (fun iaddr instr -> f faddr iaddr instr)) self#get_blocks
231231

232-
method populate_callgraph (_callgraph: callgraph_int) =
233-
self#iteri (fun _ _iaddr _instr -> ())
232+
method populate_callgraph (callgraph: callgraph_int) =
233+
let finfo = BCHFunctionInfo.get_function_info faddr in
234+
self#iteri (fun _ iaddr instr ->
235+
match instr#get_opcode with
236+
| BranchLink _
237+
| BranchLinkExchange _
238+
| Branch _ ->
239+
if finfo#has_call_target iaddr then
240+
let rec add_call_target tgt =
241+
match tgt with
242+
| StubTarget (SOFunction name)
243+
| StaticStubTarget (_, SOFunction name) ->
244+
callgraph#add_so_edge faddr name iaddr []
245+
| AppTarget a -> callgraph#add_app_edge faddr a iaddr []
246+
| UnknownTarget ->
247+
callgraph#add_unresolved_edge faddr (-1) iaddr []
248+
| IndirectTarget (_, tgts) -> List.iter add_call_target tgts
249+
| _ -> () in
250+
add_call_target (finfo#get_call_target iaddr)#get_target
251+
else
252+
()
253+
| _ -> () )
234254

235255
method includes_instruction_address (va:doubleword_int) =
236256
List.exists (fun b -> b#includes_instruction_address va) blocks

‎CodeHawk/CHB/bchlibarm32/bCHFnARMTypeConstraints.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,9 @@ object (self)
16321632
) rmdefs);
16331633
end
16341634

1635+
| ReverseSubtract (_, _, rd, _, _, _) ->
1636+
regvar_type_introduction "RSB" rd
1637+
16351638
| SignedMultiplyLong (_, _, rdlo, rdhi, rn, rm) ->
16361639
let rdloreg = rdlo#to_register in
16371640
let lhslotypevar = mk_reglhs_typevar rdloreg faddr iaddr in

0 commit comments

Comments
 (0)