Skip to content

Commit f1335cf

Browse files
committed
Generate Position-Independent Executables (PIE) if possible
The same ELF target platforms that support PIC also use PIE by default. PIE code generation is a small variation on PIC code generation. Controllable via `-fpie` / `-fno-pie` (for code generation) and `-pie` / `-no-pie` (for linking).
1 parent 36b5974 commit f1335cf

File tree

9 files changed

+42
-20
lines changed

9 files changed

+42
-20
lines changed

cfrontend/C2C.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,19 @@ let atom_is_external a =
6464
| _ -> true
6565
| exception Not_found -> true
6666

67-
(* In ELF PIC code, the rules are different: all non-static symbols
68-
must be accessed through the GOT, even if they are defined in the
69-
current compilation unit. (This is to allow symbol interposition by
70-
the dynamic loader.) *)
67+
(* In ELF PIC code, all non-static symbols must be accessed through
68+
the GOT, even if they are defined in the current compilation unit.
69+
(This is to allow symbol interposition by the dynamic loader.)
70+
In ELF PIE code, there is no interposition, so locally-defined
71+
symbols do not need GOT access.
72+
In non-PIC, non-PIE mode, the GOT is unused. *)
7173
let atom_needs_GOT_access a =
72-
!Clflags.option_fpic &&
73-
begin match Hashtbl.find decl_atom a with
74-
| { a_storage = C.Storage_static } -> false
75-
| _ -> true
76-
| exception Not_found -> true
77-
end
74+
if !Clflags.option_fpic then
75+
not (atom_is_static a)
76+
else if !Clflags.option_fpie then
77+
atom_is_external a
78+
else
79+
false
7880

7981
let atom_alignof a =
8082
try

configure

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ if test "$arch" = "x86" -a "$bitsize" = "64"; then
353353
casm_options="-m64 -c"
354354
clinker="${toolprefix}cc"
355355
clinker_options="-m64"
356+
clinker_needs_no_pie=false
356357
cprepro="${toolprefix}cc"
357358
cprepro_options="-m64 -U__GNUC__ -U__SIZEOF_INT128__ -E"
358359
system="bsd"
@@ -363,6 +364,7 @@ if test "$arch" = "x86" -a "$bitsize" = "64"; then
363364
cc_options="-m64"
364365
casm_options="-m64 -c"
365366
clinker_options="-m64"
367+
clinker_needs_no_pie=false
366368
cprepro_options="-m64 -U__GNUC__ -U__SIZEOF_INT128__ -E"
367369
system="linux"
368370
pic_supported=true
@@ -407,6 +409,7 @@ if test "$arch" = "riscV"; then
407409
cc_options="$model_options"
408410
casm_options="$model_options -c"
409411
clinker_options="$model_options"
412+
clinker_needs_no_pie=false
410413
cprepro_options="$model_options -U__GNUC__ -E"
411414
system="linux"
412415
pic_supported=true

driver/Clflags.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ let option_fpacked_structs = ref false
2323
let option_funstructured_switch = ref false
2424
let option_ffpu = ref true
2525
let option_fpic = ref false
26+
let option_fpie = ref Configuration.pic_supported
2627
let option_ffloatconstprop = ref 2
2728
let option_ftailcalls = ref true
2829
let option_fconstprop = ref true

driver/Configuration.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ val elf_target: bool
6161
(** Is the target binary format ELF? *)
6262

6363
val pic_supported: bool
64-
(** Are we able to generate PIC code? *)
64+
(** Are we able to generate PIC and PIE code? *)

