Skip to content

Commit 6d1ab23

Browse files
author
hjl
committed
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. (GNU_USER_TARGET_ENDFILE_SPEC): Likewise. (LINK_EH_SPEC): Likewise. (LINK_GCC_C_SEQUENCE_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 -static-pie. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@252034 138bc75d-0d04-0410-961f-82ee72b054a4
1 parent ca76ee4 commit 6d1ab23

File tree

7 files changed

+79
-33
lines changed

7 files changed

+79
-33
lines changed

gcc/ChangeLog

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
2017-09-12 H.J. Lu <[email protected]>
2+
3+
PR driver/81498
4+
* common.opt (-static-pie): New alias.
5+
(shared): Negate static-pie.
6+
(-no-pie): Update help text.
7+
(-pie): Likewise.
8+
(static-pie): New option.
9+
* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Add
10+
-static-pie support.
11+
(GNU_USER_TARGET_ENDFILE_SPEC): Likewise.
12+
(LINK_EH_SPEC): Likewise.
13+
(LINK_GCC_C_SEQUENCE_SPEC): Likewise.
14+
* config/i386/gnu-user.h (GNU_USER_TARGET_LINK_SPEC): Likewise.
15+
* config/i386/gnu-user64.h (GNU_USER_TARGET_LINK_SPEC): Likewise.
16+
* gcc.c (LINK_COMMAND_SPEC): Likewise.
17+
(init_gcc_specs): Likewise.
18+
(init_spec): Likewise.
19+
(display_help): Update help message for -pie.
20+
* doc/invoke.texi: Update -pie, -no-pie and -static. Document
21+
-static-pie.
22+
123
2017-09-12 Wilco Dijkstra <[email protected]>
224

325
* config/aarch64/aarch64.md (movsi_aarch64): Remove all '*'.

gcc/common.opt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ Common Alias(pedantic-errors)
352352
-pie
353353
Driver Alias(pie)
354354

355+
-static-pie
356+
Driver Alias(static-pie)
357+
355358
-pipe
356359
Driver Alias(pipe)
357360

@@ -3065,7 +3068,7 @@ x
30653068
Driver Joined Separate
30663069

30673070
shared
3068-
Driver RejectNegative Negative(pie)
3071+
Driver RejectNegative Negative(static-pie)
30693072
Create a shared library.
30703073

30713074
shared-libgcc
@@ -3111,11 +3114,15 @@ Driver
31113114

31123115
no-pie
31133116
Driver RejectNegative Negative(shared)
3114-
Don't create a position independent executable.
3117+
Don't create a dynamically linked position independent executable.
31153118

31163119
pie
31173120
Driver RejectNegative Negative(no-pie)
3118-
Create a position independent executable.
3121+
Create a dynamically linked position independent executable.
3122+
3123+
static-pie
3124+
Driver RejectNegative Negative(pie)
3125+
Create a static position independent executable.
31193126

31203127
z
31213128
Driver Joined Separate

gcc/config/gnu-user.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
5353
"%{shared:; \
5454
pg|p|profile:gcrt1.o%s; \
5555
static:crt1.o%s; \
56-
" PIE_SPEC ":Scrt1.o%s; \
56+
static-pie|" PIE_SPEC ":Scrt1.o%s; \
5757
:crt1.o%s} \
5858
crti.o%s \
5959
%{static:crtbeginT.o%s; \
60-
shared|" PIE_SPEC ":crtbeginS.o%s; \
60+
shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
6161
:crtbegin.o%s} \
6262
%{fvtable-verify=none:%s; \
6363
fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -70,7 +70,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
7070
:crt1.o%s} \
7171
crti.o%s \
7272
%{static:crtbeginT.o%s; \
73-
shared|pie:crtbeginS.o%s; \
73+
shared|pie|static-pie:crtbeginS.o%s; \
7474
:crtbegin.o%s} \
7575
%{fvtable-verify=none:%s; \
7676
fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -92,7 +92,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
9292
fvtable-verify=preinit:vtv_end_preinit.o%s; \
9393
fvtable-verify=std:vtv_end.o%s} \
9494
%{static:crtend.o%s; \
95-
shared|" PIE_SPEC ":crtendS.o%s; \
95+
shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
9696
:crtend.o%s} \
9797
crtn.o%s \
9898
" CRTOFFLOADEND
@@ -102,7 +102,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
102102
fvtable-verify=preinit:vtv_end_preinit.o%s; \
103103
fvtable-verify=std:vtv_end.o%s} \
104104
%{static:crtend.o%s; \
105-
shared|pie:crtendS.o%s; \
105+
shared|pie|static-pie:crtendS.o%s; \
106106
:crtend.o%s} \
107107
crtn.o%s \
108108
" CRTOFFLOADEND
@@ -132,12 +132,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
132132
#define LIB_SPEC GNU_USER_TARGET_LIB_SPEC
133133

