diff mbox

PING^3: [PATCH]: New configure options that make the compiler use -fPIE and -pie as default option

Message ID CAMe9rOrcADb1t2Ch0LDy9SOaS9RDoj4BGgo8qL+6jtg1tnLA8g@mail.gmail.com
State New
Headers show

Commit Message

H.J. Lu May 8, 2015, 5:35 p.m. UTC
On Thu, May 7, 2015 at 2:17 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Fri, 6 Mar 2015, H.J. Lu wrote:
>
>> +# We don't want to compile the compiler with -fPIE, it make PCH fail.
>> +COMPILER += @NO_PIE_CFLAGS@
>> +
>> +# Link with -no-pie since we compile the compiler with -fno-PIE.
>> +LINKER += @NO_PIE_FLAG@
>
> As I understand it, what we don't want is the compiler to be a PIE.  That
> is, it must be linked -no-pie (and given that the compiler is not a PIE,
> compiling -fPIE would be pointless, although it wouldn't actually break
> things to have PIE objects in the compiler as long as it's linked for a
> fixed address).
>
>> +#if defined ENABLE_DEFAULT_PIE
>> +#define GNU_USER_TARGET_STARTFILE_SPEC \
>> +  "%{!shared: %{pg|p|profile:gcrt1.o%s;: \
>> +    %{" PIE_SPEC ":Scrt1.o%s} %{" NO_PIE_SPEC ":crt1.o%s}}} \
>> +   crti.o%s %{static:crtbeginT.o%s;: %{shared:crtbeginS.o%s} \
>> +           %{" PIE_SPEC ":crtbeginS.o%s} \
>> +           %{" NO_PIE_SPEC ":crtbegin.o%s}}" \
>> +   FVTABLE_VERIFY_SPEC
>> +#else
>> +#define GNU_USER_TARGET_STARTFILE_SPEC \
>> +  "%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
>> +   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}" \
>> +   FVTABLE_VERIFY_SPEC
>> +#endif
>
> With appropriate definitions of PIE_SPEC and NO_PIE_SPEC, shouldn't a
> single definition of GNU_USER_TARGET_STARTFILE_SPEC be able to work for
> both ENABLE_DEFAULT_PIE and !ENABLE_DEFAULT_PIE?

Yes.

> <https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00393.html> noted a
> possible issue with MIPS.  Actually, rather more config/*.h and
> config/*/*.h headers contain specs testing for (-fpie, -fPIE, -fno-pie,
> -fno-PIE, -pie) options, which would be affected by these changes.  I'd
> say this patch should include an initial attempt at adjusting those config
> headers, which should be an essentially mechanical change not requiring
> understanding anything target-specific.  For link-time specs, that may
> mean using PIE_SPEC and NO_PIE_SPEC.  For compile-time specs, similar new
> macros would be added.  Given such adjustments included in the patch and
> the relevant target maintainers CC:ed, I might then be inclined to approve
> the patch on the basis of allowing a week for target maintainers to test
> the changes for their targets before commit, as I don't see any major
> problems with it beyond the need to update the target-specific specs.
>

Here is the updated patch.  I will post patches for cris, mips, powerpc
and sparc separately.  The target maintainers should be able to adjust
backend ASM_SPEC with FPIE_OR_FPIC_SPEC and
NO_FPIE_AND_FPIC_SPEC.

OK for trunk?

Thanks.

Comments

