diff mbox

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

Message ID CAMe9rOqUj4dhH3omFO7UONsPgmg9iOaXhntfK+dNByLeGnrm_Q@mail.gmail.com
State New
Headers show

Commit Message

H.J. Lu May 22, 2015, 3:18 p.m. UTC
I fixed a typo in gcc/config/openbsd.h.  Here is the updated patch.  The whole
patch is also on hjl/pie/master branch in GCC git mirror.

Comments

Bill Schmidt May 26, 2015, 9:39 p.m. UTC | #1
Hi H.J.,

I tried your patch on powerpc64le-linux-gnu, and it fails to bootstrap
as follows:

> g++ -std=c++98 -c   -g -DIN_GCC    -fno-exceptions -fno-rtti -fasynchronous-unwi
> nd-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wno-format -Wmiss
> ing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic
> -macros -Wno-overlength-strings -fno-common  -DHAVE_CONFIG_H -DGENERATOR_FILE @N
> O_PIE_CFLAGS@ -I. -Ibuild -I/home/wschmidt/gcc/gcc-mainline-test/gcc -I/home/wsc
> hmidt/gcc/gcc-mainline-test/gcc/build -I/home/wschmidt/gcc/gcc-mainline-test/gcc
> /../include  -I/home/wschmidt/gcc/gcc-mainline-test/gcc/../libcpp/include  \
>         -DBASEVER="\"6.0.0\"" -DDATESTAMP="\" 20150526\"" \
>         -DREVISION="\" [trunk revision 223719]\"" \
>         -DDEVPHASE="\" (experimental)\"" -DPKGVERSION="\"(GCC) \"" \
>         -DBUGURL="\"<http://gcc.gnu.org/bugs.html>\"" -o build/version.o /home/wschmidt/gcc/gcc-mainline-test/gcc/version.c
> g++: error: @NO_PIE_CFLAGS@: No such file or directory
> make[3]: *** [build/version.o] Error 1

Thanks,
Bill

On Fri, 2015-05-22 at 08:18 -0700, H.J. Lu wrote:
> I fixed a typo in gcc/config/openbsd.h.  Here is the updated patch.  The whole
> patch is also on hjl/pie/master branch in GCC git mirror.
>
Bill Schmidt May 26, 2015, 9:40 p.m. UTC | #2
Ah, never mind.  I guess I need to run automake first.

Bill

On Tue, 2015-05-26 at 16:39 -0500, Bill Schmidt wrote:
> Hi H.J.,
> 
> I tried your patch on powerpc64le-linux-gnu, and it fails to bootstrap
> as follows:
> 
> > g++ -std=c++98 -c   -g -DIN_GCC    -fno-exceptions -fno-rtti -fasynchronous-unwi
> > nd-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wno-format -Wmiss
> > ing-format-attribute -Woverloaded-virtual -pedantic -Wno-long-long -Wno-variadic
> > -macros -Wno-overlength-strings -fno-common  -DHAVE_CONFIG_H -DGENERATOR_FILE @N
> > O_PIE_CFLAGS@ -I. -Ibuild -I/home/wschmidt/gcc/gcc-mainline-test/gcc -I/home/wsc
> > hmidt/gcc/gcc-mainline-test/gcc/build -I/home/wschmidt/gcc/gcc-mainline-test/gcc
> > /../include  -I/home/wschmidt/gcc/gcc-mainline-test/gcc/../libcpp/include  \
> >         -DBASEVER="\"6.0.0\"" -DDATESTAMP="\" 20150526\"" \
> >         -DREVISION="\" [trunk revision 223719]\"" \
> >         -DDEVPHASE="\" (experimental)\"" -DPKGVERSION="\"(GCC) \"" \
> >         -DBUGURL="\"<http://gcc.gnu.org/bugs.html>\"" -o build/version.o /home/wschmidt/gcc/gcc-mainline-test/gcc/version.c
> > g++: error: @NO_PIE_CFLAGS@: No such file or directory
> > make[3]: *** [build/version.o] Error 1
> 
> Thanks,
> Bill
> 
> On Fri, 2015-05-22 at 08:18 -0700, H.J. Lu wrote:
> > I fixed a typo in gcc/config/openbsd.h.  Here is the updated patch.  The whole
> > patch is also on hjl/pie/master branch in GCC git mirror.
> > 
>
Peter Bergner May 27, 2015, 3:24 p.m. UTC | #3
On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
> Ah, never mind.  I guess I need to run automake first.

I ran the patch on powerpc64-linux (ie, Big Endian) both with and
without --enable-default-pie.  Both bootstraps completed with no
errors and the without --enable-default-pie regtested without any
regressions.

The --enable-default-pie regtesting shows massive failures that I
have to look into.  I'm haven't determined yet whether these are
all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
info after I dig into some of the failures.

Peter
H.J. Lu May 27, 2015, 3:36 p.m. UTC | #4
On Wed, May 27, 2015 at 8:24 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
> On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
>> Ah, never mind.  I guess I need to run automake first.
>
> I ran the patch on powerpc64-linux (ie, Big Endian) both with and
> without --enable-default-pie.  Both bootstraps completed with no
> errors and the without --enable-default-pie regtested without any
> regressions.
>
> The --enable-default-pie regtesting shows massive failures that I
> have to look into.  I'm haven't determined yet whether these are
> all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
> info after I dig into some of the failures.

Does --enable-default-pie work on powerpc64-linux?  Do you
get working PIE by default?  Some GCC tests expect non-PIE.
I fixed some of them:

commit 82923064d660e4183933b014ee3f645799a945b0
Author: hjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Thu Jan 15 16:33:37 2015 +0000

    Ignore additional linker messages on Linux/x86 with PIE

    g++.dg/other/anon5.C is expected to fail to link.  On Linux/x86 with PIE
    and the new linker, there are additional messages from linker:

    [hjl@gnu-tools-1 gcc]$ g++  -fPIE -pie
    /export/gnu/import/git/sources/gcc/gcc/testsuite/g++.dg/other/anon5.C
    /tmp/ccwg53fj.o: In function `f()': anon5.C:(.text+0x7): undefined
reference to `(anonymous namespace)::c::t'
    /usr/local/bin/ld: /tmp/ccwg53fj.o: relocation R_X86_64_PC32
