"Fossies" - the Fresh Open Source Software Archive

Member "buildroot-2021.05/package/uclibc/0001-powerpc-fix-PIE-PIC-builds-with-newer-gcc-binutils-w.patch" (6 Jun 2021, 3879 Bytes) of package /linux/privat/buildroot-2021.05.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Diff source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 From 6c95eccff5eb43c8c3ad865d3b1316b7952cc58e Mon Sep 17 00:00:00 2001
    2 From: Yann Sionneau <yann@sionneau.net>
    3 Date: Fri, 28 May 2021 08:59:58 +0200
    4 Subject: [PATCH] powerpc: fix PIE/PIC builds with newer gcc/binutils which use
    5  secureplt by default
    6 
    7 This patch fixes segfault of all user space processes (including init, which caused a panic) on recent buildroot powerpc32 builds.
    8 
    9 The issue has been reported by Romain Naour in this thread: https://mailman.uclibc-ng.org/pipermail/devel/2021-May/002068.html
   10 
   11 Recent buildroot toolchain enables secure PLT in powerpc gcc.
   12 The latter will then supply -msecure-plt to gas invocations by default.
   13 Recent buildroot also enables PIE by default.
   14 
   15 For the secure PLT to work in PIC, the r30 register needs to point to the GOT.
   16 Old "bss plt" was just a one-instruction-wide PLT slot, pointed-to by a R_PPC_JMP_SLOT relocation, which was written on-the-fly to contain a branch instruction to the correct address. It therefore had to stay writable+executable, which you generally want to avoid for security reasons.
   17 New secure PLT only contains read-only code which loads the branch address from the writable GOT.
   18 
   19 Note: secure PLT without PIC does not need r30 to be set. Because offset between plt stub code and got is known at link-time. In this case the PLT entry looks like:
   20 1009b3e0 <__uClibc_main@plt>:
   21 1009b3e0:       3d 60 10 0e     lis     r11,4110
   22 1009b3e4:       81 6b 03 74     lwz     r11,884(r11)
   23 1009b3e8:       7d 69 03 a6     mtctr   r11
   24 1009b3ec:       4e 80 04 20     bctr
   25 
   26 Whereas secure PLT with PIC - offset between plt and got is unknown at link-time - looks like this:
   27 000af800 <00000000.plt_pic32.__uClibc_main>:
   28    af800:       81 7e 03 80     lwz     r11,896(r30)
   29    af804:       7d 69 03 a6     mtctr   r11
   30    af808:       4e 80 04 20     bctr
   31    af80c:       60 00 00 00     nop
   32 
   33 Upstream status: Pending:
   34 https://mailman.uclibc-ng.org/pipermail/devel/2021-May/002091.html
   35 
   36 Signed-off-by: Yann Sionneau <yann@sionneau.net>
   37 Signed-off-by: Romain Naour <romain.naour@gmail.com>
   38 ---
   39  Rules.mak                         | 3 ++-
   40  ldso/ldso/powerpc/dl-startup.h    | 3 +++
   41  libc/sysdeps/linux/powerpc/crt1.S | 4 ++++
   42  3 files changed, 9 insertions(+), 1 deletion(-)
   43 
   44 diff --git a/Rules.mak b/Rules.mak
   45 index 10f88b3de..60ac59b85 100644
   46 --- a/Rules.mak
   47 +++ b/Rules.mak
   48 @@ -477,9 +477,10 @@ ifeq ($(TARGET_ARCH),powerpc)
   49     PICFLAG:=-fpic
   50     PIEFLAG_NAME:=-fpie
   51     PPC_HAS_REL16:=$(shell printf "\t.text\n\taddis 11,30,_GLOBAL_OFFSET_TABLE_-.@ha\n" | $(CC) -c -x assembler -o /dev/null -  2> /dev/null && echo -n y || echo -n n)
   52 +   PPC_HAS_SECUREPLT:=$(shell $(CC) --verbose 2>&1 | grep -- --enable-secureplt > /dev/null && echo -n y || echo -n n)
   53 +   CPU_CFLAGS-$(PPC_HAS_SECUREPLT) += -DPPC_HAS_SECUREPLT
   54     CPU_CFLAGS-$(PPC_HAS_REL16)+= -DHAVE_ASM_PPC_REL16
   55     CPU_CFLAGS-$(CONFIG_E500) += "-D__NO_MATH_INLINES"
   56 -
   57  endif
   58  
   59  ifeq ($(TARGET_ARCH),bfin)
   60 diff --git a/ldso/ldso/powerpc/dl-startup.h b/ldso/ldso/powerpc/dl-startup.h
   61 index 8b2a517e2..7749395eb 100644
   62 --- a/ldso/ldso/powerpc/dl-startup.h
   63 +++ b/ldso/ldso/powerpc/dl-startup.h
   64 @@ -25,6 +25,9 @@ __asm__(
   65  #else
   66      "  bl  _GLOBAL_OFFSET_TABLE_-4@local\n" /*  Put our GOT pointer in r31, */
   67      "  mflr    31\n"
   68 +#endif
   69 +#ifdef PPC_HAS_SECUREPLT
   70 +    "   mr      30,31\n"
   71  #endif
   72      "  addi    1,1,16\n" /* Restore SP */
   73      "  lwz 7,_dl_skip_args@got(31)\n" /* load EA of _dl_skip_args */
   74 diff --git a/libc/sysdeps/linux/powerpc/crt1.S b/libc/sysdeps/linux/powerpc/crt1.S
   75 index 27bfc5a5a..3f5d056c0 100644
   76 --- a/libc/sysdeps/linux/powerpc/crt1.S
   77 +++ b/libc/sysdeps/linux/powerpc/crt1.S
   78 @@ -56,6 +56,10 @@ _start:
   79  # else
   80     bl  _GLOBAL_OFFSET_TABLE_-4@local
   81     mflr    r31
   82 +# endif
   83 +   /* in PIC/PIE, plt stubs need r30 to point to the GOT if using secure-plt */
   84 +# ifdef PPC_HAS_SECUREPLT
   85 +   mr  30,31
   86  # endif
   87  #endif
   88     /* Set up the small data pointer in r13.  */
   89 -- 
   90 2.31.1
   91