PING^N: [PATCH] Add --enable-static-pie to build static PIE [BZ #19574]

Message ID CAMe9rOq=9Fah3OB65VJ_J+jyQGAOPH9cnSzwm4516tUAM0-x_A@mail.gmail.com
State New, archived
Headers

Commit Message

H.J. Lu Dec. 1, 2017, 4:18 a.m. UTC
  On Thu, Nov 30, 2017 at 5:57 PM, Maciej W. Rozycki <macro@mips.com> wrote:
> On Thu, 30 Nov 2017, H.J. Lu wrote:
>
>> > However overall all these MIPS relocations are strictly non-PIC ones, so
>> > clearly this code has not been correctly built.
>>
>> That is MIPS specific issue.  It could be bfd_link_pic vs bfd_link_executable
>> in MIPS linker backend.
>
>  Well, these relocations are produced by GAS, which obviously has nothing
> to do with linking.  You need to find a way for them not to be produced in
> the first place, as they are not valid in a PIE executable link.
>
>> >  AFAICT you need to pass -DPIC to GCC when assembling sysdeps/mips/start.S
>> > to get a position-independent intermediate object.  The same applies to
>> > our other MIPS assembly sources.
>>
>> Everything is compiled with -fPIE -DPIC:
>>
>> +ifeq (yes,$(enable-static-pie))
>> +pic-default = -DPIC
>> +pie-default = $(pie-ccflag)
>> +ifeq (yes,$(have-static-pie))
>> +default-pie-ldflag = -static-pie
>> +else
>> +default-pie-ldflag = -Wl,-pie,--no-dynamic-linker,--eh-frame-hdr,-z,text
>> +endif
>> +endif
>
>  Well, that doesn't prove that these options actually make it to the GCC
> invocation line for say rcrt1.o.  Quoting the actual line for rcrt1.o from
> a build log would be more useful.
>

This patch fixes mips build with --enable-static-pie:


Can you try native mips build with --enable-static-pie using hjl/pie/static
branch?

Thanks.
  

Comments

Maciej W. Rozycki Dec. 1, 2017, 9:58 a.m. UTC | #1
On Thu, 30 Nov 2017, H.J. Lu wrote:

> This patch fixes mips build with --enable-static-pie:
> 
> diff --git a/sysdeps/mips/Makefile b/sysdeps/mips/Makefile
> index fd891ddf09..7ac6fa5031 100644
> --- a/sysdeps/mips/Makefile
> +++ b/sysdeps/mips/Makefile
> @@ -23,6 +23,9 @@ CPPFLAGS-crtn.S += $(pic-ccflag)
>  endif
> 
>  ASFLAGS-.os += $(pic-ccflag)
> +# libc.a and libc_p.a must be compiled with -fPIE/-fpie for static PIE.
> +ASFLAGS-.o += $(pie-default)
> +ASFLAGS-.op += $(pie-default)
> 
>  ifeq ($(subdir),elf)
>  ifneq ($(o32-fpabi),)
> 
> Can you try native mips build with --enable-static-pie using hjl/pie/static
> branch?

 I'm not sure if I'm set up for a native glibc build right now, I haven't 
done one for a while now.  I could try a cross build right away, however I 
am too loaded with other stuff to commit to do testing for you at the 
moment.

 Perhaps Joseph would be kind enough, as the MIPS port maintainer -- 
Joseph?

  Maciej
  
Joseph Myers Dec. 1, 2017, 12:27 p.m. UTC | #2
On Fri, 1 Dec 2017, Maciej W. Rozycki wrote:

> > Can you try native mips build with --enable-static-pie using hjl/pie/static
> > branch?
> 
>  I'm not sure if I'm set up for a native glibc build right now, I haven't 
> done one for a while now.  I could try a cross build right away, however I 
> am too loaded with other stuff to commit to do testing for you at the 
> moment.

I've not done native MIPS builds at all, but I don't think native versus 
cross is relevant here.  The main thing is that the wiki instructions, if 
and when this feature reaches consensus for inclusion on master, give 
sufficient information on what testing is needed of whether the feature 
works on a given architecture before that architecture can be removed from 
the list of those that haven't had execution testing (whether or not the 
build completes OK for them).  For example, are any special GCC configure 
options needed, or just the glibc configure option --enable-static-pie?  
Does GCC need to be a version (i.e. GCC 8 or later) that supports the 
-static-pie option in order to test this glibc feature?
  
H.J. Lu Dec. 1, 2017, 1:35 p.m. UTC | #3
On Fri, Dec 1, 2017 at 4:27 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Fri, 1 Dec 2017, Maciej W. Rozycki wrote:
>
>> > Can you try native mips build with --enable-static-pie using hjl/pie/static
>> > branch?
>>
>>  I'm not sure if I'm set up for a native glibc build right now, I haven't
>> done one for a while now.  I could try a cross build right away, however I
>> am too loaded with other stuff to commit to do testing for you at the
>> moment.
>
> I've not done native MIPS builds at all, but I don't think native versus
> cross is relevant here.  The main thing is that the wiki instructions, if
> and when this feature reaches consensus for inclusion on master, give
> sufficient information on what testing is needed of whether the feature
> works on a given architecture before that architecture can be removed from
> the list of those that haven't had execution testing (whether or not the
> build completes OK for them).  For example, are any special GCC configure
> options needed, or just the glibc configure option --enable-static-pie?
> Does GCC need to be a version (i.e. GCC 8 or later) that supports the
> -static-pie option in order to test this glibc feature?
>

GCC must support PIE to build static PIE glibc.  The main issue is linker.
Many ELF linker backends have various issues with PIE, especially
static PIE.  Other than those, there are no special requirements.

To use static PIE glibc to create static PIE with "gcc -static-pie", GCC 8
is needed.  I also have backported it to GCC 7 on hjl/pie/gcc-7-branch
branch in gcc git repo.
  

Patch

diff --git a/sysdeps/mips/Makefile b/sysdeps/mips/Makefile
index fd891ddf09..7ac6fa5031 100644
--- a/sysdeps/mips/Makefile
+++ b/sysdeps/mips/Makefile
@@ -23,6 +23,9 @@  CPPFLAGS-crtn.S += $(pic-ccflag)
 endif

 ASFLAGS-.os += $(pic-ccflag)
+# libc.a and libc_p.a must be compiled with -fPIE/-fpie for static PIE.
+ASFLAGS-.o += $(pie-default)
+ASFLAGS-.op += $(pie-default)

 ifeq ($(subdir),elf)
 ifneq ($(o32-fpabi),)