Magnus Granberg May 18, 2015, 10:24 p.m. UTC | #1
fredag 08 maj 2015 10.35.44 skrev  H.J. Lu:
> On Thu, May 7, 2015 at 2:17 PM, Joseph Myers <joseph@codesourcery.com> 
wrote:
> > On Fri, 6 Mar 2015, H.J. Lu wrote:
> >> +# We don't want to compile the compiler with -fPIE, it make PCH fail.
> >> +COMPILER += @NO_PIE_CFLAGS@
> >> +
> >> +# Link with -no-pie since we compile the compiler with -fno-PIE.
> >> +LINKER += @NO_PIE_FLAG@
> > 
> > As I understand it, what we don't want is the compiler to be a PIE.  That
> > is, it must be linked -no-pie (and given that the compiler is not a PIE,
> > compiling -fPIE would be pointless, although it wouldn't actually break
> > things to have PIE objects in the compiler as long as it's linked for a
> > fixed address).
> > 
> >> +#if defined ENABLE_DEFAULT_PIE
> >> +#define GNU_USER_TARGET_STARTFILE_SPEC \
> >> +  "%{!shared: %{pg|p|profile:gcrt1.o%s;: \
> >> +    %{" PIE_SPEC ":Scrt1.o%s} %{" NO_PIE_SPEC ":crt1.o%s}}} \
> >> +   crti.o%s %{static:crtbeginT.o%s;: %{shared:crtbeginS.o%s} \
> >> +           %{" PIE_SPEC ":crtbeginS.o%s} \
> >> +           %{" NO_PIE_SPEC ":crtbegin.o%s}}" \
> >> +   FVTABLE_VERIFY_SPEC
> >> +#else
> >> +#define GNU_USER_TARGET_STARTFILE_SPEC \
> >> +  "%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
> >> +   crti.o%s
> >> %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s}" \ +  
> >> FVTABLE_VERIFY_SPEC
> >> +#endif
> > 
> > With appropriate definitions of PIE_SPEC and NO_PIE_SPEC, shouldn't a
> > single definition of GNU_USER_TARGET_STARTFILE_SPEC be able to work for
> > both ENABLE_DEFAULT_PIE and !ENABLE_DEFAULT_PIE?
> 
> Yes.
> 
> > <https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00393.html> noted a
> > possible issue with MIPS.  Actually, rather more config/*.h and
> > config/*/*.h headers contain specs testing for (-fpie, -fPIE, -fno-pie,
> > -fno-PIE, -pie) options, which would be affected by these changes.  I'd
> > say this patch should include an initial attempt at adjusting those config
> > headers, which should be an essentially mechanical change not requiring
> > understanding anything target-specific.  For link-time specs, that may
> > mean using PIE_SPEC and NO_PIE_SPEC.  For compile-time specs, similar new
> > macros would be added.  Given such adjustments included in the patch and
> > the relevant target maintainers CC:ed, I might then be inclined to approve
> > the patch on the basis of allowing a week for target maintainers to test
> > the changes for their targets before commit, as I don't see any major
> > problems with it beyond the need to update the target-specific specs.
> 
> Here is the updated patch.  I will post patches for cris, mips, powerpc
> and sparc separately.  The target maintainers should be able to adjust
> backend ASM_SPEC with FPIE_OR_FPIC_SPEC and
> NO_FPIE_AND_FPIC_SPEC.
> 
> OK for trunk?
> 
> Thanks.
PIng

Any progress on this?

/Magnus G.
Joseph Myers May 19, 2015, 1:11 a.m. UTC | #2
On Tue, 19 May 2015, Magnus Granberg wrote:

> > > <https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00393.html> noted a
> > > possible issue with MIPS.  Actually, rather more config/*.h and
> > > config/*/*.h headers contain specs testing for (-fpie, -fPIE, -fno-pie,
> > > -fno-PIE, -pie) options, which would be affected by these changes.  I'd
> > > say this patch should include an initial attempt at adjusting those config
> > > headers, which should be an essentially mechanical change not requiring
> > > understanding anything target-specific.  For link-time specs, that may
> > > mean using PIE_SPEC and NO_PIE_SPEC.  For compile-time specs, similar new
> > > macros would be added.  Given such adjustments included in the patch and
> > > the relevant target maintainers CC:ed, I might then be inclined to approve
> > > the patch on the basis of allowing a week for target maintainers to test
> > > the changes for their targets before commit, as I don't see any major
> > > problems with it beyond the need to update the target-specific specs.
> > 
> > Here is the updated patch.  I will post patches for cris, mips, powerpc
> > and sparc separately.  The target maintainers should be able to adjust
> > backend ASM_SPEC with FPIE_OR_FPIC_SPEC and
> > NO_FPIE_AND_FPIC_SPEC.
> > 
> > OK for trunk?
> > 
> > Thanks.
> PIng
> 
> Any progress on this?

