From patchwork Mon May 24 19:02:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yann Sionneau X-Patchwork-Id: 1482929 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=uclibc-ng.org (client-ip=89.238.66.15; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; secure) header.d=sionneau.net header.i=@sionneau.net header.a=rsa-sha1 header.s=selectormx3 header.b=tqQbNvMz; dkim-atps=neutral Received: from helium.openadk.org (helium.openadk.org [89.238.66.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FpmnP2Qksz9sCD for ; Tue, 25 May 2021 05:03:02 +1000 (AEST) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id 5D9E7100DD; Mon, 24 May 2021 21:02:55 +0200 (CEST) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from mx3.sionneau.net (mx3.sionneau.net [163.172.183.177]) by helium.openadk.org (Postfix) with ESMTPS id 8F9EE100DD for ; Mon, 24 May 2021 21:02:37 +0200 (CEST) Received: from mx3.sionneau.net (localhost [127.0.0.1]) by mx3.sionneau.net. (OpenSMTPD) with ESMTP id b2d3d63e for ; Mon, 24 May 2021 19:02:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=simple; d=sionneau.net; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; s=selectormx3; bh=i9DewEgj/L9zulTJhSLQvFBa5j4=; b=tqQbNvMzmlk6R lFt139biPFh7/AGZ+sZE/2Tjfjv+5Npm7QbuKoDoKay+5WQG7qiHjSV///JN6qOP BfPieaPgGAWVr228dJut7qXKDNqGuP4rcCKIVT1/FbazwCm/AnmtgwFCQwjeLJkQ l4R8RxTGYNZ3J1hMpDVibLFtxsWMbE= DomainKey-Signature: a=rsa-sha1; c=simple; d=sionneau.net; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; q=dns; s=selectormx3; b=DUjKdbm//ZY8wmYeU23Yfp553k7HERkv1Yqy9ku 0BpSWj632quduNVJRm2GPztdL4j5nBmSfWreXnMxrQQK+dfg3itTIs8AWCu+Shea psVotFfLc+6fnIdn02ScASs+zpsS4H9Ds6qYU3YMGCHozVhiqpYQAAtE1y9FT+uP eHe8= Received: by mx3.sionneau.net (OpenSMTPD) with ESMTPSA id 15c4d439 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 24 May 2021 19:02:31 +0000 (UTC) From: yann@sionneau.net To: devel@uclibc-ng.org Date: Mon, 24 May 2021 21:02:09 +0200 Message-Id: <20210524190209.2598507-1-yann@sionneau.net> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [uclibc-ng-devel] [PATCH] powerpc: fix builds with newer gcc/binutils which use secureplt by default X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" From: Yann Sionneau This patch fixes segfault of all user space processes (including init, which caused a panic) on recent buildroot powerpc32 builds. Recent buildroot toolchain enables secure PLT in powerpc gcc. The latter will then supply -msecure-plt to gas invocations by default. For the secure PLT to work, the r30 register needs to point to the GOT. 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. New secure PLT only contains read-only code which loads the branch address from the writable GOT. Signed-off-by: Yann Sionneau Tested-by: Romain Naour --- Rules.mak | 4 +++- ldso/ldso/powerpc/dl-startup.h | 3 +++ libc/sysdeps/linux/powerpc/crt1.S | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Rules.mak b/Rules.mak index 1fa09be23..0ab41e800 100644 --- a/Rules.mak +++ b/Rules.mak @@ -58,6 +58,7 @@ LD = $(CROSS_COMPILE)ld NM = $(CROSS_COMPILE)nm OBJDUMP = $(CROSS_COMPILE)objdump STRIPTOOL = $(CROSS_COMPILE)strip +READELF = $(CROSS_COMPILE)readelf INSTALL = install LN = ln @@ -483,9 +484,10 @@ ifeq ($(TARGET_ARCH),powerpc) PICFLAG:=-fpic PIEFLAG_NAME:=-fpie 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) + PPC_HAS_SECUREPLT:=$(shell tmpfile=$$(mktemp); printf '\#include \nint main(void) { puts("hello"); return 0;}' > $${tmpfile}.c; $(CC) -x c $${tmpfile}.c -o $${tmpfile}; $(READELF) -d $${tmpfile} | grep PPC_GOT > /dev/null && echo -n y || echo -n n; rm $${tmpfile}.c $${tmpfile}) + CPU_CFLAGS-$(PPC_HAS_SECUREPLT) += -DPPC_HAS_SECUREPLT CPU_CFLAGS-$(PPC_HAS_REL16)+= -DHAVE_ASM_PPC_REL16 CPU_CFLAGS-$(CONFIG_E500) += "-D__NO_MATH_INLINES" - endif ifeq ($(TARGET_ARCH),bfin) diff --git a/ldso/ldso/powerpc/dl-startup.h b/ldso/ldso/powerpc/dl-startup.h index 8b2a517e2..7749395eb 100644 --- a/ldso/ldso/powerpc/dl-startup.h +++ b/ldso/ldso/powerpc/dl-startup.h @@ -25,6 +25,9 @@ __asm__( #else " bl _GLOBAL_OFFSET_TABLE_-4@local\n" /* Put our GOT pointer in r31, */ " mflr 31\n" +#endif +#ifdef PPC_HAS_SECUREPLT + " mr 30,31\n" #endif " addi 1,1,16\n" /* Restore SP */ " lwz 7,_dl_skip_args@got(31)\n" /* load EA of _dl_skip_args */ diff --git a/libc/sysdeps/linux/powerpc/crt1.S b/libc/sysdeps/linux/powerpc/crt1.S index 27bfc5a5a..78b946ad6 100644 --- a/libc/sysdeps/linux/powerpc/crt1.S +++ b/libc/sysdeps/linux/powerpc/crt1.S @@ -47,7 +47,7 @@ _start: mr r9,r1 /* Save the stack pointer and pass it to __uClibc_main */ clrrwi r1,r1,4 /* Align stack ptr to 16 bytes */ -#ifdef __PIC__ +#if defined(__PIC__) || defined(PPC_HAS_SECUREPLT) # ifdef HAVE_ASM_PPC_REL16 bcl 20,31,1f 1: mflr r31 @@ -57,6 +57,9 @@ _start: bl _GLOBAL_OFFSET_TABLE_-4@local mflr r31 # endif +#endif +#ifdef PPC_HAS_SECUREPLT + mr 30,31 #endif /* Set up the small data pointer in r13. */ #ifdef __PIC__