diff mbox

Fix pr80044, -static and -pie insanity, and pr81170

Message ID 20170622152859.GG8406@bubble.grove.modra.org
State New
Headers show

Commit Message

Alan Modra June 22, 2017, 3:28 p.m. UTC
PR80044 notes that -static and -pie together behave differently when
gcc is configured with --enable-default-pie as compared to configuring
without (or --disable-default-pie).  This patch removes that
difference.  In both cases you now will have -static completely
overriding -pie.

Fixing this wasn't quite as simple as you'd expect, due to poor
separation of functionality.  PIE_SPEC didn't just mean that -pie was
on explicitly or by default, but also -r and -shared were *not* on.
Fortunately the three files touched by this patch are the only places
PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
of crtbegin*.o not properly hooked into a chain of if .. elseif ..
conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
-static and -shared.  Fixing that particular problem finally allows
PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.

Bootstrapped and regression tested powerpc64le-linux c,c++.  No
regressions and a bunch of --enable-default-pie failures squashed.
OK mainline and active branches?

Incidentally, there is a fairly strong case to be made for adding
-static to the -shared, -pie, -no-pie chain of RejectNegative's in
common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
done more than just the traditional "prevent linking with dynamic
libraries", as -static selects crtbeginT.o rather than crtbegin.o
on GNU systems.  Realizing this is what led me to close pr80044, which
I'd opened with the aim of making -pie -static work together (with the
traditional meaning of -static).  I don't that is worth doing, but
mention pr80044 in the changelog due to fixing the insane output
produced by -pie -static with --disable-default-pie.

	PR driver/80044
	PR target/81170
	* gcc.c (NO_PIE_SPEC): Delete.
	(PIE_SPEC): Define as !no-pie/pie.  Move static|shared|r exclusion..
	(LINK_PIE_SPEC): ..to here.
	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Correct
	chain of crtbegin*.o selection, update for PIE_SPEC changes and format.
	(GNU_USER_TARGET_ENDFILE_SPEC): Similarly.
	* config/sol2.h (STARTFILE_CRTBEGIN_SPEC): Similarly.
	(ENDFILE_CRTEND_SPEC): Similarly.
	* config/rs6000/sysv4.h (STARTFILE_LINUX_SPEC): Upgrade to
	match gnu-user.h startfile.
	(ENDFILE_LINUX_SPEC): Similarly.

Comments

Alan Modra June 29, 2017, 1:33 p.m. UTC | #1
Ping?  Linux startfile and endfile specs.
https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01678.html
Matthias Klose July 6, 2017, 1:34 p.m. UTC | #2
As seen in PR81295, the bootstrap is broken on powerpc-linux-gnu with
--enable-default-pie. Using that patch the bootstrap succeeds.  The bootstrap
works fine on both powerpc64 be and le targets.

Matthias

