From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc36.google.com (mail-oo1-xc36.google.com [IPv6:2607:f8b0:4864:20::c36]) by sourceware.org (Postfix) with ESMTPS id 82D3B3858C27 for ; Thu, 1 Apr 2021 01:00:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 82D3B3858C27 Received: by mail-oo1-xc36.google.com with SMTP id p2-20020a4aa8420000b02901bc7a7148c4so153607oom.11 for ; Wed, 31 Mar 2021 18:00:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=v5eOuVk8t0adInMp4M91a3eTKnz5Ljdudy7dma9dz10=; b=EFmfa1hn3sWBevjNOi8Os8sVbj/rQcc429bBsEei8f0UER/gVax53Yj8B9GnzBAcD0 ask0fluWebenHDnW6xml9lSHcIlhuMNjWEVwaqhZdofY0wwVVcATRifiWPt41IzfxHNn 8n04Qv3vl2VOG8jI44jxXuR48dTECSURxPNReu8jDd4aXiQwbHUa4WxZak89GQt8qGwx /GX01g4AyUtXEe6MxrPk9BiYVV5xQkyyoTg8b0ocr08GeQeP0TmWFbVZ+Q/jILbornwu QtOzP4C2N/jf7MGoutOStf5nwweREyJjWLWWHTUs9P2E2xVxtDPJ95CyERACo8jrY482 SamA== X-Gm-Message-State: AOAM5336g5mnHxYhAfp30QLxjJsNsByS4AwG6snR7hL3aTpCIjkywvue c/yxUm7mj0bN79TQInH7R2SitPQGegwJVXrx4FI= X-Google-Smtp-Source: ABdhPJywGYwpLLeT1tsS0hqsJsOflq9sX8R+bRGO+a01MG2wzeamqyRg8XmXXaEOqtx3dLVuL5KrIruxbRqY0rbIG0o= X-Received: by 2002:a4a:1104:: with SMTP id 4mr4854771ooc.57.1617238846833; Wed, 31 Mar 2021 18:00:46 -0700 (PDT) MIME-Version: 1.0 References: <20210331193923.2602174-1-maskray@google.com> <20210401000247.ksohwngbzh2z37ov@google.com> In-Reply-To: <20210401000247.ksohwngbzh2z37ov@google.com> From: "H.J. Lu" Date: Wed, 31 Mar 2021 18:00:10 -0700 Message-ID: Subject: Re: [PATCH v4] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] To: Fangrui Song Cc: GNU C Library , Florian Weimer Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3035.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 01 Apr 2021 01:00:52 -0000 On Wed, Mar 31, 2021 at 5:02 PM Fangrui Song wrote: > > On 2021-03-31, H.J. Lu wrote: > >On Wed, Mar 31, 2021 at 12:39 PM Fangrui Song wrote= : > >> > >> So that text_set_element/data_set_element/bss_set_element defined > >> variables will be retained by the linker. > >> > >> Note: 'used' and 'retain' are orthogonal: 'used' makes sure the variab= le > >> will not be optimized out; 'retain' prevents section garbage collectio= n > >> if the linker support SHF_GNU_RETAIN. > >> > >> GNU ld 2.37 and LLD 13 will support -z start-stop-gc which allow C > >> identifier name sections to be GCed even if there are live > >> __start_/__stop_ references. > >> > >> Without the change, there are some static linking problems, e.g. > >> _IO_cleanup (libio/genops.c) may be discarded by ld --gc-sections, so > >> stdout is not flushed on exit. > >> > >> Note: GCC may warning =E2=80=98retain=E2=80=99 attribute ignored while= __has_attribute(retain) is 1 > >> (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99587). > >> --- > >> Changes in v1 -> v2: > >> * Define attribute_used_retain_section > >> Changes in v2 -> v3: > >> * Use attribute_used_retain instead attribute_used_retain_section > >> Changes in v3 -> v4: > >> * Add LIBC_LINKER_FEATURE detection for -z start-stop-gc and add tst-c= leanup* tests > >> --- > >> config.h.in | 3 +++ > >> configure | 58 +++++++++++++++++++++++++++++++++++++++++= + > >> configure.ac | 20 +++++++++++++++ > >> include/libc-symbols.h | 14 +++++++--- > >> libio/Makefile | 32 +++++++++++++++++++++++ > >> libio/tst-cleanup.c | 33 ++++++++++++++++++++++++ > >> libio/tst-cleanup.exp | 1 + > >> 7 files changed, 157 insertions(+), 4 deletions(-) > >> create mode 100644 libio/tst-cleanup.c > >> create mode 100644 libio/tst-cleanup.exp > >> > >> diff --git a/config.h.in b/config.h.in > >> index ca1547ae67..95af1ef229 100644 > >> --- a/config.h.in > >> +++ b/config.h.in > >> @@ -187,6 +187,9 @@ > >> /* Define if gcc supports attribute ifunc. */ > >> #undef HAVE_GCC_IFUNC > >> > >> +/* Define if gcc supports attribute retain. */ > >> +#undef HAVE_GCC_RETAIN > >> + > >> /* Define if the linker defines __ehdr_start. */ > >> #undef HAVE_EHDR_START > >> > >> diff --git a/configure b/configure > >> index fcf43bf7de..fa86348342 100755 > >> --- a/configure > >> +++ b/configure > >> @@ -626,6 +626,7 @@ fno_unit_at_a_time > >> libc_cv_has_glob_dat > >> libc_cv_hashstyle > >> libc_cv_fpie > >> +libc_cv_z_start_stop_gc > >> libc_cv_z_execstack > >> libc_cv_z_combreloc > >> ASFLAGS_config > >> @@ -4105,6 +4106,29 @@ fi > >> $as_echo "$libc_cv_textrel_ifunc" >&6; } > >> > >> > >> +# Check if gcc supports attribute retain as it is used in attribute_u= sed_retain macro. > >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gcc attribute r= etain support" >&5 > >> +$as_echo_n "checking for gcc attribute retain support... " >&6; } > >> +if ${libc_cv_gcc_retain+:} false; then : > >> + $as_echo_n "(cached) " >&6 > >> +else > >> + cat > conftest.c < >> +static int var __attribute__ ((used, retain, section ("__libc_atexit= "))); > >> +EOF > >> +libc_cv_gcc_retain=3Dno > >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&5 \ > >> + 2>&5 ; then > >> + libc_cv_gcc_retain=3Dyes > >> +fi > >> +rm -f conftest* > >> +fi > >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_gcc_retain"= >&5 > >> +$as_echo "$libc_cv_gcc_retain" >&6; } > >> +if test $libc_cv_gcc_retain =3D yes; then > >> + $as_echo "#define HAVE_GCC_RETAIN 1" >>confdefs.h > >> + > >> +fi > >> + > >> # Check if gcc warns about alias for function with incompatible types= . > >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler warns a= bout alias for function with incompatible types" >&5 > >> $as_echo_n "checking if compiler warns about alias for function with = incompatible types... " >&6; } > >> @@ -5871,6 +5895,40 @@ fi > >> $as_echo "$libc_linker_feature" >&6; } > >> > >> > >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that sup= ports -z start-stop-gc" >&5 > >> +$as_echo_n "checking for linker that supports -z start-stop-gc... " >= &6; } > >> +libc_linker_feature=3Dno > >> +if test x"$gnu_ld" =3D x"yes"; then > >> + libc_linker_check=3D`$LD -v --help 2>/dev/null | grep "\-z start-st= op-gc"` > >> + if test -n "$libc_linker_check"; then > >> + cat > conftest.c < >> +int _start (void) { return 42; } > >> +EOF > >> + if { ac_try=3D'${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp > >> + -Wl,-z,start-stop-gc -nostdlib -nostar= tfiles > >> + -fPIC -shared -o conftest.so conftest.= c > >> + 1>&5' > >> + { { 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_linker_feature=3Dyes > >> + fi > >> + rm -f conftest* > >> + fi > >> +fi > >> +if test $libc_linker_feature =3D yes; then > >> + libc_cv_z_start_stop_gc=3Dyes > >> +else > >> + libc_cv_z_start_stop_gc=3Dno > >> +fi > >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature= " >&5 > >> +$as_echo "$libc_linker_feature" >&6; } > >> +config_vars=3D"$config_vars > >> +have-z-start-stop-gc =3D $libc_cv_z_start_stop_gc" > >> + > >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that sup= ports --no-dynamic-linker" >&5 > >> $as_echo_n "checking for linker that supports --no-dynamic-linker... = " >&6; } > >> libc_linker_feature=3Dno > >> diff --git a/configure.ac b/configure.ac > >> index fce967f2c2..4e2907da92 100644 > >> --- a/configure.ac > >> +++ b/configure.ac > >> @@ -707,6 +707,22 @@ fi > >> rm -f conftest*]) > >> AC_SUBST(libc_cv_textrel_ifunc) > >> > >> +# Check if gcc supports attribute retain as it is used in attribute_u= sed_retain macro. > >> +AC_CACHE_CHECK([for gcc attribute retain support], > >> + libc_cv_gcc_retain, [dnl > >> +cat > conftest.c < >> +static int var __attribute__ ((used, retain, section ("__libc_atexit= "))); > >> +EOF > >> +libc_cv_gcc_retain=3Dno > >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&AS_MESSAGE_LOG_FD \ > >> + 2>&AS_MESSAGE_LOG_FD ; then > >> + libc_cv_gcc_retain=3Dyes > >> +fi > >> +rm -f conftest*]) > >> +if test $libc_cv_gcc_retain =3D yes; then > >> + AC_DEFINE(HAVE_GCC_RETAIN) > >> +fi > >> + > >> # Check if gcc warns about alias for function with incompatible types= . > >> AC_CACHE_CHECK([if compiler warns about alias for function with incom= patible types], > >> libc_cv_gcc_incompatible_alias, [dnl > >> @@ -1317,6 +1333,10 @@ LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,exe= cstack], > >> [libc_cv_z_execstack=3Dyes], [libc_cv_z_execstack= =3Dno]) > >> AC_SUBST(libc_cv_z_execstack) > >> > >> +LIBC_LINKER_FEATURE([-z start-stop-gc], [-Wl,-z,start-stop-gc], > >> + [libc_cv_z_start_stop_gc=3Dyes], [libc_cv_z_start_= stop_gc=3Dno]) > >> +LIBC_CONFIG_VAR([have-z-start-stop-gc], [$libc_cv_z_start_stop_gc]) > >> + > >> LIBC_LINKER_FEATURE([--no-dynamic-linker], > >> [-Wl,--no-dynamic-linker], > >> [libc_cv_no_dynamic_linker=3Dyes], > >> diff --git a/include/libc-symbols.h b/include/libc-symbols.h > >> index 546fc26a7b..b311c90f0c 100644 > >> --- a/include/libc-symbols.h > >> +++ b/include/libc-symbols.h > >> @@ -352,6 +352,12 @@ for linking") > >> > >> */ > >> > >> +#ifdef HAVE_GCC_RETAIN > >> +# define attribute_used_retain __attribute__ ((__used__, __retain__)) > >> +#else > >> +# define attribute_used_retain __attribute__ ((__used__)) > >> +#endif > >> + > >> /* Symbol set support macros. */ > >> > >> /* Make SYMBOL, which is in the text segment, an element of SET. */ > >> @@ -367,12 +373,12 @@ for linking") > >> /* When building a shared library, make the set section writable, > >> because it will need to be relocated at run time anyway. */ > >> # define _elf_set_element(set, symbol) \ > >> - static const void *__elf_set_##set##_element_##symbol##__ \ > >> - __attribute__ ((used, section (#set))) =3D &(symbol) > >> + static const void *__elf_set_##set##_element_##symbol##__ \ > >> + attribute_used_retain __attribute__ ((section (#set))) =3D &(sy= mbol) > >> #else > >> # define _elf_set_element(set, symbol) \ > >> - static const void *const __elf_set_##set##_element_##symbol##__ \ > >> - __attribute__ ((used, section (#set))) =3D &(symbol) > >> + static const void *const __elf_set_##set##_element_##symbol##__ \ > >> + attribute_used_retain __attribute__ ((section (#set))) =3D &(sy= mbol) > >> #endif > >> > >> /* Define SET as a symbol set. This may be required (it is in a.out)= to > >> diff --git a/libio/Makefile b/libio/Makefile > >> index 12ce41038f..439d22e057 100644 > >> --- a/libio/Makefile > >> +++ b/libio/Makefile > >> @@ -195,6 +195,20 @@ ifeq (yes,$(build-shared)) > >> tests-special +=3D $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenlo= c-mem.out \ > >> $(objpfx)tst-bz24228-mem.out > >> endif > >> + > >> +tests +=3D tst-cleanup-default > >> +tests-static +=3D tst-cleanup-default > >> +tests-special +=3D $(objpfx)tst-cleanup-default-cmp.out > >> +LDFLAGS-tst-cleanup-default =3D -Wl,--gc-sections > >> + > >> +ifeq ($(have-z-start-stop-gc),yes) > >> +tests +=3D tst-cleanup-start-stop-gc tst-cleanup-nostart-stop-gc > >> +tests-static +=3D tst-cleanup-start-stop-gc tst-cleanup-nostart-stop-= gc > >> +tests-special +=3D $(objpfx)tst-cleanup-start-stop-gc-cmp.out \ > >> + $(objpfx)tst-cleanup-nostart-stop-gc-cmp.out > >> +LDFLAGS-tst-cleanup-start-stop-gc =3D -Wl,--gc-sections,-z,start-stop= -gc > > > >Won't this test fail if linker supports -z start-stop-gc and GCC doesn't= support > >retain attribute? > > Yes, but that doesn't happen in practice. If ld supports -z start-stop-gc= , > the GNU as should be new and the GCC should support 'retain'. I am using GCC 10 and binutils 2.37. > But perhaps it is good to prevent the issue, I can change > > +ifeq ($(have-z-start-stop-gc),yes) > > to > > +ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes) > > and add a LIBC_CONFIG_VAR in configure.ac. > Yes, please. Thanks. --=20 H.J.