Skip to content

Commit a99406b

Browse files
bschommerxavierleroy
authored andcommitted
Fix for AArch64 alignment problem (#206)
In addressing modes for load and store instructions, the offset must be a multiple of the memory size being accessed. When accessing global variables, this may not be the case if the alignment of the variable is less than its size. Errors occur at link time. This PR extends the check for a representable offset for the addressing of global variables to also check whether the variable is correctly aligned. Only if both conditions are met can we generate the short sequence Padrp / ADadr. Otherwise we go through the generic loadsymbol sequence.
1 parent 5dcd78a commit a99406b

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

aarch64/Asmgen.v

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Local Open Scope string_scope.
2020
Local Open Scope list_scope.
2121
Local Open Scope error_monad_scope.
2222

23+
(** Alignment check for symbols *)
24+
25+
Parameter symbol_is_aligned : ident -> Z -> bool.
26+
(** [symbol_is_aligned id sz] checks whether the symbol [id] is [sz] aligned *)
27+
2328
(** Extracting integer or float registers. *)
2429

2530
Definition ireg_of (r: mreg) : res ireg :=
@@ -942,7 +947,7 @@ Definition transl_addressing (sz: Z) (addr: Op.addressing) (args: list mreg)
942947
(insn (ADimm X16 Int64.zero) :: k))
943948
| Aglobal id ofs, nil =>
944949
assertion (negb (Archi.pic_code tt));
945-
if Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero
950+
if Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero && symbol_is_aligned id sz
946951
then OK (Padrp X16 id ofs :: insn (ADadr X16 id ofs) :: k)
947952
else OK (loadsymbol X16 id ofs (insn (ADimm X16 Int64.zero) :: k))
948953
| Ainstack ofs, nil =>

aarch64/Asmgenproof1.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ Proof.
15921592
simpl; rewrite Int64.add_zero; auto.
15931593
intros. apply C; eauto with asmgen.
15941594
- (* Aglobal *)
1595-
destruct (Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero); inv TR.
1595+
destruct (Ptrofs.eq (Ptrofs.modu ofs (Ptrofs.repr sz)) Ptrofs.zero && symbol_is_aligned id sz); inv TR.
15961596
+ econstructor; econstructor; split.
15971597
apply exec_straight_opt_intro. apply exec_straight_one. simpl; eauto. auto.
15981598
split. simpl. Simpl. rewrite symbol_high_low. simpl in EV. congruence.

aarch64/extractionMachdep.v

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ Extract Constant Archi.pic_code => "fun () -> false". (* for the time being *)
2121
(* Asm *)
2222
Extract Constant Asm.symbol_low => "fun _ _ _ -> assert false".
2323
Extract Constant Asm.symbol_high => "fun _ _ _ -> assert false".
24+
Extract Constant Asmgen.symbol_is_aligned => "C2C.atom_is_aligned".

cfrontend/C2C.ml

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ let atom_alignof a =
6161
with Not_found ->
6262
None
6363

64+
let atom_is_aligned a sz =
65+
match atom_alignof a with
66+
| None -> false
67+
| Some align -> align mod (Z.to_int sz) = 0
68+
6469
let atom_sections a =
6570
try
6671
(Hashtbl.find decl_atom a).a_sections

0 commit comments

Comments
 (0)