On 22.06.2017 17:28, Alan Modra wrote:
> PR80044 notes that -static and -pie together behave differently when
> gcc is configured with --enable-default-pie as compared to configuring
> without (or --disable-default-pie).  This patch removes that
> difference.  In both cases you now will have -static completely
> overriding -pie.
> 
> Fixing this wasn't quite as simple as you'd expect, due to poor
> separation of functionality.  PIE_SPEC didn't just mean that -pie was
> on explicitly or by default, but also -r and -shared were *not* on.
> Fortunately the three files touched by this patch are the only places
> PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
> the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
> PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
> addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
> another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
> of crtbegin*.o not properly hooked into a chain of if .. elseif ..
> conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
> -static and -shared.  Fixing that particular problem finally allows
> PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
> 
> Bootstrapped and regression tested powerpc64le-linux c,c++.  No
> regressions and a bunch of --enable-default-pie failures squashed.
> OK mainline and active branches?
> 
> Incidentally, there is a fairly strong case to be made for adding
> -static to the -shared, -pie, -no-pie chain of RejectNegative's in
> common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
> done more than just the traditional "prevent linking with dynamic
> libraries", as -static selects crtbeginT.o rather than crtbegin.o
> on GNU systems.  Realizing this is what led me to close pr80044, which
> I'd opened with the aim of making -pie -static work together (with the
> traditional meaning of -static).  I don't that is worth doing, but
> mention pr80044 in the changelog due to fixing the insane output
> produced by -pie -static with --disable-default-pie.
> 
> 	PR driver/80044
> 	PR target/81170
> 	* gcc.c (NO_PIE_SPEC): Delete.
> 	(PIE_SPEC): Define as !no-pie/pie.  Move static|shared|r exclusion..
> 	(LINK_PIE_SPEC): ..to here.
> 	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Correct
> 	chain of crtbegin*.o selection, update for PIE_SPEC changes and format.
> 	(GNU_USER_TARGET_ENDFILE_SPEC): Similarly.
> 	* config/sol2.h (STARTFILE_CRTBEGIN_SPEC): Similarly.
> 	(ENDFILE_CRTEND_SPEC): Similarly.
> 	* config/rs6000/sysv4.h (STARTFILE_LINUX_SPEC): Upgrade to
> 	match gnu-user.h startfile.
> 	(ENDFILE_LINUX_SPEC): Similarly.
> 
> diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
> index 2787a3d..de605b0 100644
> --- a/gcc/config/gnu-user.h
> +++ b/gcc/config/gnu-user.h
> @@ -50,19 +50,28 @@ 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_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}} \
> +  "%{shared:; \
> +     pg|p|profile:gcrt1.o%s; \
> +     static:crt1.o%s; \
> +     " PIE_SPEC ":Scrt1.o%s; \
> +     :crt1.o%s} \
> +   crti.o%s \
> +   %{static:crtbeginT.o%s; \
> +     shared|" PIE_SPEC ":crtbeginS.o%s; \
> +     :crtbegin.o%s} \
>     %{fvtable-verify=none:%s; \
>       fvtable-verify=preinit:vtv_start_preinit.o%s; \
>       fvtable-verify=std:vtv_start.o%s} \
>     " CRTOFFLOADBEGIN
>  #else
>  #define GNU_USER_TARGET_STARTFILE_SPEC \
> -  "%{!shared: %{pg|p|profile:gcrt1.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; \
> +     :crt1.o%s} \
> +   crti.o%s \
> +   %{static:crtbeginT.o%s; \
> +     shared|pie:crtbeginS.o%s; \
> +     :crtbegin.o%s} \
>     %{fvtable-verify=none:%s; \
>       fvtable-verify=preinit:vtv_start_preinit.o%s; \
>       fvtable-verify=std:vtv_start.o%s} \
> @@ -82,15 +91,20 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
>    "%{fvtable-verify=none:%s; \
>       fvtable-verify=preinit:vtv_end_preinit.o%s; \
>       fvtable-verify=std:vtv_end.o%s} \
> -   %{shared:crtendS.o%s;: %{" PIE_SPEC ":crtendS.o%s} \
> -   %{" NO_PIE_SPEC ":crtend.o%s}} crtn.o%s \
> +   %{static:crtend.o%s; \
> +     shared|" PIE_SPEC ":crtendS.o%s; \
> +     :crtend.o%s} \
> +   crtn.o%s \
>     " CRTOFFLOADEND
>  #else
>  #define GNU_USER_TARGET_ENDFILE_SPEC \
>    "%{fvtable-verify=none:%s; \
>       fvtable-verify=preinit:vtv_end_preinit.o%s; \
>       fvtable-verify=std:vtv_end.o%s} \
> -   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s \
> +   %{static:crtend.o%s; \
> +     shared|pie:crtendS.o%s; \
> +     :crtend.o%s} \
> +   crtn.o%s \
>     " CRTOFFLOADEND
>  #endif
>  #undef  ENDFILE_SPEC
> diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
> index 1ae7f9f..bc53a1c 100644
> --- a/gcc/config/sol2.h
> +++ b/gcc/config/sol2.h
> @@ -174,9 +174,9 @@ along with GCC; see the file COPYING3.  If not see
>  			    %{!ansi:values-Xa.o%s}"
>  
>  #if defined(HAVE_LD_PIE) && defined(HAVE_SOLARIS_CRTS)
> -#define STARTFILE_CRTBEGIN_SPEC "%{shared:crtbeginS.o%s} \
> -				 %{" PIE_SPEC ":crtbeginS.o%s} \
> -				 %{" NO_PIE_SPEC ":crtbegin.o%s}"
> +#define STARTFILE_CRTBEGIN_SPEC "%{static:crtbegin.o%s; \
> +				   shared|" PIE_SPEC ":crtbeginS.o%s; \
> +				   :crtbegin.o%s}"
>  #else
>  #define STARTFILE_CRTBEGIN_SPEC	"crtbegin.o%s"
>  #endif
> @@ -224,9 +224,9 @@ along with GCC; see the file COPYING3.  If not see
>  #endif
>  
>  #if defined(HAVE_LD_PIE) && defined(HAVE_SOLARIS_CRTS)
> -#define ENDFILE_CRTEND_SPEC "%{shared:crtendS.o%s;: \
> -			       %{" PIE_SPEC ":crtendS.o%s} \
> -			       %{" NO_PIE_SPEC ":crtend.o%s}}"
> +#define ENDFILE_CRTEND_SPEC "%{static:crtend.o%s; \
> +			       shared|" PIE_SPEC ":crtendS.o%s; \
> +			       :crtend.o%s}"
>  #else
>  #define ENDFILE_CRTEND_SPEC "crtend.o%s"
>  #endif
> diff --git a/gcc/gcc.c b/gcc/gcc.c
> index 6d724b2..625fae1 100644
> --- a/gcc/gcc.c
> +++ b/gcc/gcc.c
> @@ -878,8 +878,7 @@ proper position among the other output files.  */
>  #endif
>  
>  #ifdef ENABLE_DEFAULT_PIE
> -#define NO_PIE_SPEC		"no-pie|static"
> -#define PIE_SPEC		NO_PIE_SPEC "|r|shared:;"
> +#define PIE_SPEC		"!no-pie"
>  #define NO_FPIE1_SPEC		"fno-pie"
>  #define FPIE1_SPEC		NO_FPIE1_SPEC ":;"
>  #define NO_FPIE2_SPEC		"fno-PIE"
> @@ -900,7 +899,6 @@ proper position among the other output files.  */
>  #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"
> @@ -929,7 +927,7 @@ proper position among the other output files.  */
>  #else
>  #define LD_PIE_SPEC ""
>  #endif
> -#define LINK_PIE_SPEC "%{no-pie:} " "%{" PIE_SPEC ":" LD_PIE_SPEC "} "
> +#define LINK_PIE_SPEC "%{static|shared|r:;" PIE_SPEC ":" LD_PIE_SPEC "} "
>  #endif
>  
>  #ifndef LINK_BUILDID_SPEC
> diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
> index de38629..c80fcea 100644
> --- a/gcc/config/rs6000/sysv4.h
> +++ b/gcc/config/rs6000/sysv4.h
> @@ -757,24 +757,61 @@ ENDIAN_SELECT(" -mbig", " -mlittle", DEFAULT_ASM_ENDIAN)
>  #define CRTOFFLOADEND ""
>  #endif
>  
> +/* STARTFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_STARTFILE_SPEC
> +   but with the mnewlib ecrti.o%s selection substituted for crti.o%s.  */
>  #ifdef HAVE_LD_PIE
> -#define	STARTFILE_LINUX_SPEC "\
> -%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
> -%{mnewlib:ecrti.o%s;:crti.o%s} \
> -%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
> -" CRTOFFLOADBEGIN
> +#define	STARTFILE_LINUX_SPEC \
> +  "%{shared:; \
> +     pg|p|profile:gcrt1.o%s; \
> +     static:crt1.o%s; \
> +     " PIE_SPEC ":Scrt1.o%s; \
> +     :crt1.o%s} \
> +   %{mnewlib:ecrti.o%s;:crti.o%s} \
> +   %{static:crtbeginT.o%s; \
> +     shared|" PIE_SPEC ":crtbeginS.o%s; \
> +     :crtbegin.o%s} \
> +   %{fvtable-verify=none:%s; \
> +     fvtable-verify=preinit:vtv_start_preinit.o%s; \
> +     fvtable-verify=std:vtv_start.o%s} \
> +   " CRTOFFLOADBEGIN
>  #else
> -#define	STARTFILE_LINUX_SPEC "\
> -%{!shared: %{pg|p|profile:gcrt1.o%s;:crt1.o%s}} \
> -%{mnewlib:ecrti.o%s;:crti.o%s} \
> -%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
> -" CRTOFFLOADBEGIN
> +#define	STARTFILE_LINUX_SPEC \
> +  "%{shared:; \
> +     pg|p|profile:gcrt1.o%s; \
> +     :crt1.o%s} \
> +   %{mnewlib:ecrti.o%s;:crti.o%s} \
> +   %{static:crtbeginT.o%s; \
> +     shared|pie:crtbeginS.o%s; \
> +     :crtbegin.o%s} \
> +   %{fvtable-verify=none:%s; \
> +     fvtable-verify=preinit:vtv_start_preinit.o%s; \
> +     fvtable-verify=std:vtv_start.o%s} \
> +   " CRTOFFLOADBEGIN
>  #endif
>  
> -#define	ENDFILE_LINUX_SPEC "\
> -%{shared|pie:crtendS.o%s;:crtend.o%s} \
> -%{mnewlib:ecrtn.o%s;:crtn.o%s} \
> -" CRTOFFLOADEND
> +/* ENDFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_ENDFILE_SPEC
> +   but with the mnewlib ecrtn.o%s selection substituted for crtn.o%s.  */
> +#if defined HAVE_LD_PIE
> +#define ENDFILE_LINUX_SPEC \
> +  "%{fvtable-verify=none:%s; \
> +     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; \
> +     :crtend.o%s} \
> +   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
> +   " CRTOFFLOADEND
> +#else
> +#define ENDFILE_LINUX_SPEC \
> +  "%{fvtable-verify=none:%s; \
> +     fvtable-verify=preinit:vtv_end_preinit.o%s; \
> +     fvtable-verify=std:vtv_end.o%s} \
> +   %{static:crtend.o%s; \
> +     shared|pie:crtendS.o%s; \
> +     :crtend.o%s} \
> +   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
> +   " CRTOFFLOADEND
> +#endif
>  
>  #define LINK_START_LINUX_SPEC ""
>  
>
Alan Modra July 15, 2017, 12:45 p.m. UTC | #3
On Thu, Jun 29, 2017 at 11:03:56PM +0930, Alan Modra wrote:
> Ping?  Linux startfile and endfile specs.
> https://gcc.gnu.org/ml/gcc-patches/2017-06/msg01678.html