Have updates for all affected specs for all targets been posted?  I just 
saw a small and apparently arbitrary subset of targets with patches, and 
no explanation of how those targets were identified or why the other 
targets with specs mentioning the options in question did not need 
updates.
H.J. Lu May 19, 2015, 1:25 a.m. UTC | #3
On Mon, May 18, 2015 at 6:11 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Tue, 19 May 2015, Magnus Granberg wrote:
>
>> > > <https://gcc.gnu.org/ml/gcc-patches/2015-03/msg00393.html> noted a
>> > > possible issue with MIPS.  Actually, rather more config/*.h and
>> > > config/*/*.h headers contain specs testing for (-fpie, -fPIE, -fno-pie,
>> > > -fno-PIE, -pie) options, which would be affected by these changes.  I'd
>> > > say this patch should include an initial attempt at adjusting those config
>> > > headers, which should be an essentially mechanical change not requiring
>> > > understanding anything target-specific.  For link-time specs, that may
>> > > mean using PIE_SPEC and NO_PIE_SPEC.  For compile-time specs, similar new
>> > > macros would be added.  Given such adjustments included in the patch and
>> > > the relevant target maintainers CC:ed, I might then be inclined to approve
>> > > the patch on the basis of allowing a week for target maintainers to test
>> > > the changes for their targets before commit, as I don't see any major
>> > > problems with it beyond the need to update the target-specific specs.
>> >
>> > Here is the updated patch.  I will post patches for cris, mips, powerpc
>> > and sparc separately.  The target maintainers should be able to adjust
>> > backend ASM_SPEC with FPIE_OR_FPIC_SPEC and
>> > NO_FPIE_AND_FPIC_SPEC.
>> >
>> > OK for trunk?
>> >
>> > Thanks.
>> PIng
>>
>> Any progress on this?
>
> Have updates for all affected specs for all targets been posted?  I just
> saw a small and apparently arbitrary subset of targets with patches, and
> no explanation of how those targets were identified or why the other
> targets with specs mentioning the options in question did not need
> updates.
>

I only posted patches for an  arbitrary subset of targets because

1. Not everyone is interested in --enable-default-pie.
2. I can't tests all targets myself.

If patches for all targets is the only blocker before the patch
will be approved or target maintainers will help me test the patch,
I will post patches for each target affected.
Joseph Myers May 19, 2015, 3:21 p.m. UTC | #4
On Mon, 18 May 2015, H.J. Lu wrote:

> > Have updates for all affected specs for all targets been posted?  I just
> > saw a small and apparently arbitrary subset of targets with patches, and
> > no explanation of how those targets were identified or why the other
> > targets with specs mentioning the options in question did not need
> > updates.
> >
> 
> I only posted patches for an  arbitrary subset of targets because
> 
> 1. Not everyone is interested in --enable-default-pie.
> 2. I can't tests all targets myself.
> 
> If patches for all targets is the only blocker before the patch
> will be approved or target maintainers will help me test the patch,
> I will post patches for each target affected.

I think the whole thing should be posted as one patch, with both the 
target-independent changes and the target-specific changes for all 
targets.
H.J. Lu May 19, 2015, 3:27 p.m. UTC | #5
On Tue, May 19, 2015 at 8:21 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Mon, 18 May 2015, H.J. Lu wrote:
>
>> > Have updates for all affected specs for all targets been posted?  I just
>> > saw a small and apparently arbitrary subset of targets with patches, and
>> > no explanation of how those targets were identified or why the other
>> > targets with specs mentioning the options in question did not need
>> > updates.
>> >
>>
>> I only posted patches for an  arbitrary subset of targets because
>>
>> 1. Not everyone is interested in --enable-default-pie.
>> 2. I can't tests all targets myself.
>>
>> If patches for all targets is the only blocker before the patch
>> will be approved or target maintainers will help me test the patch,
>> I will post patches for each target affected.
>
> I think the whole thing should be posted as one patch, with both the
> target-independent changes and the target-specific changes for all
> targets.
>

That is what makes me concerned.  I have some simple target-specified
patches which weren't reviewed for years. What will happen if no one
reviews some simple target-specified changes due to

1. Reviewers don't have access to those targets.
2. Target maintainers aren't review them.
3. There are no clear maintainers for those targets.

As the result, my patch may go nowhere.
Paul Koning May 19, 2015, 3:33 p.m. UTC | #6

Joseph Myers May 19, 2015, 3:33 p.m. UTC | #7
On Tue, 19 May 2015, H.J. Lu wrote:

> > I think the whole thing should be posted as one patch, with both the
> > target-independent changes and the target-specific changes for all
> > targets.
> >
> 
> That is what makes me concerned.  I have some simple target-specified
> patches which weren't reviewed for years. What will happen if no one

For any unreviewed patch, keep pinging weekly.

> reviews some simple target-specified changes due to
> 
> 1. Reviewers don't have access to those targets.
> 2. Target maintainers aren't review them.
> 3. There are no clear maintainers for those targets.