driver/Driver.ml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Processing options:
201201
-fif-conversion Perform if-conversion (generation of conditional moves) [on]
202202
Code generation options: (use -fno-<opt> to turn off -f<opt>)
203203
-fpic / -fPIC Generate position-independent code [off]
204+
-fpie / -fPIE Generate position-independent executables [on if supported]
204205
-ffpu Use FP registers for some integer operations [on]
205206
-fsmall-data <n> Set maximal size <n> for allocation in small data area
206207
-fsmall-const <n> Set maximal size <n> for allocation in small constant area
@@ -272,6 +273,11 @@ let cmdline_actions =
272273
then option_fpic := true
273274
else warning no_loc Unnamed
274275
"option -fpic not supported on this platform, ignored" in
276+
let set_pie_mode () =
277+
if Configuration.pic_supported
278+
then option_fpie := true
279+
else warning no_loc Unnamed
280+
"option -fpie not supported on this platform, ignored" in
275281
[
276282
(* Getting help *)
277283
Exact "-help", Unit print_usage_and_exit;
@@ -311,7 +317,11 @@ let cmdline_actions =
311317
Exact "-fpic", Unit set_pic_mode;
312318
Exact "-fPIC", Unit set_pic_mode;
313319
Exact "-fno-pic", Unset option_fpic;
314-
Exact "-fno-PIC", Unset option_fpic ] @
320+
Exact "-fno-PIC", Unset option_fpic;
321+
Exact "-fpie", Unit set_pie_mode;
322+
Exact "-fPIE", Unit set_pie_mode;
323+
Exact "-fno-pie", Unset option_fpie;
324+
Exact "-fno-PIE", Unset option_fpie ] @
315325
f_opt "common" option_fcommon @
316326
(* Target processor options *)
317327
(if Configuration.arch = "arm" then

driver/Linker.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ let linker exe_name files =
3535

3636

3737
let gnu_linker_help =
38-
{| -shared Produce a shared library instead of an executable
39-
-nodefaultlibs Do not use the standard system libraries when
38+
{| -nodefaultlibs Do not use the standard system libraries when
4039
linking
4140
-nostdlib Do not use the standard system startup files or
4241
libraries when linking
42+
-no-pie Do not produce a position-independent executable
43+
-pie Produce a position-independent executable
44+
-shared Produce a shared library instead of an executable
4345
|}
4446

4547
let linker_help =
@@ -73,6 +75,8 @@ let linker_actions =
7375
(if Configuration.gnu_toolchain then
7476
[ Exact "-nodefaultlibs", Self push_linker_arg;
7577
Exact "-nostdlib", Self push_linker_arg;
78+
Exact "-pie", Self push_linker_arg;
79+
Exact "-no-pie", Self push_linker_arg;
7680
Exact "-shared", Self push_linker_arg]
7781
else []) @
7882
[ Exact "-s", Self push_linker_arg;

riscV/TargetPrinter.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ module Target : TARGET =
147147
(* Emit the target of a call, with a `@plt` suffix in PIC mode. *)
148148

149149
let symbol_plt oc s =
150-
if !Clflags.option_fpic
150+
if SelectOp.symbol_is_relocatable s
151151
then fprintf oc "%a@plt" symbol s
152152
else symbol oc s
153153

@@ -604,7 +604,9 @@ module Target : TARGET =
604604

605605
let print_prologue oc =
606606
fprintf oc " .option %s\n"
607-
(if !Clflags.option_fpic then "pic" else "nopic");
607+
(if !Clflags.option_fpic || !Clflags.option_fpie
608+
then "pic"
609+
else "nopic");
608610
if !Clflags.option_g then begin
609611
section oc Section_text;
610612
end

tools/runner.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ case "$target,$os" in
182182
case "$1" in
183183
1) Run_test "$simu_aarch64" "";;
184184
2) Run_test "$simu_aarch64" "-fpic";;
185-
3) Run_test "$simu_aarch64" "-Os";;
185+
3) Run_test "$simu_aarch64" "-Os -fno-pie -no-pie";;
186186
esac;;
187187
aarch64,macos)
188188
case "$1" in
@@ -206,7 +206,7 @@ case "$target,$os" in
206206
case "$1" in
207207
1) Run_test "$simu_rv64" "";;
208208
2) Run_test "$simu_rv64" "-fpic";;
209-
3) Run_test "$simu_rv64" "-Os";;
209+
3) Run_test "$simu_rv64" "-Os -fno-pie -no-pie";;
210210
esac;;
211211
x86_32,*)
212212
case "$1" in
@@ -217,7 +217,7 @@ case "$target,$os" in
217217
case "$1" in
218218
1) Run_test "" "";;
219219
2) Run_test "" "-fpic";;
220-
3) Run_test "" "-Os";;
220+
3) Run_test "" "-Os -fno-pie -no-pie";;
221221
esac;;
222222
*)
223223
Fatal "Unknown configuration \"$target\" - \"$os\""

x86/TargetPrinter.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ module ELF_System : SYSTEM =
140140

141141
let symbol_function oc symb =
142142
symbol_paren oc symb;
143-
if !Clflags.option_fpic then fprintf oc "@PLT"
143+
if SelectOp.symbol_is_relocatable symb then fprintf oc "@PLT"
144144

145145
let label = elf_label
146146

0 commit comments

Comments
 (0)