3 week ping.  Also fixes PR81295.
H.J. Lu July 15, 2017, 1:32 p.m. UTC | #4
On Thu, Jun 22, 2017 at 8:28 AM, Alan Modra <amodra@gmail.com> wrote:
> PR80044 notes that -static and -pie together behave differently when
> gcc is configured with --enable-default-pie as compared to configuring
> without (or --disable-default-pie).  This patch removes that
> difference.  In both cases you now will have -static completely
> overriding -pie.
>
> Fixing this wasn't quite as simple as you'd expect, due to poor
> separation of functionality.  PIE_SPEC didn't just mean that -pie was
> on explicitly or by default, but also -r and -shared were *not* on.
> Fortunately the three files touched by this patch are the only places
> PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
> the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
> PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
> addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
> another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
> of crtbegin*.o not properly hooked into a chain of if .. elseif ..
> conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
> -static and -shared.  Fixing that particular problem finally allows
> PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
>
> Bootstrapped and regression tested powerpc64le-linux c,c++.  No
> regressions and a bunch of --enable-default-pie failures squashed.
> OK mainline and active branches?
>
> Incidentally, there is a fairly strong case to be made for adding
> -static to the -shared, -pie, -no-pie chain of RejectNegative's in
> common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
> done more than just the traditional "prevent linking with dynamic
> libraries", as -static selects crtbeginT.o rather than crtbegin.o
> on GNU systems.  Realizing this is what led me to close pr80044, which
> I'd opened with the aim of making -pie -static work together (with the
> traditional meaning of -static).  I don't that is worth doing, but
> mention pr80044 in the changelog due to fixing the insane output
> produced by -pie -static with --disable-default-pie.
>

On x86-64, without --enable-default-pie, "-static -pie" and "-pie -static"
never worked since both -static and -pie are passed to linker, which
uses libc.a to build PIE.  With --enable-default-pie, -static and -pie
override each other.  What does your patch do on x86-64?  Make
with and without --enable-default-pie behave the same?  Does it
mean that both fail to create executable?
Alan Modra July 17, 2017, 12:33 p.m. UTC | #5
On Sat, Jul 15, 2017 at 06:32:40AM -0700, H.J. Lu wrote:
> On Thu, Jun 22, 2017 at 8:28 AM, Alan Modra <amodra@gmail.com> wrote:
> > PR80044 notes that -static and -pie together behave differently when
> > gcc is configured with --enable-default-pie as compared to configuring
> > without (or --disable-default-pie).  This patch removes that
> > difference.  In both cases you now will have -static completely
> > overriding -pie.
> >
> > Fixing this wasn't quite as simple as you'd expect, due to poor
> > separation of functionality.  PIE_SPEC didn't just mean that -pie was
> > on explicitly or by default, but also -r and -shared were *not* on.
> > Fortunately the three files touched by this patch are the only places
> > PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
> > the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
> > PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
> > addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
> > another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
> > of crtbegin*.o not properly hooked into a chain of if .. elseif ..
> > conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
> > -static and -shared.  Fixing that particular problem finally allows
> > PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
> >
> > Bootstrapped and regression tested powerpc64le-linux c,c++.  No
> > regressions and a bunch of --enable-default-pie failures squashed.
> > OK mainline and active branches?
> >
> > Incidentally, there is a fairly strong case to be made for adding
> > -static to the -shared, -pie, -no-pie chain of RejectNegative's in
> > common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
> > done more than just the traditional "prevent linking with dynamic
> > libraries", as -static selects crtbeginT.o rather than crtbegin.o
> > on GNU systems.  Realizing this is what led me to close pr80044, which
> > I'd opened with the aim of making -pie -static work together (with the
> > traditional meaning of -static).  I don't that is worth doing, but
> > mention pr80044 in the changelog due to fixing the insane output
> > produced by -pie -static with --disable-default-pie.
> >
> 
> On x86-64, without --enable-default-pie, "-static -pie" and "-pie -static"
> never worked since both -static and -pie are passed to linker, which
> uses libc.a to build PIE.

Yes, it's broken.

>  With --enable-default-pie, -static and -pie
> override each other.