I've already said in 
<https://gcc.gnu.org/ml/gcc-patches/2015-05/msg00593.html> that, given 
target maintainers CC:ed, I might be inclined to approve the patch on the 
basis of allowing them a week to test their target changes.
H.J. Lu May 19, 2015, 3:37 p.m. UTC | #8
On Tue, May 19, 2015 at 8:33 AM,  <Paul_Koning@dell.com> wrote:
>
> ________________________________________
> From: gcc-patches-owner@gcc.gnu.org [gcc-patches-owner@gcc.gnu.org] on behalf of H.J. Lu [hjl.tools@gmail.com]
> Sent: Tuesday, May 19, 2015 11:27 AM
> To: Joseph Myers
> Cc: Magnus Granberg; GCC Patches
> Subject: Re: PING^3: [PATCH]: New configure options that make the compiler use -fPIE and -pie as default option
>
> On Tue, May 19, 2015 at 8:21 AM, Joseph Myers <joseph@codesourcery.com> wrote:
>> ...
>> I think the whole thing should be posted as one patch, with both the
>> target-independent changes and the target-specific changes for all
>> targets.
>>
>
> That is what makes me concerned.  I have some simple target-specified
> patches which weren't reviewed for years. What will happen if no one
> reviews some simple target-specified changes due to
>
> 1. Reviewers don't have access to those targets.
> 2. Target maintainers aren't review them.
> 3. There are no clear maintainers for those targets.
>
> As the result, my patch may go nowhere.
> ---
>
> But that hasn't stopped others from posting patches like that, or getting them approved.  And we also have global maintainers who can approve things.  It feels a bit like a hypothetical issue is being used as a reason to do part of the job.

It is not hypothetical.  See:

https://gcc.gnu.org/ml/gcc-patches/2012-09/msg01558.html

It happened before.  I don't want to make it harder for global maintainers
to review a patch which has zero-impact on a target if --enable-default-pie
isn't used.
Matthew Fortune May 26, 2015, 10:11 p.m. UTC | #9
The change for MIPS looks fine by visual inspection and I've built both
a default pie and default no-pie compiler. The default pie won't build
glibc but I am pretty sure it is not down to this patch. I haven't had
time to look into why it won't build though, something related to
selecting the CRT files.

Thanks,
Matthew
H.J. Lu May 26, 2015, 10:53 p.m. UTC | #10
On Tue, May 26, 2015 at 3:11 PM, Matthew Fortune
<Matthew.Fortune@imgtec.com> wrote:
> The change for MIPS looks fine by visual inspection and I've built both
> a default pie and default no-pie compiler. The default pie won't build
> glibc but I am pretty sure it is not down to this patch. I haven't had
> time to look into why it won't build though, something related to
> selecting the CRT files.

It is:

https://sourceware.org/bugzilla/show_bug.cgi?id=17841

I will submit my glibc patch after GCC change is checked in.
diff mbox

Patch

From 57e2d527af4891a4bf05b57b01d9ac97d336e959 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 6 Mar 2015 09:07:54 -0800
Subject: [PATCH 1/5] Add --enable-default-pie option to GCC configure

Add --enable-default-pie option to configure GCC to generate PIE by
default.

gcc/

2015-03-06  Magnus Granberg  <zorry@gentoo.org>
	    H.J. Lu  <hongjiu.lu@intel.com>

	* Makefile.in (COMPILER): Add @NO_PIE_CFLAGS@.
	(BUILD_CFLAGS): Likewise.
	(BUILD_CXXFLAGS): Likewise.
	(LINKER): Add @NO_PIE_FLAG@.
	(BUILD_LDFLAGS): Likewise.
	(libgcc.mvars): Set NO_PIE_CFLAGS to -fno-PIE for
	--enable-default-pie.
	* common.opt (fPIE): Initialize to -1.
	(fpie): Likewise.
	(no-pie): New option.
	(pie): Replace "Negative(shared)" with "Negative(no-pie)".
	* configure.ac: Add --enable-default-pie.
	(NO_PIE_CFLAGS): New.  Check if -fno-PIE works.  AC_SUBST.
	(NO_PIE_FLAG): New.  Check if -no-pie works.  AC_SUBST.
	* defaults.h (DEFAULT_FLAG_PIE): New.  Default PIE to -fPIE.
	* gcc.c (NO_PIE_SPEC): New.
	(PIE_SPEC): Likewise.
	(NO_FPIE_SPEC): Likewise.
	(FPIE_SPEC): Likewise.
	(NO_FPIE_AND_FPIC_SPEC): Likewise.
	(FPIE_OR_FPIC_SPEC): Likewise.
	(LD_PIE_SPEC): Likewise.
	(LINK_PIE_SPEC): Handle -no-pie.  Use PIE_SPEC and LD_PIE_SPEC.
	* opts.c (DEFAULT_FLAG_PIE): New.  Set to 0 if ENABLE_DEFAULT_PIE
	is undefined.
	(finish_options): Update opts->x_flag_pie if it is -1.
	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Use
	PIE_SPEC and NO_PIE_SPEC if HAVE_LD_PIE is defined.
	* doc/install.texi: Document --enable-default-pie.
	* doc/invoke.texi: Document -no-pie.
	* config.in: Regenerated.
	* configure: Likewise.

