Skip to content

Commit 9d7a374

Browse files
committed
Add --enable-static-pie configure option to build static PIE [BZ #19574]
Static PIE extends address space layout randomization to static executables. It provides additional security hardening benefits at the cost of some memory and performance. Dynamic linker, ld.so, is a standalone program which can be loaded at any address. This patch adds a configure option, --enable-static-pie, to embed the part of ld.so in static executable to create static position independent executable (static PIE). A static PIE is similar to static executable, but can be loaded at any address without help from a dynamic linker. When --enable-static-pie is used to configure glibc, libc.a is built as PIE and all static executables, including tests, are built as static PIE. The resulting libc.a can be used together with GCC 8 or above to build static PIE with the compiler option, -static-pie. But GCC 8 isn't required to build glibc with --enable-static-pie. Only GCC with PIE support is needed. When an older GCC is used to build glibc with --enable-static-pie, proper input files are passed to linker to create static executables as static PIE, together with "-z text" to prevent dynamic relocations in read-only segments, which are not allowed in static PIE. The following changes are made for static PIE: 1. Add a new function, _dl_relocate_static_pie, to: a. Get the run-time load address. b. Read the dynamic section. c. Perform dynamic relocations. Dynamic linker also performs these steps. But static PIE doesn't load any shared objects. 2. Call _dl_relocate_static_pie at entrance of LIBC_START_MAIN in libc.a. crt1.o, which is used to create dynamic and non-PIE static executables, is updated to include a dummy _dl_relocate_static_pie. rcrt1.o is added to create static PIE, which will link in the real _dl_relocate_static_pie. grcrt1.o is also added to create static PIE with -pg. GCC 8 has been updated to support rcrt1.o and grcrt1.o for static PIE. Static PIE can work on all architectures which support PIE, provided: 1. Target must support accessing of local functions without dynamic relocations, which is needed in start.S to call __libc_start_main with function addresses of __libc_csu_init, __libc_csu_fini and main. All functions in static PIE are local functions. If PIE start.S can't reach main () defined in a shared object, the code sequence: pass address of local_main to __libc_start_main ... local_main: tail call to main via PLT can be used. 2. start.S is updated to check PIC instead SHARED for PIC code path and avoid dynamic relocation, when PIC is defined and SHARED isn't defined, to support static PIE. 3. All assembly codes are updated check PIC instead SHARED for PIC code path to avoid dynamic relocations in read-only sections. 4. All assembly codes are updated check SHARED instead PIC for static symbol name. 5. elf_machine_load_address in dl-machine.h are updated to support static PIE. 6. __brk works without TLS nor dynamic relocations in read-only section so that it can be used by __libc_setup_tls to initializes TLS in static PIE. NB: When glibc is built with GCC defaulted to PIE, libc.a is compiled with -fPIE, regardless if --enable-static-pie is used to configure glibc. When glibc is configured with --enable-static-pie, libc.a is compiled with -fPIE, regardless whether GCC defaults to PIE or not. The same libc.a can be used to build both static executable and static PIE. There is no need for separate PIE copy of libc.a. On x86-64, the normal static sln: text data bss dec hex filename 625425 8284 5456 639165 9c0bd elf/sln the static PIE sln: text data bss dec hex filename 657626 20636 5392 683654 a6e86 elf/sln The code size is increased by 5% and the binary size is increased by 7%. Linker requirements to build glibc with --enable-static-pie: 1. Linker supports --no-dynamic-linker to remove PT_INTERP segment from static PIE. 2. Linker can create working static PIE. The x86-64 linker needs the fix for https://sourceware.org/bugzilla/show_bug.cgi?id=21782 The i386 linker needs to be able to convert "movl main@GOT(%ebx), %eax" to "leal main@GOTOFF(%ebx), %eax" if main is defined locally. Binutils 2.29 or above are OK for i686 and x86-64. But linker status for other targets need to be verified. 3. Linker should resolve undefined weak symbols to 0 in static PIE: https://sourceware.org/bugzilla/show_bug.cgi?id=22269 4. Many ELF backend linkers incorrectly check bfd_link_pic for TLS relocations, which should check bfd_link_executable instead: https://sourceware.org/bugzilla/show_bug.cgi?id=22263 Tested on aarch64, i686 and x86-64. Using GCC 7 and binutils master branch, build-many-glibcs.py with --enable-static-pie with all patches for static PIE applied have the following build successes: PASS: glibcs-aarch64_be-linux-gnu build PASS: glibcs-aarch64-linux-gnu build PASS: glibcs-armeb-linux-gnueabi-be8 build PASS: glibcs-armeb-linux-gnueabi build PASS: glibcs-armeb-linux-gnueabihf-be8 build PASS: glibcs-armeb-linux-gnueabihf build PASS: glibcs-arm-linux-gnueabi build PASS: glibcs-arm-linux-gnueabihf build PASS: glibcs-arm-linux-gnueabihf-v7a build PASS: glibcs-arm-linux-gnueabihf-v7a-disable-multi-arch build PASS: glibcs-m68k-linux-gnu build PASS: glibcs-microblazeel-linux-gnu build PASS: glibcs-microblaze-linux-gnu build PASS: glibcs-mips64el-linux-gnu-n32 build PASS: glibcs-mips64el-linux-gnu-n32-nan2008 build PASS: glibcs-mips64el-linux-gnu-n32-nan2008-soft build PASS: glibcs-mips64el-linux-gnu-n32-soft build PASS: glibcs-mips64el-linux-gnu-n64 build PASS: glibcs-mips64el-linux-gnu-n64-nan2008 build PASS: glibcs-mips64el-linux-gnu-n64-nan2008-soft build PASS: glibcs-mips64el-linux-gnu-n64-soft build PASS: glibcs-mips64-linux-gnu-n32 build PASS: glibcs-mips64-linux-gnu-n32-nan2008 build PASS: glibcs-mips64-linux-gnu-n32-nan2008-soft build PASS: glibcs-mips64-linux-gnu-n32-soft build PASS: glibcs-mips64-linux-gnu-n64 build PASS: glibcs-mips64-linux-gnu-n64-nan2008 build PASS: glibcs-mips64-linux-gnu-n64-nan2008-soft build PASS: glibcs-mips64-linux-gnu-n64-soft build PASS: glibcs-mipsel-linux-gnu build PASS: glibcs-mipsel-linux-gnu-nan2008 build PASS: glibcs-mipsel-linux-gnu-nan2008-soft build PASS: glibcs-mipsel-linux-gnu-soft build PASS: glibcs-mips-linux-gnu build PASS: glibcs-mips-linux-gnu-nan2008 build PASS: glibcs-mips-linux-gnu-nan2008-soft build PASS: glibcs-mips-linux-gnu-soft build PASS: glibcs-nios2-linux-gnu build PASS: glibcs-powerpc64le-linux-gnu build PASS: glibcs-powerpc64-linux-gnu build PASS: glibcs-tilegxbe-linux-gnu-32 build PASS: glibcs-tilegxbe-linux-gnu build PASS: glibcs-tilegx-linux-gnu-32 build PASS: glibcs-tilegx-linux-gnu build PASS: glibcs-tilepro-linux-gnu build and the following build failures: FAIL: glibcs-alpha-linux-gnu build elf/sln is failed to link due to: assertion fail bfd/elf64-alpha.c:4125 This is caused by linker bug and/or non-PIC code in PIE libc.a. FAIL: glibcs-hppa-linux-gnu build elf/sln is failed to link due to: collect2: fatal error: ld terminated with signal 11 [Segmentation fault] https://sourceware.org/bugzilla/show_bug.cgi?id=22537 FAIL: glibcs-ia64-linux-gnu build elf/sln is failed to link due to: collect2: fatal error: ld terminated with signal 11 [Segmentation fault] FAIL: glibcs-powerpc-linux-gnu build FAIL: glibcs-powerpc-linux-gnu-soft build FAIL: glibcs-powerpc-linux-gnuspe build FAIL: glibcs-powerpc-linux-gnuspe-e500v1 build elf/sln is failed to link due to: ld: read-only segment has dynamic relocations. This is caused by linker bug and/or non-PIC code in PIE libc.a. See: https://sourceware.org/bugzilla/show_bug.cgi?id=22264 FAIL: glibcs-powerpc-linux-gnu-power4 build elf/sln is failed to link due to: findlocale.c:96:(.text+0x22c): @Local call to ifunc memchr This is caused by linker bug and/or non-PIC code in PIE libc.a. FAIL: glibcs-s390-linux-gnu build elf/sln is failed to link due to: collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped assertion fail bfd/elflink.c:14299 This is caused by linker bug and/or non-PIC code in PIE libc.a. FAIL: glibcs-sh3eb-linux-gnu build FAIL: glibcs-sh3-linux-gnu build FAIL: glibcs-sh4eb-linux-gnu build FAIL: glibcs-sh4eb-linux-gnu-soft build FAIL: glibcs-sh4-linux-gnu build FAIL: glibcs-sh4-linux-gnu-soft build elf/sln is failed to link due to: ld: read-only segment has dynamic relocations. This is caused by linker bug and/or non-PIC code in PIE libc.a. See: https://sourceware.org/bugzilla/show_bug.cgi?id=22263 Also TLS code sequence in SH assembly syscalls in glibc doesn't match TLS code sequence expected by ld: https://sourceware.org/bugzilla/show_bug.cgi?id=22270 FAIL: glibcs-sparc64-linux-gnu build FAIL: glibcs-sparcv9-linux-gnu build FAIL: glibcs-tilegxbe-linux-gnu build FAIL: glibcs-tilegxbe-linux-gnu-32 build FAIL: glibcs-tilegx-linux-gnu build FAIL: glibcs-tilegx-linux-gnu-32 build FAIL: glibcs-tilepro-linux-gnu build elf/sln is failed to link due to: ld: read-only segment has dynamic relocations. This is caused by linker bug and/or non-PIC code in PIE libc.a. See: https://sourceware.org/bugzilla/show_bug.cgi?id=22263 [BZ #19574] * INSTALL: Regenerated. * Makeconfig (real-static-start-installed-name): New. (pic-default): Updated for --enable-static-pie. (pie-default): New for --enable-static-pie. (default-pie-ldflag): Likewise. (+link-static-before-libc): Replace $(DEFAULT-LDFLAGS-$(@f)) with $(if $($(@f)-no-pie),$(no-pie-ldflag),$(default-pie-ldflag)). Replace $(static-start-installed-name) with $(real-static-start-installed-name). (+prectorT): Updated for --enable-static-pie. (+postctorT): Likewise. (CFLAGS-.o): Add $(pie-default). (CFLAGS-.op): Likewise. * NEWS: Mention --enable-static-pie. * config.h.in (ENABLE_STATIC_PIE): New. * configure.ac (--enable-static-pie): New configure option. (have-no-dynamic-linker): New LIBC_CONFIG_VAR. (have-static-pie): Likewise. Enable static PIE if linker supports --no-dynamic-linker. (ENABLE_STATIC_PIE): New AC_DEFINE. (enable-static-pie): New LIBC_CONFIG_VAR. * configure: Regenerated. * csu/Makefile (omit-deps): Add r$(start-installed-name) and gr$(start-installed-name) for --enable-static-pie. (extra-objs): Likewise. (install-lib): Likewise. (extra-objs): Add static-reloc.o and static-reloc.os ($(objpfx)$(start-installed-name)): Also depend on $(objpfx)static-reloc.o. ($(objpfx)r$(start-installed-name)): New. ($(objpfx)g$(start-installed-name)): Also depend on $(objpfx)static-reloc.os. ($(objpfx)gr$(start-installed-name)): New. * csu/libc-start.c (LIBC_START_MAIN): Call _dl_relocate_static_pie in libc.a. * csu/libc-tls.c (__libc_setup_tls): Add main_map->l_addr to initimage. * csu/static-reloc.c: New file. * elf/Makefile (routines): Add dl-reloc-static-pie. (elide-routines.os): Likewise. (DEFAULT-LDFLAGS-tst-tls1-static-non-pie): Removed. (tst-tls1-static-non-pie-no-pie): New. * elf/dl-reloc-static-pie.c: New file. * elf/dl-support.c (_dl_get_dl_main_map): New function. * elf/dynamic-link.h (ELF_DURING_STARTUP): Also check STATIC_PIE_BOOTSTRAP. * elf/get-dynamic-info.h (elf_get_dynamic_info): Likewise. * gmon/Makefile (tests): Add tst-gmon-static-pie. (tests-static): Likewise. (DEFAULT-LDFLAGS-tst-gmon-static): Removed. (tst-gmon-static-no-pie): New. (CFLAGS-tst-gmon-static-pie.c): Likewise. (CRT-tst-gmon-static-pie): Likewise. (tst-gmon-static-pie-ENV): Likewise. (tests-special): Likewise. ($(objpfx)tst-gmon-static-pie.out): Likewise. (clean-tst-gmon-static-pie-data): Likewise. ($(objpfx)tst-gmon-static-pie-gprof.out): Likewise. * gmon/tst-gmon-static-pie.c: New file. * manual/install.texi: Document --enable-static-pie. * sysdeps/generic/ldsodefs.h (_dl_relocate_static_pie): New. (_dl_get_dl_main_map): Likewise. * sysdeps/i386/configure.ac: Check if linker supports static PIE. * sysdeps/x86_64/configure.ac: Likewise. * sysdeps/i386/configure: Regenerated. * sysdeps/x86_64/configure: Likewise. * sysdeps/mips/Makefile (ASFLAGS-.o): Add $(pie-default). (ASFLAGS-.op): Likewise.
1 parent 95511aa commit 9d7a374

25 files changed

+521
-17
lines changed

ChangeLog

+72
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,75 @@
1+
2017-12-15 H.J. Lu <hongjiu.lu@intel.com>
2+
3+
[BZ #19574]
4+
* INSTALL: Regenerated.
5+
* Makeconfig (real-static-start-installed-name): New.
6+
(pic-default): Updated for --enable-static-pie.
7+
(pie-default): New for --enable-static-pie.
8+
(default-pie-ldflag): Likewise.
9+
(+link-static-before-libc): Replace $(DEFAULT-LDFLAGS-$(@F))
10+
with $(if $($(@F)-no-pie),$(no-pie-ldflag),$(default-pie-ldflag)).
11+
Replace $(static-start-installed-name) with
12+
$(real-static-start-installed-name).
13+
(+prectorT): Updated for --enable-static-pie.
14+
(+postctorT): Likewise.
15+
(CFLAGS-.o): Add $(pie-default).
16+
(CFLAGS-.op): Likewise.
17+
* NEWS: Mention --enable-static-pie.
18+
* config.h.in (ENABLE_STATIC_PIE): New.
19+
* configure.ac (--enable-static-pie): New configure option.
20+
(have-no-dynamic-linker): New LIBC_CONFIG_VAR.
21+
(have-static-pie): Likewise.
22+
Enable static PIE if linker supports --no-dynamic-linker.
23+
(ENABLE_STATIC_PIE): New AC_DEFINE.
24+
(enable-static-pie): New LIBC_CONFIG_VAR.
25+
* configure: Regenerated.
26+
* csu/Makefile (omit-deps): Add r$(start-installed-name) and
27+
gr$(start-installed-name) for --enable-static-pie.
28+
(extra-objs): Likewise.
29+
(install-lib): Likewise.
30+
(extra-objs): Add static-reloc.o and static-reloc.os
31+
($(objpfx)$(start-installed-name)): Also depend on
32+
$(objpfx)static-reloc.o.
33+
($(objpfx)r$(start-installed-name)): New.
34+
($(objpfx)g$(start-installed-name)): Also depend on
35+
$(objpfx)static-reloc.os.
36+
($(objpfx)gr$(start-installed-name)): New.
37+
* csu/libc-start.c (LIBC_START_MAIN): Call _dl_relocate_static_pie
38+
in libc.a.
39+
* csu/libc-tls.c (__libc_setup_tls): Add main_map->l_addr to
40+
initimage.
41+
* csu/static-reloc.c: New file.
42+
* elf/Makefile (routines): Add dl-reloc-static-pie.
43+
(elide-routines.os): Likewise.
44+
(DEFAULT-LDFLAGS-tst-tls1-static-non-pie): Removed.
45+
(tst-tls1-static-non-pie-no-pie): New.
46+
* elf/dl-reloc-static-pie.c: New file.
47+
* elf/dl-support.c (_dl_get_dl_main_map): New function.
48+
* elf/dynamic-link.h (ELF_DURING_STARTUP): Also check
49+
STATIC_PIE_BOOTSTRAP.
50+
* elf/get-dynamic-info.h (elf_get_dynamic_info): Likewise.
51+
* gmon/Makefile (tests): Add tst-gmon-static-pie.
52+
(tests-static): Likewise.
53+
(DEFAULT-LDFLAGS-tst-gmon-static): Removed.
54+
(tst-gmon-static-no-pie): New.
55+
(CFLAGS-tst-gmon-static-pie.c): Likewise.
56+
(CRT-tst-gmon-static-pie): Likewise.
57+
(tst-gmon-static-pie-ENV): Likewise.
58+
(tests-special): Likewise.
59+
($(objpfx)tst-gmon-static-pie.out): Likewise.
60+
(clean-tst-gmon-static-pie-data): Likewise.
61+
($(objpfx)tst-gmon-static-pie-gprof.out): Likewise.
62+
* gmon/tst-gmon-static-pie.c: New file.
63+
* manual/install.texi: Document --enable-static-pie.
64+
* sysdeps/generic/ldsodefs.h (_dl_relocate_static_pie): New.
65+
(_dl_get_dl_main_map): Likewise.
66+
* sysdeps/i386/configure.ac: Check if linker supports static PIE.
67+
* sysdeps/x86_64/configure.ac: Likewise.
68+
* sysdeps/i386/configure: Regenerated.
69+
* sysdeps/x86_64/configure: Likewise.
70+
* sysdeps/mips/Makefile (ASFLAGS-.o): Add $(pie-default).
71+
(ASFLAGS-.op): Likewise.
72+
173
2017-12-15 Joseph Myers <joseph@codesourcery.com>
274

375
* io/Makefile (tst-open-tmpfile-ARGS): New variable.

INSTALL

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ will be used, and CFLAGS sets optimization options for the compiler.
8686
systems support shared libraries; you need ELF support and
8787
(currently) the GNU linker.
8888

89+
'--enable-static-pie'
90+
Enable static position independent executable (static PIE) support.
91+
Static PIE is similar to static executable, but can be loaded at
92+
any address without help from a dynamic linker. All static
93+
programs as well as static tests are built as static PIE, except
94+
for those marked with no-pie. The resulting glibc can be used with
95+
the GCC option, -static-pie, which is available with GCC 8 or
96+
above, to create static PIE.
97+
8998
'--disable-profile'
9099
Don't build libraries with profiling information. You may want to
91100
use this option if you don't plan to do profiling.

Makeconfig

+34-4
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ ifndef static-start-installed-name
352352
static-start-installed-name = $(start-installed-name)
353353
endif
354354

355+
ifeq (yes,$(enable-static-pie))
356+
# Link with rcrt1.o, instead of crt1.o, to call _dl_relocate_static_pie
357+
# to relocate static PIE.
358+
real-static-start-installed-name = r$(static-start-installed-name)
359+
else
360+
real-static-start-installed-name = $(static-start-installed-name)
361+
endif
362+
355363
ifeq (yesyes,$(build-shared)$(have-z-combreloc))
356364
combreloc-LDFLAGS = -Wl,-z,combreloc
357365
LDFLAGS.so += $(combreloc-LDFLAGS)
@@ -371,6 +379,20 @@ LDFLAGS.so += $(hashstyle-LDFLAGS)
371379
LDFLAGS-rtld += $(hashstyle-LDFLAGS)
372380
endif
373381

382+
ifeq (yes,$(enable-static-pie))
383+
pic-default = -DPIC
384+
# Compile libc.a and libc_p.a with -fPIE/-fpie for static PIE.
385+
pie-default = $(pie-ccflag)
386+
ifeq (yes,$(have-static-pie))
387+
default-pie-ldflag = -static-pie
388+
else
389+
# Static PIE can't have dynamic relocations in read-only segments since
390+
# static PIE is mapped into memory by kernel. --eh-frame-hdr is needed
391+
# for PIE to support exception.
392+
default-pie-ldflag = -Wl,-pie,--no-dynamic-linker,--eh-frame-hdr,-z,text
393+
endif
394+
endif
395+
374396
# If lazy relocations are disabled, add the -z now flag. Use
375397
# LDFLAGS-lib.so instead of LDFLAGS.so, to avoid adding the flag to
376398
# test modules.
@@ -420,9 +442,9 @@ endif
420442
# Command for statically linking programs with the C library.
421443
ifndef +link-static
422444
+link-static-before-libc = $(CC) -nostdlib -nostartfiles -static -o $@ \
423-
$(DEFAULT-LDFLAGS-$(@F)) \
445+
$(if $($(@F)-no-pie),$(no-pie-ldflag),$(default-pie-ldflag)) \
424446
$(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
425-
$(firstword $(CRT-$(@F)) $(csu-objpfx)$(static-start-installed-name)) \
447+
$(firstword $(CRT-$(@F)) $(csu-objpfx)$(real-static-start-installed-name)) \
426448
$(+preinit) $(+prectorT) \
427449
$(filter-out $(addprefix $(csu-objpfx),start.o \
428450
$(start-installed-name))\
@@ -637,8 +659,14 @@ endif
637659
+prectorS = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtbeginS.o`
638660
+postctorS = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtendS.o`
639661
# Variants of the two previous definitions for statically linking programs.
662+
ifeq (yes,$(enable-static-pie))
663+
# Static PIE must use PIE variants.
664+
+prectorT = $(+prectorS)
665+
+postctorT = $(+postctorS)
666+
else
640667
+prectorT = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtbeginT.o`
641668
+postctorT = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtend.o`
669+
endif
642670
csu-objpfx = $(common-objpfx)csu/
643671
elf-objpfx = $(common-objpfx)elf/
644672

@@ -959,7 +987,8 @@ libtypes = $(foreach o,$(object-suffixes-for-libc),$(libtype$o))
959987
all-object-suffixes := .o .os .oS
960988
object-suffixes :=
961989
CPPFLAGS-.o = $(pic-default)
962-
CFLAGS-.o = $(filter %frame-pointer,$(+cflags))
990+
# libc.a must be compiled with -fPIE/-fpie for static PIE.
991+
CFLAGS-.o = $(filter %frame-pointer,$(+cflags)) $(pie-default)
963992
libtype.o := lib%.a
964993
object-suffixes += .o
965994
ifeq (yes,$(build-shared))
@@ -984,7 +1013,8 @@ ifeq (yes,$(build-profile))
9841013
all-object-suffixes += .op
9851014
object-suffixes += .op
9861015
CPPFLAGS-.op = -DPROF $(pic-default)
987-
CFLAGS-.op = -pg
1016+
# libc_p.a must be compiled with -fPIE/-fpie for static PIE.
1017+
CFLAGS-.op = -pg $(pie-default)
9881018
libtype.op = lib%_p.a
9891019
endif
9901020

NEWS

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ Version 2.27
99

1010
Major new features:
1111

12+
* The GNU C Library can now be compiled with support for building static
13+
PIE executables (See --enable-static-pie in INSTALL). These static PIE
14+
exectuables are like static executables but can be loaded at any address
15+
and provide additional security hardening benefits at the cost of some
16+
memory and performance. When the library is built with --enable-static-pie
17+
the resulting libc.a is usable with GCC 8 and above to create static PIE
18+
executables using the GCC option '-static-pie'. This feature is currently
19+
supported on i386, x86_64 and x32.
20+
1221
* Optimized x86-64 asin, atan2, exp, expf, log, pow, atan, sin, cosf,
1322
sinf and tan with FMA, contributed by Arjan van de Ven and H.J. Lu
1423
from Intel.

config.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@
238238
/* Build glibc with tunables support. */
239239
#define HAVE_TUNABLES 0
240240

241+
/* Define if static PIE is enabled. */
242+
#define ENABLE_STATIC_PIE 0
243+
241244
/* Some compiler options may now allow to use ebp in __asm__ (used mainly
242245
in i386 6 argument syscall issue). */
243246
#define CAN_USE_REGISTER_ASM_EBP 0

configure

+79
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,7 @@ with_default_link
763763
enable_sanity_checks
764764
enable_shared
765765
enable_profile
766+
enable_static_pie
766767
enable_timezone_tools
767768
enable_hardcoded_path_in_tests
768769
enable_stackguard_randomization
@@ -1417,6 +1418,8 @@ Optional Features:
14171418
in special situations) [default=yes]
14181419
--enable-shared build shared library [default=yes if GNU ld]
14191420
--enable-profile build profiled library [default=no]
1421+
--enable-static-pie enable static PIE support and use it in the
1422+
testsuite [default=no]
14201423
--disable-timezone-tools
14211424
do not install timezone tools [default=install]
14221425
--enable-hardcoded-path-in-tests
@@ -3350,6 +3353,13 @@ else
33503353
profile=no
33513354
fi
33523355

3356+
# Check whether --enable-static-pie was given.
3357+
if test "${enable_static_pie+set}" = set; then :
3358+
enableval=$enable_static_pie; static_pie=$enableval
3359+
else
3360+
static_pie=no
3361+
fi
3362+
33533363
# Check whether --enable-timezone-tools was given.
33543364
if test "${enable_timezone_tools+set}" = set; then :
33553365
enableval=$enable_timezone_tools; enable_timezone_tools=$enableval
@@ -5806,6 +5816,62 @@ fi
58065816
$as_echo "$libc_linker_feature" >&6; }
58075817

58085818

5819+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports --no-dynamic-linker" >&5
5820+
$as_echo_n "checking for linker that supports --no-dynamic-linker... " >&6; }
5821+
libc_linker_feature=no
5822+
if test x"$gnu_ld" = x"yes"; then
5823+
libc_linker_check=`$LD -v --help 2>/dev/null | grep "\--no-dynamic-linker"`
5824+
if test -n "$libc_linker_check"; then
5825+
cat > conftest.c <<EOF
5826+
int _start (void) { return 42; }
5827+
EOF
5828+
if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp
5829+
-Wl,--no-dynamic-linker -nostdlib -nostartfiles
5830+
-fPIC -shared -o conftest.so conftest.c
5831+
1>&5'
5832+
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
5833+
(eval $ac_try) 2>&5
5834+
ac_status=$?
5835+
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
5836+
test $ac_status = 0; }; }
5837+
then
5838+
libc_linker_feature=yes
5839+
fi
5840+
rm -f conftest*
5841+
fi
5842+
fi
5843+
if test $libc_linker_feature = yes; then
5844+
libc_cv_no_dynamic_linker=yes
5845+
else
5846+
libc_cv_no_dynamic_linker=no
5847+
fi
5848+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5
5849+
$as_echo "$libc_linker_feature" >&6; }
5850+
config_vars="$config_vars
5851+
have-no-dynamic-linker = $libc_cv_no_dynamic_linker"
5852+
5853+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -static-pie" >&5
5854+
$as_echo_n "checking for -static-pie... " >&6; }
5855+
if ${libc_cv_static_pie+:} false; then :
5856+
$as_echo_n "(cached) " >&6
5857+
else
5858+
if { ac_try='${CC-cc} -static-pie -xc /dev/null -S -o /dev/null'
5859+
{ { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
5860+
(eval $ac_try) 2>&5
5861+
ac_status=$?
5862+
$as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
5863+
test $ac_status = 0; }; }; then :
5864+
libc_cv_static_pie=yes
5865+
else
5866+
libc_cv_static_pie=no
5867+
fi
5868+
5869+
fi
5870+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_static_pie" >&5
5871+
$as_echo "$libc_cv_static_pie" >&6; }
5872+
config_vars="$config_vars
5873+
have-static-pie = $libc_cv_static_pie"
5874+
58095875
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -fpie" >&5
58105876
$as_echo_n "checking for -fpie... " >&6; }
58115877
if ${libc_cv_fpie+:} false; then :
@@ -6705,6 +6771,19 @@ $as_echo "$libc_cv_pie_default" >&6; }
67056771
libc_cv_multidir=`${CC-cc} $CFLAGS $CPPFLAGS -print-multi-directory`
67066772