No they don't.  -static overrides -pie.

>  What does your patch do on x86-64?  Make
> with and without --enable-default-pie behave the same?

Yes, as I said in my original post first paragraph.

>  Does it
> mean that both fail to create executable?

I try to leave that sort of patch to those better qualified.
Bootstrap and regression testing on x86_64-linux both
--enable-default-pie and --disable-default-pie was complete June 23.
H.J. Lu July 17, 2017, 1:01 p.m. UTC | #6
On Mon, Jul 17, 2017 at 5:33 AM, Alan Modra <amodra@gmail.com> wrote:
> On Sat, Jul 15, 2017 at 06:32:40AM -0700, H.J. Lu wrote:
>> On Thu, Jun 22, 2017 at 8:28 AM, Alan Modra <amodra@gmail.com> wrote:
>> > PR80044 notes that -static and -pie together behave differently when
>> > gcc is configured with --enable-default-pie as compared to configuring
>> > without (or --disable-default-pie).  This patch removes that
>> > difference.  In both cases you now will have -static completely
>> > overriding -pie.
>> >
>> > Fixing this wasn't quite as simple as you'd expect, due to poor
>> > separation of functionality.  PIE_SPEC didn't just mean that -pie was
>> > on explicitly or by default, but also -r and -shared were *not* on.
>> > Fortunately the three files touched by this patch are the only places
>> > PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
>> > the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
>> > PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
>> > addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
>> > another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
>> > of crtbegin*.o not properly hooked into a chain of if .. elseif ..
>> > conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
>> > -static and -shared.  Fixing that particular problem finally allows
>> > PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
>> >
>> > Bootstrapped and regression tested powerpc64le-linux c,c++.  No
>> > regressions and a bunch of --enable-default-pie failures squashed.
>> > OK mainline and active branches?
>> >
>> > Incidentally, there is a fairly strong case to be made for adding
>> > -static to the -shared, -pie, -no-pie chain of RejectNegative's in
>> > common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
>> > done more than just the traditional "prevent linking with dynamic
>> > libraries", as -static selects crtbeginT.o rather than crtbegin.o
>> > on GNU systems.  Realizing this is what led me to close pr80044, which
>> > I'd opened with the aim of making -pie -static work together (with the
>> > traditional meaning of -static).  I don't that is worth doing, but
>> > mention pr80044 in the changelog due to fixing the insane output
>> > produced by -pie -static with --disable-default-pie.
>> >
>>
>> On x86-64, without --enable-default-pie, "-static -pie" and "-pie -static"
>> never worked since both -static and -pie are passed to linker, which
>> uses libc.a to build PIE.
>
> Yes, it's broken.

This behavior may be useful for static PIE when libc.a is compiled with
-fPIE.

>>  With --enable-default-pie, -static and -pie
>> override each other.
>
> No they don't.  -static overrides -pie.
>
>>  What does your patch do on x86-64?  Make
>> with and without --enable-default-pie behave the same?
>
> Yes, as I said in my original post first paragraph.
>
>>  Does it
>> mean that both fail to create executable?
>
> I try to leave that sort of patch to those better qualified.
> Bootstrap and regression testing on x86_64-linux both
> --enable-default-pie and --disable-default-pie was complete June 23.
>

What is the new behavior?  The old  --disable-default-pie or old
--enable-default-pie?  Will static PIE be supported if libc is
compiled with -fPIE by default?
Alan Modra July 18, 2017, 4:02 a.m. UTC | #7
On Mon, Jul 17, 2017 at 06:01:47AM -0700, H.J. Lu wrote:
> On Mon, Jul 17, 2017 at 5:33 AM, Alan Modra <amodra@gmail.com> wrote:
> > On Sat, Jul 15, 2017 at 06:32:40AM -0700, H.J. Lu wrote:
> >> On Thu, Jun 22, 2017 at 8:28 AM, Alan Modra <amodra@gmail.com> wrote:
> >> > PR80044 notes that -static and -pie together behave differently when
> >> > gcc is configured with --enable-default-pie as compared to configuring
> >> > without (or --disable-default-pie).  This patch removes that
> >> > difference.  In both cases you now will have -static completely
> >> > overriding -pie.
> >> >
> >> > Fixing this wasn't quite as simple as you'd expect, due to poor
> >> > separation of functionality.  PIE_SPEC didn't just mean that -pie was
> >> > on explicitly or by default, but also -r and -shared were *not* on.
> >> > Fortunately the three files touched by this patch are the only places
> >> > PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
> >> > the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
> >> > PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
> >> > addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
> >> > another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
> >> > of crtbegin*.o not properly hooked into a chain of if .. elseif ..
> >> > conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
> >> > -static and -shared.  Fixing that particular problem finally allows
> >> > PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
> >> >
> >> > Bootstrapped and regression tested powerpc64le-linux c,c++.  No
> >> > regressions and a bunch of --enable-default-pie failures squashed.
> >> > OK mainline and active branches?
> >> >
> >> > Incidentally, there is a fairly strong case to be made for adding
> >> > -static to the -shared, -pie, -no-pie chain of RejectNegative's in
> >> > common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
> >> > done more than just the traditional "prevent linking with dynamic
> >> > libraries", as -static selects crtbeginT.o rather than crtbegin.o
> >> > on GNU systems.  Realizing this is what led me to close pr80044, which
> >> > I'd opened with the aim of making -pie -static work together (with the
> >> > traditional meaning of -static).  I don't that is worth doing, but
> >> > mention pr80044 in the changelog due to fixing the insane output
> >> > produced by -pie -static with --disable-default-pie.
> >> >
> >>
> >> On x86-64, without --enable-default-pie, "-static -pie" and "-pie -static"
> >> never worked since both -static and -pie are passed to linker, which
> >> uses libc.a to build PIE.
> >
> > Yes, it's broken.
> 
> This behavior may be useful for static PIE when libc.a is compiled with
> -fPIE.

Building a PIE from static archives using -static -pie or -pie -static
right now is broken, even if the archives are compiled -fpie/PIE.
I've looked into fixing it, and decided it wasn't worth the effort.
There are multiple problems.  See
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80044#c1