gcc/ada/

2015-03-06  H.J. Lu  <hongjiu.lu@intel.com>

	* gcc-interface/Makefile.in (TOOLS_LIBS): Add @NO_PIE_FLAG@.

libgcc/

2015-03-06  H.J. Lu  <hongjiu.lu@intel.com>

	* Makefile.in (CRTSTUFF_CFLAGS): Add $(NO_PIE_CFLAGS).
---
 gcc/Makefile.in                   | 15 ++++++++
 gcc/ada/gcc-interface/Makefile.in |  3 ++
 gcc/common.opt                    | 10 +++--
 gcc/config.in                     |  6 +++
 gcc/config/gnu-user.h             |  7 +++-
 gcc/configure                     | 79 ++++++++++++++++++++++++++++++++++++++-
 gcc/configure.ac                  | 40 ++++++++++++++++++++
 gcc/defaults.h                    |  5 +++
 gcc/doc/install.texi              |  3 ++
 gcc/doc/invoke.texi               |  4 ++
 gcc/gcc.c                         | 21 ++++++++++-
 gcc/opts.c                        | 14 +++++++
 libgcc/Makefile.in                |  2 +-
 13 files changed, 199 insertions(+), 10 deletions(-)

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 652b3e71d..26faa2b 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -252,6 +252,12 @@  LINKER = $(CC)
 LINKER_FLAGS = $(CFLAGS)
 endif
 
+# We don't want to compile the compiler with -fPIE, it make PCH fail.
+COMPILER += @NO_PIE_CFLAGS@
+
+# Link with -no-pie since we compile the compiler with -fno-PIE.
+LINKER += @NO_PIE_FLAG@
+
 # Like LINKER, but use a mutex for serializing front end links.
 ifeq (@DO_LINK_MUTEX@,true)
 LLINKER = $(SHELL) $(srcdir)/lock-and-run.sh linkfe.lck $(LINKER)
@@ -749,6 +755,8 @@  CC_FOR_BUILD = @CC_FOR_BUILD@
 CXX_FOR_BUILD = @CXX_FOR_BUILD@
 BUILD_CFLAGS= @BUILD_CFLAGS@ -DGENERATOR_FILE
 BUILD_CXXFLAGS = @BUILD_CXXFLAGS@ -DGENERATOR_FILE
+BUILD_CFLAGS += @NO_PIE_CFLAGS@
+BUILD_CXXFLAGS += @NO_PIE_CFLAGS@
 
 # Native compiler that we use.  This may be C++ some day.
 COMPILER_FOR_BUILD = $(CXX_FOR_BUILD)
@@ -760,6 +768,7 @@  BUILD_LINKERFLAGS = $(BUILD_CXXFLAGS)
 
 # Native linker and preprocessor flags.  For x-fragment overrides.
 BUILD_LDFLAGS=@BUILD_LDFLAGS@
+BUILD_LDFLAGS += @NO_PIE_FLAG@
 BUILD_CPPFLAGS= -I. -I$(@D) -I$(srcdir) -I$(srcdir)/$(@D) \
 		-I$(srcdir)/../include @INCINTL@ $(CPPINC) $(CPPFLAGS)
 
@@ -1863,6 +1872,12 @@  libgcc.mvars: config.status Makefile specs xgcc$(exeext)
 	echo GCC_CFLAGS = '$(GCC_CFLAGS)' >> tmp-libgcc.mvars
 	echo INHIBIT_LIBC_CFLAGS = '$(INHIBIT_LIBC_CFLAGS)' >> tmp-libgcc.mvars
 	echo TARGET_SYSTEM_ROOT = '$(TARGET_SYSTEM_ROOT)' >> tmp-libgcc.mvars
+	if test @enable_default_pie@ = yes; then \
+	  NO_PIE_CFLAGS="-fno-PIE"; \
+	else \
+	  NO_PIE_CFLAGS=; \
+	fi; \
+	echo NO_PIE_CFLAGS = "$$NO_PIE_CFLAGS" >> tmp-libgcc.mvars
 
 	mv tmp-libgcc.mvars libgcc.mvars
 
