Skip to content

Commit 059024d

Browse files
committed
CHB: preserve address expression of global variable
1 parent 367a321 commit 059024d

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

CodeHawk/CHB/bchlib/bCHFloc.ml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ object (self)
12731273
__FILE__ __LINE__
12741274
["addrvalue: " ^ (x2s addrvalue);
12751275
"btype: " ^ (btype_to_string btype);
1276-
"size: " ^ (if Option.is_some size then (string_of_int (Option.get size)) else "?")] in
1276+
"size: " ^ (opti2s size)] in
12771277

12781278
match self#normalize_addrvalue addrvalue with
12791279
| XOp ((Xf "addressofvar"), [XVar v]) when self#env#is_global_variable v ->
@@ -1341,7 +1341,8 @@ object (self)
13411341
(TR.tmap
13421342
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__))
13431343
(fun offset -> self#f#env#mk_gloc_variable gloc offset)
1344-
(gloc#address_memory_offset ~tgtsize:size ~tgtbtype:btype self#l addrvalue))
1344+
(gloc#address_memory_offset
1345+
~tgtsize:size ~tgtbtype:btype self#l addrvalue))
13451346
| _ ->
13461347
let (memref_r, memoff_r) = self#decompose_memaddr addrvalue in
13471348
TR.tmap2
@@ -2362,15 +2363,7 @@ object (self)
23622363
| XConst (IntConst n) when n#gt CHNumerical.numerical_zero ->
23632364
let dw = numerical_mod_to_doubleword n in
23642365
if memmap#has_location dw then
2365-
TR.tfold
2366-
~ok:(fun gv -> XOp ((Xf "addressofvar"), [XVar gv]))
2367-
~error:(fun e ->
2368-
begin
2369-
log_result
2370-
~tag:"assign global variable address" __FILE__ __LINE__ e;
2371-
rhs
2372-
end)
2373-
(self#f#env#mk_global_variable self#l n)
2366+
TR.tvalue (self#f#env#mk_global_variable_address dw) ~default:rhs
23742367
else
23752368
rhs
23762369
| _ -> rhs in

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,22 @@ object (self)
840840
end
841841
end
842842

843+
method mk_global_variable_address (dw: doubleword_int): xpr_t traceresult =
844+
match memmap#containing_location dw with
845+
| Some gloc when dw#equal gloc#address ->
846+
let gvar =
847+
self#mk_variable (self#varmgr#make_global_variable dw#to_numerical) in
848+
let ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
849+
begin
850+
self#set_variable_name gvar gloc#name;
851+
self#set_variable_name ivar (gloc#name ^ "_in");
852+
Ok (XOp ((Xf "addressofvar"), [XVar gvar]))
853+
end
854+
| _ ->
855+
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
856+
^ dw#to_hex_string
857+
^ " is not the address of a known global variable"]
858+
843859
method mk_global_variable
844860
?(size=4)
845861
?(btype=t_unknown)
@@ -848,10 +864,6 @@ object (self)
848864
let dw = numerical_mod_to_doubleword base in
849865
match memmap#containing_location dw with
850866
| Some gloc ->
851-
let gvar =
852-
self#mk_variable
853-
(self#varmgr#make_global_variable gloc#address#to_numerical) in
854-
let _ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
855867
tmap
856868
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": memref:global")
857869
(fun offset ->

CodeHawk/CHB/bchlib/bCHGlobalMemoryMap.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ object (self)
484484
~msg:(p2s loc#toPretty)
485485
~tag:"mmap:address-offset-memory-offset"
486486
__FILE__ __LINE__
487-
["xoffset: " ^ (x2s xoffset)
487+
["gloc: " ^ self#address#to_hex_string
488+
^ "; xoffset: " ^ (x2s xoffset)
488489
^ "; tgtsize: " ^ (opti2s tgtsize)
489490
^ "; tgtbtype: " ^ (ty2s tgtbtype)] in
490491
match xoffset with
@@ -517,7 +518,9 @@ object (self)
517518
else
518519
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ":"
519520
^ (btype_to_string self#btype)
520-
^ " is not known to be a struct or array"]
521+
^ " is not known to be a struct or array";
522+
"gloc_address: " ^ self#address#to_hex_string;
523+
"xoffset: " ^ (x2s xoffset)]
521524

522525
method address_memory_offset
523526
?(tgtsize=None)

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,6 +4778,8 @@ class type function_environment_int =
47784778

47794779
(** {2 Memory variables} *)
47804780

4781+
method mk_global_variable_address: doubleword_int -> xpr_t traceresult
4782+
47814783
(** [mk_global_variable offset] attempts to find a containing global
47824784
location for [offset]. If successful a memory offset will be computed
47834785
for that offset relative to the address of the containing location

0 commit comments

Comments
 (0)