67076773

6774+
if test "$static_pie" = yes; then
6775+
# The linker must support --no-dynamic-linker.
6776+
if test "$libc_cv_no_dynamic_linker" != yes; then
6777+
as_fn_error $? "linker support for --no-dynamic-linker needed" "$LINENO" 5
6778+
fi
6779+
# Default to PIE.
6780+
libc_cv_pie_default=yes
6781+
$as_echo "#define ENABLE_STATIC_PIE 1" >>confdefs.h
6782+
6783+
fi
6784+
config_vars="$config_vars
6785+
enable-static-pie = $static_pie"
6786+
67086787

67096788

67106789

configure.ac

+29
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ AC_ARG_ENABLE([profile],
170170
[build profiled library @<:@default=no@:>@]),
171171
[profile=$enableval],
172172
[profile=no])
173+
AC_ARG_ENABLE([static-pie],
174+
AC_HELP_STRING([--enable-static-pie],
175+
[enable static PIE support and use it in the testsuite @<:@default=no@:>@]),
176+
[static_pie=$enableval],
177+
[static_pie=no])
173178
AC_ARG_ENABLE([timezone-tools],
174179
AC_HELP_STRING([--disable-timezone-tools],
175180
[do not install timezone tools @<:@default=install@:>@]),
@@ -1287,6 +1292,19 @@ LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,execstack],
12871292
[libc_cv_z_execstack=yes], [libc_cv_z_execstack=no])
12881293
AC_SUBST(libc_cv_z_execstack)
12891294

