Skip to content

Commit 36b5974

Browse files
committed
RISC-V support for -fpic
1 parent e99d3df commit 36b5974

File tree

12 files changed

+38
-18
lines changed

12 files changed

+38
-18
lines changed

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ if test "$arch" = "riscV"; then
409409
clinker_options="$model_options"
410410
cprepro_options="$model_options -U__GNUC__ -E"
411411
system="linux"
412+
pic_supported=true
412413
fi
413414

414415
#

riscV/Archi.v

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,3 @@ Global Opaque ptr64 big_endian splitlong
7171
fma_order fma_invalid_mul_is_nan
7272
float_of_single_preserves_sNaN
7373
float_conversion_default_nan.
74-
75-
(** Whether to generate position-independent code or not *)
76-
77-
Parameter pic_code: unit -> bool.

riscV/Asmgen.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Require Archi.
2121
Require Import Coqlib Errors.
2222
Require Import AST Integers Floats Memdata.
2323
Require Import Op Locations Mach Asm.
24+
Require SelectOp.
2425

2526
Local Open Scope string_scope.
2627
Local Open Scope error_monad_scope.
@@ -419,7 +420,7 @@ Definition transl_op
419420
else Ploadsi rd f :: k)
420421
| Oaddrsymbol s ofs, nil =>
421422
do rd <- ireg_of res;
422-
OK (if Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)
423+
OK (if SelectOp.symbol_is_relocatable s && negb (Ptrofs.eq ofs Ptrofs.zero)
423424
then Ploadsymbol rd s Ptrofs.zero :: addptrofs rd rd ofs k
424425
else Ploadsymbol rd s ofs :: k)
425426
| Oaddrstack n, nil =>

riscV/Asmgenproof.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Opaque Int.eq.
278278
- destruct (preg_of r); try discriminate; destruct (preg_of m); inv H; TailNoLabel.
279279
- destruct (Float.eq_dec n Float.zero); TailNoLabel.
280280
- destruct (Float32.eq_dec n Float32.zero); TailNoLabel.
281-
- destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
281+
- destruct (SelectOp.symbol_is_relocatable id && negb (Ptrofs.eq ofs Ptrofs.zero)).
282282
+ eapply tail_nolabel_trans; [|apply addptrofs_label]. TailNoLabel.
283283
+ TailNoLabel.
284284
- apply opimm32_label; intros; exact I.

