public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Add -static-pie to GCC driver to create static PIE
@ 2017-08-08 22:19 H.J. Lu
  2017-08-09  5:36 ` Richard Biener
  2017-08-28 16:59 ` Joseph Myers
  0 siblings, 2 replies; 11+ messages in thread
From: H.J. Lu @ 2017-08-08 22:19 UTC (permalink / raw)
  To: gcc-patches

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.

OK for trunk?

H.J.
---
	PR driver/81498
	* common.opt (-static-pie): New alias.
	(shared): Negate static-pie.
	(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.
	* doc/invoke.texi: Document -static-pie.
---
 gcc/common.opt               |  9 ++++++++-
 gcc/config/gnu-user.h        | 15 ++++++++-------
 gcc/config/i386/gnu-user.h   |  7 ++++---
 gcc/config/i386/gnu-user64.h | 11 ++++++-----
 gcc/doc/invoke.texi          | 11 ++++++++++-
 gcc/gcc.c                    | 17 ++++++++++-------
 6 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 1cb1c83d306..246566168cc 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -353,6 +353,9 @@ Common Alias(pedantic-errors)
 -pie
 Driver Alias(pie)
 
+-static-pie
+Driver Alias(static-pie)
+
 -pipe
 Driver Alias(pipe)
 
@@ -3062,7 +3065,7 @@ x
 Driver Joined Separate
 
 shared
-Driver RejectNegative Negative(pie)
+Driver RejectNegative Negative(static-pie)
 Create a shared library.
 
 shared-libgcc
@@ -3114,6 +3117,10 @@ pie
 Driver RejectNegative Negative(no-pie)
 Create a position independent executable.
 
+static-pie
+Driver RejectNegative Negative(pie)
+Create a static position independent executable.
+
 z
 Driver Joined Separate
 
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index de605b0c466..a967b69a350 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -53,11 +53,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   "%{shared:; \
      pg|p|profile:gcrt1.o%s; \
      static:crt1.o%s; \
-     " PIE_SPEC ":Scrt1.o%s; \
+     static-pie|" PIE_SPEC ":Scrt1.o%s; \
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|" PIE_SPEC ":crtbeginS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -70,7 +70,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|pie:crtbeginS.o%s; \
+     shared|pie|static-pie:crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -92,7 +92,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|" PIE_SPEC ":crtendS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -102,7 +102,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|pie:crtendS.o%s; \
+     shared|pie|static-pie:crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -132,12 +132,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define LIB_SPEC GNU_USER_TARGET_LIB_SPEC
 
 #if defined(HAVE_LD_EH_FRAME_HDR)
-#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
+#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
 #endif
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static:--start-group} %G %L %{static:--end-group}%{!static:%G}"
+  "%{static|static-pie:--start-group} %G %L \
+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
 #ifdef HAVE_LD_AS_NEEDED
diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
index a4c88f1a848..8983dc9ecd7 100644
--- a/gcc/config/i386/gnu-user.h
+++ b/gcc/config/i386/gnu-user.h
@@ -77,9 +77,10 @@ along with GCC; see the file COPYING3.  If not see
 #define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      -dynamic-linker %(dynamic_linker)} \
-      %{static:-static}}"
+      %{!static-pie: \
+	%{rdynamic:-export-dynamic} \
+	-dynamic-linker %(dynamic_linker)}} \
+      %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h
index 39f5ef6a68b..6fc9eae6f6b 100644
--- a/gcc/config/i386/gnu-user64.h
+++ b/gcc/config/i386/gnu-user64.h
@@ -59,11 +59,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      %{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
-      %{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
-      %{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}} \
-    %{static:-static}}"
+      %{!static-static: \
+	%{rdynamic:-export-dynamic} \
+	%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
+	%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
+	%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}} \
+    %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index cc0f5a00a4f..322bde9178f 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -496,7 +496,7 @@ Objective-C and Objective-C++ Dialects}.
 @xref{Link Options,,Options for Linking}.
 @gccoptlist{@var{object-file-name}  -fuse-ld=@var{linker}  -l@var{library} @gol
 -nostartfiles  -nodefaultlibs  -nostdlib  -pie  -pthread  -rdynamic @gol
--s  -static  -static-libgcc  -static-libstdc++ @gol
+-s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
 -static-libasan  -static-libtsan  -static-liblsan  -static-libubsan @gol
 -static-libmpx  -static-libmpxwrappers @gol
 -shared  -shared-libgcc  -symbolic @gol
@@ -11780,6 +11780,15 @@ or model suboptions) when you specify this linker option.
 @opindex no-pie
 Don't produce a position independent executable.
 
+@item -static-pie
+@opindex static-pie
+Produce a static position independent executable on targets that support
+it.  A static position independent executable is similar to static
+executable, but can be loaded at any address without a dynamic linker.
+For predictable results, you must also specify the same set of options
+used for compilation (@option{-fpie}, @option{-fPIE}, or model
+suboptions) when you specify this linker option.
+
 @item -pthread
 @opindex pthread
 Link with the POSIX threads library.  This option is supported on 
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 987eff55aa6..1b60d59726e 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1015,9 +1015,10 @@ proper position among the other output files.  */
 #endif
 
 /* -u* was put back because both BSD and SysV seem to support it.  */
-/* %{static|no-pie:} simply prevents an error message:
+/* %{static|no-pie|static-pie:} simply prevents an error message:
    1. If the target machine doesn't handle -static.
    2. If PIE isn't enabled by default.
+   3. If the target machine doesn't handle -static-pie.
  */
 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
    scripts which exist in user specified directories, or in standard
@@ -1035,7 +1036,7 @@ proper position among the other output files.  */
    "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
    "%X %{o*} %{e*} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