1295+
LIBC_LINKER_FEATURE([--no-dynamic-linker],
1296+
[-Wl,--no-dynamic-linker],
1297+
[libc_cv_no_dynamic_linker=yes],
1298+
[libc_cv_no_dynamic_linker=no])
1299+
LIBC_CONFIG_VAR([have-no-dynamic-linker], [$libc_cv_no_dynamic_linker])
1300+
1301+
AC_CACHE_CHECK(for -static-pie, libc_cv_static_pie, [dnl
1302+
LIBC_TRY_CC_OPTION([-static-pie],
1303+
[libc_cv_static_pie=yes],
1304+
[libc_cv_static_pie=no])
1305+
])
1306+
LIBC_CONFIG_VAR([have-static-pie], [$libc_cv_static_pie])
1307+
12901308
AC_CACHE_CHECK(for -fpie, libc_cv_fpie, [dnl
12911309
LIBC_TRY_CC_OPTION([-fpie], [libc_cv_fpie=yes], [libc_cv_fpie=no])
12921310
])
@@ -1794,6 +1812,17 @@ AC_SUBST(libc_cv_pie_default)
17941812
libc_cv_multidir=`${CC-cc} $CFLAGS $CPPFLAGS -print-multi-directory`
17951813
AC_SUBST(libc_cv_multidir)
17961814

1815+
if test "$static_pie" = yes; then
1816+
# The linker must support --no-dynamic-linker.
1817+
if test "$libc_cv_no_dynamic_linker" != yes; then
1818+
AC_MSG_ERROR([linker support for --no-dynamic-linker needed])
1819+
fi
1820+
# Default to PIE.
1821+
libc_cv_pie_default=yes
1822+
AC_DEFINE(ENABLE_STATIC_PIE)
1823+
fi
1824+
LIBC_CONFIG_VAR([enable-static-pie], [$static_pie])
1825+
17971826
AC_SUBST(profile)
17981827
AC_SUBST(static_nss)
17991828

0 commit comments

Comments
 (0)