Skip to content

Commit ab8cdf0

Browse files
committed
CHB: moving global variable handling to GlobalMemoryMap
1 parent c35f32d commit ab8cdf0

15 files changed

+451
-38
lines changed

CodeHawk/CHB/bchcil/bCHParseCilFile.ml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,22 @@ open Frontc
3232
(* chlib *)
3333
open CHPretty
3434

35-
(* chutil *)
36-
open CHLogger
37-
3835
(* bchlib *)
3936
open BCHBCFiles
4037
open BCHBCTypes
41-
open BCHBCTypePretty
4238
open BCHBCTypeUtil
4339
open BCHCilToCBasic
44-
open BCHConstantDefinitions
4540

4641

4742
let update_symbolic_address_types () =
48-
let globalvarnames = get_untyped_symbolic_address_names () in
49-
begin
50-
List.iter (fun name ->
51-
if bcfiles#has_varinfo name then
52-
let vinfo = bcfiles#get_varinfo name in
53-
begin
54-
update_symbolic_address_btype name vinfo.bvtype;
55-
chlog#add
56-
"symbolic address: update with vinfo"
57-
(LBLOCK [STR name; STR ": "; STR (btype_to_string vinfo.bvtype)])
58-
end
59-
else
60-
chlog#add "symbolic address: no update" (STR name)) globalvarnames;
61-
chlog#add
62-
"symbolic address updates"
63-
(LBLOCK [STR "Names: "; STR (String.concat ", " globalvarnames)])
64-
end
43+
let gfunnames = bcfiles#get_gfun_names in
44+
let varinfos = bcfiles#get_varinfos in
45+
List.iter
46+
(fun vinfo ->
47+
if List.mem vinfo.bvname gfunnames then
48+
()
49+
else
50+
BCHGlobalMemoryMap.update_global_location_type vinfo) varinfos
6551

6652

6753
let parse_cil_file ?(computeCFG=true) ?(removeUnused=true) (filename: string) =

CodeHawk/CHB/bchcmdline/bCHXBinaryAnalyzer.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,9 @@ let main () =
537537
let t = ref (Unix.gettimeofday ()) in
538538
let _ = load_elf_files () in
539539
let _ = pr_timing [STR "elf files loaded"] in
540-
let _ = List.iter parse_cil_file system_info#ifiles in
540+
let _ =
541+
List.iter (fun f ->
542+
parse_cil_file ~removeUnused:false f) system_info#ifiles in
541543
let _ =
542544
if (List.length system_info#ifiles) > 0 then
543545
pr_timing [STR "c header files loaded"] in

CodeHawk/CHB/bchlib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ MLIS := \
6060
bCHFunctionData \
6161
bCHSystemData \
6262
bCHConstantDefinitions \
63+
bCHGlobalMemoryMap \
6364
bCHStructTables \
6465
bCHCallbackTables \
6566
bCHImmediate \
@@ -153,6 +154,7 @@ SOURCES := \
153154
bCHCodegraph \
154155
bCHSystemData \
155156
bCHConstantDefinitions \
157+
bCHGlobalMemoryMap \
156158
bCHStructTables \
157159
bCHCallbackTables \
158160
bCHImmediate \

CodeHawk/CHB/bchlib/bCHBCFiles.ml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ object (self)
7070

7171
method add_bcfile (f: bcfile_t) =
7272
let i = bcd#index_location in
73+
begin
7374
List.iter (fun g ->
7475
match g with
7576
| GType (tinfo, loc) ->
@@ -92,8 +93,10 @@ object (self)
9293
| GEnumTagDecl (einfo, loc) ->
9394
H.replace genumtagdecls einfo.bename (bcd#index_enuminfo einfo, i loc)
9495
| GVarDecl (vinfo, loc) ->
96+
let _ = chlog#add "bcfiles:add gvardecl" (STR vinfo.bvname) in
9597
H.replace gvardecls vinfo.bvname (bcd#index_varinfo vinfo, i loc)
9698
| GVar (vinfo, iinfo, loc) ->
99+
let _ = chlog#add "bcfiles:add gvar" (STR vinfo.bvname) in
97100
H.replace gvars
98101
vinfo.bvname
99102
(bcd#index_varinfo vinfo,
@@ -102,8 +105,20 @@ object (self)
102105
| _ -> (-1)),
103106
i loc)
104107
| GFun (fundec, loc) ->
108+
let _ = chlog#add "bcfiles:add gfun" (STR fundec.bsvar.bvname) in
105109
H.replace gfuns fundec.bsvar.bvname (fundec, bcd#index_location loc);
106-
| _ -> ()) f.bglobals
110+
| _ -> ()) f.bglobals;
111+
chlog#add
112+
"bcfiles:add_bcfile"
113+
(LBLOCK [
114+
STR "gvars: ";
115+
INT (H.length gvars);
116+
STR "; gvardecls: ";
117+
INT (H.length gvardecls);
118+
STR "; gfuns: ";
119+
INT (H.length gfuns)
120+
])
121+
end
107122

108123
method update_global (g: bglobal_t) =
109124
let i = bcd#index_location in
@@ -370,6 +385,16 @@ object (self)
370385
(BCH_failure
371386
(LBLOCK [STR "No varinfo found with name "; STR name]))
372387

388+
method get_varinfos =
389+
let result = ref [] in
390+
begin
391+
H.iter (fun _ (ix, _, _) ->
392+
result := (bcd#get_varinfo ix) :: !result) gvars;
393+
H.iter (fun _ (ix, _) ->
394+
result := (bcd#get_varinfo ix) :: !result) gvardecls;
395+
!result
396+
end
397+
373398
method list_varinfos =
374399
let result = ref [] in
375400
let v2s v = v.bvname ^ ": " ^ (btype_to_string v.bvtype) in

CodeHawk/CHB/bchlib/bCHBCTypes.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,9 @@ class type bcfiles_int =
594594
@raise BCH_failure if no varinfo exists with name [name].*)
595595
method get_varinfo: ?prefix:bool -> string -> bvarinfo_t
596596

597+
(** Returns all global varinfos (including functions) *)
598+
method get_varinfos: bvarinfo_t list
599+
597600
(** [has_varinfo name] returns true if there exists either a defined or
598601
declared variable with name [name]. Note that this includes function
599602
names.*)

CodeHawk/CHB/bchlib/bCHFunctionData.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ object (self)
261261
method get_library_stubs: doubleword_int list =
262262
self#retrieve_addresses (fun f -> f#is_library_stub)
263263

264+
method is_in_function_stub ?(size=3) (va: doubleword_int): bool =
265+
let libstubs = self#get_library_stubs in
266+
List.exists (fun s -> s#le va && va#lt(s#add_int (size * 4))) libstubs
267+
264268
method is_function_entry_point (fa:doubleword_int) = H.mem table fa#index
265269

266270
method has_function_name (fa:doubleword_int) =

0 commit comments

Comments
 (0)