diff --git a/gcc/ada/gcc-interface/Makefile.in b/gcc/ada/gcc-interface/Makefile.in
index ecc443e..90aedb5 100644
--- a/gcc/ada/gcc-interface/Makefile.in
+++ b/gcc/ada/gcc-interface/Makefile.in
@@ -267,6 +267,9 @@  TOOLS_LIBS = ../link.o ../targext.o ../../ggc-none.o ../../libcommon-target.a \
   ../../libcommon.a ../../../libcpp/libcpp.a $(LIBGNAT) $(LIBINTL) $(LIBICONV) \
   ../$(LIBBACKTRACE) ../$(LIBIBERTY) $(SYSLIBS) $(TGT_LIB)
 
+# Add -no-pie to TOOLS_LIBS since some of them are compiled with -fno-PIE.
+TOOLS_LIBS += @NO_PIE_FLAG@
+
 # Specify the directories to be searched for header files.
 # Both . and srcdir are used, in that order,
 # so that tm.h and config.h will be found in the compilation
diff --git a/gcc/common.opt b/gcc/common.opt
index 51833c1..5b1735f8 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1770,7 +1770,7 @@  Common Report Var(flag_pic,2) Negative(fPIE)
 Generate position-independent code if possible (large mode)
 
 fPIE
-Common Report Var(flag_pie,2) Negative(fpic)
+Common Report Var(flag_pie,2) Negative(fpic) Init(-1)
 Generate position-independent code for executables if possible (large mode)
 
 fpic
@@ -1778,7 +1778,7 @@  Common Report Var(flag_pic,1) Negative(fpie)
 Generate position-independent code if possible (small mode)
 
 fpie
-Common Report Var(flag_pie,1) Negative(fPIC)
+Common Report Var(flag_pie,1) Negative(fPIC) Init(-1)
 Generate position-independent code for executables if possible (small mode)
 
 fplugin=
@@ -2851,8 +2851,12 @@  Driver
 symbolic
 Driver
 
-pie
+no-pie
 Driver RejectNegative Negative(shared)
+Don't create a position independent executable
+
+pie
+Driver RejectNegative Negative(no-pie)
 Create a position independent executable
 
 z
diff --git a/gcc/config.in b/gcc/config.in
index 231c9ab..daaf906 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -89,6 +89,12 @@ 
 #endif
 
 
+/* Define if your target supports default PIE and it is enabled. */
+#ifndef USED_FOR_TARGET
+#undef ENABLE_DEFAULT_PIE
+#endif
+
+
 /* Define if you want more run-time sanity checks for dataflow. */
 #ifndef USED_FOR_TARGET
 #undef ENABLE_DF_CHECKING
diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index 2faa2e5..2fcb55d 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -42,8 +42,11 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #if defined HAVE_LD_PIE
 #define GNU_USER_TARGET_STARTFILE_SPEC \
-  "%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
-   crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
+  "%{!shared: %{pg|p|profile:gcrt1.o%s;: \
+    %{" PIE_SPEC ":Scrt1.o%s} %{" NO_PIE_SPEC ":crt1.o%s}}} \
+   crti.o%s %{static:crtbeginT.o%s;: %{shared:crtbeginS.o%s} \
+	      %{" PIE_SPEC ":crtbeginS.o%s} \
+	      %{" NO_PIE_SPEC ":crtbegin.o%s}} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
      fvtable-verify=std:vtv_start.o%s}"
diff --git a/gcc/configure b/gcc/configure
index b99eb6d..3a03dce 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -600,6 +600,9 @@  ac_includes_default="\
 
 ac_subst_vars='LTLIBOBJS
 LIBOBJS
+NO_PIE_FLAG
+NO_PIE_CFLAGS
+enable_default_pie
 PICFLAG
 enable_host_shared
 enable_plugin
@@ -938,6 +941,7 @@  enable_host_shared
 enable_libquadmath_support
 with_linker_hash_style
 with_diagnostics_color
+enable_default_pie
 '
       ac_precious_vars='build_alias
 host_alias
@@ -1674,6 +1678,7 @@  Optional Features:
   --enable-host-shared    build host code as shared libraries
   --disable-libquadmath-support
                           disable libquadmath support for Fortran
+  --enable-default-pie    enable Position Independent Executable as default
 
 Optional Packages:
   --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
@@ -18162,7 +18167,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 18165 "configure"
+#line 18170 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -18268,7 +18273,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 18271 "configure"
+#line 18276 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -28571,6 +28576,76 @@  cat > gcc-driver-name.h <<EOF
 #define GCC_DRIVER_NAME "${target_noncanonical}-gcc-${gcc_BASEVER}${exeext}"
 EOF
 
