This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
On Tue, Jul 18, 2017 at 10:11 PM, Alan Modra <amodra@gmail.com> wrote:
> On Tue, Jul 18, 2017 at 07:49:48AM -0700, H.J. Lu wrote:
>> The difference is with --enable-default-pie, the gcc driver doesn't pass
>> both -pie and -static ld when "-static -pie" is used. Does your change
>> pass both -pie and -static ld when "-static -pie" is used?
>
> Again, as I said in the original post: "In both cases you now will
> have -static completely overriding -pie".
>
> That means "gcc -pie -static" and "gcc -static -pie" just pass
> "-static" to ld, and select the appropriate startup files for a static
> executable, when configured with --disable-default-pie. Which is what
> happens currently for --enable-default-pie.
>
> None of this is rocket science. I know what I'm doing where the
> linker and startup files are concerned, and I'm comfortable with the
> gcc specs language. The patch is simple! It should be easy to
> review, except for trying to understand the "-" lines. Yet it has sat
> unreviewed for nearly four weeks. And it fixes a powerpc
> --enable-default-pie bootstrap failure (pr81295).
>
> Joseph, would you please take a look?
> https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01678.html
>
> I know there is more to do in this area, for example, it seems to me
> that the HAVE_LD_PIE definition of GNU_USER_TARGET_STARTFILE_SPEC is
> good for !HAVE_LD_PIE, and similarly for GNU_USE_TARGET_ENDFILE_SPEC.
> And yes, I propagated that duplication into rs6000/sysv4.h, which
> needs some serious tidying. rs6000/sysv4.h linux support ought to be
> using the gnu-user.h defines rather than copying them, something I've
> told Segher I'll look at after this patch goes in.
>
Before your patch, I can creating static PIE when GCC is configured
with --disable-default-pie. After your change, both --enable-default-pie
and --disable-default-pie prevent creating static PIE. I opened a bug
report for -static-pie:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81498
This patch, which should be applied on top of your patch, adds support
for -static-pie. Can we merge 2 patches into 1?
Thanks.
--
H.J.
From b65e6477a8c41c7c8b4602c22e92b7b8b10960ab 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 for static PIE
A static position independent executable 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 passed to linker to prevent dynamic relocations in readonly 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_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 (init_gcc_specs): Likewise.
* doc/invoke.texi: Document -static-pie.
---
gcc/common.opt | 9 ++++++++-
gcc/config/gnu-user.h | 13 +++++++------
gcc/config/i386/gnu-user.h | 7 ++++---
gcc/config/i386/gnu-user64.h | 11 ++++++-----
gcc/doc/invoke.texi | 9 ++++++++-
gcc/gcc.c | 6 +++---
6 files changed, 36 insertions(+), 19 deletions(-)
diff --git a/gcc/common.opt b/gcc/common.opt
index e81165c488b..4ada85a65a0 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -346,6 +346,9 @@ Common Alias(pedantic-errors)
-pie
Driver Alias(pie)
+-static-pie
+Driver Alias(static-pie)
+
-pipe
Driver Alias(pipe)
@@ -3050,7 +3053,7 @@ x
Driver Joined Separate
shared
-Driver RejectNegative Negative(pie)
+Driver RejectNegative Negative(static-pie)
Create a shared library.
shared-libgcc
@@ -3102,6 +3105,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..f0cbde3343c 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
@@ -137,7 +137,8 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
#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 dfde403cbf2..027d9a97d6b 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
@@ -11746,6 +11746,13 @@ 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. 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 80a8b8b2bee..ffd6e42114e 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -1668,8 +1668,8 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
{
char *buf;
- buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
- "%{!static:%{!static-libgcc:"
+ buf = concat ("%{static|static-libgcc|static-pie:", static_name, " ", eh_name, "}"
+ "%{!static:%{!static-libgcc:%{!static-pie:"
#if USE_LD_AS_NEEDED
"%{!shared-libgcc:",
static_name, " " LD_AS_NEEDED_OPTION " ",
@@ -1677,7 +1677,7 @@ init_gcc_specs (struct obstack *obstack, const char *shared_name,
"}"
"%{shared-libgcc:",
shared_name, "%{!shared: ", static_name, "}"
- "}"
+ "}" "}"
#else
"%{!shared:"
"%{!shared-libgcc:", static_name, " ", eh_name, "}"
--
2.13.3
- References:
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170
- Re: [PATCH] Fix pr80044, -static and -pie insanity, and pr81170