-    %{static|no-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
+    %{static|no-pie|static-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
     VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
     %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
 	%:include(libgomp.spec)%(link_gomp)}\
@@ -1670,17 +1671,19 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
 {
   char *buf;
 
-  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
-		"%{!static:%{!static-libgcc:"
 #if USE_LD_AS_NEEDED
+  buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:%{!static-pie:"
 		"%{!shared-libgcc:",
 		static_name, " " LD_AS_NEEDED_OPTION " ",
 		shared_name, " " LD_NO_AS_NEEDED_OPTION
 		"}"
 		"%{shared-libgcc:",
 		shared_name, "%{!shared: ", static_name, "}"
-		"}"
+		"}}"
 #else
+  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:"
 		"%{!shared:"
 		"%{!shared-libgcc:", static_name, " ", eh_name, "}"
 		"%{shared-libgcc:", shared_name, " ", static_name, "}"
@@ -1788,8 +1791,8 @@ init_spec (void)
 			    "-lgcc_eh"
 #ifdef USE_LIBUNWIND_EXCEPTIONS
 # ifdef HAVE_LD_STATIC_DYNAMIC
-			    " %{!static:" LD_STATIC_OPTION "} -lunwind"
-			    " %{!static:" LD_DYNAMIC_OPTION "}"
+			    " %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
+			    " %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
 # else
 			    " -lunwind"
 # endif
-- 
2.13.4

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-08 22:19 [PATCH] Add -static-pie to GCC driver to create static PIE H.J. Lu
@ 2017-08-09  5:36 ` Richard Biener
  2017-08-09 10:39   ` H.J. Lu
  2017-08-28 16:59 ` Joseph Myers
  1 sibling, 1 reply; 11+ messages in thread
From: Richard Biener @ 2017-08-09  5:36 UTC (permalink / raw)
  To: H.J. Lu, H.J. Lu, gcc-patches

On August 9, 2017 12:18:41 AM GMT+02:00, "H.J. Lu" <hongjiu.lu@intel.com> wrote:
>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.
>
>OK for trunk?

What's wrong with -static -pie?

>H.J.
>---
>	PR driver/81498
>	* common.opt (-static-pie): New alias.
>	(shared): Negate static-pie.
>	(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.
>	* doc/invoke.texi: Document -static-pie.
>---
> gcc/common.opt               |  9 ++++++++-
> gcc/config/gnu-user.h        | 15 ++++++++-------
> gcc/config/i386/gnu-user.h   |  7 ++++---
> gcc/config/i386/gnu-user64.h | 11 ++++++-----
> gcc/doc/invoke.texi          | 11 ++++++++++-
> gcc/gcc.c                    | 17 ++++++++++-------
> 6 files changed, 46 insertions(+), 24 deletions(-)
>
>diff --git a/gcc/common.opt b/gcc/common.opt
>index 1cb1c83d306..246566168cc 100644
>--- a/gcc/common.opt
>+++ b/gcc/common.opt
>@@ -353,6 +353,9 @@ Common Alias(pedantic-errors)
> -pie
> Driver Alias(pie)
> 
>+-static-pie
>+Driver Alias(static-pie)
>+
> -pipe
> Driver Alias(pipe)
> 
>@@ -3062,7 +3065,7 @@ x
> Driver Joined Separate
> 
> shared
>-Driver RejectNegative Negative(pie)
>+Driver RejectNegative Negative(static-pie)
> Create a shared library.
> 
> shared-libgcc
>@@ -3114,6 +3117,10 @@ pie
> Driver RejectNegative Negative(no-pie)
> Create a position independent executable.
> 
>+static-pie
>+Driver RejectNegative Negative(pie)
>+Create a static position independent executable.
>+
> z
> Driver Joined Separate
> 
>diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
>index de605b0c466..a967b69a350 100644
>--- a/gcc/config/gnu-user.h
>+++ b/gcc/config/gnu-user.h
>@@ -53,11 +53,11 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
>   "%{shared:; \
>      pg|p|profile:gcrt1.o%s; \
>      static:crt1.o%s; \
>-     " PIE_SPEC ":Scrt1.o%s; \
>+     static-pie|" PIE_SPEC ":Scrt1.o%s; \
>      :crt1.o%s} \
>    crti.o%s \
>    %{static:crtbeginT.o%s; \
>-     shared|" PIE_SPEC ":crtbeginS.o%s; \
>+     shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
>      :crtbegin.o%s} \
>    %{fvtable-verify=none:%s; \
>      fvtable-verify=preinit:vtv_start_preinit.o%s; \
>@@ -70,7 +70,7 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
>      :crt1.o%s} \
>    crti.o%s \
>    %{static:crtbeginT.o%s; \
>-     shared|pie:crtbeginS.o%s; \
>+     shared|pie|static-pie:crtbeginS.o%s; \
>      :crtbegin.o%s} \
>    %{fvtable-verify=none:%s; \
>      fvtable-verify=preinit:vtv_start_preinit.o%s; \
>@@ -92,7 +92,7 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
>      fvtable-verify=preinit:vtv_end_preinit.o%s; \
>      fvtable-verify=std:vtv_end.o%s} \
>    %{static:crtend.o%s; \
>-     shared|" PIE_SPEC ":crtendS.o%s; \
>+     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
>      :crtend.o%s} \
>    crtn.o%s \
>    " CRTOFFLOADEND
>@@ -102,7 +102,7 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
>      fvtable-verify=preinit:vtv_end_preinit.o%s; \
>      fvtable-verify=std:vtv_end.o%s} \
>    %{static:crtend.o%s; \
>-     shared|pie:crtendS.o%s; \
>+     shared|pie|static-pie:crtendS.o%s; \
>      :crtend.o%s} \
>    crtn.o%s \
>    " CRTOFFLOADEND
>@@ -132,12 +132,13 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
> #define LIB_SPEC GNU_USER_TARGET_LIB_SPEC
> 
> #if defined(HAVE_LD_EH_FRAME_HDR)
>-#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
>+#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
> #endif
> 
> #undef LINK_GCC_C_SEQUENCE_SPEC
> #define LINK_GCC_C_SEQUENCE_SPEC \
>-  "%{static:--start-group} %G %L %{static:--end-group}%{!static:%G}"
>+  "%{static|static-pie:--start-group} %G %L \
>+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
> 
> /* Use --as-needed -lgcc_s for eh support.  */
> #ifdef HAVE_LD_AS_NEEDED
>diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
>index a4c88f1a848..8983dc9ecd7 100644
>--- a/gcc/config/i386/gnu-user.h
>+++ b/gcc/config/i386/gnu-user.h
>@@ -77,9 +77,10 @@ along with GCC; see the file COPYING3.  If not see
>#define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation)
>%{shared:-shared} \
>   %{!shared: \
>     %{!static: \
>-      %{rdynamic:-export-dynamic} \
>-      -dynamic-linker %(dynamic_linker)} \
>-      %{static:-static}}"
>+      %{!static-pie: \
>+	%{rdynamic:-export-dynamic} \
>+	-dynamic-linker %(dynamic_linker)}} \
>+      %{static:-static} %{static-pie:-static -pie --no-dynamic-linker
>-z text}}"
> 
> #undef	LINK_SPEC
> #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
>diff --git a/gcc/config/i386/gnu-user64.h
>b/gcc/config/i386/gnu-user64.h
>index 39f5ef6a68b..6fc9eae6f6b 100644
>--- a/gcc/config/i386/gnu-user64.h
>+++ b/gcc/config/i386/gnu-user64.h
>@@ -59,11 +59,12 @@ see the files COPYING3 and COPYING.RUNTIME
>respectively.  If not, see
>   %{shared:-shared} \
>   %{!shared: \
>     %{!static: \
>-      %{rdynamic:-export-dynamic} \
>-      %{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
>-      %{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
>-      %{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}
>\
>-    %{static:-static}}"
>+      %{!static-static: \
>+	%{rdynamic:-export-dynamic} \
>+	%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
>+	%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
>+	%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}} \
>+    %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z
>text}}"
> 
> #undef	LINK_SPEC
> #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
>diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
>index cc0f5a00a4f..322bde9178f 100644
>--- a/gcc/doc/invoke.texi
>+++ b/gcc/doc/invoke.texi
>@@ -496,7 +496,7 @@ Objective-C and Objective-C++ Dialects}.
> @xref{Link Options,,Options for Linking}.
>@gccoptlist{@var{object-file-name}  -fuse-ld=@var{linker} 
>-l@var{library} @gol
>-nostartfiles  -nodefaultlibs  -nostdlib  -pie  -pthread  -rdynamic
>@gol
>--s  -static  -static-libgcc  -static-libstdc++ @gol
>+-s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
>-static-libasan  -static-libtsan  -static-liblsan  -static-libubsan
>@gol
> -static-libmpx  -static-libmpxwrappers @gol
> -shared  -shared-libgcc  -symbolic @gol
>@@ -11780,6 +11780,15 @@ or model suboptions) when you specify this
>linker option.
> @opindex no-pie
> Don't produce a position independent executable.
> 
>+@item -static-pie
>+@opindex static-pie
>+Produce a static position independent executable on targets that
>support
>+it.  A static position independent executable is similar to static
>+executable, but can be loaded at any address without a dynamic linker.
>+For predictable results, you must also specify the same set of options
>+used for compilation (@option{-fpie}, @option{-fPIE}, or model
>+suboptions) when you specify this linker option.
>+
> @item -pthread
> @opindex pthread
> Link with the POSIX threads library.  This option is supported on 
>diff --git a/gcc/gcc.c b/gcc/gcc.c
>index 987eff55aa6..1b60d59726e 100644
>--- a/gcc/gcc.c
>+++ b/gcc/gcc.c
>@@ -1015,9 +1015,10 @@ proper position among the other output files. 
>*/
> #endif
> 
> /* -u* was put back because both BSD and SysV seem to support it.  */
>-/* %{static|no-pie:} simply prevents an error message:
>+/* %{static|no-pie|static-pie:} simply prevents an error message:
>    1. If the target machine doesn't handle -static.
>    2. If PIE isn't enabled by default.
>+   3. If the target machine doesn't handle -static-pie.
>  */
>/* We want %{T*} after %{L*} and %D so that it can be used to specify
>linker
>    scripts which exist in user specified directories, or in standard
>@@ -1035,7 +1036,7 @@ proper position among the other output files.  */
>    "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
>    "%X %{o*} %{e*} %{N} %{n} %{r}\
>     %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
>-    %{static|no-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
>+    %{static|no-pie|static-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
> VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
>     %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
> 	%:include(libgomp.spec)%(link_gomp)}\
>@@ -1670,17 +1671,19 @@ init_gcc_specs (struct obstack *obstack, const
>char *shared_name,
> {
>   char *buf;
> 
>-  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
>"}"
>-		"%{!static:%{!static-libgcc:"
> #if USE_LD_AS_NEEDED
>+  buf = concat ("%{static|static-libgcc|static-pie:", static_name, "
>", eh_name, "}"
>+		"%{!static:%{!static-libgcc:%{!static-pie:"
> 		"%{!shared-libgcc:",
> 		static_name, " " LD_AS_NEEDED_OPTION " ",
> 		shared_name, " " LD_NO_AS_NEEDED_OPTION
> 		"}"
> 		"%{shared-libgcc:",
> 		shared_name, "%{!shared: ", static_name, "}"
>-		"}"
>+		"}}"
> #else
>+  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
>"}"
>+		"%{!static:%{!static-libgcc:"
> 		"%{!shared:"
> 		"%{!shared-libgcc:", static_name, " ", eh_name, "}"
> 		"%{shared-libgcc:", shared_name, " ", static_name, "}"
>@@ -1788,8 +1791,8 @@ init_spec (void)
> 			    "-lgcc_eh"
> #ifdef USE_LIBUNWIND_EXCEPTIONS
> # ifdef HAVE_LD_STATIC_DYNAMIC
>-			    " %{!static:" LD_STATIC_OPTION "} -lunwind"
>-			    " %{!static:" LD_DYNAMIC_OPTION "}"
>+			    " %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
>+			    " %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
> # else
> 			    " -lunwind"
> # endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-09  5:36 ` Richard Biener
@ 2017-08-09 10:39   ` H.J. Lu
  2017-08-24 21:04     ` H.J. Lu
  0 siblings, 1 reply; 11+ messages in thread
From: H.J. Lu @ 2017-08-09 10:39 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches

On Tue, Aug 8, 2017 at 10:36 PM, Richard Biener
<richard.guenther@gmail.com> wrote:
> On August 9, 2017 12:18:41 AM GMT+02:00, "H.J. Lu" <hongjiu.lu@intel.com> wrote:
>>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.
>>
>>OK for trunk?
>
> What's wrong with -static -pie?
>

-static -pie behaved differently depending on if --enable-default-pie
was used:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81523

I just checked in a patch to make -static always  override -pie.


-- 
H.J.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-09 10:39   ` H.J. Lu
@ 2017-08-24 21:04     ` H.J. Lu
  0 siblings, 0 replies; 11+ messages in thread
From: H.J. Lu @ 2017-08-24 21:04 UTC (permalink / raw)
  To: Richard Biener; +Cc: GCC Patches, Joseph S. Myers

[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]

On Wed, Aug 9, 2017 at 3:39 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Tue, Aug 8, 2017 at 10:36 PM, Richard Biener
> <richard.guenther@gmail.com> wrote:
>> On August 9, 2017 12:18:41 AM GMT+02:00, "H.J. Lu" <hongjiu.lu@intel.com> wrote:
>>>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.
>>>
>>>OK for trunk?
>>
>> What's wrong with -static -pie?
>>
>
> -static -pie behaved differently depending on if --enable-default-pie
> was used:
>
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81523
>
> I just checked in a patch to make -static always  override -pie.
>

Hi Joseph,

Can you take a look at this patch?  I am enclosing it here.

Thanks.


-- 
H.J.

[-- Attachment #2: 0001-Add-static-pie-to-GCC-driver-to-create-static-PIE.patch --]
[-- Type: text/x-patch, Size: 10105 bytes --]

From d5b7ffdb48d18b65e071b9e064e7d73ac58573da Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 20 Jul 2017 14:08:18 -0700
Subject: [PATCH] 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.
	(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.
	* doc/invoke.texi: Document -static-pie.
---
 gcc/common.opt               |  9 ++++++++-
 gcc/config/gnu-user.h        | 15 ++++++++-------
 gcc/config/i386/gnu-user.h   |  7 ++++---
 gcc/config/i386/gnu-user64.h | 11 ++++++-----
 gcc/doc/invoke.texi          | 11 ++++++++++-
 gcc/gcc.c                    | 17 ++++++++++-------
 6 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 1cb1c83d306..246566168cc 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -353,6 +353,9 @@ Common Alias(pedantic-errors)
 -pie
 Driver Alias(pie)
 
+-static-pie
+Driver Alias(static-pie)
+
 -pipe
 Driver Alias(pipe)
 
@@ -3062,7 +3065,7 @@ x
 Driver Joined Separate
 
 shared
-Driver RejectNegative Negative(pie)
+Driver RejectNegative Negative(static-pie)
 Create a shared library.
 
 shared-libgcc
@@ -3114,6 +3117,10 @@ pie
 Driver RejectNegative Negative(no-pie)
 Create a position independent executable.
 
+static-pie
+Driver RejectNegative Negative(pie)
+Create a static position independent executable.
+
 z
 Driver Joined Separate
 
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index de605b0c466..a967b69a350 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -53,11 +53,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   "%{shared:; \
      pg|p|profile:gcrt1.o%s; \
      static:crt1.o%s; \
-     " PIE_SPEC ":Scrt1.o%s; \
+     static-pie|" PIE_SPEC ":Scrt1.o%s; \
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|" PIE_SPEC ":crtbeginS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -70,7 +70,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|pie:crtbeginS.o%s; \
+     shared|pie|static-pie:crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -92,7 +92,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|" PIE_SPEC ":crtendS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -102,7 +102,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|pie:crtendS.o%s; \
+     shared|pie|static-pie:crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -132,12 +132,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define LIB_SPEC GNU_USER_TARGET_LIB_SPEC
 
 #if defined(HAVE_LD_EH_FRAME_HDR)
-#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
+#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
 #endif
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static:--start-group} %G %L %{static:--end-group}%{!static:%G}"
+  "%{static|static-pie:--start-group} %G %L \
+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
 #ifdef HAVE_LD_AS_NEEDED
diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
index a4c88f1a848..8983dc9ecd7 100644
--- a/gcc/config/i386/gnu-user.h
+++ b/gcc/config/i386/gnu-user.h
@@ -77,9 +77,10 @@ along with GCC; see the file COPYING3.  If not see
 #define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      -dynamic-linker %(dynamic_linker)} \
-      %{static:-static}}"
+      %{!static-pie: \
+	%{rdynamic:-export-dynamic} \
+	-dynamic-linker %(dynamic_linker)}} \
+      %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h
index 39f5ef6a68b..6fc9eae6f6b 100644
--- a/gcc/config/i386/gnu-user64.h
+++ b/gcc/config/i386/gnu-user64.h
@@ -59,11 +59,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      %{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
-      %{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
-      %{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}} \
-    %{static:-static}}"
+      %{!static-static: \
+	%{rdynamic:-export-dynamic} \
+	%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
+	%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
+	%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}} \
+    %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index cc0f5a00a4f..322bde9178f 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -496,7 +496,7 @@ Objective-C and Objective-C++ Dialects}.
 @xref{Link Options,,Options for Linking}.
 @gccoptlist{@var{object-file-name}  -fuse-ld=@var{linker}  -l@var{library} @gol
 -nostartfiles  -nodefaultlibs  -nostdlib  -pie  -pthread  -rdynamic @gol
--s  -static  -static-libgcc  -static-libstdc++ @gol
+-s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
 -static-libasan  -static-libtsan  -static-liblsan  -static-libubsan @gol
 -static-libmpx  -static-libmpxwrappers @gol
 -shared  -shared-libgcc  -symbolic @gol
@@ -11780,6 +11780,15 @@ or model suboptions) when you specify this linker option.
 @opindex no-pie
 Don't produce a position independent executable.
 