+# Check whether --enable-default-pie was given.
+# Check whether --enable-default-pie was given.
+if test "${enable_default_pie+set}" = set; then :
+  enableval=$enable_default_pie; enable_default_pie=$enableval
+else
+  enable_default_pie=no
+fi
+
+if test x$enable_default_pie == xyes ; then
+
+$as_echo "#define ENABLE_DEFAULT_PIE 1" >>confdefs.h
+
+fi
+
+
+# Check if -fno-PIE works.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -fno-PIE option" >&5
+$as_echo_n "checking for -fno-PIE option... " >&6; }
+if test "${gcc_cv_c_no_fpie+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  saved_CFLAGS="$CFLAGS"
+   CFLAGS="$CFLAGS -fno-PIE"
+   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+int main(void) {return 0;}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  gcc_cv_c_no_fpie=yes
+else
+  gcc_cv_c_no_fpie=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+   CFLAGS="$saved_CFLAGS"
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_c_no_fpie" >&5
+$as_echo "$gcc_cv_c_no_fpie" >&6; }
+if test "$gcc_cv_c_no_fpie" = "yes"; then
+  NO_PIE_CFLAGS="-fno-PIE"
+fi
+
+
+# Check if -no-pie works.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -no-pie option" >&5
+$as_echo_n "checking for -no-pie option... " >&6; }
+if test "${gcc_cv_no_pie+set}" = set; then :
+  $as_echo_n "(cached) " >&6
+else
+  saved_LDFLAGS="$LDFLAGS"
+   LDFLAGS="$LDFLAGS -no-pie"
+   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+int main(void) {return 0;}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  gcc_cv_no_pie=yes
+else
+  gcc_cv_no_pie=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+   LDFLAGS="$saved_LDFLAGS"
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_no_pie" >&5
+$as_echo "$gcc_cv_no_pie" >&6; }
+if test "$gcc_cv_no_pie" = "yes"; then
+  NO_PIE_FLAG="-no-pie"
+fi
+
+
 # Configure the subdirectories
 # AC_CONFIG_SUBDIRS($subdirs)
 
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 810725c..73de1ad 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5826,6 +5826,46 @@  cat > gcc-driver-name.h <<EOF
 #define GCC_DRIVER_NAME "${target_noncanonical}-gcc-${gcc_BASEVER}${exeext}"
 EOF
 
+# Check whether --enable-default-pie was given.
+AC_ARG_ENABLE(default-pie,
+[AS_HELP_STRING([--enable-default-pie],
+  [enable Position Independent Executable as default])],
+enable_default_pie=$enableval,
+enable_default_pie=no)
+if test x$enable_default_pie == xyes ; then
+  AC_DEFINE(ENABLE_DEFAULT_PIE, 1,
+      [Define if your target supports default PIE and it is enabled.])
+fi
+AC_SUBST([enable_default_pie])
+
+# Check if -fno-PIE works.
+AC_CACHE_CHECK([for -fno-PIE option],
+  [gcc_cv_c_no_fpie],
+  [saved_CFLAGS="$CFLAGS"
+   CFLAGS="$CFLAGS -fno-PIE"
+   AC_COMPILE_IFELSE([int main(void) {return 0;}],
+     [gcc_cv_c_no_fpie=yes],
+     [gcc_cv_c_no_fpie=no])
+   CFLAGS="$saved_CFLAGS"])
+if test "$gcc_cv_c_no_fpie" = "yes"; then
+  NO_PIE_CFLAGS="-fno-PIE"
+fi
+AC_SUBST([NO_PIE_CFLAGS])
+
+# Check if -no-pie works.
+AC_CACHE_CHECK([for -no-pie option],
+  [gcc_cv_no_pie],
+  [saved_LDFLAGS="$LDFLAGS"
+   LDFLAGS="$LDFLAGS -no-pie"
+   AC_LINK_IFELSE([int main(void) {return 0;}],
+     [gcc_cv_no_pie=yes],
+     [gcc_cv_no_pie=no])
+   LDFLAGS="$saved_LDFLAGS"])
+if test "$gcc_cv_no_pie" = "yes"; then
+  NO_PIE_FLAG="-no-pie"
+fi
+AC_SUBST([NO_PIE_FLAG])
+
 # Configure the subdirectories
 # AC_CONFIG_SUBDIRS($subdirs)
 
diff --git a/gcc/defaults.h b/gcc/defaults.h
index 47fdcf4..a7da1a3 100644
--- a/gcc/defaults.h
+++ b/gcc/defaults.h
@@ -1229,6 +1229,11 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #define ARGS_GROW_DOWNWARD 0
 #endif
 
