Bug 81498 - Support creating static PIE
Summary: Support creating static PIE
Alias: None
Product: gcc
Classification: Unclassified
Component: driver (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
Depends on: 81523
  Show dependency treegraph
Reported: 2017-07-20 19:51 UTC by H.J. Lu
Modified: 2017-09-12 16:40 UTC (History)
0 users

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2017-07-20 19:51:58 UTC
Glibc hjl/pie/static branch at


supports static PIE, which is similar to static executable, but can be loaded
at any address without a dynamic linker:

[hjl@gnu-6 elf]$ readelf -hl sln
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x8290
  Start of program headers:          64 (bytes into file)
  Start of section headers:          4532888 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         8
  Size of section headers:           64 (bytes)
  Number of section headers:         45
  Section header string table index: 44

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000099905 0x0000000000099905  R E    0x200000
  LOAD           0x000000000009a5a0 0x000000000029a5a0 0x000000000029a5a0
                 0x0000000000004d40 0x0000000000006230  RW     0x200000
  DYNAMIC        0x000000000009cd48 0x000000000029cd48 0x000000000029cd48
                 0x00000000000001a0 0x00000000000001a0  RW     0x8
  NOTE           0x0000000000000200 0x0000000000000200 0x0000000000000200
                 0x0000000000000044 0x0000000000000044  R      0x4
  TLS            0x000000000009a5a0 0x000000000029a5a0 0x000000000029a5a0
                 0x0000000000000020 0x0000000000000060  R      0x8
  GNU_EH_FRAME   0x000000000008ee18 0x000000000008ee18 0x000000000008ee18
                 0x00000000000018bc 0x00000000000018bc  R      0x4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x10
  GNU_RELRO      0x000000000009a5a0 0x000000000029a5a0 0x000000000029a5a0
                 0x0000000000002a60 0x0000000000002a60  R      0x1

 Section to Segment mapping:
  Segment Sections...
   00     .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .rela.dyn .rela.plt .init .plt .plt.got .text __libc_freeres_fn __libc_thread_freeres_fn .fini .rodata .eh_frame_hdr .eh_frame .gcc_except_table 
   01     .tdata .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .data __libc_subfreeres __libc_IO_vtables __libc_atexit __libc_thread_subfreeres .bss __libc_freeres_ptrs 
   02     .dynamic 
   03     .note.ABI-tag .note.gnu.build-id 
   04     .tdata .tbss 
   05     .eh_frame_hdr 
   07     .tdata .init_array .fini_array .data.rel.ro .dynamic .got 
[hjl@gnu-6 elf]$ sln
Usage: sln src dest|file

For bug reporting instructions, please see:
[hjl@gnu-6 elf]$ 

To do that, we need to pass both -static and -pie as well as the PIE version
of crt*.o files to linker.
Comment 1 hjl@gcc.gnu.org 2017-09-12 16:31:00 UTC
Author: hjl
Date: Tue Sep 12 16:30:28 2017
New Revision: 252034

URL: https://gcc.gnu.org/viewcvs?rev=252034&root=gcc&view=rev
Add -static-pie to GCC driver to create static PIE

This patch adds -static-pie to GCC driver to create static PIE.  A static
position independent executable (PIE) is similar to static executable,
but can be loaded at any address without a dynamic linker.  All linker
input files must be compiled with -fpie or -fPIE and linker must support
--no-dynamic-linker to avoid linking with dynamic linker.  "-z text" is
also needed to prevent dynamic relocations in read-only segments.

	PR driver/81498
	* common.opt (-static-pie): New alias.
	(shared): Negate static-pie.
	(-no-pie): Update help text.
	(-pie): Likewise.
	(static-pie): New option.
	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Add
	-static-pie support.
	(LINK_EH_SPEC): Likewise.
	* config/i386/gnu-user.h (GNU_USER_TARGET_LINK_SPEC): Likewise.
	* config/i386/gnu-user64.h (GNU_USER_TARGET_LINK_SPEC): Likewise.
	* gcc.c (LINK_COMMAND_SPEC): Likewise.
	(init_gcc_specs): Likewise.
	(init_spec): Likewise.
	(display_help): Update help message for -pie.
	* doc/invoke.texi: Update -pie, -no-pie and -static.  Document

Comment 2 H.J. Lu 2017-09-12 16:40:32 UTC
Fixed for GCC 8.