One reason why I believe it isn't worth fixing is that the meaning of
-static has changed over the years, from "link using static archives"
to "produce a static executable", and most certainly the meaning of
-static and -pie together is not clear.  I'll cite gold behaviour as
evidence: -static with -pie results in an error from gold.  See
https://sourceware.org/ml/binutils/2012-02/msg00119.html and following
discussion.

> >>  With --enable-default-pie, -static and -pie
> >> override each other.
> >
> > No they don't.  -static overrides -pie.
> >
> >>  What does your patch do on x86-64?  Make
> >> with and without --enable-default-pie behave the same?
> >
> > Yes, as I said in my original post first paragraph.
> >
> >>  Does it
> >> mean that both fail to create executable?
> >
> > I try to leave that sort of patch to those better qualified.
> > Bootstrap and regression testing on x86_64-linux both
> > --enable-default-pie and --disable-default-pie was complete June 23.
> >
> 
> What is the new behavior?  The old  --disable-default-pie or old
> --enable-default-pie?

You are asking questions to which the answer is given in the very
first paragraph posted in this thread, if you knew the current
--enable-default-pie behaviour.  -static overrides -pie.  ie. current
--enable-default-pie behaviour is unchanged.

> Will static PIE be supported if libc is
> compiled with -fPIE by default?

I covered this above, if you're asking about -static and -pie
together.  Unsupported both before and after my patch.  You *can* link
a working PIE from -fPIE archives, if that is what you want, with
"-pie -Wl,-Bstatic", both before and after my patch.

I'll ask a question of you.  Have you reviewed the patch and found
anything wrong with it?
H.J. Lu July 18, 2017, 12:36 p.m. UTC | #8
On Mon, Jul 17, 2017 at 9:02 PM, Alan Modra <amodra@gmail.com> wrote:
> On Mon, Jul 17, 2017 at 06:01:47AM -0700, H.J. Lu wrote:
>> On Mon, Jul 17, 2017 at 5:33 AM, Alan Modra <amodra@gmail.com> wrote:
>> > On Sat, Jul 15, 2017 at 06:32:40AM -0700, H.J. Lu wrote:
>> >> On Thu, Jun 22, 2017 at 8:28 AM, Alan Modra <amodra@gmail.com> wrote:
>> >> > PR80044 notes that -static and -pie together behave differently when
>> >> > gcc is configured with --enable-default-pie as compared to configuring
>> >> > without (or --disable-default-pie).  This patch removes that
>> >> > difference.  In both cases you now will have -static completely
>> >> > overriding -pie.
>> >> >
>> >> > Fixing this wasn't quite as simple as you'd expect, due to poor
>> >> > separation of functionality.  PIE_SPEC didn't just mean that -pie was
>> >> > on explicitly or by default, but also -r and -shared were *not* on.
>> >> > Fortunately the three files touched by this patch are the only places
>> >> > PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
>> >> > the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
>> >> > PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
>> >> > addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
>> >> > another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
>> >> > of crtbegin*.o not properly hooked into a chain of if .. elseif ..
>> >> > conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
>> >> > -static and -shared.  Fixing that particular problem finally allows
>> >> > PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
>> >> >
>> >> > Bootstrapped and regression tested powerpc64le-linux c,c++.  No
>> >> > regressions and a bunch of --enable-default-pie failures squashed.
>> >> > OK mainline and active branches?
>> >> >
>> >> > Incidentally, there is a fairly strong case to be made for adding
>> >> > -static to the -shared, -pie, -no-pie chain of RejectNegative's in
>> >> > common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
>> >> > done more than just the traditional "prevent linking with dynamic
>> >> > libraries", as -static selects crtbeginT.o rather than crtbegin.o
>> >> > on GNU systems.  Realizing this is what led me to close pr80044, which
>> >> > I'd opened with the aim of making -pie -static work together (with the
>> >> > traditional meaning of -static).  I don't that is worth doing, but
>> >> > mention pr80044 in the changelog due to fixing the insane output
>> >> > produced by -pie -static with --disable-default-pie.
>> >> >
>> >>
>> >> On x86-64, without --enable-default-pie, "-static -pie" and "-pie -static"
>> >> never worked since both -static and -pie are passed to linker, which
>> >> uses libc.a to build PIE.
>> >
>> > Yes, it's broken.
>>
>> This behavior may be useful for static PIE when libc.a is compiled with
>> -fPIE.
>
> Building a PIE from static archives using -static -pie or -pie -static
> right now is broken, even if the archives are compiled -fpie/PIE.
> I've looked into fixing it, and decided it wasn't worth the effort.
> There are multiple problems.  See
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80044#c1
>
> One reason why I believe it isn't worth fixing is that the meaning of
> -static has changed over the years, from "link using static archives"
> to "produce a static executable", and most certainly the meaning of
> -static and -pie together is not clear.  I'll cite gold behaviour as
> evidence: -static with -pie results in an error from gold.  See
> https://sourceware.org/ml/binutils/2012-02/msg00119.html and following
> discussion.
>
>> >>  With --enable-default-pie, -static and -pie
>> >> override each other.
>> >
>> > No they don't.  -static overrides -pie.
>> >
>> >>  What does your patch do on x86-64?  Make
>> >> with and without --enable-default-pie behave the same?
>> >
>> > Yes, as I said in my original post first paragraph.
>> >
>> >>  Does it
>> >> mean that both fail to create executable?
>> >
>> > I try to leave that sort of patch to those better qualified.
>> > Bootstrap and regression testing on x86_64-linux both
>> > --enable-default-pie and --disable-default-pie was complete June 23.
>> >
>>
>> What is the new behavior?  The old  --disable-default-pie or old
>> --enable-default-pie?
>
> You are asking questions to which the answer is given in the very
> first paragraph posted in this thread, if you knew the current
> --enable-default-pie behaviour.  -static overrides -pie.  ie. current
> --enable-default-pie behaviour is unchanged.
>
>> Will static PIE be supported if libc is
>> compiled with -fPIE by default?
>
> I covered this above, if you're asking about -static and -pie
> together.  Unsupported both before and after my patch.  You *can* link
> a working PIE from -fPIE archives, if that is what you want, with
> "-pie -Wl,-Bstatic", both before and after my patch.

I am working on compiling libc.a in glibc with -fPIE and building static
PIE.  This creates static executable with PIE:

gcc -nostdlib -nostartfiles -static -o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln -pie
-Wl,--no-dynamic-linker
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
--print-file-name=crtbeginS.o`
/export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
 -Wl,--start-group
/export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
-Wl,--end-group `gcc  --print-file-name=crtendS.o`
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o

Currently, it only works with gcc configured with -disable-default-pie.
With --enable-default-pie, I got

[hjl@gnu-tools-1 build-x86_64-linux]$ /usr/gcc-7.1.1-x32-pie/bin/gcc
-nostdlib -nostartfiles -static -o /tmp/sln -pie
-Wl,--no-dynamic-linker
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
--print-file-name=crtbeginS.o`
/export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
 -Wl,--start-group
/export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
-Wl,--end-group `gcc  --print-file-name=crtendS.o`
/export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o
/export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
In function `elf_machine_load_address':
/export/gnu/import/git/sources/glibc/elf/../sysdeps/x86_64/dl-machine.h:59:
undefined reference to `_DYNAMIC'
/export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
In function `elf_get_dynamic_info':
/export/gnu/import/git/sources/glibc/elf/get-dynamic-info.h:48:
undefined reference to `_DYNAMIC'
collect2: error: ld returned 1 exit status
[hjl@gnu-tools-1 build-x86_64-linux]$

Will your change fix it?
Alan Modra July 18, 2017, 2:09 p.m. UTC | #9
On Tue, Jul 18, 2017 at 05:36:49AM -0700, H.J. Lu wrote:
> I am working on compiling libc.a in glibc with -fPIE and building static
> PIE.  This creates static executable with PIE:
> 
> gcc -nostdlib -nostartfiles -static -o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln -pie
> -Wl,--no-dynamic-linker
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
> --print-file-name=crtbeginS.o`
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
>  -Wl,--start-group
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
> -Wl,--end-group `gcc  --print-file-name=crtendS.o`
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o
> 
> Currently, it only works with gcc configured with -disable-default-pie.
> With --enable-default-pie, I got
> 
> [hjl@gnu-tools-1 build-x86_64-linux]$ /usr/gcc-7.1.1-x32-pie/bin/gcc
> -nostdlib -nostartfiles -static -o /tmp/sln -pie
> -Wl,--no-dynamic-linker
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
> --print-file-name=crtbeginS.o`
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
>  -Wl,--start-group
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
> -Wl,--end-group `gcc  --print-file-name=crtendS.o`
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
> In function `elf_machine_load_address':
> /export/gnu/import/git/sources/glibc/elf/../sysdeps/x86_64/dl-machine.h:59:
> undefined reference to `_DYNAMIC'
> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
> In function `elf_get_dynamic_info':
> /export/gnu/import/git/sources/glibc/elf/get-dynamic-info.h:48:
> undefined reference to `_DYNAMIC'
> collect2: error: ld returned 1 exit status
> [hjl@gnu-tools-1 build-x86_64-linux]$
> 
> Will your change fix it?