+@item -static-pie
+@opindex static-pie
+Produce a static position independent executable on targets that support
+it.  A static position independent executable is similar to static
+executable, but can be loaded at any address without a dynamic linker.
+For predictable results, you must also specify the same set of options
+used for compilation (@option{-fpie}, @option{-fPIE}, or model
+suboptions) when you specify this linker option.
+
 @item -pthread
 @opindex pthread
 Link with the POSIX threads library.  This option is supported on 
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 987eff55aa6..1b60d59726e 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1015,9 +1015,10 @@ proper position among the other output files.  */
 #endif
 
 /* -u* was put back because both BSD and SysV seem to support it.  */
-/* %{static|no-pie:} simply prevents an error message:
+/* %{static|no-pie|static-pie:} simply prevents an error message:
    1. If the target machine doesn't handle -static.
    2. If PIE isn't enabled by default.
+   3. If the target machine doesn't handle -static-pie.
  */
 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
    scripts which exist in user specified directories, or in standard
@@ -1035,7 +1036,7 @@ proper position among the other output files.  */
    "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
    "%X %{o*} %{e*} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
-    %{static|no-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
+    %{static|no-pie|static-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
     VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
     %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
 	%:include(libgomp.spec)%(link_gomp)}\
@@ -1670,17 +1671,19 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
 {
   char *buf;
 
-  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
-		"%{!static:%{!static-libgcc:"
 #if USE_LD_AS_NEEDED
+  buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:%{!static-pie:"
 		"%{!shared-libgcc:",
 		static_name, " " LD_AS_NEEDED_OPTION " ",
 		shared_name, " " LD_NO_AS_NEEDED_OPTION
 		"}"
 		"%{shared-libgcc:",
 		shared_name, "%{!shared: ", static_name, "}"
-		"}"
+		"}}"
 #else
+  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:"
 		"%{!shared:"
 		"%{!shared-libgcc:", static_name, " ", eh_name, "}"
 		"%{shared-libgcc:", shared_name, " ", static_name, "}"
@@ -1788,8 +1791,8 @@ init_spec (void)
 			    "-lgcc_eh"
 #ifdef USE_LIBUNWIND_EXCEPTIONS
 # ifdef HAVE_LD_STATIC_DYNAMIC
-			    " %{!static:" LD_STATIC_OPTION "} -lunwind"
-			    " %{!static:" LD_DYNAMIC_OPTION "}"
+			    " %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
+			    " %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
 # else
 			    " -lunwind"
 # endif
-- 
2.13.5


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-08 22:19 [PATCH] Add -static-pie to GCC driver to create static PIE H.J. Lu
  2017-08-09  5:36 ` Richard Biener
@ 2017-08-28 16:59 ` Joseph Myers
  2017-08-28 18:25   ` H.J. Lu
  1 sibling, 1 reply; 11+ messages in thread
From: Joseph Myers @ 2017-08-28 16:59 UTC (permalink / raw)
  To: H.J. Lu; +Cc: gcc-patches

On Tue, 8 Aug 2017, H.J. Lu wrote:

> 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.
> 
> OK for trunk?

I think the documentation for various options needs updating to clarify 
exactly what they mean.  (And potentially help text, which for driver 
options is in gcc.c:display_help with the common.opt text being ignored in 
that case.)

-static is no longer just "prevents linking with the shared libraries" as 
the documentation says, given it's also overriding (explicit or 
configure-time default) -pie.  -pie is no longer just "Produce a position 
independent executable", it's producing a *dynamically linked* PIE.

> +@item -static-pie
> +@opindex static-pie
> +Produce a static position independent executable on targets that support
> +it.  A static position independent executable is similar to static
> +executable, but can be loaded at any address without a dynamic linker.

"to a static executable".

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-28 16:59 ` Joseph Myers
@ 2017-08-28 18:25   ` H.J. Lu
  2017-09-12 16:20     ` Joseph Myers
  0 siblings, 1 reply; 11+ messages in thread
From: H.J. Lu @ 2017-08-28 18:25 UTC (permalink / raw)
  To: Joseph Myers; +Cc: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 1498 bytes --]

On Mon, Aug 28, 2017 at 9:10 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Tue, 8 Aug 2017, H.J. Lu wrote:
>
>> 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.
>>
>> OK for trunk?
>
> I think the documentation for various options needs updating to clarify
> exactly what they mean.  (And potentially help text, which for driver
> options is in gcc.c:display_help with the common.opt text being ignored in
> that case.)

Done.

> -static is no longer just "prevents linking with the shared libraries" as
> the documentation says, given it's also overriding (explicit or
> configure-time default) -pie.  -pie is no longer just "Produce a position
> independent executable", it's producing a *dynamically linked* PIE.

Done.

>> +@item -static-pie
>> +@opindex static-pie
>> +Produce a static position independent executable on targets that support
>> +it.  A static position independent executable is similar to static
>> +executable, but can be loaded at any address without a dynamic linker.
>
> "to a static executable".
>

Done.

Here is the updated patch.   OK for trunk?

Thanks.


-- 
H.J.

[-- Attachment #2: 0001-Add-static-pie-to-GCC-driver-to-create-static-PIE.patch --]
[-- Type: text/x-patch, Size: 12203 bytes --]

From da4fdd54c53cfafbd45c7a7fe996f907b6d141f4 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 20 Jul 2017 14:08:18 -0700
Subject: [PATCH] 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.
---
 gcc/common.opt               | 13 ++++++++++---
 gcc/config/gnu-user.h        | 15 ++++++++-------
 gcc/config/i386/gnu-user.h   |  7 ++++---
 gcc/config/i386/gnu-user64.h | 11 ++++++-----
 gcc/doc/invoke.texi          | 24 +++++++++++++++++-------
 gcc/gcc.c                    | 20 ++++++++++++--------
 6 files changed, 57 insertions(+), 33 deletions(-)

diff --git a/gcc/common.opt b/gcc/common.opt
index 1331008f811..fb88ed655fe 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -353,6 +353,9 @@ Common Alias(pedantic-errors)
 -pie
 Driver Alias(pie)
 
+-static-pie
+Driver Alias(static-pie)
+
 -pipe
 Driver Alias(pipe)
 
@@ -3062,7 +3065,7 @@ x
 Driver Joined Separate
 
 shared
-Driver RejectNegative Negative(pie)
+Driver RejectNegative Negative(static-pie)
 Create a shared library.
 
 shared-libgcc
@@ -3108,11 +3111,15 @@ Driver
 
 no-pie
 Driver RejectNegative Negative(shared)
-Don't create a position independent executable.
+Don't create a dynamically linked position independent executable.
 
 pie
 Driver RejectNegative Negative(no-pie)
-Create a position independent executable.
+Create a dynamically linked position independent executable.
+
+static-pie
+Driver RejectNegative Negative(pie)
+Create a static position independent executable.
 
 z
 Driver Joined Separate
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index de605b0c466..a967b69a350 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -53,11 +53,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   "%{shared:; \
      pg|p|profile:gcrt1.o%s; \
      static:crt1.o%s; \
-     " PIE_SPEC ":Scrt1.o%s; \
+     static-pie|" PIE_SPEC ":Scrt1.o%s; \
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|" PIE_SPEC ":crtbeginS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -70,7 +70,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      :crt1.o%s} \
    crti.o%s \
    %{static:crtbeginT.o%s; \
-     shared|pie:crtbeginS.o%s; \
+     shared|pie|static-pie:crtbeginS.o%s; \
      :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
@@ -92,7 +92,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|" PIE_SPEC ":crtendS.o%s; \
+     shared|static-pie|" PIE_SPEC ":crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -102,7 +102,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
    %{static:crtend.o%s; \
-     shared|pie:crtendS.o%s; \
+     shared|pie|static-pie:crtendS.o%s; \
      :crtend.o%s} \
    crtn.o%s \
    " CRTOFFLOADEND
@@ -132,12 +132,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define LIB_SPEC GNU_USER_TARGET_LIB_SPEC
 
 #if defined(HAVE_LD_EH_FRAME_HDR)
-#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
+#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
 #endif
 
 #undef LINK_GCC_C_SEQUENCE_SPEC
 #define LINK_GCC_C_SEQUENCE_SPEC \
-  "%{static:--start-group} %G %L %{static:--end-group}%{!static:%G}"
+  "%{static|static-pie:--start-group} %G %L \
+   %{static|static-pie:--end-group}%{!static:%{!static-pie:%G}}"
 
 /* Use --as-needed -lgcc_s for eh support.  */
 #ifdef HAVE_LD_AS_NEEDED
diff --git a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h
index a4c88f1a848..8983dc9ecd7 100644
--- a/gcc/config/i386/gnu-user.h
+++ b/gcc/config/i386/gnu-user.h
@@ -77,9 +77,10 @@ along with GCC; see the file COPYING3.  If not see
 #define GNU_USER_TARGET_LINK_SPEC "-m %(link_emulation) %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      -dynamic-linker %(dynamic_linker)} \
-      %{static:-static}}"
+      %{!static-pie: \
+	%{rdynamic:-export-dynamic} \
+	-dynamic-linker %(dynamic_linker)}} \
+      %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h
index 39f5ef6a68b..6fc9eae6f6b 100644
--- a/gcc/config/i386/gnu-user64.h
+++ b/gcc/config/i386/gnu-user64.h
@@ -59,11 +59,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   %{shared:-shared} \
   %{!shared: \
     %{!static: \
-      %{rdynamic:-export-dynamic} \
-      %{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
-      %{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
-      %{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}} \
-    %{static:-static}}"
+      %{!static-static: \
+	%{rdynamic:-export-dynamic} \
+	%{" SPEC_32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER32 "} \
+	%{" SPEC_64 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKER64 "} \
+	%{" SPEC_X32 ":-dynamic-linker " GNU_USER_DYNAMIC_LINKERX32 "}}} \
+    %{static:-static} %{static-pie:-static -pie --no-dynamic-linker -z text}}"
 
 #undef	LINK_SPEC
 #define LINK_SPEC GNU_USER_TARGET_LINK_SPEC
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 64363e54a00..cfb6481db9d 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -497,7 +497,7 @@ Objective-C and Objective-C++ Dialects}.
 @xref{Link Options,,Options for Linking}.
 @gccoptlist{@var{object-file-name}  -fuse-ld=@var{linker}  -l@var{library} @gol
 -nostartfiles  -nodefaultlibs  -nostdlib  -pie  -pthread  -rdynamic @gol
--s  -static  -static-libgcc  -static-libstdc++ @gol
+-s  -static -static-pie -static-libgcc  -static-libstdc++ @gol
 -static-libasan  -static-libtsan  -static-liblsan  -static-libubsan @gol
 -static-libmpx  -static-libmpxwrappers @gol
 -shared  -shared-libgcc  -symbolic @gol
@@ -11797,14 +11797,23 @@ GNU Compiler Collection (GCC) Internals}.)
 
 @item -pie
 @opindex pie