134134
#if defined(HAVE_LD_EH_FRAME_HDR)
135-
#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
135+
#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
136136
#endif
137137

138138
#undef LINK_GCC_C_SEQUENCE_SPEC
139139
#define LINK_GCC_C_SEQUENCE_SPEC \
140-
"%{static:--start-group} %G %L %{static:--end-group}%{!static:%G}"
140+
"%{static|static-pie:--start-group} %G %L \
141+
%{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
141142

142143
/* Use --as-needed -lgcc_s for eh support. */
143144
#ifdef HAVE_LD_AS_NEEDED

gcc/config/i386/gnu-user.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ along with GCC; see the file COPYING3. If not see
7777
#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
7878
%{!shared: \
7979
%{!static: \
80-
%{rdynamic:-export-dynamic} \
81-
-dynamic-linker %(dynamic_linker)} \
82-
%{static:-static}}"
80+
%{!static-pie: \
81+
%{rdynamic:-export-dynamic} \
82+
-dynamic-linker %(dynamic_linker)}} \
83+
%{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
8384

8485
#undef LINK_SPEC
8586
#define LINK_SPEC GNU_USER_TARGET_LINK_SPEC

gcc/config/i386/gnu-user64.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
5959
%{shared:-shared} \
6060
%{!shared: \
6161
%{!static: \
62-
%{rdynamic:-export-dynamic} \
63-
%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
64-
%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
65-
%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}} \
66-
%{static:-static}}"
62+
%{!static-static: \
63+
%{rdynamic:-export-dynamic} \
64+
%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
65+
%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
66+
%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}} \
67+
%{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
6768

6869
#undef LINK_SPEC
6970
#define LINK_SPEC GNU_USER_TARGET_LINK_SPEC

gcc/doc/invoke.texi

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ Objective-C and Objective-C++ Dialects}.
497497
@xref{Link Options,,Options for Linking}.
498498
@gccoptlist{@var{object-file-name} -fuse-ld=@var{linker} -l@var{library} @gol
499499
-nostartfiles -nodefaultlibs -nostdlib -pie -pthread -rdynamic @gol
500-
-s -static -static-libgcc -static-libstdc++ @gol
500+
-s -static -static-pie -static-libgcc -static-libstdc++ @gol
501501
-static-libasan -static-libtsan -static-liblsan -static-libubsan @gol
502502
-static-libmpx -static-libmpxwrappers @gol
503503
-shared -shared-libgcc -symbolic @gol
@@ -11825,14 +11825,23 @@ GNU Compiler Collection (GCC) Internals}.)
1182511825

