Skip to content

Commit d368ca0

Browse files
authored
Merge pull request #551 from AbsInt/PIC
Support PIC and PIE for AArch64, RISC-V and x86-64
2 parents 771f79d + f1335cf commit d368ca0

28 files changed

+148
-41
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ jobs:
6666
run: tools/runner.sh test1
6767
- name: Test alternate configuration
6868
run: tools/runner.sh test2
69+
- name: Test alternate configuration 2
70+
run: tools/runner.sh test3

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ compcert.ini: Makefile.config
330330
echo "has_runtime_lib=$(HAS_RUNTIME_LIB)"; \
331331
echo "has_standard_headers=$(HAS_STANDARD_HEADERS)"; \
332332
echo "asm_supports_cfi=$(ASM_SUPPORTS_CFI)"; \
333-
echo "response_file_style=$(RESPONSEFILE)";) \
333+
echo "response_file_style=$(RESPONSEFILE)"; \
334+
echo "pic_supported=$(PIC_SUPPORTED)") \
334335
> compcert.ini
335336

336337
compcert.config: Makefile.config

aarch64/extractionMachdep.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Extract Constant Archi.abi =>
3030
Extract Constant SelectOp.symbol_is_relocatable =>
3131
"match Configuration.system with
3232
| ""macos"" -> C2C.atom_is_external
33-
| _ -> (fun _ -> false)".
33+
| _ -> C2C.atom_needs_GOT_access".
3434

3535
(* Asm *)
3636

cfrontend/C2C.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ let atom_is_static a =
5252
with Not_found ->
5353
false
5454

55-
(* Is it possible for symbol [a] to be defined in a DLL? *)
55+
(* Is it possible for symbol [a] to be defined in a DLL?
56+
Yes, unless [a] is defined in the current compilation unit, or is static.
57+
(This criterion is appropriate for macOS and for Cygwin; for ELF,
58+
see [atom_needs_GOT_access] below.) *)
5659
let atom_is_external a =
5760
match Hashtbl.find decl_atom a with
5861
| { a_defined = true } -> false
@@ -61,6 +64,20 @@ let atom_is_external a =
6164
| _ -> true
6265
| exception Not_found -> true
6366

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. *)
73+
let atom_needs_GOT_access a =
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
80+
6481
let atom_alignof a =
6582
try
6683
(Hashtbl.find decl_atom a).a_alignment

configure

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ cprepro_options="-E"
234234
archiver="${toolprefix}ar rcs"
235235
libmath="-lm"
236236
responsefile="gnu"
237+
pic_supported=false
237238

238239
#
239240
# ARM Target Configuration
@@ -352,17 +353,21 @@ if test "$arch" = "x86" -a "$bitsize" = "64"; then
352353
casm_options="-m64 -c"
353354
clinker="${toolprefix}cc"
354355
clinker_options="-m64"
356+
clinker_needs_no_pie=false
355357
cprepro="${toolprefix}cc"
356358
cprepro_options="-m64 -U__GNUC__ -U__SIZEOF_INT128__ -E"
357359
system="bsd"
360+
pic_supported=true
358361
;;
359362
linux)
360363
abi="standard"
361364
cc_options="-m64"
362365
casm_options="-m64 -c"
363366
clinker_options="-m64"
367+
clinker_needs_no_pie=false
364368
cprepro_options="-m64 -U__GNUC__ -U__SIZEOF_INT128__ -E"
365369
system="linux"
370+
pic_supported=true
366371
;;
367372
macos|macosx)
368373
abi="macos"
@@ -373,6 +378,7 @@ if test "$arch" = "x86" -a "$bitsize" = "64"; then
373378
cprepro_options="-arch x86_64 -U__GNUC__ -U__SIZEOF_INT128__ -U__clang__ -U__BLOCKS__ '-D__attribute__(x)=' '-D__asm(x)=' '-D_Nullable=' '-D_Nonnull=' '-D__DARWIN_OS_INLINE=static inline' -Wno-\\#warnings -E"
374379
libmath=""
375380
system="macos"
381+
pic_supported=true
376382
;;
377383
cygwin)
378384
abi="standard"
@@ -403,8 +409,10 @@ if test "$arch" = "riscV"; then
403409
cc_options="$model_options"
404410
casm_options="$model_options -c"
405411
clinker_options="$model_options"
412+
clinker_needs_no_pie=false
406413
cprepro_options="$model_options -U__GNUC__ -E"
407414
system="linux"
415+
pic_supported=true
408416
fi
409417

