Skip to content

Commit 004b016

Browse files
committed
CHB: initial version of sourcemap
1 parent 57c11ec commit 004b016

File tree

5 files changed

+111
-17
lines changed

5 files changed

+111
-17
lines changed

CodeHawk/CHB/bchcmdline/bCHXBinaryAnalyzer.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ let main () =
558558
let _ = disassembly_summary#set_disassembly_metrics
559559
(get_arm_disassembly_metrics ()) in
560560
let _ = pr_debug [NL; NL; disassembly_summary#toPretty; NL] in
561+
let _ = save_bc_files () in
561562
(*
562563
let _ =
563564
pr_debug [

CodeHawk/CHB/bchlib/bCHBCAttributes.ml

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
66
7-
Copyright (c) 2024 Aarno Labs LLC
7+
Copyright (c) 2024-2025 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -50,6 +50,40 @@ let gcc_attributes_to_precondition_attributes
5050
| _ -> acc) [] attrs
5151

5252

53+
let gcc_attributes_to_srcmapinfo
54+
(attrs: b_attributes_t): srcmapinfo_t option =
55+
let optsrcloc =
56+
List.fold_left (fun acc a ->
57+
match acc with
58+
| Some _ -> acc
59+
| _ ->
60+
match a with
61+
| Attr ("chkc_srcloc", params) ->
62+
(match params with
63+
| [AStr filename; AInt linenumber] ->
64+
Some {srcloc_filename=filename;
65+
srcloc_linenumber=linenumber;
66+
srcloc_notes=[]}
67+
| _ -> None)
68+
| _ -> None) None attrs in
69+
match optsrcloc with
70+
| Some srcloc ->
71+
let binloc =
72+
List.fold_left (fun acc a ->
73+
match acc with
74+
| Some _ -> acc
75+
| _ ->
76+
match a with
77+
| Attr ("chkx_binloc", params) ->
78+
(match params with
79+
| [AStr address] -> Some address
80+
| _ -> None)
81+
| _ -> None) None attrs in
82+
Some {srcmap_srcloc = srcloc; srcmap_binloc = binloc}
83+
| _ -> None
84+
85+
86+
5387
let precondition_attributes_t_to_gcc_attributes
5488
(preattrs: precondition_attribute_t list): b_attributes_t =
5589
let get_params (refindex: int) (optsizeindex: int option) =
@@ -67,8 +101,3 @@ let precondition_attributes_t_to_gcc_attributes
67101
| APCReadWrite (refindex, optsizeindex) ->
68102
(get_access "read_write" (get_params refindex optsizeindex)) :: acc
69103
| _ -> acc) [] preattrs
70-
71-
72-
73-
74-

CodeHawk/CHB/bchlib/bCHBCAttributes.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ open BCHBCTypes
3131
val gcc_attributes_to_precondition_attributes:
3232
b_attributes_t -> precondition_attribute_t list
3333

34+
val gcc_attributes_to_srcmapinfo:
35+
b_attributes_t -> srcmapinfo_t option
36+
3437

3538
val precondition_attributes_t_to_gcc_attributes:
3639
precondition_attribute_t list -> b_attributes_t

CodeHawk/CHB/bchlib/bCHBCFiles.ml

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author: Henny Sipma
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
6-
6+
77
Copyright (c) 2021-2024 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -12,10 +12,10 @@
1212
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313
copies of the Software, and to permit persons to whom the Software is
1414
furnished to do so, subject to the following conditions:
15-
15+
1616
The above copyright notice and this permission notice shall be included in all
1717
copies or substantial portions of the Software.
18-
18+
1919
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -60,6 +60,9 @@ object (self)
6060
val gvardecls = H.create 3 (* bvname -> (varinfo.ix, loc.ix) *)
6161
val gvars = H.create 3 (* idem *)
6262
val gfuns = H.create 3 (* bsvar.bvname -> (fundec, loc.ix) *)
63+
val vinfo_srcmap = H.create 3
64+
(* varinfo.ix -> (ix(filename), linenr, binloc, [ix(notes)]) *)
65+
6366
val mutable varinfo_vid_counter = 10000
6467

6568
method private get_varinfo_id =
@@ -70,6 +73,24 @@ object (self)
7073

7174
method add_bcfile (f: bcfile_t) =
7275
let i = bcd#index_location in
76+
77+
let add_srcmapinfo (vinfo: bvarinfo_t) =
78+
match BCHBCAttributes.gcc_attributes_to_srcmapinfo vinfo.bvattr with
79+
| Some srcmap ->
80+
let vix = bcd#index_varinfo vinfo in
81+
if H.mem vinfo_srcmap vix then
82+
()
83+
else
84+
let srcloc = srcmap.srcmap_srcloc in
85+
let ixfilename = bcd#index_string srcloc.srcloc_filename in
86+
let linenumber = srcloc.srcloc_linenumber in
87+
let ixnotes = List.map bcd#index_string srcloc.srcloc_notes in
88+
let binloc =
89+
match srcmap.srcmap_binloc with
90+
| Some s -> s
91+
| _ -> "none" in
92+
H.add vinfo_srcmap vix (ixfilename, linenumber, binloc, ixnotes)
93+
| _ -> () in
7394
begin
7495
List.iter (fun g ->
7596
match g with
@@ -100,8 +121,10 @@ object (self)
100121
()
101122
else
102123
let _ = chlog#add "bcfiles:add gvardecl" (STR vinfo.bvname) in
124+
let _ = add_srcmapinfo vinfo in
103125
H.replace gvardecls vinfo.bvname (bcd#index_varinfo vinfo, i loc)
104126
| GVar (vinfo, iinfo, loc) ->
127+
let _ = add_srcmapinfo vinfo in
105128
let _ = chlog#add "bcfiles:add gvar" (STR vinfo.bvname) in
106129
H.replace gvars
107130
vinfo.bvname
@@ -112,7 +135,7 @@ object (self)
112135
i loc)
113136
| GFun (fundec, loc) ->
114137
let _ = chlog#add "bcfiles:add gfun" (STR fundec.bsvar.bvname) in
115-
H.replace gfuns fundec.bsvar.bvname (fundec, bcd#index_location loc);
138+
H.replace gfuns fundec.bsvar.bvname (fundec, bcd#index_location loc);
116139
| _ -> ()) f.bglobals;
117140
chlog#add
118141
"bcfiles:add_bcfile"
@@ -411,7 +434,7 @@ object (self)
411434
result := (v2s (bcd#get_varinfo ix)) :: !result) gvardecls;
412435
!result
413436
end
414-
437+
415438
method has_gfun (name: string) = H.mem gfuns name
416439

417440
method get_gfun (name: string) =
@@ -495,7 +518,7 @@ object (self)
495518
match cn#getIntListAttribute "ixs" with
496519
| [cix; locix] -> H.add gcomptags (name, ckey) (cix, locix)
497520
| _ -> ()) (getcc "cid")
498-
521+
499522
method private write_xml_genums (node: xml_element_int) =
500523
let genums = H.fold (fun k v a -> (k, v)::a) genumtags [] in
501524
node#appendChildren
@@ -533,7 +556,7 @@ object (self)
533556
match en#getIntListAttribute "ixs" with
534557
| [eix; locix] -> H.add genumtagdecls name (eix, locix)
535558
| _ -> ()) (getcc "eid")
536-
559+
537560
method private write_xml_gvars (node: xml_element_int) =
538561
let gvarinfos = H.fold (fun k v a -> (k, v)::a) gvars [] in
539562
node#appendChildren
@@ -591,7 +614,29 @@ object (self)
591614
let locix = gn#getIntAttribute "locix" in
592615
let fundec = read_xml_function_definition gn in
593616
H.add gfuns name (fundec, locix)) (getcc "gfun")
594-
617+
618+
method private write_xml_srcmap (node: xml_element_int) =
619+
let srcmapentries = H.fold (fun k v a -> (k, v)::a) vinfo_srcmap [] in
620+
node#appendChildren
621+
(List.map (fun (vix, (ixfn, lnr, binloc, _ixnotes)) ->
622+
let snode = xmlElement "srcloc" in
623+
begin
624+
snode#setIntAttribute "vix" vix;
625+
snode#setIntAttribute "ixfn" ixfn;
626+
snode#setIntAttribute "lnr" lnr;
627+
snode#setAttribute "binloc" binloc;
628+
snode
629+
end) srcmapentries)
630+
631+
method private read_xml_srcmap (node: xml_element_int) =
632+
let getcc = node#getTaggedChildren in
633+
List.iter (fun gs ->
634+
let vix = gs#getIntAttribute "vix" in
635+
let ixfn = gs#getIntAttribute "ixfn" in
636+
let lnr = gs#getIntAttribute "lnr" in
637+
let binloc = gs#getAttribute "binloc" in
638+
H.add vinfo_srcmap vix (ixfn, lnr, binloc, [])) (getcc "srcloc")
639+
595640
method write_xml (node: xml_element_int) =
596641
let tnode = xmlElement "typeinfos" in
597642
let cnode = xmlElement "compinfos" in
@@ -601,6 +646,7 @@ object (self)
601646
let vnode = xmlElement "varinfos" in
602647
let vdnode = xmlElement "varinfodecls" in
603648
let gfunnode = xmlElement "gfuns" in
649+
let srcmapnode = xmlElement "srcmap" in
604650
begin
605651
self#write_xml_gtypes tnode;
606652
self#write_xml_gcomps cnode;
@@ -610,8 +656,9 @@ object (self)
610656
self#write_xml_gvars vnode;
611657
self#write_xml_gvardecls vdnode;
612658
self#write_xml_gfuns gfunnode;
659+
self#write_xml_srcmap srcmapnode;
613660
node#appendChildren[
614-
tnode; cnode; cdnode; enode; ednode; vnode; vdnode; gfunnode]
661+
tnode; cnode; cdnode; enode; ednode; vnode; vdnode; gfunnode; srcmapnode]
615662
end
616663

617664
method read_xml (node: xml_element_int) =
@@ -624,9 +671,10 @@ object (self)
624671
self#read_xml_genumdecls (getc "enuminfodecls");
625672
self#read_xml_gvars (getc "varinfos");
626673
self#read_xml_gvardecls (getc "varinfodecls");
627-
self#read_xml_gfuns (getc "gfuns")
674+
self#read_xml_gfuns (getc "gfuns");
675+
self#read_xml_srcmap (getc "srcmap")
628676
end
629-
677+
630678
end
631679

632680

CodeHawk/CHB/bchlib/bCHBCTypes.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,19 @@ type bcfile_t = {
419419
}
420420

421421

422+
type srclocinfo_t = {
423+
srcloc_filename: string;
424+
srcloc_linenumber: int;
425+
srcloc_notes: string list
426+
}
427+
428+
429+
type srcmapinfo_t = {
430+
srcmap_srcloc: srclocinfo_t;
431+
srcmap_binloc: string option
432+
}
433+
434+
422435
class type bcdictionary_int =
423436
object
424437

0 commit comments

Comments
 (0)