riscV/Asmgenproof1.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ Opaque Int.eq.
989989
apply exec_straight_one. simpl; eauto. auto.
990990
split; intros; Simpl.
991991
- (* addrsymbol *)
992-
destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
992+
destruct (SelectOp.symbol_is_relocatable id && negb (Ptrofs.eq ofs Ptrofs.zero)).
993993
+ set (rs1 := nextinstr (rs#x <- (Genv.symbol_address ge id Ptrofs.zero))).
994994
exploit (addptrofs_correct x x ofs k rs1 m); eauto with asmgen.
995995
intros (rs2 & A & B & C).

riscV/ConstpropOp.vp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Require Import Coqlib Compopts.
1818
Require Import AST Integers Floats.
1919
Require Import Op Registers.
2020
Require Import ValueDomain.
21+
Require SelectOp.
2122

2223
(** * Converting known values to constants *)
2324

@@ -298,7 +299,7 @@ Nondetfunction addr_strength_reduction
298299
(addr: addressing) (args: list reg) (vl: list aval) :=
299300
match addr, args, vl with
300301
| Aindexed n, r1 :: nil, Ptr(Gl symb n1) :: nil =>
301-
if Archi.pic_code tt
302+
if SelectOp.symbol_is_relocatable symb
302303
then (addr, args)
303304
else (Aglobal symb (Ptrofs.add n1 n), nil)
304305
| Aindexed n, r1 :: nil, Ptr(Stk n1) :: nil =>

riscV/ConstpropOpproof.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ Proof.
734734
intros until res. unfold addr_strength_reduction.
735735
destruct (addr_strength_reduction_match addr args vl); simpl;
736736
intros VL EA; InvApproxRegs; SimplVM; try (inv EA).
737-
- destruct (Archi.pic_code tt).
737+
- destruct (SelectOp.symbol_is_relocatable symb).
738738
+ exists (Val.offset_ptr e#r1 n); auto.
739739
+ simpl. rewrite Genv.shift_symbol_address. econstructor; split; eauto.
740740
inv H0; simpl; auto.

riscV/SelectOp.vp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,10 +450,18 @@ Definition select (ty: typ) (cond: condition) (args: exprlist) (e1 e2: expr) :=
450450

451451
(** ** Recognition of addressing modes for load and store operations *)
452452

453+
(** Some symbols are relocatable (e.g. global symbols in PIC mode)
454+
and cannot be used with [Aglobal] addressing mode. *)
455+
456+
Parameter symbol_is_relocatable: ident -> bool.
457+
453458
Nondetfunction addressing (chunk: memory_chunk) (e: expr) :=
454459
match e with
455460
| Eop (Oaddrstack n) Enil => (Ainstack n, Enil)
456-
| Eop (Oaddrsymbol id ofs) Enil => if Archi.pic_code tt then (Aindexed Ptrofs.zero, e:::Enil) else (Aglobal id ofs, Enil)
461+
| Eop (Oaddrsymbol id ofs) Enil =>
462+
if symbol_is_relocatable id
463+
then (Aindexed Ptrofs.zero, e:::Enil)
464+
else (Aglobal id ofs, Enil)
457465
| Eop (Oaddimm n) (e1:::Enil) => (Aindexed (Ptrofs.of_int n), e1:::Enil)
458466
| Eop (Oaddlimm n) (e1:::Enil) => (Aindexed (Ptrofs.of_int64 n), e1:::Enil)
459467
| _ => (Aindexed Ptrofs.zero, e:::Enil)

riscV/SelectOpproof.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ Theorem eval_addressing:
893893
Proof.
894894
intros until v. unfold addressing; case (addressing_match a); intros; InvEval.
895895
- exists (@nil val); split. eauto with evalexpr. simpl. auto.
896-
- destruct (Archi.pic_code tt).
896+
- destruct (symbol_is_relocatable id).
897897
+ exists (Vptr b ofs0 :: nil); split.
898898
constructor. EvalOp. simpl. congruence. constructor. simpl. rewrite Ptrofs.add_zero. congruence.
899899
+ exists (@nil val); split. constructor. simpl; auto.

riscV/TargetPrinter.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ module Target : TARGET =
111111
(* Generate code to load the address of id + ofs in register r *)
112112

113113
let loadsymbol oc r id ofs =
114-
if Archi.pic_code () then begin
114+
if SelectOp.symbol_is_relocatable id then begin
115115
assert (ofs = Integers.Ptrofs.zero);
116116
fprintf oc " la %a, %s\n" ireg r (extern_atom id)
117117
end else begin
@@ -144,6 +144,13 @@ module Target : TARGET =
144144
assert (!latest_auipc = Some(id, ofs));
145145
fprintf oc "%%pcrel_lo(1b)"
146146

147+
(* Emit the target of a call, with a `@plt` suffix in PIC mode. *)
148+
149+
let symbol_plt oc s =
150+
if !Clflags.option_fpic
151+
then fprintf oc "%a@plt" symbol s
152+
else symbol oc s
153+
147154
(* Printing of instructions *)
148155
let print_instruction oc = function
149156
| Pmv(rd, rs) ->
@@ -277,11 +284,11 @@ module Target : TARGET =
277284
| Pj_l(l) ->
278285
fprintf oc " j %a\n" print_label l
279286
| Pj_s(s, sg) ->
280-
fprintf oc " jump %a, x31\n" symbol s
287+
fprintf oc " tail %a\n" symbol_plt s
281288
| Pj_r(r, sg) ->
282289
fprintf oc " jr %a\n" ireg r
283290
| Pjal_s(s, sg) ->
284-
fprintf oc " call %a\n" symbol s
291+
fprintf oc " call %a\n" symbol_plt s
285292
| Pjal_r(r, sg) ->
286293
fprintf oc " jalr %a\n" ireg r
287294

@@ -596,7 +603,8 @@ module Target : TARGET =
596603
let address = if Archi.ptr64 then ".quad" else ".long"
597604

598605
let print_prologue oc =
599-
fprintf oc " .option %s\n" (if Archi.pic_code() then "pic" else "nopic");
606+
fprintf oc " .option %s\n"
607+
(if !Clflags.option_fpic then "pic" else "nopic");
600608
if !Clflags.option_g then begin
601609
section oc Section_text;
602610
end

0 commit comments

Comments
 (0)