From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2c.google.com (mail-oa1-x2c.google.com [IPv6:2001:4860:4864:20::2c]) by sourceware.org (Postfix) with ESMTPS id 53AD53858C52 for ; Fri, 23 Sep 2022 13:23:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 53AD53858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-1280590722dso269112fac.1 for ; Fri, 23 Sep 2022 06:23:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date; bh=7r7UO7u9uyrP9TpQ3F2xBOkEsPUL2vO1HWYLlhtb71k=; b=YVUUtUVi5/ojm6+NlWvfw1Ws7j//4WvI4nmuRF/eXuawh+QAD+p6R7KcwvPVOjRQga sOig4TiIYviRmrBg7cQhq8yjEyIzCpUfdJ963TOkMa0rz6m5N0+6d1JyBbaHTq4+yrFH 3JQASP/jNhP0sF+2NG2dhEtZXmldY6HlaJ1nPHS3w6dIDOP8xu7UxS8lblunbNgaSC09 zUeZ8uXObCQpy0ldP8WRLCHcmvE7KM5WsUwKmK7votXbgso8yXra6AXFBOQxtoxXjE8U xv1qBCK7PPMfK5jMrWAM2pSLsj+dk9awm+2eySL/emmtJQacNXcsD+XAXHMOOl1Qa45h FoHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date; bh=7r7UO7u9uyrP9TpQ3F2xBOkEsPUL2vO1HWYLlhtb71k=; b=qAbWrAN/ONvoUUO0a3iQCXSi94XlKyhr1aNufYsMdMA5ARdpCXMEsIoHCmfJH6Zuot lhbktTggGP3KugeUz3B9ce7jrmYLjOMe5X4qbITwzGWJnNezSvW/8v9J52uE7B7WUfh5 gVmPAkoAN01ix8Tv0P/xSYT44Scz4XxoqOU0IPtLZtlizJYF0fb5clyEyX8vFj/da34m oH5tgF5invXVq7s5StDpoHGE4Q0R1MTxfiZZkE4qCsggoNKjipmi0dE0GCjqMcr9eBro Lt4yMtirewXocFuVr5rmHQs/LUORF05k5+MAZWiUtUZ306fihXm/2UHim8alEtpJV+r/ cmbw== X-Gm-Message-State: ACrzQf2gndC5rr/nXLALgXKOd84SYXPibVLDtfymlNmmAFpLhu7I3Q15 wFy/U9MVZ0YPLPeAbgMZhb/UlaDTU0icF+A9 X-Google-Smtp-Source: AMsMyM5ByTA1lmkfFXw7A2FqbybZYEMh4MLuIbAeC97TVqewvwZmPDDHDtURTEAjicAp/JHXktbDRQ== X-Received: by 2002:a05:6871:6a7:b0:127:a331:1e65 with SMTP id l39-20020a05687106a700b00127a3311e65mr10909258oao.11.1663939418650; Fri, 23 Sep 2022 06:23:38 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c1:c266:202e:f71c:c0e7:6b4e? ([2804:1b3:a7c1:c266:202e:f71c:c0e7:6b4e]) by smtp.gmail.com with ESMTPSA id y1-20020a056870b00100b0011d5228d8e6sm4489675oae.25.2022.09.23.06.23.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 23 Sep 2022 06:23:38 -0700 (PDT) Message-ID: Date: Fri, 23 Sep 2022 10:23:35 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.3.0 Subject: Re: [PATCH v2 1/1] LoongArch: Add static PIE support Content-Language: en-US To: Xi Ruoyao , libc-alpha@sourceware.org Cc: caiyinyu , Chenghua Xu , liuzhensong , Lulu Cheng , Wang Xuerui References: <20220923111647.48303-1-xry111@xry111.site> <20220923111647.48303-2-xry111@xry111.site> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20220923111647.48303-2-xry111@xry111.site> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: On 23/09/22 08:16, Xi Ruoyao via Libc-alpha wrote: > If the compiler is new enough, enable static PIE support. In the stati= c > PIE version of _start (in rcrt1.o), use la.pcrel instead of la.got > because in a static PIE we cannot use GOT entries until the dynamic > relocations for GOT are resolved. The patch looks ok and with a recent GCC I see both SUPPORT_STATIC_PIE and ENABLE_STATIC_PIE being set. $ loongarch64-glibc-linux-gnu-gcc -v 2>&1 | tail -n1 gcc version 13.0.0 20220923 (experimental) [releases/gcc-12 r12-8552-g659= 41a91091] (GCC) $ loongarch64-glibc-linux-gnu-ld -v GNU ld (GNU Binutils) 2.39.0.20220923 However I can't actually build glibc: make[2]: Entering directory '/home/azanella/Projects/glibc/glibc-git/supp= ort' /home/azanella/toolchain/install/compilers//loongarch64-linux-gnu-lp64d/b= in/loongarch64-glibc-linux-gnu-gcc -o /home/azanella/Projects/glibc/build= /loongarch64-linux-gnu-lp64d/support/test-run-command -nostdlib -nostartf= iles -static -static-pie /home/azanella/Projects/glibc/build/loongarc= h64-linux-gnu-lp64d/csu/rcrt1.o /home/azanella/Projects/glibc/build/loong= arch64-linux-gnu-lp64d/csu/crti.o `/home/azanella/toolchain/install/compi= lers//loongarch64-linux-gnu-lp64d/bin/loongarch64-glibc-linux-gnu-gcc --= print-file-name=3DcrtbeginS.o` /home/azanella/Projects/glibc/build/loonga= rch64-linux-gnu-lp64d/support/test-run-command.o /home/azanella/Projects/= glibc/build/loongarch64-linux-gnu-lp64d/elf/static-stubs.o /home/azanella= /Projects/glibc/build/loongarch64-linux-gnu-lp64d/support/libsupport_nons= hared.a -Wl,-z,now -Wl,--start-group /home/azanella/Projects/glibc/build/= loongarch64-linux-gnu-lp64d/libc.a -lgcc -Wl,--end-group `/home/azanella= /toolchain/install/compilers//loongarch64-linux-gnu-lp64d/bin/loongarch64= -glibc-linux-gnu-gcc --print-file-name=3DcrtendS.o` /home/azanella/Proje= cts/glibc/build/loongarch64-linux-gnu-lp64d/csu/crtn.o /home/azanella/Projects/glibc/build/loongarch64-linux-gnu-lp64d/libc.a(se= tlocale.o)(__libc_freeres_fn+0x144): error: R_LARCH_SOP_PUSH_TLS_GOT agai= nst [undefweak] `_nl_current_LC_COLLATE': Internal: Shouldn't be resolved to const. Using --disable-default-pie avoids this build issue. > --- > sysdeps/loongarch/configure | 37 ++++++++++++++++++++++++++++++++++= > sysdeps/loongarch/configure.ac | 20 ++++++++++++++++++ > sysdeps/loongarch/start.S | 14 ++++++++++--- > 3 files changed, 68 insertions(+), 3 deletions(-) >=20 > diff --git a/sysdeps/loongarch/configure b/sysdeps/loongarch/configure > index 43b54d4965..4c1dd303bf 100644 > --- a/sysdeps/loongarch/configure > +++ b/sysdeps/loongarch/configure > @@ -3,3 +3,40 @@ > =20 > $as_echo "#define HIDDEN_VAR_NEEDS_DYNAMIC_RELOC 1" >>confdefs.h > =20 > + > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if the compiler is s= ufficient to build static PIE on LoongArch" >&5 > +$as_echo_n "checking if the compiler is sufficient to build static PIE= on LoongArch... " >&6; } > +if ${libc_cv_static_pie_on_loongarch+:} false; then : > + $as_echo_n "(cached) " >&6 > +else > + > + cat > conftest.c << EOF > +extern int x __asm__("y"); > +int y; > +void *_start() { return &x; } > +EOF > + libc_cv_static_pie_on_loongarch=3Dno > + if { ac_try=3D'${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nost= dlib -fPIE -o conftest conftest.c' > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5= > + (eval $ac_try) 2>&5 > + ac_status=3D$? > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? =3D $ac_status" >&5 > + test $ac_status =3D 0; }; } \ > + && { ac_try=3D'LC_ALL=3DC $READELF -Wr conftest | grep -q R_LARCH= _RELATIVE' > + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5= > + (eval $ac_try) 2>&5 > + ac_status=3D$? > + $as_echo "$as_me:${as_lineno-$LINENO}: \$? =3D $ac_status" >&5 > + test $ac_status =3D 0; }; } > + then > + libc_cv_static_pie_on_loongarch=3Dyes > + fi > + rm -rf conftest.* > +fi > +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_static_pie_o= n_loongarch" >&5 > +$as_echo "$libc_cv_static_pie_on_loongarch" >&6; } > + > +if test "$libc_cv_static_pie_on_loongarch" =3D yes; then > + $as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h > + > +fi > diff --git a/sysdeps/loongarch/configure.ac b/sysdeps/loongarch/configu= re.ac > index f744367bf3..f389679459 100644 > --- a/sysdeps/loongarch/configure.ac > +++ b/sysdeps/loongarch/configure.ac > @@ -4,3 +4,23 @@ GLIBC_PROVIDES dnl See aclocal.m4 in the top level sou= rce directory. > dnl It is always possible to access static and hidden symbols in an > dnl position independent way. > AC_DEFINE(HIDDEN_VAR_NEEDS_DYNAMIC_RELOC) > + > +dnl test if GCC is new enough for static PIE. > +AC_CACHE_CHECK([if the compiler is sufficient to build static PIE on L= oongArch], > +libc_cv_static_pie_on_loongarch, [ > + cat > conftest.c << EOF > +extern int x __asm__("y"); > +int y; > +void *_start() { return &x; } > +EOF > + libc_cv_static_pie_on_loongarch=3Dno > + if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -= nostdlib -fPIE -o conftest conftest.c]) \ > + && AC_TRY_COMMAND([LC_ALL=3DC $READELF -Wr conftest | grep -q R_L= ARCH_RELATIVE]) > + then > + libc_cv_static_pie_on_loongarch=3Dyes > + fi > + rm -rf conftest.*]) > + > +if test "$libc_cv_static_pie_on_loongarch" =3D yes; then > + AC_DEFINE(SUPPORT_STATIC_PIE) > +fi > diff --git a/sysdeps/loongarch/start.S b/sysdeps/loongarch/start.S > index e66af16d57..05cabd9b96 100644 > --- a/sysdeps/loongarch/start.S > +++ b/sysdeps/loongarch/start.S > @@ -60,9 +60,17 @@ ENTRY (ENTRY_POINT) > cfi_undefined (1) > or a5, a0, zero /* rtld_fini */ > =20 > +#if defined(PIC) && !defined(SHARED) > +/* For static PIE, the GOT cannot be used in _start because the GOT en= tries are > + offsets instead of real addresses before __libc_start_main. */ > +# define LA la.pcrel > +#else > /* We must get symbol main through GOT table, since main may not be lo= cal. > For instance: googletest defines main in dynamic library. */ > - la.got a0, t0, main > +# define LA la.got > +#endif > + > + LA a0, t0, main > REG_L a1, sp, 0 > ADDI a2, sp, SZREG > =20 > @@ -73,9 +81,9 @@ ENTRY (ENTRY_POINT) > move a4, zero /* used to be fini */ > or a6, sp, zero /* stack_end */ > =20 > - la.got ra, t0, __libc_start_main > + LA ra, t0, __libc_start_main > jirl ra, ra, 0 > =20 > - la.got ra, t0, abort > + LA ra, t0, abort > jirl ra, ra, 0 > END (ENTRY_POINT)