From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 4F0C23858CDA for ; Fri, 7 Oct 2022 09:54:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4F0C23858CDA Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1665136461; bh=vvMGBh/6DK4v5Ti8OI6CjHi0YKySJSDwo8lonwJcXdg=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=iQT4TCA8rbd+XXwVsKo9CdTlfJfz7Xw3csBQEooUA+S+KDdardfZ6qZ5PXR2eJHNd nru7/T/TEOzm0I/wQGxilSDUpWPpzTXESSEUbPrnohSjh3o+bBPRPfm0lcILWpk+wn 8WZG7jmoPSLIw5qL6v0ITc0SwVXRD4WL67YGMyrA= Received: from localhost.localdomain (xry111.site [IPv6:2001:470:683e::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 50B256687D; Fri, 7 Oct 2022 05:54:20 -0400 (EDT) Message-ID: <7ceff00687e8f768388e877d8d0c732c323238d5.camel@xry111.site> Subject: Ping: [PATCH] LoongArch: Fix the condition to use PC-relative addressing in start.S From: Xi Ruoyao To: libc-alpha@sourceware.org Cc: caiyinyu , Joseph Myers , Wang Xuerui Date: Fri, 07 Oct 2022 17:54:18 +0800 In-Reply-To: <20221002142309.900714-1-xry111@xry111.site> References: <20221002142309.900714-1-xry111@xry111.site> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.0 MIME-Version: 1.0 X-Spam-Status: No, score=-6.3 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FROM_SUSPICIOUS_NTLD,GIT_PATCH_0,LIKELY_SPAM_FROM,SPF_HELO_PASS,SPF_PASS,TXREP,T_PDS_OTHER_BAD_TLD autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Gentle ping. I see the conclusion from review meeting is: "Should just commit these as machine maintainers." Has caiyinyu been formally promoted as a machine maintainer now? On Sun, 2022-10-02 at 22:23 +0800, Xi Ruoyao wrote: > A start.o compiled from start.S with -DPIC and no -DSHARED is used by > both crt1.o and rcrt1.o.=C2=A0 So the LoongArch static PIE patch > unintentionally introduced PC-relative addressing for main and > __libc_start_main into crt1.o. >=20 > While the latest Binutils (trunk, which will be released as 2.40) > supports the PC-relative relocs against an external function by > creating > a PLT entry, the 2.39 release branch doesn't (and won't) support this. > An error is raised: >=20 > =C2=A0=C2=A0=C2=A0 "PLT stub does not represent and symbol not defined." >=20 > So, we need the following changes: >=20 > 1. Check if ld supports the PC-relative relocs against an external > =C2=A0=C2=A0 function.=C2=A0 If it's not supported, we deem static PIE un= supported. > 2. Change start.S.=C2=A0 If static PIE is supported, use PC-relative > =C2=A0=C2=A0 addressing for main and __libc_start_main and rely on the li= nker to > =C2=A0=C2=A0 create PLT entries.=C2=A0 Otherwise, restore the old behavio= r (using GOT > =C2=A0=C2=A0 to address these functions). >=20 > An alternative would be adding a new "static-pie-start.S", and some > custom logic into Makefile to build rcrt1.o with it.=C2=A0 And, restore > start.S to the state before static PIE change so crt1.o won't contain > PC-relative relocs against external symbols.=C2=A0 But I can't see any > benefit of this alternative, so I'd just keep it simple. >=20 > Tested by building glibc with the following configurations: >=20 > 1. Binutils trunk + GCC trunk.=C2=A0 Static PIE enabled.=C2=A0 All tests > =C2=A0=C2=A0 passed. > 2. Binutils 2.39 branch + GCC trunk.=C2=A0 Static PIE disabled.=C2=A0 Tes= ts > =C2=A0=C2=A0 related to ifunc failed (it's a known issue).=C2=A0 All othe= r tests > =C2=A0=C2=A0 passed. > 3. Binutils 2.39 branch + GCC 12 branch, cross compilation with > =C2=A0=C2=A0 build-many-glibcs.py from x86_64-linux-gnu.=C2=A0 Static PIE= disabled. > =C2=A0=C2=A0 Build succeeded. > --- > =C2=A0sysdeps/loongarch/configure=C2=A0=C2=A0=C2=A0 | 29 ++++++++++++++++= +++++++++---- > =C2=A0sysdeps/loongarch/configure.ac | 19 +++++++++++++++---- > =C2=A0sysdeps/loongarch/start.S=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 11 ++++++= +---- > =C2=A03 files changed, 47 insertions(+), 12 deletions(-) >=20 > diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure > index 6edd6d08a5..3046915ce2 100644 > --- a/sysdeps/loongarch/configure > +++ b/sysdeps/loongarch/configure > @@ -10,7 +10,7 @@ if ${libc_cv_static_pie_on_loongarch+:} false; then > : > =C2=A0=C2=A0 $as_echo_n "(cached) " >&6 > =C2=A0else > =C2=A0 > -=C2=A0 cat > conftest.S << EOF > +=C2=A0 cat > conftest1.S << EOF > =C2=A0.global _start > =C2=A0.type _start, @function > =C2=A0_start: > @@ -26,14 +26,35 @@ x: > =C2=A0=C2=A0 /* This should produce an R_LARCH_RELATIVE in the static PIE= .=C2=A0 */ > =C2=A0=C2=A0 .dword _start > =C2=A0EOF > +=C2=A0 cat > conftest2.S << EOF > +.global f > +.type f, @function > +f: > +=C2=A0 /* The linker should be able to handle this and produce a PLT > entry.=C2=A0 */ > +=C2=A0 la.pcrel \$t0, \$t0, external_func > +=C2=A0 jirl \$zero, \$t0, 0 > +EOF > + > =C2=A0=C2=A0 libc_cv_static_pie_on_loongarch=3Dno > -=C2=A0 if { ac_try=3D'${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie - > nostdlib -fPIE -o conftest conftest.S' > +=C2=A0 if { ac_try=3D'${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie - > nostdlib -fPIE -o conftest1 conftest1.S' > +=C2=A0 { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } > >&5 > +=C2=A0 (eval $ac_try) 2>&5 > +=C2=A0 ac_status=3D$? > +=C2=A0 $as_echo "$as_me:${as_lineno-$LINENO}: \$? =3D $ac_status" >&5 > +=C2=A0 test $ac_status =3D 0; }; } \ > +=C2=A0=C2=A0=C2=A0=C2=A0 && { ac_try=3D'LC_ALL=3DC $READELF -Wr conftest= 1 | grep -q > R_LARCH_RELATIVE' > +=C2=A0 { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } > >&5 > +=C2=A0 (eval $ac_try) 2>&5 > +=C2=A0 ac_status=3D$? > +=C2=A0 $as_echo "$as_me:${as_lineno-$LINENO}: \$? =3D $ac_status" >&5 > +=C2=A0 test $ac_status =3D 0; }; } \ > +=C2=A0=C2=A0=C2=A0=C2=A0 && { ac_try=3D'${CC-cc} $CFLAGS $CPPFLAGS $LDFL= AGS -shared -fPIC - > o conftest2.so conftest2.S' > =C2=A0=C2=A0 { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\"= "; } > >&5 > =C2=A0=C2=A0 (eval $ac_try) 2>&5 > =C2=A0=C2=A0 ac_status=3D$? > =C2=A0=C2=A0 $as_echo "$as_me:${as_lineno-$LINENO}: \$? =3D $ac_status" >= &5 > =C2=A0=C2=A0 test $ac_status =3D 0; }; } \ > -=C2=A0=C2=A0=C2=A0=C2=A0 && { ac_try=3D'LC_ALL=3DC $READELF -Wr conftest= | grep -q > R_LARCH_RELATIVE' > +=C2=A0=C2=A0=C2=A0=C2=A0 && { ac_try=3D'LC_ALL=3DC $READELF -Wr conftest= 2.so | grep -q > 'R_LARCH_JUMP_SLOT.*external_func'' > =C2=A0=C2=A0 { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\"= "; } > >&5 > =C2=A0=C2=A0 (eval $ac_try) 2>&5 > =C2=A0=C2=A0 ac_status=3D$? > @@ -42,7 +63,7 @@ EOF > =C2=A0=C2=A0 then > =C2=A0=C2=A0=C2=A0=C2=A0 libc_cv_static_pie_on_loongarch=3Dyes > =C2=A0=C2=A0 fi > -=C2=A0 rm -rf conftest.* > +=C2=A0 rm -rf conftest* > =C2=A0fi > =C2=A0{ $as_echo "$as_me:${as_lineno-$LINENO}: result: > $libc_cv_static_pie_on_loongarch" >&5 > =C2=A0$as_echo "$libc_cv_static_pie_on_loongarch" >&6; } > diff --git a/sysdeps/loongarch/configure.ac > b/sysdeps/loongarch/configure.ac > index a8a373bea3..06dd408ad9 100644 > --- a/sysdeps/loongarch/configure.ac > +++ b/sysdeps/loongarch/configure.ac > @@ -13,7 +13,7 @@ dnl satisify the requirement, but a distro may > backport static PIE support into > =C2=A0dnl earlier GCC or Binutils releases as well. > =C2=A0AC_CACHE_CHECK([if the toolchain is sufficient to build static PIE = on > LoongArch], > =C2=A0libc_cv_static_pie_on_loongarch, [ > -=C2=A0 cat > conftest.S << EOF > +=C2=A0 cat > conftest1.S << EOF > =C2=A0.global _start > =C2=A0.type _start, @function > =C2=A0_start: > @@ -29,13 +29,24 @@ x: > =C2=A0=C2=A0 /* This should produce an R_LARCH_RELATIVE in the static PIE= .=C2=A0 */ > =C2=A0=C2=A0 .dword _start > =C2=A0EOF > +=C2=A0 cat > conftest2.S << EOF > +.global f > +.type f, @function > +f: > +=C2=A0 /* The linker should be able to handle this and produce a PLT > entry.=C2=A0 */ > +=C2=A0 la.pcrel \$t0, \$t0, external_func > +=C2=A0 jirl \$zero, \$t0, 0 > +EOF > + > =C2=A0=C2=A0 libc_cv_static_pie_on_loongarch=3Dno > -=C2=A0 if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pi= e > -nostdlib -fPIE -o conftest conftest.S]) \ > -=C2=A0=C2=A0=C2=A0=C2=A0 && AC_TRY_COMMAND([LC_ALL=3DC $READELF -Wr conf= test | grep -q > R_LARCH_RELATIVE]) > +=C2=A0 if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pi= e > -nostdlib -fPIE -o conftest1 conftest1.S]) \ > +=C2=A0=C2=A0=C2=A0=C2=A0 && AC_TRY_COMMAND([LC_ALL=3DC $READELF -Wr conf= test1 | grep -q > R_LARCH_RELATIVE]) \ > +=C2=A0=C2=A0=C2=A0=C2=A0 && AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $= LDFLAGS -shared - > fPIC -o conftest2.so conftest2.S]) \ > +=C2=A0=C2=A0=C2=A0=C2=A0 && AC_TRY_COMMAND([LC_ALL=3DC $READELF -Wr conf= test2.so | grep -q > 'R_LARCH_JUMP_SLOT.*external_func']) > =C2=A0=C2=A0 then > =C2=A0=C2=A0=C2=A0=C2=A0 libc_cv_static_pie_on_loongarch=3Dyes > =C2=A0=C2=A0 fi > -=C2=A0 rm -rf conftest.*]) > +=C2=A0 rm -rf conftest* ]) > =C2=A0 > =C2=A0if test "$libc_cv_static_pie_on_loongarch" =3D yes; then > =C2=A0=C2=A0 AC_DEFINE(SUPPORT_STATIC_PIE) > diff --git a/sysdeps/loongarch/start.S b/sysdeps/loongarch/start.S > index 05cabd9b96..09e5a3c59c 100644 > --- a/sysdeps/loongarch/start.S > +++ b/sysdeps/loongarch/start.S > @@ -60,13 +60,16 @@ ENTRY (ENTRY_POINT) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0cfi_undefined (1) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0or=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0a5, a0, zero= /* rtld_fini */ > =C2=A0 > -#if defined(PIC) && !defined(SHARED) > +#if ENABLE_STATIC_PIE > =C2=A0/* For static PIE, the GOT cannot be used in _start because the GOT > entries are > -=C2=A0=C2=A0 offsets instead of real addresses before __libc_start_main.= =C2=A0 */ > +=C2=A0=C2=A0 offsets instead of real addresses before __libc_start_main. > +=C2=A0=C2=A0 __libc_start_main and/or main may be not local, so we rely = on the > linker to > +=C2=A0=C2=A0 produce PLT entries for them.=C2=A0 GNU ld >=3D 2.40 suppor= ts this.=C2=A0 */ > =C2=A0# define LA la.pcrel > =C2=A0#else > -/* We must get symbol main through GOT table, since main may not be > local. > -=C2=A0=C2=A0 For instance: googletest defines main in dynamic library.= =C2=A0 */ > +/* Old GNU ld (< 2.40) cannot handle PC relative address against a > non-local > +=C2=A0=C2=A0 function correctly.=C2=A0 We deem these old linkers failing= to support > static PIE > +=C2=A0=C2=A0 and load the addresses from GOT.=C2=A0 */ > =C2=A0# define LA la.got > =C2=A0#endif > =C2=A0 --=20 Xi Ruoyao School of Aerospace Science and Technology, Xidian University