+/* Default PIE to -fPIE.  */
+#ifndef DEFAULT_FLAG_PIE
+# define DEFAULT_FLAG_PIE 2
+#endif
+
 #ifdef GCC_INSN_FLAGS_H
 /* Dependent default target macro definitions
 
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index d8d3939..36502ee 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1583,6 +1583,9 @@  not be built.
 Specify that the run-time libraries for stack smashing protection
 should not be built.
 
+@item --enable-default-pie
+Turn on @option{-fPIE} and @option{-pie} by default.
+
 @item --disable-libquadmath
 Specify that the GCC quad-precision math library should not be built.
 On some systems, the library is required to be linkable when building
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 9169731..ac34539 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -11251,6 +11251,10 @@  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.
+
 @item -rdynamic
 @opindex rdynamic
 Pass the flag @option{-export-dynamic} to the ELF linker, on targets
diff --git a/gcc/gcc.c b/gcc/gcc.c
index d956c36..4ba3229 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -737,12 +737,29 @@  proper position among the other output files.  */
 #endif
 #endif
 
+#ifdef ENABLE_DEFAULT_PIE
+#define NO_PIE_SPEC		"no-pie|static"
+#define PIE_SPEC		NO_PIE_SPEC "|r|shared:;"
+#define NO_FPIE_SPEC		"fno-pie|fno-PIE"
+#define FPIE_SPEC		NO_FPIE_SPEC ":;"
+#define NO_FPIE_AND_FPIC_SPEC	NO_FPIE_SPEC "|fno-pic|fno-PIC"
+#define FPIE_OR_FPIC_SPEC	NO_FPIE_AND_FPIC_SPEC ":;"
+#else
+#define PIE_SPEC		"pie"
+#define NO_PIE_SPEC		PIE_SPEC "|r|shared:;"
+#define FPIE_SPEC		"fpie|fPIE"
+#define NO_FPIE_SPEC		FPIE_SPEC ":;"
+#define FPIE_OR_FPIC_SPEC	FPIE_SPEC "|fpic|fPIC"
+#define NO_FPIE_AND_FPIC_SPEC	FPIE_OR_FPIC_SPEC ":;"
+#endif
+
 #ifndef LINK_PIE_SPEC
 #ifdef HAVE_LD_PIE
-#define LINK_PIE_SPEC "%{pie:-pie} "
+#define LD_PIE_SPEC "-pie"
 #else
-#define LINK_PIE_SPEC "%{pie:} "
+#define LD_PIE_SPEC ""
 #endif
+#define LINK_PIE_SPEC "%{no-pie:} " "%{" PIE_SPEC ":" LD_PIE_SPEC "} "
 #endif
 
 #ifndef LINK_BUILDID_SPEC
diff --git a/gcc/opts.c b/gcc/opts.c
index cf92334..c10126a 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -739,8 +739,22 @@  finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
       opts->x_flag_section_anchors = 0;
     }
 
+#ifndef ENABLE_DEFAULT_PIE
+#undef DEFAULT_FLAG_PIE
+#define DEFAULT_FLAG_PIE 0
+#endif
+
   if (!opts->x_flag_opts_finished)
     {
+      /* We initialize opts->x_flag_pie to -1 so that targets can set a
+	 default value.  */
+      if (opts->x_flag_pie == -1)
+	{
+	  if (opts->x_flag_pic == 0)
+	    opts->x_flag_pie = DEFAULT_FLAG_PIE;
+	  else
+	    opts->x_flag_pie = 0;
+	}
       if (opts->x_flag_pie)
 	opts->x_flag_pic = opts->x_flag_pie;
       if (opts->x_flag_pic && !opts->x_flag_pie)
diff --git a/libgcc/Makefile.in b/libgcc/Makefile.in
index 1ab188d..b8fc1d8 100644
--- a/libgcc/Makefile.in
+++ b/libgcc/Makefile.in
@@ -285,7 +285,7 @@  INTERNAL_CFLAGS = $(CFLAGS) $(LIBGCC2_CFLAGS) $(HOST_LIBGCC2_CFLAGS) \
 
 # Options to use when compiling crtbegin/end.
 CRTSTUFF_CFLAGS = -O2 $(GCC_CFLAGS) $(INCLUDES) $(MULTILIB_CFLAGS) -g0 \
-  -finhibit-size-directive -fno-inline -fno-exceptions \
+  $(NO_PIE_CFLAGS) -finhibit-size-directive -fno-inline -fno-exceptions \
   -fno-zero-initialized-in-bss -fno-toplevel-reorder -fno-tree-vectorize \
   -fbuilding-libgcc -fno-stack-protector $(FORCE_EXPLICIT_EH_REGISTRY) \
   $(INHIBIT_LIBC_CFLAGS)
-- 
1.9.3