against undefined symbol `_ZN12_GLOBAL__N_11c1tE' can not be used when
making a shared object; recompile with -fPIC
    /usr/local/bin/ld: final link failed: Bad value
    collect2: error: ld returned 1 exit status
    [hjl@gnu-tools-1 gcc]$

    This patch ignores additional linker messages on Linux/x86 with PIE.

      * g++.dg/other/anon5.C: Ignore additional messages on Linux/x86
      with PIE.


    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@219667
138bc75d-0d04-0410-961f-82ee72b054a4
Peter Bergner May 27, 2015, 4:05 p.m. UTC | #5
On Wed, 2015-05-27 at 08:36 -0700, H.J. Lu wrote:
> On Wed, May 27, 2015 at 8:24 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
> > On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
> >> Ah, never mind.  I guess I need to run automake first.
> >
> > I ran the patch on powerpc64-linux (ie, Big Endian) both with and
> > without --enable-default-pie.  Both bootstraps completed with no
> > errors and the without --enable-default-pie regtested without any
> > regressions.
> >
> > The --enable-default-pie regtesting shows massive failures that I
> > have to look into.  I'm haven't determined yet whether these are
> > all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
> > info after I dig into some of the failures.
> 
> Does --enable-default-pie work on powerpc64-linux?  Do you
> get working PIE by default?  Some GCC tests expect non-PIE.
> I fixed some of them:

I haven't looked into any of the failures yet.  That said,
powerpc64-linux is PIC by default, so I thought maybe PIE
would just work.  Maybe it does and it's just powerpc-linux
tests that are failing (I run the testsuite with both
-m32 and -m64).  I won't know until I get some time to have
a deeper look.  That said, if there is something you know
of that I should look for or at, I'd appreciate it.

Peter
H.J. Lu May 27, 2015, 4:18 p.m. UTC | #6
On Wed, May 27, 2015 at 9:05 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
> On Wed, 2015-05-27 at 08:36 -0700, H.J. Lu wrote:
>> On Wed, May 27, 2015 at 8:24 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
>> > On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
>> >> Ah, never mind.  I guess I need to run automake first.
>> >
>> > I ran the patch on powerpc64-linux (ie, Big Endian) both with and
>> > without --enable-default-pie.  Both bootstraps completed with no
>> > errors and the without --enable-default-pie regtested without any
>> > regressions.
>> >
>> > The --enable-default-pie regtesting shows massive failures that I
>> > have to look into.  I'm haven't determined yet whether these are
>> > all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
>> > info after I dig into some of the failures.
>>
>> Does --enable-default-pie work on powerpc64-linux?  Do you
>> get working PIE by default?  Some GCC tests expect non-PIE.
>> I fixed some of them:
>
> I haven't looked into any of the failures yet.  That said,
> powerpc64-linux is PIC by default, so I thought maybe PIE

PIC != PIE.  Is PIE the default for powerpc64-linux? Please
show me

# readelf -h /bin/ls

on powerpc64-linux.

> would just work.  Maybe it does and it's just powerpc-linux
> tests that are failing (I run the testsuite with both
> -m32 and -m64).  I won't know until I get some time to have
> a deeper look.  That said, if there is something you know
> of that I should look for or at, I'd appreciate it.
>

You should first verify if --enable-default-pie generates a GCC which
can produce a simple hello program.
David Edelsohn May 27, 2015, 4:59 p.m. UTC | #7
On Wed, May 27, 2015 at 12:18 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Wed, May 27, 2015 at 9:05 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
>> On Wed, 2015-05-27 at 08:36 -0700, H.J. Lu wrote:
>>> On Wed, May 27, 2015 at 8:24 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
>>> > On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
>>> >> Ah, never mind.  I guess I need to run automake first.
>>> >
>>> > I ran the patch on powerpc64-linux (ie, Big Endian) both with and
>>> > without --enable-default-pie.  Both bootstraps completed with no
>>> > errors and the without --enable-default-pie regtested without any
>>> > regressions.
>>> >
>>> > The --enable-default-pie regtesting shows massive failures that I
>>> > have to look into.  I'm haven't determined yet whether these are
>>> > all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
>>> > info after I dig into some of the failures.
>>>
>>> Does --enable-default-pie work on powerpc64-linux?  Do you
>>> get working PIE by default?  Some GCC tests expect non-PIE.
>>> I fixed some of them:
>>
>> I haven't looked into any of the failures yet.  That said,
>> powerpc64-linux is PIC by default, so I thought maybe PIE
>
> PIC != PIE.  Is PIE the default for powerpc64-linux? Please
> show me
>
> # readelf -h /bin/ls
>
> on powerpc64-linux.
>
>> would just work.  Maybe it does and it's just powerpc-linux
>> tests that are failing (I run the testsuite with both
>> -m32 and -m64).  I won't know until I get some time to have
>> a deeper look.  That said, if there is something you know
>> of that I should look for or at, I'd appreciate it.
>>
>
> You should first verify if --enable-default-pie generates a GCC which
> can produce a simple hello program.

The --enable-default-pie patch itself works correctly -- as intended
-- for powerpc64-linux and powerpc64le-linux and the rs6000 parts of
the patch are okay with me.  In other words, the option is fine but is
not enabled by default so it will not cause any problems.

Debugging the testsuite failures is a separate issue.

Thanks, David
H.J. Lu May 27, 2015, 5:02 p.m. UTC | #8
On Wed, May 27, 2015 at 9:59 AM, David Edelsohn <dje.gcc@gmail.com> wrote:
> On Wed, May 27, 2015 at 12:18 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Wed, May 27, 2015 at 9:05 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
>>> On Wed, 2015-05-27 at 08:36 -0700, H.J. Lu wrote:
>>>> On Wed, May 27, 2015 at 8:24 AM, Peter Bergner <bergner@vnet.ibm.com> wrote:
>>>> > On Tue, 2015-05-26 at 16:40 -0500, Bill Schmidt wrote:
>>>> >> Ah, never mind.  I guess I need to run automake first.
>>>> >
>>>> > I ran the patch on powerpc64-linux (ie, Big Endian) both with and
>>>> > without --enable-default-pie.  Both bootstraps completed with no
>>>> > errors and the without --enable-default-pie regtested without any
>>>> > regressions.
>>>> >
>>>> > The --enable-default-pie regtesting shows massive failures that I
>>>> > have to look into.  I'm haven't determined yet whether these are
>>>> > all -m32 FAILs or -m64 FAILS or both.  I'll report back with more
>>>> > info after I dig into some of the failures.
>>>>
>>>> Does --enable-default-pie work on powerpc64-linux?  Do you
>>>> get working PIE by default?  Some GCC tests expect non-PIE.
>>>> I fixed some of them:
>>>
>>> I haven't looked into any of the failures yet.  That said,
>>> powerpc64-linux is PIC by default, so I thought maybe PIE
>>
>> PIC != PIE.  Is PIE the default for powerpc64-linux? Please
>> show me
>>
>> # readelf -h /bin/ls
>>
>> on powerpc64-linux.
>>
>>> would just work.  Maybe it does and it's just powerpc-linux
>>> tests that are failing (I run the testsuite with both
>>> -m32 and -m64).  I won't know until I get some time to have
>>> a deeper look.  That said, if there is something you know
>>> of that I should look for or at, I'd appreciate it.
>>>
>>
>> You should first verify if --enable-default-pie generates a GCC which
>> can produce a simple hello program.
>
> The --enable-default-pie patch itself works correctly -- as intended
> -- for powerpc64-linux and powerpc64le-linux and the rs6000 parts of
> the patch are okay with me.  In other words, the option is fine but is
> not enabled by default so it will not cause any problems.

Good to know.

> Debugging the testsuite failures is a separate issue.

Thanks.
Andreas Schwab May 30, 2015, 3:10 p.m. UTC | #9
"H.J. Lu" <hjl.tools@gmail.com> writes:

> +# 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])
> +

That doesn't work:

configure:28726: checking for -fno-PIE option
configure:28737: g++ -c -g   conftest.cpp >&5
configure:28737: $? = 0
configure:28745: result: yes

> +# 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])
> +

That doesn't work with gcc 4.3:

configure:28753: checking for -no-pie option
configure:28764: g++ -std=c++98 -o conftest -g     -no-pie conftest.cpp  >&5
g++: unrecognized option '-no-pie'
configure:28764: $? = 0
configure:28773: result: yes

Andreas.
H.J. Lu May 30, 2015, 3:42 p.m. UTC | #10
On Sat, May 30, 2015 at 8:10 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>> +# 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])
>> +
>
> That doesn't work:
>
> configure:28726: checking for -fno-PIE option
> configure:28737: g++ -c -g   conftest.cpp >&5
> configure:28737: $? = 0
> configure:28745: result: yes
>
>> +# 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])
>> +
>
> That doesn't work with gcc 4.3:
>
> configure:28753: checking for -no-pie option
> configure:28764: g++ -std=c++98 -o conftest -g     -no-pie conftest.cpp  >&5
> g++: unrecognized option '-no-pie'
> configure:28764: $? = 0
> configure:28773: result: yes
>

Starting GCC 4.6, the unrecognized option became an error.
Before 4.6, it was ignored.  Does it cause any problems for
bootstrap?
Andreas Schwab May 30, 2015, 3:54 p.m. UTC | #11
"H.J. Lu" <hjl.tools@gmail.com> writes:

> Starting GCC 4.6, the unrecognized option became an error.
> Before 4.6, it was ignored.  Does it cause any problems for
> bootstrap?

No, only spurious messages.

Andreas.
H.J. Lu May 30, 2015, 4:09 p.m. UTC | #12
On Sat, May 30, 2015 at 8:54 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>> Starting GCC 4.6, the unrecognized option became an error.
>> Before 4.6, it was ignored.  Does it cause any problems for
>> bootstrap?
>
> No, only spurious messages.
>

There is no harm and the message is gone in stage 2 and 3,
We can treat it the same as other spurious messages from the
bootstrap compiler.
Andreas Schwab May 30, 2015, 4:32 p.m. UTC | #13
"H.J. Lu" <hjl.tools@gmail.com> writes:

> diff --git a/gcc/config/m68k/m68k.h b/gcc/config/m68k/m68k.h
> index 88356cc..74ebff7 100644
> --- a/gcc/config/m68k/m68k.h
> +++ b/gcc/config/m68k/m68k.h
> @@ -40,7 +40,8 @@ along with GCC; see the file COPYING3.  If not see
>  %{m68020-40:-m68040}%{m68020-60:-m68040}\
>  %{mcpu=*:-mcpu=%*}%{march=*:-march=%*}\
>  "
> -#define ASM_PCREL_SPEC "%{fPIC|fpic|mpcrel:--pcrel} \
> +#define ASM_PCREL_SPEC "%{" FPIC_SPEC ":--pcrel} \
> + %{mpcrel:%{" NO_FPIC_SPEC ":--pcrel}} \
>   %{msep-data|mid-shared-library:--pcrel} \

That expands to a spurious --pcrel with -fno-PIE, causing the assembler
to error out while compiling crtstuff if the compiler is configured with
--enable-default-pie.

Andreas.
Andreas Schwab May 30, 2015, 4:44 p.m. UTC | #14
"H.J. Lu" <hjl.tools@gmail.com> writes:

> There is no harm and the message is gone in stage 2 and 3,
> We can treat it the same as other spurious messages from the
> bootstrap compiler.

But you still need to fix the first test, since it doesn't test
anything.

Andreas.
H.J. Lu May 30, 2015, 8:08 p.m. UTC | #15
On Sat, May 30, 2015 at 9:44 AM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>> There is no harm and the message is gone in stage 2 and 3,
>> We can treat it the same as other spurious messages from the
>> bootstrap compiler.
>
> But you still need to fix the first test, since it doesn't test
> anything.
>

What is wrong with it?  Its purpose is to always compile GCC
without PIE.  If the bootstrap compiler allows -fno-PIE, we add
it.  Does the test pass when the bootstrap compiler errors
on -fno-PIE?
Andreas Schwab May 30, 2015, 9:18 p.m. UTC | #16
"H.J. Lu" <hjl.tools@gmail.com> writes:

> What is wrong with it?

It tests nothing.

Andreas.
H.J. Lu May 30, 2015, 9:54 p.m. UTC | #17
On Sat, May 30, 2015 at 2:18 PM, Andreas Schwab <schwab@linux-m68k.org> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>> What is wrong with it?
>
> It tests nothing.
>

Can you explain?
Andreas Schwab May 31, 2015, 7:04 a.m. UTC | #18
configure:28726: checking for -fno-PIE option
configure:28737: g++ -c -g   conftest.cpp >&5
configure:28737: $? = 0
configure:28745: result: yes

Andreas.
Marcus Shawcroft Oct. 23, 2015, 11:54 a.m. UTC | #19
Hi,

This patch breaks the distinction between build and host. For example
consider a configure along these lines:

./configure --host=aarch64-none-linux-gnu
--target=aarch64-none-linux-gnu --build=x86_64-pc-linux-gnu

Will result in:

CXX_FOR_BUILD='g++'
CXX='aarch64-none-linux-gnu-g++'

the gcc/configure fragment:

+# 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])

will check if CXX supports -no-pic and set NO_PIE_FLAG accordingly.
The gcc/Makefile.in fragment:

@@ -761,6 +769,7 @@ BUILD_LINKERFLAGS = $(BUILD_CXXFLAGS)

 # Native linker and preprocessor flags.  For x-fragment overrides.
 BUILD_LDFLAGS=@BUILD_LDFLAGS@
+BUILD_LDFLAGS += @NO_PIE_FLAG@

constructs the flags which will ultimately be used to compile the
build machines gen* programs.  That compilation uses CXX_FOR_BUILD
with the NO_PIE_FLAG that was originally probed for CXX.  This is
incorrect if CXX_FOR_BUILD != CXX

HJ, could you take a look into this issue?

Thanks
/Marcus
diff mbox

Patch

From 64364101d6c888e20eb1146ee2baac4b08e684cf Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 19 May 2015 10:12:20 -0700
Subject: [PATCH] Add --enable-default-pie option to GCC configure

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

gcc/

2015-05-19  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_FPIE1_SPEC): Likewise.
	(FPIE1_SPEC): Likewise.
	(NO_FPIE2_SPEC): Likewise.
	(FPIE2_SPEC): Likewise.
	(NO_FPIE2_SPEC): Likewise.
	(FPIE_SPEC): Likewise.
	(NO_FPIE_SPEC): Likewise.
	(NO_FPIC1_SPEC): Likewise.
	(FPIC1_SPEC): Likewise.
	(NO_FPIC2_SPEC): Likewise.
	(FPIC2_SPEC): Likewise.
	(NO_FPIC2_SPEC): Likewise.
	(FPIC_SPEC): Likewise.
	(NO_FPIC_SPEC): Likewise.
	(NO_FPIE1_AND_FPIC1_SPEC): Likewise.
	(FPIE1_OR_FPIC1_SPEC): Likewise.
	(NO_FPIE2_AND_FPIC2_SPEC): Likewise.
	(FPIE2_OR_FPIC2_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 (finish_options): Update opts->x_flag_pie if it is -1.
	* config/darwin.h (PIE_SPEC): Renamed to ...
	(DARWIN_PIE_SPEC): This.
	(LINK_SPEC): Replace PIE_SPEC with DARWIN_PIE_SPEC.
	* config/darwin9.h (PIE_SPEC): Renamed to ...
	(DARWIN_PIE_SPEC): This.
	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Use
	PIE_SPEC and NO_PIE_SPEC if HAVE_LD_PIE is defined.
	* config/openbsd.h (ASM_SPEC): Use FPIE1_OR_FPIC1_SPEC and
	FPIE2_OR_FPIC2_SPEC.
	* config/m68k/netbsd-elf.h (ASM_SPEC): Likewise.
	* config/m68k/openbsd.h (ASM_SPEC): Likewise.
	* gcc/config/sol2.h (ASM_PIC_SPEC): Likewise.
	* config/arm/freebsd.h (SUBTARGET_EXTRA_ASM_SPEC): Likewise.
	* config/arm/netbsd-elf.h (SUBTARGET_EXTRA_ASM_SPEC): Likewise.
	* config/arm/semi.h (SUBTARGET_EXTRA_ASM_SPEC): Likewise.
	* config/cris/linux.h (CRIS_ASM_SUBTARGET_SPEC): Likewise.
	* config/m32r/m32r.h (ASM_SPEC): Likewise.
	* config/m68k/uclinux.h (DRIVER_SELF_SPECS): Likewise.
	* config/rs6000/linux64.h (ASM_SPEC32): Likewise.
	* config/rs6000/sysv4.h (ASM_SPEC): Likewise.
	* config/sparc/freebsd.h (ASM_SPEC): Likewise.
	* config/sparc/linux.h (ASM_SPEC): Likewise.
	* config/sparc/linux64.h (ASM_SPEC): Likewise.
	* config/sparc/netbsd-elf.h (ASM_SPEC): Likewise.
	* config/sparc/openbsd64.h (ASM_SPEC): Likewise.
	* config/sparc/sp-elf.h (ASM_SPEC): Likewise.
	* config/sparc/sp64-elf.h (ASM_SPEC): Likewise.
	* config/sparc/sparc.h (ASM_SPEC): Likewise.
	* config/sparc/sysv4.h (ASM_SPEC): Likewise.
	* config/sparc/vxworks.h (ASM_SPEC): Likewise.
	* config/c6x/elf-common.h (ASM_SPEC): Use NO_FPIC2_SPEC,
	FPIC2_SPEC, FPIC1_SPEC and FPIC2_SPEC.
	* config/c6x/uclinux-elf.h (LINK_SPEC): Use FPIE_SPEC.
	* config/frv/frv.h (DRIVER_SELF_SPECS): Use FPIC_SPEC,
	NO_FPIC_SPEC and NO_FPIE1_AND_FPIC1_SPEC.
	(ASM_SPEC): Use FPIE1_OR_FPIC1_SPEC and FPIE2_OR_FPIC2_SPEC.
	* config/m68k/m68k.h (ASM_PCREL_SPEC): Use FPIC_SPEC and
	NO_FPIC_SPEC.
	* config/mips/gnu-user.h (NO_SHARED_SPECS): Use
	NO_FPIE_AND_FPIC_SPEC.
	* config/mips/vxworks.h (SUBTARGET_ASM_SPEC): Use FPIC_SPEC.
	* config/rs6000/freebsd64.h (ASM_SPEC32): Likewise.
	* config/rs6000/vxworks.h (ASM_SPEC): Likewise.
	* config/vax/linux.h (ASM_SPEC): Likewise.
	* doc/install.texi: Document --enable-default-pie.
	* doc/invoke.texi: Document -no-pie.
	* config.in: Regenerated.
	* configure: Likewise.

gcc/ada/

2015-05-19  H.J. Lu  <hongjiu.lu@intel.com>

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

libgcc/

2015-05-19  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/arm/freebsd.h          |  2 +-
 gcc/config/arm/netbsd-elf.h       |  2 +-
 gcc/config/arm/semi.h             |  2 +-
 gcc/config/c6x/elf-common.h       |  6 ++-
 gcc/config/c6x/uclinux-elf.h      |  2 +-
 gcc/config/cris/linux.h           |  4 +-
 gcc/config/darwin.h               |  4 +-
 gcc/config/darwin9.h              |  4 +-
 gcc/config/frv/frv.h              | 10 ++---
 gcc/config/gnu-user.h             |  7 +++-
 gcc/config/m32r/m32r.h            |  2 +-
 gcc/config/m68k/m68k.h            |  3 +-
 gcc/config/m68k/netbsd-elf.h      |  3 +-
 gcc/config/m68k/openbsd.h         |  3 +-
 gcc/config/m68k/uclinux.h         |  2 +-
 gcc/config/mips/gnu-user.h        |  2 +-
 gcc/config/mips/vxworks.h         |  2 +-
 gcc/config/openbsd.h              |  2 +-
 gcc/config/rs6000/freebsd64.h     |  2 +-
 gcc/config/rs6000/linux64.h       |  2 +-
 gcc/config/rs6000/sysv4.h         |  2 +-
 gcc/config/rs6000/vxworks.h       |  2 +-
 gcc/config/sol2.h                 |  2 +-
 gcc/config/sparc/freebsd.h        |  2 +-
 gcc/config/sparc/linux.h          |  2 +-
 gcc/config/sparc/linux64.h        |  2 +-
 gcc/config/sparc/netbsd-elf.h     |  2 +-
 gcc/config/sparc/openbsd64.h      |  2 +-
 gcc/config/sparc/sp-elf.h         |  2 +-
 gcc/config/sparc/sp64-elf.h       |  2 +-
 gcc/config/sparc/sparc.h          |  2 +-
 gcc/config/sparc/sysv4.h          |  2 +-
 gcc/config/sparc/vxworks.h        |  2 +-
 gcc/config/vax/linux.h            |  2 +-
 gcc/configure                     | 79 ++++++++++++++++++++++++++++++++++++++-
 gcc/configure.ac                  | 40 ++++++++++++++++++++
 gcc/defaults.h                    | 12 ++++++
 gcc/doc/install.texi              |  3 ++
 gcc/doc/invoke.texi               |  4 ++
 gcc/gcc.c                         | 49 +++++++++++++++++++++++-
 gcc/opts.c                        |  9 +++++
 libgcc/Makefile.in                |  2 +-
 46 files changed, 275 insertions(+), 51 deletions(-)

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index ab9b637..512e7c8 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -253,6 +253,12 @@  LINKER = $(CC)
 LINKER_FLAGS = $(CFLAGS)
 endif
 
+# We don't want to compile the compilers 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)
@@ -750,6 +756,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)
@@ -761,6 +769,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)
 
@@ -1864,6 +1873,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 562d34a..6e15af2 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1778,7 +1778,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
@@ -1786,7 +1786,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)
 
 fplt
@@ -2863,8 +2863,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/arm/freebsd.h b/gcc/config/arm/freebsd.h
index 040d7c1..2febf4b 100644
--- a/gcc/config/arm/freebsd.h
+++ b/gcc/config/arm/freebsd.h
@@ -34,7 +34,7 @@ 
 #undef SUBTARGET_EXTRA_ASM_SPEC
 #define SUBTARGET_EXTRA_ASM_SPEC \
   "%{mabi=apcs-gnu|mabi=atpcs:-meabi=gnu;:-meabi=5} " TARGET_FIX_V4BX_SPEC " \
-  %{fpic|fpie:-k} %{fPIC|fPIE:-k}"
+  %{" FPIE_OR_FPIC_SPEC ":-k}"
 
 #undef SUBTARGET_ASM_FLOAT_SPEC
 #ifdef TARGET_FREEBSD_ARM_HARD_FLOAT
diff --git a/gcc/config/arm/netbsd-elf.h b/gcc/config/arm/netbsd-elf.h
index ee085a6..6546097 100644
--- a/gcc/config/arm/netbsd-elf.h
+++ b/gcc/config/arm/netbsd-elf.h
@@ -55,7 +55,7 @@ 
 
 #undef SUBTARGET_EXTRA_ASM_SPEC
 #define SUBTARGET_EXTRA_ASM_SPEC	\
-  "-matpcs %{fpic|fpie:-k} %{fPIC|fPIE:-k}"
+  "-matpcs %{" FPIE_OR_FPIC_SPEC ":-k}"
 
 /* Default to full VFP if -mfloat-abi=hard is specified.  */
 #undef SUBTARGET_ASM_FLOAT_SPEC
diff --git a/gcc/config/arm/semi.h b/gcc/config/arm/semi.h
index 18a4443..1c938d3 100644
--- a/gcc/config/arm/semi.h
+++ b/gcc/config/arm/semi.h
@@ -58,7 +58,7 @@ 
    binutils can't.  */
 #ifndef ASM_SPEC
 #define ASM_SPEC "\
-%{fpic|fpie: -k} %{fPIC|fPIE: -k} \
+%{" FPIE_OR_FPIC_SPEC ": -k} \
 %{mbig-endian:-EB} \
 %(arm_cpu_spec) \
 %{mapcs-float:-mfloat} \
diff --git a/gcc/config/c6x/elf-common.h b/gcc/config/c6x/elf-common.h
index 844172d..2a7cd52 100644
--- a/gcc/config/c6x/elf-common.h
+++ b/gcc/config/c6x/elf-common.h
@@ -24,8 +24,10 @@ 
 
 #undef ASM_SPEC
 #define ASM_SPEC "%{march=*:-march=%*} %{mbig-endian:-mbig-endian} \
- %{mdsbt:-mdsbt %{!fPIC:-mpid=near} %{fPIC:-mpid=far -mpic} %{fpic:-mpic}} \
- %{!mdsbt:%{fpic:-mpic -mpid=near} %{fPIC:-mpic -mpid=far}}"
+ %{mdsbt:-mdsbt %{" NO_FPIC2_SPEC ":-mpid=near} \
+   %{" FPIC2_SPEC ":-mpid=far -mpic} %{" FPIC1_SPEC ":-mpic}} \
+ %{!mdsbt:%{" FPIC1_SPEC ":-mpic -mpid=near} \
+   %{" FPIC2_SPEC ":-mpic -mpid=far}}"
 
 #undef DATA_SECTION_ASM_OP
 #define DATA_SECTION_ASM_OP "\t.section\t\".fardata\",\"aw\""
diff --git a/gcc/config/c6x/uclinux-elf.h b/gcc/config/c6x/uclinux-elf.h
index 5048b71..5a04b9d 100644
--- a/gcc/config/c6x/uclinux-elf.h
+++ b/gcc/config/c6x/uclinux-elf.h
@@ -40,7 +40,7 @@ 
 
 #undef LINK_SPEC
 #define LINK_SPEC ENDIAN_LINK_SPEC \
-  "%{shared} %{fpie|fPIE:-pie} \
+  "%{shared} %{" FPIE_SPEC ":-pie} \
   %{!shared: %{!static: \
    %{rdynamic:-export-dynamic} \
    %{!dynamic-linker:-dynamic-linker " UCLIBC_DYNAMIC_LINKER "}} \
diff --git a/gcc/config/cris/linux.h b/gcc/config/cris/linux.h
index bd57986..262aac5 100644
--- a/gcc/config/cris/linux.h
+++ b/gcc/config/cris/linux.h
@@ -79,13 +79,13 @@  along with GCC; see the file COPYING3.  If not see
  "--em=criself \
   %{!march=*:%{!mcpu=*:--march=v32}} \
   %{!fleading-underscore:--no-underscore}\
-  %{fPIC|fpic|fPIE|fpie: --pic}"
+  %{" FPIE_OR_FPIC_SPEC ": --pic}"
 #else
 # define CRIS_ASM_SUBTARGET_SPEC \
  "--em=criself \
   %{!march=*:%{!mcpu=*:--march=v10}} \
   %{!fleading-underscore:--no-underscore}\
-  %{fPIC|fpic|fPIE|fpie: --pic}"
+  %{" FPIE_OR_FPIC_SPEC ": --pic}"
 #endif
 
 /* Previously controlled by target_flags.  */
diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 66f504e..9a31952 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -214,7 +214,7 @@  extern GTY(()) int darwin_ms_struct;
 #define LINK_SYSROOT_SPEC "%{isysroot*:-syslibroot %*}"
 #endif
 
-#define PIE_SPEC "%{fpie|pie|fPIE:}"
+#define DARWIN_PIE_SPEC "%{fpie|pie|fPIE:}"
 
 /* Please keep the random linker options in alphabetical order (modulo
    'Z' and 'no' prefixes). Note that options taking arguments may appear
@@ -280,7 +280,7 @@  extern GTY(()) int darwin_ms_struct;
      %:version-compare(< 10.5 mmacosx-version-min= -multiply_defined) \
      %:version-compare(< 10.5 mmacosx-version-min= suppress)}} \
    %{Zmultiplydefinedunused*:-multiply_defined_unused %*} \
-   " PIE_SPEC " \
+   " DARWIN_PIE_SPEC " \
    %{prebind} %{noprebind} %{nofixprebinding} %{prebind_all_twolevel_modules} \
    %{read_only_relocs} \
    %{sectcreate*} %{sectorder*} %{seg1addr*} %{segprot*} \
diff --git a/gcc/config/darwin9.h b/gcc/config/darwin9.h
index 4204825..309c835 100644
--- a/gcc/config/darwin9.h
+++ b/gcc/config/darwin9.h
@@ -35,8 +35,8 @@  along with GCC; see the file COPYING3.  If not see
 /* Tell collect2 to run dsymutil for us as necessary.  */
 #define COLLECT_RUN_DSYMUTIL 1
 
-#undef PIE_SPEC
-#define PIE_SPEC \
+#undef DARWIN_PIE_SPEC
+#define DARWIN_PIE_SPEC \
   "%{fpie|pie|fPIE: \
      %{mdynamic-no-pic: %n'-mdynamic-no-pic' overrides '-pie', '-fpie' or '-fPIE'; \
       :-pie}}"
diff --git a/gcc/config/frv/frv.h b/gcc/config/frv/frv.h
index 978fefa..2d4cbdd 100644
--- a/gcc/config/frv/frv.h
+++ b/gcc/config/frv/frv.h
@@ -42,12 +42,12 @@ 
 "%{mno-pack:\
    %{!mhard-float:-msoft-float}\
    %{!mmedia:-mno-media}}\
- %{!mfdpic:%{fpic|fPIC: -multilib-library-pic}}\
+ %{!mfdpic:%{" FPIC_SPEC ": -multilib-library-pic}}\
  %{mfdpic:%{!fpic:%{!fpie:%{!fPIC:%{!fPIE:\
    	    %{!fno-pic:%{!fno-pie:%{!fno-PIC:%{!fno-PIE:-fPIE}}}}}}}} \
-	  %{!mno-inline-plt:%{O*:%{!O0:%{!Os:%{fpic|fPIC:-minline-plt} \
-                    %{!fpic:%{!fPIC:%{!O:%{!O1:%{!O2:-minline-plt}}}}}}}}} \
-	  %{!mno-gprel-ro:%{!fpic:%{!fpie:-mgprel-ro}}}} \
+	  %{!mno-inline-plt:%{O*:%{!O0:%{!Os:%{" FPIC_SPEC ":-minline-plt} \
+                    %{" NO_FPIC_SPEC ":%{!O:%{!O1:%{!O2:-minline-plt}}}}}}}} \
+	  %{!mno-gprel-ro:%{" NO_FPIE1_AND_FPIC1_SPEC ":-mgprel-ro}}} \
 "
 #ifndef SUBTARGET_DRIVER_SELF_SPECS
 # define SUBTARGET_DRIVER_SELF_SPECS
@@ -67,7 +67,7 @@ 
     %{mmuladd} %{mno-muladd} \
     %{mpack} %{mno-pack} \
     %{mno-fdpic:-mnopic} %{mfdpic} \
-    %{fpic|fpie: -mpic} %{fPIC|fPIE: -mPIC} %{mlibrary-pic}}"
+    %{" FPIE1_OR_FPIC1_SPEC ":-mpic} %{" FPIE2_OR_FPIC2_SPEC ":-mPIC} %{mlibrary-pic}}"
 
 #undef  STARTFILE_SPEC
 #define STARTFILE_SPEC "crt0%O%s frvbegin%O%s"
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/config/m32r/m32r.h b/gcc/config/m32r/m32r.h
index 66ceae0..2163de4 100644
--- a/gcc/config/m32r/m32r.h
+++ b/gcc/config/m32r/m32r.h
@@ -159,7 +159,7 @@ 
 
 /* Options to pass on to the assembler.  */
 #undef  ASM_SPEC
-#define ASM_SPEC "%(asm_cpu) %(relax) %{fpic|fpie:-K PIC} %{fPIC|fPIE:-K PIC}"
+#define ASM_SPEC "%(asm_cpu) %(relax) %{" FPIE_OR_FPIC_SPEC ":-K PIC}"
 
 #define LINK_SPEC "%{v} %(link_cpu) %(relax)"
 
diff --git a/gcc/config/m68k/m68k.h b/gcc/config/m68k/m68k.h
index 88356cc..74ebff7 100644
--- a/gcc/config/m68k/m68k.h
+++ b/gcc/config/m68k/m68k.h
@@ -40,7 +40,8 @@  along with GCC; see the file COPYING3.  If not see
 %{m68020-40:-m68040}%{m68020-60:-m68040}\
 %{mcpu=*:-mcpu=%*}%{march=*:-march=%*}\
 "
-#define ASM_PCREL_SPEC "%{fPIC|fpic|mpcrel:--pcrel} \
+#define ASM_PCREL_SPEC "%{" FPIC_SPEC ":--pcrel} \
+ %{mpcrel:%{" NO_FPIC_SPEC ":--pcrel}} \
  %{msep-data|mid-shared-library:--pcrel} \
 "
 
diff --git a/gcc/config/m68k/netbsd-elf.h b/gcc/config/m68k/netbsd-elf.h
index 3922d25..a180667 100644
--- a/gcc/config/m68k/netbsd-elf.h
+++ b/gcc/config/m68k/netbsd-elf.h
@@ -56,7 +56,8 @@  along with GCC; see the file COPYING3.  If not see
    to pass PIC code generation options.  */
 
 #undef ASM_SPEC
-#define ASM_SPEC "%(asm_cpu_spec) %{fpic|fpie:-k} %{fPIC|fPIE:-k -K}"
+#define ASM_SPEC \
+  "%(asm_cpu_spec) %{" FPIE1_OR_FPIC1_SPEC ":-k} %{" FPIE2_OR_FPIC2_SPEC ":-k -K}"
 
 /* Provide a LINK_SPEC appropriate for a NetBSD/m68k ELF target.  */
 
diff --git a/gcc/config/m68k/openbsd.h b/gcc/config/m68k/openbsd.h
index 34ea77f..379ca85 100644
--- a/gcc/config/m68k/openbsd.h
+++ b/gcc/config/m68k/openbsd.h
@@ -34,7 +34,8 @@  along with GCC; see the file COPYING3.  If not see
 #define CPP_SPEC "%{!msoft-float:-D__HAVE_68881__ -D__HAVE_FPU__} %{posix:-D_POSIX_SOURCE} %{pthread:-D_POSIX_THREADS}"
 
 #undef ASM_SPEC
-#define ASM_SPEC "%(asm_cpu_spec) %{fpic|fpie:-k} %{fPIC|fPIE:-k -K}"
+#define ASM_SPEC \
+  "%(asm_cpu_spec) %{" FPIE1_OR_FPIC1_SPEC ":-k} %{" FPIE2_OR_FPIC2_SPEC ":-k -K}"
 
 /* Layout of source language data types.  */
 
diff --git a/gcc/config/m68k/uclinux.h b/gcc/config/m68k/uclinux.h
index a6c3f7f..ef6a3a0 100644
--- a/gcc/config/m68k/uclinux.h
+++ b/gcc/config/m68k/uclinux.h
@@ -60,7 +60,7 @@  along with GCC; see the file COPYING3.  If not see
 
 /* -msep-data is the default PIC mode on this target.  */
 #define DRIVER_SELF_SPECS \
-  "%{fpie|fPIE|fpic|fPIC:%{!msep-data:%{!mid-shared-library: -msep-data}}}"
+  "%{" FPIE_OR_FPIC_SPEC ":%{!msep-data:%{!mid-shared-library: -msep-data}}}"
 
 /* The uclinux binary format relies on relocations against a segment being
    within that segment.  Conservatively apply this rule to individual
diff --git a/gcc/config/mips/gnu-user.h b/gcc/config/mips/gnu-user.h
index 28b00ed..dd4cf11 100644
--- a/gcc/config/mips/gnu-user.h
+++ b/gcc/config/mips/gnu-user.h
@@ -100,7 +100,7 @@  along with GCC; see the file COPYING3.  If not see
 #ifdef HAVE_AS_NO_SHARED
 /* Default to -mno-shared for non-PIC.  */
 # define NO_SHARED_SPECS \
-  " %{mshared|mno-shared|fpic|fPIC|fpie|fPIE:;:-mno-shared}"
+  " %{mshared|mno-shared:;:%{" NO_FPIE_AND_FPIC_SPEC ":-mno-shared}}"
 #else
 # define NO_SHARED_SPECS ""
 #endif
diff --git a/gcc/config/mips/vxworks.h b/gcc/config/mips/vxworks.h
index 96512a2..0fa40b0 100644
--- a/gcc/config/mips/vxworks.h
+++ b/gcc/config/mips/vxworks.h
@@ -70,7 +70,7 @@  VXWORKS_LINK_SPEC
 #define FUNCTION_PROFILER VXWORKS_FUNCTION_PROFILER
 
 #undef SUBTARGET_ASM_SPEC
-#define SUBTARGET_ASM_SPEC "%{mrtp:%{fPIC|fpic:-mvxworks-pic}}"
+#define SUBTARGET_ASM_SPEC "%{mrtp:%{" FPIC_SPEC ":-mvxworks-pic}}"
 
 #undef SUBTARGET_OVERRIDE_OPTIONS
 #define SUBTARGET_OVERRIDE_OPTIONS VXWORKS_OVERRIDE_OPTIONS
diff --git a/gcc/config/openbsd.h b/gcc/config/openbsd.h
index 5890078..cf66548 100644
--- a/gcc/config/openbsd.h
+++ b/gcc/config/openbsd.h
@@ -126,7 +126,7 @@  while (0)
    still uses a special flavor of gas that needs to be told when generating 
    pic code.  */
 #undef ASM_SPEC
-#define ASM_SPEC "%{fpic|fpie:-k} %{fPIC|fPIE:-k -K}"
+#define ASM_SPEC "%{" FPIE1_OR_FPIC1_SPEC ":-k} %{" FPIE2_OR_FPIC2_SPEC ":-k -K}"
 #endif
 
 /* Since we use gas, stdin -> - is a good idea.  */
diff --git a/gcc/config/rs6000/freebsd64.h b/gcc/config/rs6000/freebsd64.h
index 84066f6..7e630231 100644
--- a/gcc/config/rs6000/freebsd64.h
+++ b/gcc/config/rs6000/freebsd64.h
@@ -130,7 +130,7 @@  extern int dot_symbols;
 #define	LINK_OS_FREEBSD_SPEC "%{m32:%(link_os_freebsd_spec32)}%{!m32:%(link_os_freebsd_spec64)}"
 
 #define ASM_SPEC32 "-a32 \
-%{mrelocatable} %{mrelocatable-lib} %{fpic:-K PIC} %{fPIC:-K PIC} \
+%{mrelocatable} %{mrelocatable-lib} %{" FPIC_SPEC ":-K PIC} \
 %{memb} %{!memb: %{msdata=eabi: -memb}} \
 %{!mlittle: %{!mlittle-endian: %{!mbig: %{!mbig-endian: \
     %{mcall-freebsd: -mbig} \
diff --git a/gcc/config/rs6000/linux64.h b/gcc/config/rs6000/linux64.h
index 1b7f695..bec0fd9 100644
--- a/gcc/config/rs6000/linux64.h
+++ b/gcc/config/rs6000/linux64.h
@@ -192,7 +192,7 @@  extern int dot_symbols;
 #endif
 
 #define ASM_SPEC32 "-a32 \
-%{mrelocatable} %{mrelocatable-lib} %{fpic|fpie|fPIC|fPIE:-K PIC} \
+%{mrelocatable} %{mrelocatable-lib} %{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %{memb|msdata=eabi: -memb}"
 
 #define ASM_SPEC64 "-a64"
diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
index 41ed4ef..24618e3 100644
--- a/gcc/config/rs6000/sysv4.h
+++ b/gcc/config/rs6000/sysv4.h
@@ -530,7 +530,7 @@  extern int fixuplabelno;
 #undef	ASM_SPEC
 #define	ASM_SPEC "%(asm_cpu) \
 %{,assembler|,assembler-with-cpp: %{mregnames} %{mno-regnames}} \
-%{mrelocatable} %{mrelocatable-lib} %{fpic|fpie|fPIC|fPIE:-K PIC} \
+%{mrelocatable} %{mrelocatable-lib} %{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %{memb|msdata=eabi: -memb}" \
 ENDIAN_SELECT(" -mbig", " -mlittle", DEFAULT_ASM_ENDIAN)
 
diff --git a/gcc/config/rs6000/vxworks.h b/gcc/config/rs6000/vxworks.h
index b4f88de..7440172 100644
--- a/gcc/config/rs6000/vxworks.h
+++ b/gcc/config/rs6000/vxworks.h
@@ -90,7 +90,7 @@  VXWORKS_ADDITIONAL_CPP_SPEC
 #define ASM_SPEC \
 "%(asm_cpu) \
  %{,assembler|,assembler-with-cpp: %{mregnames} %{mno-regnames}} \
- %{mrelocatable} %{mrelocatable-lib} %{fpic:-K PIC} %{fPIC:-K PIC} -mbig"
+ %{mrelocatable} %{mrelocatable-lib} %{" FPIC_SPEC ":-K PIC} -mbig"
 
 #undef  LIB_SPEC
 #define LIB_SPEC VXWORKS_LIB_SPEC
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
index 4dceb16..adf6f3b 100644
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -127,7 +127,7 @@  along with GCC; see the file COPYING3.  If not see
 #define ASM_SPEC_BASE \
 "%{v:-V} %{Qy:} %{!Qn:-Qy} %{Ym,*} -s %(asm_cpu)"
 
-#define ASM_PIC_SPEC " %{fpic|fpie|fPIC|fPIE:-K PIC}"
+#define ASM_PIC_SPEC " %{" FPIE_OR_FPIC_SPEC ":-K PIC}"
 
 #undef ASM_CPU_DEFAULT_SPEC
 #define ASM_CPU_DEFAULT_SPEC \
diff --git a/gcc/config/sparc/freebsd.h b/gcc/config/sparc/freebsd.h
index fa40d4e..f469bf1 100644
--- a/gcc/config/sparc/freebsd.h
+++ b/gcc/config/sparc/freebsd.h
@@ -38,7 +38,7 @@  along with GCC; see the file COPYING3.  If not see
   while (0)
 
 #undef ASM_SPEC
-#define ASM_SPEC "%{fpic|fPIC|fpie|fPIE:-K PIC} %(asm_cpu)"
+#define ASM_SPEC "%{" FPIE_OR_FPIC_SPEC ":-K PIC} %(asm_cpu)"
 
 #define LINK_SPEC "%(link_arch)						\
   %{!mno-relax:%{!r:-relax}}						\
diff --git a/gcc/config/sparc/linux.h b/gcc/config/sparc/linux.h
index 56def4b..17e1e86 100644
--- a/gcc/config/sparc/linux.h
+++ b/gcc/config/sparc/linux.h
@@ -98,7 +98,7 @@  extern const char *host_detect_local_cpu (int argc, const char **argv);
 #undef ASM_SPEC
 #define ASM_SPEC "\
 -s \
-%{fpic|fPIC|fpie|fPIE:-K PIC} \
+%{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %{!.c:%{findirect-dispatch:-K PIC}} \
 %(asm_cpu) %(asm_relax)"
 
diff --git a/gcc/config/sparc/linux64.h b/gcc/config/sparc/linux64.h
index fa805fd..43da848 100644
--- a/gcc/config/sparc/linux64.h
+++ b/gcc/config/sparc/linux64.h
@@ -208,7 +208,7 @@  extern const char *host_detect_local_cpu (int argc, const char **argv);
 #undef ASM_SPEC
 #define ASM_SPEC "\
 -s \
-%{fpic|fPIC|fpie|fPIE:-K PIC} \
+%{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %{!.c:%{findirect-dispatch:-K PIC}} \
 %(asm_cpu) %(asm_arch) %(asm_relax)"
 
diff --git a/gcc/config/sparc/netbsd-elf.h b/gcc/config/sparc/netbsd-elf.h
index 165660c..6f99ed4 100644
--- a/gcc/config/sparc/netbsd-elf.h
+++ b/gcc/config/sparc/netbsd-elf.h
@@ -67,7 +67,7 @@  along with GCC; see the file COPYING3.  If not see
 #define USER_LABEL_PREFIX ""
 
 #undef ASM_SPEC
-#define ASM_SPEC "%{fpic|fPIC|fpie|fPIE:-K PIC} \
+#define ASM_SPEC "%{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %(asm_cpu) %(asm_arch) %(asm_relax)"
 
 #undef STDC_0_IN_SYSTEM_HEADERS
diff --git a/gcc/config/sparc/openbsd64.h b/gcc/config/sparc/openbsd64.h
index a84d210..b62f9c3 100644
--- a/gcc/config/sparc/openbsd64.h
+++ b/gcc/config/sparc/openbsd64.h
@@ -46,7 +46,7 @@  along with GCC; see the file COPYING3.  If not see
 
 #undef ASM_SPEC
 #define ASM_SPEC "\
--s %{fpic|fPIC|fpie|fPIE:-K PIC} \
+-s %{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %(asm_cpu) %(asm_arch) \
 "
 
diff --git a/gcc/config/sparc/sp-elf.h b/gcc/config/sparc/sp-elf.h
index 85da652..371bb6a 100644
--- a/gcc/config/sparc/sp-elf.h
+++ b/gcc/config/sparc/sp-elf.h
@@ -25,7 +25,7 @@  along with GCC; see the file COPYING3.  If not see
 #undef ASM_SPEC
 #define ASM_SPEC \
   "-s \
-   %{fpic|fpie|fPIC|fPIE:-K PIC} %(asm_cpu)"
+   %{" FPIE_OR_FPIC_SPEC ":-K PIC} %(asm_cpu)"
 
 /* Use the default.  */
 #undef LINK_SPEC
diff --git a/gcc/config/sparc/sp64-elf.h b/gcc/config/sparc/sp64-elf.h
index 2fbdfd0..43adbb5 100644
--- a/gcc/config/sparc/sp64-elf.h
+++ b/gcc/config/sparc/sp64-elf.h
@@ -32,7 +32,7 @@  along with GCC; see the file COPYING3.  If not see
 
 #undef ASM_SPEC
 #define ASM_SPEC "\
--s %{fpic|fPIC|fpie|fPIE:-K PIC} \
+-s %{" FPIE_OR_FPIC_SPEC ":-K PIC} \
 %(asm_cpu) %(asm_arch) \
 "
 
diff --git a/gcc/config/sparc/sparc.h b/gcc/config/sparc/sparc.h
index ce1b68b..106d993 100644
--- a/gcc/config/sparc/sparc.h
+++ b/gcc/config/sparc/sparc.h
@@ -380,7 +380,7 @@  extern enum cmodel sparc_cmodel;
 /* Special flags to the Sun-4 assembler when using pipe for input.  */
 
 #define ASM_SPEC "\
-%{!pg:%{!p:%{fpic|fPIC|fpie|fPIE:-k}}} %{keep-local-as-symbols:-L} \
+%{!pg:%{!p:%{" FPIE_OR_FPIC_SPEC ":-k}}} %{keep-local-as-symbols:-L} \
 %(asm_cpu) %(asm_relax)"
 
 /* This macro defines names of additional specifications to put in the specs
diff --git a/gcc/config/sparc/sysv4.h b/gcc/config/sparc/sysv4.h
index ff886b1..0b358bb 100644
--- a/gcc/config/sparc/sysv4.h
+++ b/gcc/config/sparc/sysv4.h
@@ -41,7 +41,7 @@  along with GCC; see the file COPYING3.  If not see
 #undef ASM_SPEC
 #define ASM_SPEC \
   "%{v:-V} %{Qy:} %{!Qn:-Qy} %{Ym,*} \
-   %{fpic|fPIC|fpie|fPIE:-K PIC} %(asm_cpu)"
+   %{" FPIE_OR_FPIC_SPEC ":-K PIC} %(asm_cpu)"
 
 /* Define the names of various pseudo-op used by the SPARC/svr4 assembler.
    Note that many of these are different from the typical pseudo-ops used
diff --git a/gcc/config/sparc/vxworks.h b/gcc/config/sparc/vxworks.h
index c66dba8..e356b25 100644
--- a/gcc/config/sparc/vxworks.h
+++ b/gcc/config/sparc/vxworks.h
@@ -34,7 +34,7 @@  along with GCC; see the file COPYING3.  If not see
 #define CPP_SUBTARGET_SPEC VXWORKS_ADDITIONAL_CPP_SPEC
 
 #undef ASM_SPEC
-#define ASM_SPEC "%{fpic|fPIC|fpie|fPIE:-K PIC} %(asm_cpu)"
+#define ASM_SPEC "%{" FPIE_OR_FPIC_SPEC ":-K PIC} %(asm_cpu)"
 
 #undef LIB_SPEC
 #define LIB_SPEC VXWORKS_LIB_SPEC
diff --git a/gcc/config/vax/linux.h b/gcc/config/vax/linux.h
index dfcad5b..0765ed9 100644
--- a/gcc/config/vax/linux.h
+++ b/gcc/config/vax/linux.h
@@ -32,7 +32,7 @@  along with GCC; see the file COPYING3.  If not see
 #define CPP_SPEC "%{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT}"
 
 #undef ASM_SPEC
-#define ASM_SPEC "%{fpic|fPIC:-k}"
+#define ASM_SPEC "%{" FPIC_SPEC ":-k}"
 
 #undef LINK_SPEC
 #define LINK_SPEC \
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 e7bbcb8..aa2a775 100644
--- a/gcc/defaults.h
+++ b/gcc/defaults.h
@@ -1241,6 +1241,18 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #endif
 #endif
 
+/* Default value for flag_pie when flag_pie is initialized to -1:
+   --enable-default-pie: Default flag_pie to -fPIE.
+   --disable-default-pie: Default flag_pie to 0.
+ */
+#ifdef ENABLE_DEFAULT_PIE
+# ifndef DEFAULT_FLAG_PIE
+#  define DEFAULT_FLAG_PIE 2
+# endif
+#else
+# define DEFAULT_FLAG_PIE 0
+#endif
+
 #ifdef GCC_INSN_FLAGS_H
 /* Dependent default target macro definitions
 
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 04332c1..24c4a33 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1621,6 +1621,9 @@  defaulted to o32.
 Currently, this option only affects sparc-linux, powerpc-linux, x86-linux,
 mips-linux and s390-linux.
 
+@item --enable-default-pie
+Turn on @option{-fPIE} and @option{-pie} by default.
+
 @item --enable-secureplt
 This option enables @option{-msecure-plt} by default for powerpc-linux.
 @ifnothtml
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index a451537..e25bd62 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -11302,6 +11302,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 8f01e42..a8e9c8c 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -737,12 +737,57 @@  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_FPIE1_SPEC		"fno-pie"
+#define FPIE1_SPEC		NO_FPIE1_SPEC ":;"
+#define NO_FPIE2_SPEC		"fno-PIE"
+#define FPIE2_SPEC		NO_FPIE2_SPEC ":;"
+#define NO_FPIE_SPEC		NO_FPIE1_SPEC "|" NO_FPIE2_SPEC
+#define FPIE_SPEC		NO_FPIE_SPEC ":;"
+#define NO_FPIC1_SPEC		"fno-pic"
+#define FPIC1_SPEC		NO_FPIC1_SPEC ":;"
+#define NO_FPIC2_SPEC		"fno-PIC"
+#define FPIC2_SPEC		NO_FPIC2_SPEC ":;"
+#define NO_FPIC_SPEC		NO_FPIC1_SPEC "|" NO_FPIC2_SPEC
+#define FPIC_SPEC		NO_FPIC_SPEC ":;"
+#define NO_FPIE1_AND_FPIC1_SPEC	NO_FPIE1_SPEC "|" NO_FPIC1_SPEC
+#define FPIE1_OR_FPIC1_SPEC	NO_FPIE1_AND_FPIC1_SPEC ":;"
+#define NO_FPIE2_AND_FPIC2_SPEC	NO_FPIE2_SPEC "|" NO_FPIC2_SPEC
+#define FPIE2_OR_FPIC2_SPEC	NO_FPIE2_AND_FPIC2_SPEC ":;"
+#define NO_FPIE_AND_FPIC_SPEC	NO_FPIE_SPEC "|" NO_FPIC_SPEC
+#define FPIE_OR_FPIC_SPEC	NO_FPIE_AND_FPIC_SPEC ":;"
+#else
+#define PIE_SPEC		"pie"
+#define NO_PIE_SPEC		PIE_SPEC "|r|shared:;"
+#define FPIE1_SPEC		"fpie"
+#define NO_FPIE1_SPEC		FPIE1_SPEC ":;"
+#define FPIE2_SPEC		"fPIE"
+#define NO_FPIE2_SPEC		FPIE2_SPEC ":;"
+#define FPIE_SPEC		FPIE1_SPEC "|" FPIE2_SPEC
+#define NO_FPIE_SPEC		FPIE_SPEC ":;"
+#define FPIC1_SPEC		"fpic"
+#define NO_FPIC1_SPEC		FPIC1_SPEC ":;"
+#define FPIC2_SPEC		"fPIC"
+#define NO_FPIC2_SPEC		FPIC2_SPEC ":;"
+#define FPIC_SPEC		FPIC1_SPEC "|" FPIC2_SPEC
+#define NO_FPIC_SPEC		FPIC_SPEC ":;"
+#define FPIE1_OR_FPIC1_SPEC	FPIE1_SPEC "|" FPIC1_SPEC
+#define NO_FPIE1_AND_FPIC1_SPEC	FPIE1_OR_FPIC1_SPEC ":;"
+#define FPIE2_OR_FPIC2_SPEC	FPIE2_SPEC "|" FPIC2_SPEC
+#define NO_FPIE2_AND_FPIC2_SPEC	FPIE1_OR_FPIC2_SPEC ":;"
+#define FPIE_OR_FPIC_SPEC	FPIE_SPEC "|" FPIC_SPEC
+#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 9deb8df..9793999 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -741,6 +741,15 @@  finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
 
   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 1f2b806..527d87b 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