410418
#
@@ -415,7 +423,9 @@ if test "$arch" = "aarch64"; then
415423
linux)
416424
abi="standard"
417425
cprepro_options="-U__GNUC__ -E"
418-
system="linux";;
426+
system="linux"
427+
pic_supported=true
428+
;;
419429
macos|macosx)
420430
abi="apple"
421431
casm="${toolprefix}cc"
@@ -427,6 +437,7 @@ if test "$arch" = "aarch64"; then
427437
cprepro_options="-arch arm64 -U__GNUC__ -U__clang__ -U__BLOCKS__ '-D__attribute__(x)=' '-D__asm(x)=' '-D_Nullable=' '-D_Nonnull=' '-D__DARWIN_OS_INLINE=static inline' -Wno-\\#warnings -E"
428438
libmath=""
429439
system="macos"
440+
pic_supported=true
430441
;;
431442
*)
432443
echo "Error: invalid eabi/system '$target' for architecture AArch64." 1>&2
@@ -697,6 +708,7 @@ HAS_STANDARD_HEADERS=$has_standard_headers
697708
INSTALL_COQDEV=$install_coqdev
698709
LIBMATH=$libmath
699710
MODEL=$model
711+
PIC_SUPPORTED=$pic_supported
700712
SYSTEM=$system
701713
RESPONSEFILE=$responsefile
702714
LIBRARY_FLOCQ=$library_Flocq
@@ -760,6 +772,10 @@ ENDIANNESS=
760772
# SYSTEM=cygwin
761773
SYSTEM=
762774
775+
# Are we able to produce position-independent code (with the `-fpic` option)?
776+
#PIC_SUPPORTED=true
777+
PIC_SUPPORTED=false
778+
763779
# C compiler (for testing only)
764780
CC=cc
765781
@@ -858,6 +874,7 @@ CompCert configuration:
858874
Hardware model................ $model
859875
Application binary interface.. $abi
860876
Endianness.................... $endianness
877+
PIC generation supported...... $pic_supported
861878
OS and development env........ $system
862879
C compiler.................... $cc $cc_options
863880
C preprocessor................ $cprepro $cprepro_options

driver/Clflags.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ let option_funprototyped = ref true
2222
let option_fpacked_structs = ref false
2323
let option_funstructured_switch = ref false
2424
let option_ffpu = ref true
25+
let option_fpic = ref false
26+
let option_fpie = ref Configuration.pic_supported
2527
let option_ffloatconstprop = ref 2
2628
let option_ftailcalls = ref true
2729
let option_fconstprop = ref true

driver/Configuration.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ let stdlib_path =
142142
else
143143
""
144144
let asm_supports_cfi = get_bool_config "asm_supports_cfi"
145+
let pic_supported = get_bool_config "pic_supported"
145146

146147
type response_file_style =
147148
| Gnu (* responsefiles in gnu compatible syntax *)

driver/Configuration.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ val gnu_toolchain: bool
5959

6060
val elf_target: bool
6161
(** Is the target binary format ELF? *)
62+
63+
val pic_supported: bool
64+
(** Are we able to generate PIC and PIE code? *)

driver/Driver.ml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ Processing options:
200200
single caller [on]
201201
-fif-conversion Perform if-conversion (generation of conditional moves) [on]
202202
Code generation options: (use -fno-<opt> to turn off -f<opt>)
203+
-fpic / -fPIC Generate position-independent code [off]
204+
-fpie / -fPIE Generate position-independent executables [on if supported]
203205
-ffpu Use FP registers for some integer operations [on]
204206
-fsmall-data <n> Set maximal size <n> for allocation in small data area
205207
-fsmall-const <n> Set maximal size <n> for allocation in small constant area
@@ -266,6 +268,16 @@ let cmdline_actions =
266268
if n <= 0 || ((n land (n - 1)) <> 0) then
267269
error no_loc "requested alignment %d is not a power of 2" n
268270
in
271+
let set_pic_mode () =
272+
if Configuration.pic_supported
273+
then option_fpic := true
274+
else warning no_loc Unnamed
275+
"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
269281
[
270282
(* Getting help *)
271283
Exact "-help", Unit print_usage_and_exit;
@@ -301,7 +313,15 @@ let cmdline_actions =
301313
Exact "-ffloat-const-prop", Integer(fun n -> option_ffloatconstprop := n);
302314
Exact "-falign-functions", Integer(fun n -> check_align n; option_falignfunctions := Some n);
303315
Exact "-falign-branch-targets", Integer(fun n -> check_align n; option_falignbranchtargets := n);
304-
Exact "-falign-cond-branches", Integer(fun n -> check_align n; option_faligncondbranchs := n);] @
316+
Exact "-falign-cond-branches", Integer(fun n -> check_align n; option_faligncondbranchs := n);
317+
Exact "-fpic", Unit set_pic_mode;
318+
Exact "-fPIC", Unit set_pic_mode;
319+
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 ] @
305325
f_opt "common" option_fcommon @
306326
(* Target processor options *)
307327
(if Configuration.arch = "arm" then

driver/Linker.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ let gnu_linker_help =
3939
linking
4040
-nostdlib Do not use the standard system startup files or
4141
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
4245
|}
4346

4447
let linker_help =
@@ -71,7 +74,10 @@ let linker_actions =
7174
] @
7275
(if Configuration.gnu_toolchain then
7376
[ Exact "-nodefaultlibs", Self push_linker_arg;
74-
Exact "-nostdlib", Self push_linker_arg;]
77+
Exact "-nostdlib", Self push_linker_arg;
78+
Exact "-pie", Self push_linker_arg;
79+
Exact "-no-pie", Self push_linker_arg;
80+
Exact "-shared", Self push_linker_arg]
7581
else []) @
7682
[ Exact "-s", Self push_linker_arg;
7783
Exact "-static", Self push_linker_arg;

0 commit comments

Comments
 (0)