-Produce a position independent executable on targets that support it.
-For predictable results, you must also specify the same set of options
-used for compilation (@option{-fpie}, @option{-fPIE},
+Produce a dynamically linked position independent executable on targets
+that support it.  For predictable results, you must also specify the same
+set of options used for compilation (@option{-fpie}, @option{-fPIE},
 or model suboptions) when you specify this linker option.
 
 @item -no-pie
 @opindex no-pie
-Don't produce a position independent executable.
+Don't produce a dynamically linked position independent executable.
+
+@item -static-pie
+@opindex static-pie
+Produce a static position independent executable on targets that support
+it.  A static position independent executable is similar to a static
+executable, but can be loaded at any address without a dynamic linker.
+For predictable results, you must also specify the same set of options
+used for compilation (@option{-fpie}, @option{-fPIE}, or model
+suboptions) when you specify this linker option.
 
 @item -pthread
 @opindex pthread
@@ -11828,8 +11837,9 @@ Remove all symbol table and relocation information from the executable.
 
 @item -static
 @opindex static
-On systems that support dynamic linking, this prevents linking with the shared
-libraries.  On other systems, this option has no effect.
+On systems that support dynamic linking, this overrides @option{-pie}
+and prevents linking with the shared libraries.  On other systems, this
+option has no effect.
 
 @item -shared
 @opindex shared
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 6fa523140e7..e5d85f7f375 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1015,9 +1015,10 @@ proper position among the other output files.  */
 #endif
 
 /* -u* was put back because both BSD and SysV seem to support it.  */
-/* %{static|no-pie:} simply prevents an error message:
+/* %{static|no-pie|static-pie:} simply prevents an error message:
    1. If the target machine doesn't handle -static.
    2. If PIE isn't enabled by default.
+   3. If the target machine doesn't handle -static-pie.
  */
 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
    scripts which exist in user specified directories, or in standard
@@ -1035,7 +1036,7 @@ proper position among the other output files.  */
    "%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
    "%X %{o*} %{e*} %{N} %{n} %{r}\
     %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
-    %{static|no-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
+    %{static|no-pie|static-pie:} %{L*} %(mfwrap) %(link_libgcc) " \
     VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
     %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
 	%:include(libgomp.spec)%(link_gomp)}\
@@ -1670,17 +1671,19 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
 {
   char *buf;
 
-  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
-		"%{!static:%{!static-libgcc:"
 #if USE_LD_AS_NEEDED
+  buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:%{!static-pie:"
 		"%{!shared-libgcc:",
 		static_name, " " LD_AS_NEEDED_OPTION " ",
 		shared_name, " " LD_NO_AS_NEEDED_OPTION
 		"}"
 		"%{shared-libgcc:",
 		shared_name, "%{!shared: ", static_name, "}"
-		"}"
+		"}}"
 #else
+  buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
+		"%{!static:%{!static-libgcc:"
 		"%{!shared:"
 		"%{!shared-libgcc:", static_name, " ", eh_name, "}"
 		"%{shared-libgcc:", shared_name, " ", static_name, "}"
@@ -1788,8 +1791,8 @@ init_spec (void)
 			    "-lgcc_eh"
 #ifdef USE_LIBUNWIND_EXCEPTIONS
 # ifdef HAVE_LD_STATIC_DYNAMIC
-			    " %{!static:" LD_STATIC_OPTION "} -lunwind"
-			    " %{!static:" LD_DYNAMIC_OPTION "}"
+			    " %{!static:%{!static-pie:" LD_STATIC_OPTION "}} -lunwind"
+			    " %{!static:%{!static-pie:" LD_DYNAMIC_OPTION "}}"
 # else
 			    " -lunwind"
 # endif
@@ -3478,7 +3481,8 @@ display_help (void)
   fputs (_("  -S                       Compile only; do not assemble or link.\n"), stdout);
   fputs (_("  -c                       Compile and assemble, but do not link.\n"), stdout);
   fputs (_("  -o <file>                Place the output into <file>.\n"), stdout);
-  fputs (_("  -pie                     Create a position independent executable.\n"), stdout);
+  fputs (_("  -pie                     Create a dynamically linked position independent\n\
+                           executable.\n"), stdout);
   fputs (_("  -shared                  Create a shared library.\n"), stdout);
   fputs (_("\
   -x <language>            Specify the language of the following input files.\n\
-- 
2.13.5


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-08-28 18:25   ` H.J. Lu
@ 2017-09-12 16:20     ` Joseph Myers
  2017-09-12 18:48       ` Aaron Sawdey
  0 siblings, 1 reply; 11+ messages in thread
From: Joseph Myers @ 2017-09-12 16:20 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GCC Patches

On Mon, 28 Aug 2017, H.J. Lu wrote:

> Here is the updated patch.   OK for trunk?

OK.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-09-12 16:20     ` Joseph Myers
@ 2017-09-12 18:48       ` Aaron Sawdey
  2017-09-12 19:23         ` H.J. Lu
  2017-09-13  6:57         ` Markus Trippelsdorf
  0 siblings, 2 replies; 11+ messages in thread
From: Aaron Sawdey @ 2017-09-12 18:48 UTC (permalink / raw)
  To: Joseph Myers, H.J. Lu; +Cc: GCC Patches, Segher Boessenkool

On Tue, 2017-09-12 at 16:20 +0000, Joseph Myers wrote:
> On Mon, 28 Aug 2017, H.J. Lu wrote:
> 
> > Here is the updated patch.   OK for trunk?
> 
> OK.

This seems to be causing an issue for me on powerpc:

/home/sawdey/src/gcc/trunk/build/./prev-gcc/xg++ -B/home/sawdey/src/gcc/trunk/build/./prev-gcc/ -B/home/sawdey/install/gcc/trunk_str/powerpc64le-unknown-linux-gnu/bin/ -nostdinc++ -B/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/src/.libs -B/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/libsupc++/.libs  -I/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/include/powerpc64le-unknown-linux-gnu  -I/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/include  -I/home/sawdey/src/gcc/trunk/trunk/libstdc++-v3/libsupc++ -L/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/src/.libs -L/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/libsupc++/.libs -c   -g -O2 -gtoggle -DIN_GCC     -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H -DGENERATOR_FILE -fno-PIE -I. -Ibuild -I../../trunk/gcc -I../../trunk/gcc/build -I../../trunk/gcc/../include  -I../../trunk/gcc/../libcpp/include  \
                -o build/gencheck.o ../../trunk/gcc/gencheck.c
In file included from ./tm.h:30:0,
                 from ../../trunk/gcc/gencheck.c:23:
../../trunk/gcc/config/rs6000/sysv4.h:819:0: error: "LINK_EH_SPEC" redefined [-Werror]
 # define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
 
In file included from ./tm.h:28:0,
                 from ../../trunk/gcc/gencheck.c:23:
../../trunk/gcc/config/gnu-user.h:135:0: note: this is the location of the previous definition
 #define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "

I don't see the problem on 252033.

Thanks,
   Aaron

-- 
Aaron Sawdey, Ph.D.  acsawdey@linux.vnet.ibm.com
050-2/C113  (507) 253-7520 home: 507/263-0782
IBM Linux Technology Center - PPC Toolchain

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-09-12 18:48       ` Aaron Sawdey
@ 2017-09-12 19:23         ` H.J. Lu
  2017-09-13  6:57         ` Markus Trippelsdorf
  1 sibling, 0 replies; 11+ messages in thread
From: H.J. Lu @ 2017-09-12 19:23 UTC (permalink / raw)
  To: Aaron Sawdey; +Cc: Joseph Myers, GCC Patches, Segher Boessenkool

On Tue, Sep 12, 2017 at 11:48 AM, Aaron Sawdey
<acsawdey@linux.vnet.ibm.com> wrote:
> On Tue, 2017-09-12 at 16:20 +0000, Joseph Myers wrote:
>> On Mon, 28 Aug 2017, H.J. Lu wrote:
>>
>> > Here is the updated patch.   OK for trunk?
>>
>> OK.
>
> This seems to be causing an issue for me on powerpc:
>
> /home/sawdey/src/gcc/trunk/build/./prev-gcc/xg++ -B/home/sawdey/src/gcc/trunk/build/./prev-gcc/ -B/home/sawdey/install/gcc/trunk_str/powerpc64le-unknown-linux-gnu/bin/ -nostdinc++ -B/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/src/.libs -B/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/libsupc++/.libs  -I/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/include/powerpc64le-unknown-linux-gnu  -I/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/include  -I/home/sawdey/src/gcc/trunk/trunk/libstdc++-v3/libsupc++ -L/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/src/.libs -L/home/sawdey/src/gcc/trunk/build/prev-powerpc64le-unknown-linux-gnu/libstdc++-v3/libsupc++/.libs -c   -g -O2 -gtoggle -DIN_GCC     -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings -Werror -fno-common  -DHAVE_CONFIG_H -DGENERATOR_FILE -fno-PIE -I. -Ibuild -I../../trunk/gcc -I../../trunk/gcc/build -I../../trunk/gcc/../include  -I../../trunk/gcc/../libcpp/include  \
>                 -o build/gencheck.o ../../trunk/gcc/gencheck.c
> In file included from ./tm.h:30:0,
>                  from ../../trunk/gcc/gencheck.c:23:
> ../../trunk/gcc/config/rs6000/sysv4.h:819:0: error: "LINK_EH_SPEC" redefined [-Werror]
>  # define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
>
> In file included from ./tm.h:28:0,
>                  from ../../trunk/gcc/gencheck.c:23:
> ../../trunk/gcc/config/gnu-user.h:135:0: note: this is the location of the previous definition
>  #define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
>
> I don't see the problem on 252033.
>

There are several problems in rs6000/sysv4.h:

1. It doesn't undef LINK_EH_SPEC before define it.
2. It shouldn't define it for Linux since it has been defined in
gnu-user.h.   You didn't see the problem since there were the
same before my patch.

Can you not define LINK_EH_SPEC for Linux targets?


-- 
H.J.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-09-12 18:48       ` Aaron Sawdey
  2017-09-12 19:23         ` H.J. Lu
@ 2017-09-13  6:57         ` Markus Trippelsdorf
  2017-09-13  9:12           ` Jakub Jelinek
  1 sibling, 1 reply; 11+ messages in thread
From: Markus Trippelsdorf @ 2017-09-13  6:57 UTC (permalink / raw)
  To: Aaron Sawdey; +Cc: Joseph Myers, H.J. Lu, GCC Patches, Segher Boessenkool

On 2017.09.12 at 13:48 -0500, Aaron Sawdey wrote:
> On Tue, 2017-09-12 at 16:20 +0000, Joseph Myers wrote:
> > On Mon, 28 Aug 2017, H.J. Lu wrote:
> > 
> > > Here is the updated patch.   OK for trunk?
> > 
> > OK.
> 
> This seems to be causing an issue for me on powerpc:
> 
> ../../trunk/gcc/config/rs6000/sysv4.h:819:0: error: "LINK_EH_SPEC" redefined [-Werror]
>  # define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "

It will cause problems on other platforms as well:

gcc/config/alpha/elf.h:171:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/alpha/vms.h:209:#define LINK_EH_SPEC "vms-dwarf2eh.o%s "
gcc/config/dragonfly.h:64:#define LINK_EH_SPEC "--eh-frame-hdr"
gcc/config/freebsd.h:48:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/gnu-user.h:135:#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
gcc/config/netbsd.h:128:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/openbsd.h:139:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/powerpcspe/sysv4.h:808:# define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/rs6000/sysv4.h:819:# define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
gcc/config/sol2.h:375:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "


-- 
Markus

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add -static-pie to GCC driver to create static PIE
  2017-09-13  6:57         ` Markus Trippelsdorf
@ 2017-09-13  9:12           ` Jakub Jelinek
  0 siblings, 0 replies; 11+ messages in thread
From: Jakub Jelinek @ 2017-09-13  9:12 UTC (permalink / raw)
  To: Markus Trippelsdorf
  Cc: Aaron Sawdey, Joseph Myers, H.J. Lu, GCC Patches, Segher Boessenkool

On Wed, Sep 13, 2017 at 08:57:39AM +0200, Markus Trippelsdorf wrote:
> On 2017.09.12 at 13:48 -0500, Aaron Sawdey wrote:
> > On Tue, 2017-09-12 at 16:20 +0000, Joseph Myers wrote:
> > > On Mon, 28 Aug 2017, H.J. Lu wrote:
> > > 
> > > > Here is the updated patch.   OK for trunk?
> > > 
> > > OK.
> > 
> > This seems to be causing an issue for me on powerpc:
> > 
> > ../../trunk/gcc/config/rs6000/sysv4.h:819:0: error: "LINK_EH_SPEC" redefined [-Werror]
> >  # define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> 
> It will cause problems on other platforms as well:
> 
> gcc/config/alpha/elf.h:171:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/alpha/vms.h:209:#define LINK_EH_SPEC "vms-dwarf2eh.o%s "
> gcc/config/dragonfly.h:64:#define LINK_EH_SPEC "--eh-frame-hdr"
> gcc/config/freebsd.h:48:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/gnu-user.h:135:#define LINK_EH_SPEC "%{!static|static-pie:--eh-frame-hdr} "
> gcc/config/netbsd.h:128:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/openbsd.h:139:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/powerpcspe/sysv4.h:808:# define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/rs6000/sysv4.h:819:# define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "
> gcc/config/sol2.h:375:#define LINK_EH_SPEC "%{!static:--eh-frame-hdr} "

Most of these aren't including gnu-user.h and then the other header.
It is only rs6000/sysv4.h and powerpcspe/sysv4.h where gnu-user.h is
(sometimes, which is why you can't remove it from there altogether)
included first, and alpha/elf.h where gnu-user.h is not included, yet it is
a linux target where you probably want to handle it similarly.
Although, perhaps -static-pie should be supported by other ELF targets like
Solaris, NetBSD, OpenBSD, FreeBSD and therefore the other LINK_EH_SPEC that
have !static:--eh-frame-hdr should be tweaked too.

I'd say the urgent thing is to patch rs6000/sysv4.h to match the new
gnu-user.h definition.  Reorganizing headers so that for linux and hurd
it is solely gnu-user.h that defines this kind of macros is lots of work.

	Jakub

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-09-13  9:12 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-08 22:19 [PATCH] Add -static-pie to GCC driver to create static PIE H.J. Lu
2017-08-09  5:36 ` Richard Biener
2017-08-09 10:39   ` H.J. Lu
2017-08-24 21:04     ` H.J. Lu
2017-08-28 16:59 ` Joseph Myers
2017-08-28 18:25   ` H.J. Lu
2017-09-12 16:20     ` Joseph Myers
2017-09-12 18:48       ` Aaron Sawdey
2017-09-12 19:23         ` H.J. Lu
2017-09-13  6:57         ` Markus Trippelsdorf
2017-09-13  9:12           ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).