1182611826
@item -pie
1182711827
@opindex pie
11828-
Produce a position independent executable on targets that support it.
11829-
For predictable results, you must also specify the same set of options
11830-
used for compilation (@option{-fpie}, @option{-fPIE},
11828+
Produce a dynamically linked position independent executable on targets
11829+
that support it. For predictable results, you must also specify the same
11830+
set of options used for compilation (@option{-fpie}, @option{-fPIE},
1183111831
or model suboptions) when you specify this linker option.
1183211832

1183311833
@item -no-pie
1183411834
@opindex no-pie
11835-
Don't produce a position independent executable.
11835+
Don't produce a dynamically linked position independent executable.
11836+
11837+
@item -static-pie
11838+
@opindex static-pie
11839+
Produce a static position independent executable on targets that support
11840+
it. A static position independent executable is similar to a static
11841+
executable, but can be loaded at any address without a dynamic linker.
11842+
For predictable results, you must also specify the same set of options
11843+
used for compilation (@option{-fpie}, @option{-fPIE}, or model
11844+
suboptions) when you specify this linker option.
1183611845

1183711846
@item -pthread
1183811847
@opindex pthread
@@ -11856,8 +11865,9 @@ Remove all symbol table and relocation information from the executable.
1185611865

1185711866
@item -static
1185811867
@opindex static
11859-
On systems that support dynamic linking, this prevents linking with the shared
11860-
libraries. On other systems, this option has no effect.
11868+
On systems that support dynamic linking, this overrides @option{-pie}
11869+
and prevents linking with the shared libraries. On other systems, this
11870+
option has no effect.
1186111871

1186211872
@item -shared
1186311873
@opindex shared

gcc/gcc.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,10 @@ proper position among the other output files. */
10151015
#endif
10161016

10171017
/* -u* was put back because both BSD and SysV seem to support it. */
1018-
/* %{static|no-pie:} simply prevents an error message:
1018+
/* %{static|no-pie|static-pie:} simply prevents an error message:
10191019
1. If the target machine doesn't handle -static.
10201020
2. If PIE isn't enabled by default.
1021+
3. If the target machine doesn't handle -static-pie.
10211022
*/
10221023
/* We want %{T*} after %{L*} and %D so that it can be used to specify linker
10231024
scripts which exist in user specified directories, or in standard
@@ -1035,7 +1036,7 @@ proper position among the other output files. */
10351036
"%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
10361037
"%X %{o*} %{e*} %{N} %{n} %{r}\
10371038
%{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
1038-
%{static|no-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
1039+
%{static|no-pie|static-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
10391040
VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
10401041
%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
10411042
%:include(libgomp.spec)%(link_gomp)}\
@@ -1670,17 +1671,19 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
16701671
{
16711672
char *buf;
16721673

1673-
buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1674-
"%{!static:%{!static-libgcc:"
16751674
#if USE_LD_AS_NEEDED
1675+
buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
1676+
"%{!static:%{!static-libgcc:%{!static-pie:"
16761677
"%{!shared-libgcc:",
16771678
static_name, " " LD_AS_NEEDED_OPTION " ",
16781679
shared_name, " " LD_NO_AS_NEEDED_OPTION
16791680
"}"
16801681
"%{shared-libgcc:",
16811682
shared_name, "%{!shared: ", static_name, "}"
1682-
"}"
1683+
"}}"
16831684
#else
1685+
buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1686+
"%{!static:%{!static-libgcc:"
16841687
"%{!shared:"
16851688
"%{!shared-libgcc:", static_name, " ", eh_name, "}"
16861689
"%{shared-libgcc:", shared_name, " ", static_name, "}"
@@ -1788,8 +1791,8 @@ init_spec (void)
17881791
"-lgcc_eh"
17891792
#ifdef USE_LIBUNWIND_EXCEPTIONS
17901793
# ifdef HAVE_LD_STATIC_DYNAMIC
1791-
" %{!static:" LD_STATIC_OPTION "} -lunwind"
1792-
" %{!static:" LD_DYNAMIC_OPTION "}"
1794+
" %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
1795+
" %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
17931796
# else
17941797
" -lunwind"
17951798
# endif
@@ -3478,7 +3481,8 @@ display_help (void)
34783481
fputs (_(" -S Compile only; do not assemble or link.\n"), stdout);
34793482
fputs (_(" -c Compile and assemble, but do not link.\n"), stdout);
34803483
fputs (_(" -o <file> Place the output into <file>.\n"), stdout);
3481-
fputs (_(" -pie Create a position independent executable.\n"), stdout);
3484+
fputs (_(" -pie Create a dynamically linked position independent\n\
3485+
executable.\n"), stdout);
34823486
fputs (_(" -shared Create a shared library.\n"), stdout);
34833487
fputs (_("\
34843488
-x <language> Specify the language of the following input files.\n\

0 commit comments

Comments
 (0)