You have got to be joking!  How should I know whether something will
work with some hare-brained scheme of yours?  One that seemingly
requires you to specify startup files by hand!
H.J. Lu July 18, 2017, 2:49 p.m. UTC | #10
On Tue, Jul 18, 2017 at 7:09 AM, Alan Modra <amodra@gmail.com> wrote:
> On Tue, Jul 18, 2017 at 05:36:49AM -0700, H.J. Lu wrote:
>> I am working on compiling libc.a in glibc with -fPIE and building static
>> PIE.  This creates static executable with PIE:
>>
>> gcc -nostdlib -nostartfiles -static -o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln -pie
>> -Wl,--no-dynamic-linker
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
>> --print-file-name=crtbeginS.o`
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
>>  -Wl,--start-group
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
>> -Wl,--end-group `gcc  --print-file-name=crtendS.o`
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o
>>
>> Currently, it only works with gcc configured with -disable-default-pie.
>> With --enable-default-pie, I got
>>
>> [hjl@gnu-tools-1 build-x86_64-linux]$ /usr/gcc-7.1.1-x32-pie/bin/gcc
>> -nostdlib -nostartfiles -static -o /tmp/sln -pie
>> -Wl,--no-dynamic-linker
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crt1.o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crti.o `gcc
>> --print-file-name=crtbeginS.o`
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/sln.o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/elf/static-stubs.o
>>  -Wl,--start-group
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a -lgcc
>> -Wl,--end-group `gcc  --print-file-name=crtendS.o`
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/csu/crtn.o
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
>> In function `elf_machine_load_address':
>> /export/gnu/import/git/sources/glibc/elf/../sysdeps/x86_64/dl-machine.h:59:
>> undefined reference to `_DYNAMIC'
>> /export/build/gnu/glibc-static-pie/build-x86_64-linux/libc.a(dl-support.o):
>> In function `elf_get_dynamic_info':
>> /export/gnu/import/git/sources/glibc/elf/get-dynamic-info.h:48:
>> undefined reference to `_DYNAMIC'
>> collect2: error: ld returned 1 exit status
>> [hjl@gnu-tools-1 build-x86_64-linux]$
>>
>> Will your change fix it?
>
> You have got to be joking!  How should I know whether something will
> work with some hare-brained scheme of yours?  One that seemingly
> requires you to specify startup files by hand!
>

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?
Alan Modra July 19, 2017, 5:11 a.m. UTC | #11
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.
Jeff Law Aug. 28, 2017, 2:27 p.m. UTC | #12
On 06/22/2017 09:28 AM, Alan Modra wrote:
> PR80044 notes that -static and -pie together behave differently when
> gcc is configured with --enable-default-pie as compared to configuring
> without (or --disable-default-pie).  This patch removes that
> difference.  In both cases you now will have -static completely
> overriding -pie.
> 
> Fixing this wasn't quite as simple as you'd expect, due to poor
> separation of functionality.  PIE_SPEC didn't just mean that -pie was
> on explicitly or by default, but also -r and -shared were *not* on.
> Fortunately the three files touched by this patch are the only places
> PIE_SPEC and NO_PIE_SPEC are used, so it isn't too hard to see that
> the reason PIE_SPEC and NO_PIE_SPEC are not inverses is the use of
> PIE_SPEC in LINK_PIE_SPEC.  So, move the inelegant symmetry breaking
> addition, to LINK_PIE_SPEC where it belongs.  Doing that showed
> another problem in gnu-user.h, with PIE_SPEC and NO_PIE_SPEC selection
> of crtbegin*.o not properly hooked into a chain of if .. elseif ..
> conditions, which required both PIE_SPEC and NO_PIE_SPEC to exclude
> -static and -shared.  Fixing that particular problem finally allows
> PIE_SPEC to serve just one purpose, and NO_PIE_SPEC to disappear.
> 
> Bootstrapped and regression tested powerpc64le-linux c,c++.  No
> regressions and a bunch of --enable-default-pie failures squashed.
> OK mainline and active branches?
> 
> Incidentally, there is a fairly strong case to be made for adding
> -static to the -shared, -pie, -no-pie chain of RejectNegative's in
> common.opt.  Since git 0d6378a9e (svn r48039) 2001-11-15, -static has
> done more than just the traditional "prevent linking with dynamic
> libraries", as -static selects crtbeginT.o rather than crtbegin.o
> on GNU systems.  Realizing this is what led me to close pr80044, which
> I'd opened with the aim of making -pie -static work together (with the
> traditional meaning of -static).  I don't that is worth doing, but
> mention pr80044 in the changelog due to fixing the insane output
> produced by -pie -static with --disable-default-pie.
> 
> 	PR driver/80044
> 	PR target/81170
> 	* gcc.c (NO_PIE_SPEC): Delete.
> 	(PIE_SPEC): Define as !no-pie/pie.  Move static|shared|r exclusion..
> 	(LINK_PIE_SPEC): ..to here.
> 	* config/gnu-user.h (GNU_USER_TARGET_STARTFILE_SPEC): Correct
> 	chain of crtbegin*.o selection, update for PIE_SPEC changes and format.
> 	(GNU_USER_TARGET_ENDFILE_SPEC): Similarly.
> 	* config/sol2.h (STARTFILE_CRTBEGIN_SPEC): Similarly.
> 	(ENDFILE_CRTEND_SPEC): Similarly.
> 	* config/rs6000/sysv4.h (STARTFILE_LINUX_SPEC): Upgrade to
> 	match gnu-user.h startfile.
> 	(ENDFILE_LINUX_SPEC): Similarly.
So sorry for the horrible delay.  What was the final resolution here?  I
saw a lot of back and forth with HJ and yourself.  80044 is
CLOSED/WONTFIX and 81170 has a patch attached to it, but is still in the
ASSIGNED state.

jeff
Alan Modra Aug. 28, 2017, 10:33 p.m. UTC | #13
On Mon, Aug 28, 2017 at 08:27:35AM -0600, Jeff Law wrote:
> So sorry for the horrible delay.  What was the final resolution here?  I
> saw a lot of back and forth with HJ and yourself.  80044 is
> CLOSED/WONTFIX and 81170 has a patch attached to it, but is still in the
> ASSIGNED state.

The patch went in trunk as 5a402d649 (r250974) and a9b2df6cc
(r251065).  PR81170 and PR81295 are still open due to needing a fix
for powerpc --enable-default-pie on the branches.  Last I checked,
both patches apply without any difficulty.

https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00831.html
Jeff Law Aug. 29, 2017, 5:21 a.m. UTC | #14
On 08/28/2017 04:33 PM, Alan Modra wrote:
> On Mon, Aug 28, 2017 at 08:27:35AM -0600, Jeff Law wrote:
>> So sorry for the horrible delay.  What was the final resolution here?  I
>> saw a lot of back and forth with HJ and yourself.  80044 is
>> CLOSED/WONTFIX and 81170 has a patch attached to it, but is still in the
>> ASSIGNED state.
> 
> The patch went in trunk as 5a402d649 (r250974) and a9b2df6cc
> (r251065).  PR81170 and PR81295 are still open due to needing a fix
> for powerpc --enable-default-pie on the branches.  Last I checked,
> both patches apply without any difficulty.
> 
> https://gcc.gnu.org/ml/gcc-patches/2017-08/msg00831.html
I think if you want to backport, go ahead.

jeff
diff mbox

Patch

diff --git a/gcc/config/gnu-user.h b/gcc/config/gnu-user.h
index 2787a3d..de605b0 100644
--- a/gcc/config/gnu-user.h
+++ b/gcc/config/gnu-user.h
@@ -50,19 +50,28 @@  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_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}} \
+  "%{shared:; \
+     pg|p|profile:gcrt1.o%s; \
+     static:crt1.o%s; \
+     " PIE_SPEC ":Scrt1.o%s; \
+     :crt1.o%s} \
+   crti.o%s \
+   %{static:crtbeginT.o%s; \
+     shared|" PIE_SPEC ":crtbeginS.o%s; \
+     :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
      fvtable-verify=std:vtv_start.o%s} \
    " CRTOFFLOADBEGIN
 #else
 #define GNU_USER_TARGET_STARTFILE_SPEC \
-  "%{!shared: %{pg|p|profile:gcrt1.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; \
+     :crt1.o%s} \
+   crti.o%s \
+   %{static:crtbeginT.o%s; \
+     shared|pie:crtbeginS.o%s; \
+     :crtbegin.o%s} \
    %{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_start_preinit.o%s; \
      fvtable-verify=std:vtv_start.o%s} \
@@ -82,15 +91,20 @@  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   "%{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
-   %{shared:crtendS.o%s;: %{" PIE_SPEC ":crtendS.o%s} \
-   %{" NO_PIE_SPEC ":crtend.o%s}} crtn.o%s \
+   %{static:crtend.o%s; \
+     shared|" PIE_SPEC ":crtendS.o%s; \
+     :crtend.o%s} \
+   crtn.o%s \
    " CRTOFFLOADEND
 #else
 #define GNU_USER_TARGET_ENDFILE_SPEC \
   "%{fvtable-verify=none:%s; \
      fvtable-verify=preinit:vtv_end_preinit.o%s; \
      fvtable-verify=std:vtv_end.o%s} \
-   %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s \
+   %{static:crtend.o%s; \
+     shared|pie:crtendS.o%s; \
+     :crtend.o%s} \
+   crtn.o%s \
    " CRTOFFLOADEND
 #endif
 #undef  ENDFILE_SPEC
diff --git a/gcc/config/sol2.h b/gcc/config/sol2.h
index 1ae7f9f..bc53a1c 100644
--- a/gcc/config/sol2.h
+++ b/gcc/config/sol2.h
@@ -174,9 +174,9 @@  along with GCC; see the file COPYING3.  If not see
 			    %{!ansi:values-Xa.o%s}"
 
 #if defined(HAVE_LD_PIE) && defined(HAVE_SOLARIS_CRTS)
-#define STARTFILE_CRTBEGIN_SPEC "%{shared:crtbeginS.o%s} \
-				 %{" PIE_SPEC ":crtbeginS.o%s} \
-				 %{" NO_PIE_SPEC ":crtbegin.o%s}"
+#define STARTFILE_CRTBEGIN_SPEC "%{static:crtbegin.o%s; \
+				   shared|" PIE_SPEC ":crtbeginS.o%s; \
+				   :crtbegin.o%s}"
 #else
 #define STARTFILE_CRTBEGIN_SPEC	"crtbegin.o%s"
 #endif
@@ -224,9 +224,9 @@  along with GCC; see the file COPYING3.  If not see
 #endif
 
 #if defined(HAVE_LD_PIE) && defined(HAVE_SOLARIS_CRTS)
-#define ENDFILE_CRTEND_SPEC "%{shared:crtendS.o%s;: \
-			       %{" PIE_SPEC ":crtendS.o%s} \
-			       %{" NO_PIE_SPEC ":crtend.o%s}}"
+#define ENDFILE_CRTEND_SPEC "%{static:crtend.o%s; \
+			       shared|" PIE_SPEC ":crtendS.o%s; \
+			       :crtend.o%s}"
 #else
 #define ENDFILE_CRTEND_SPEC "crtend.o%s"
 #endif
diff --git a/gcc/gcc.c b/gcc/gcc.c
index 6d724b2..625fae1 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -878,8 +878,7 @@  proper position among the other output files.  */
 #endif
 
 #ifdef ENABLE_DEFAULT_PIE
-#define NO_PIE_SPEC		"no-pie|static"
-#define PIE_SPEC		NO_PIE_SPEC "|r|shared:;"
+#define PIE_SPEC		"!no-pie"
 #define NO_FPIE1_SPEC		"fno-pie"
 #define FPIE1_SPEC		NO_FPIE1_SPEC ":;"
 #define NO_FPIE2_SPEC		"fno-PIE"
@@ -900,7 +899,6 @@  proper position among the other output files.  */
 #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"
@@ -929,7 +927,7 @@  proper position among the other output files.  */
 #else
 #define LD_PIE_SPEC ""
 #endif
-#define LINK_PIE_SPEC "%{no-pie:} " "%{" PIE_SPEC ":" LD_PIE_SPEC "} "
+#define LINK_PIE_SPEC "%{static|shared|r:;" PIE_SPEC ":" LD_PIE_SPEC "} "
 #endif
 
 #ifndef LINK_BUILDID_SPEC
diff --git a/gcc/config/rs6000/sysv4.h b/gcc/config/rs6000/sysv4.h
index de38629..c80fcea 100644
--- a/gcc/config/rs6000/sysv4.h
+++ b/gcc/config/rs6000/sysv4.h
@@ -757,24 +757,61 @@  ENDIAN_SELECT(" -mbig", " -mlittle", DEFAULT_ASM_ENDIAN)
 #define CRTOFFLOADEND ""
 #endif
 
+/* STARTFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_STARTFILE_SPEC
+   but with the mnewlib ecrti.o%s selection substituted for crti.o%s.  */
 #ifdef HAVE_LD_PIE
-#define	STARTFILE_LINUX_SPEC "\
-%{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} \
-%{mnewlib:ecrti.o%s;:crti.o%s} \
-%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
-" CRTOFFLOADBEGIN
+#define	STARTFILE_LINUX_SPEC \
+  "%{shared:; \
+     pg|p|profile:gcrt1.o%s; \
+     static:crt1.o%s; \
+     " PIE_SPEC ":Scrt1.o%s; \
+     :crt1.o%s} \
+   %{mnewlib:ecrti.o%s;:crti.o%s} \
+   %{static:crtbeginT.o%s; \
+     shared|" PIE_SPEC ":crtbeginS.o%s; \
+     :crtbegin.o%s} \
+   %{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_start_preinit.o%s; \
+     fvtable-verify=std:vtv_start.o%s} \
+   " CRTOFFLOADBEGIN
 #else
-#define	STARTFILE_LINUX_SPEC "\
-%{!shared: %{pg|p|profile:gcrt1.o%s;:crt1.o%s}} \
-%{mnewlib:ecrti.o%s;:crti.o%s} \
-%{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} \
-" CRTOFFLOADBEGIN
+#define	STARTFILE_LINUX_SPEC \
+  "%{shared:; \
+     pg|p|profile:gcrt1.o%s; \
+     :crt1.o%s} \
+   %{mnewlib:ecrti.o%s;:crti.o%s} \
+   %{static:crtbeginT.o%s; \
+     shared|pie:crtbeginS.o%s; \
+     :crtbegin.o%s} \
+   %{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_start_preinit.o%s; \
+     fvtable-verify=std:vtv_start.o%s} \
+   " CRTOFFLOADBEGIN
 #endif
 
-#define	ENDFILE_LINUX_SPEC "\
-%{shared|pie:crtendS.o%s;:crtend.o%s} \
-%{mnewlib:ecrtn.o%s;:crtn.o%s} \
-" CRTOFFLOADEND
+/* ENDFILE_LINUX_SPEC should be the same as GNU_USER_TARGET_ENDFILE_SPEC
+   but with the mnewlib ecrtn.o%s selection substituted for crtn.o%s.  */
+#if defined HAVE_LD_PIE
+#define ENDFILE_LINUX_SPEC \
+  "%{fvtable-verify=none:%s; \
+     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; \
+     :crtend.o%s} \
+   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
+   " CRTOFFLOADEND
+#else
+#define ENDFILE_LINUX_SPEC \
+  "%{fvtable-verify=none:%s; \
+     fvtable-verify=preinit:vtv_end_preinit.o%s; \
+     fvtable-verify=std:vtv_end.o%s} \
+   %{static:crtend.o%s; \
+     shared|pie:crtendS.o%s; \
+     :crtend.o%s} \
+   %{mnewlib:ecrtn.o%s;:crtn.o%s} \
+   " CRTOFFLOADEND
+#endif
 
 #define LINK_START_LINUX_SPEC ""