From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 325983861038 for ; Tue, 30 Mar 2021 19:17:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 325983861038 Received: by mail-pl1-x62d.google.com with SMTP id d8so6672509plh.11 for ; Tue, 30 Mar 2021 12:17:35 -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=sV1EyT4SQ7d39kPvE36+RwKj42yUrj+HnySTplCXus0=; b=Z2qpQ/peSYDMHvpCL8srNy6dFi1LQl3xF85lQVrK3jbbttvCUUGxXCi6rZrhEb/M4D NIgy0nj3tzhYzfpcpuldqYQrcsWaRjskdwxzO3X2dg7pLriYLP7aEBNGrdR0N1Smfis4 ZNRV7YC/tD0Nn2d0C1BQyEYOjXDoBaFeIYP1GkgjtcpDs9XbkgBBuyr4zj2Sfhu4JqOc HbEsd7nCOiwsN9wrL16w3DD3Bt2q/Ot11RNBQtSng9Lu4SjBElMXmgVWM7tLgCrHBLPu EbD40uNyRXm09WAg7xDNkhjZqvL6acrS5Xui+RheIexioiJ32Mgnc6+oIk/SH/ZtNh33 cwpw== X-Gm-Message-State: AOAM532iomJJBT4In9Yexq+sFxKX2R03htik9K1qhz1rOSBlJoRwsPOe woDLNRbh5rrMRoYEllLVvuoDe28r0WBZr62ZuRRhGxkin8I= X-Google-Smtp-Source: ABdhPJwJB0SvOUyfKoAMwtKmV77W7oTpidHOcvc4JyrUIKoA0PBu35OcI5GUzpRTldwWjAestKS5rwf3I75M+VMJVEM= X-Received: by 2002:a17:90a:1049:: with SMTP id y9mr5648248pjd.173.1617131854124; Tue, 30 Mar 2021 12:17:34 -0700 (PDT) MIME-Version: 1.0 References: <20210329232440.3083493-1-maskray@google.com> In-Reply-To: From: =?UTF-8?B?RsSBbmctcnXDrCBTw7JuZw==?= Date: Tue, 30 Mar 2021 12:17:22 -0700 Message-ID: Subject: Re: [PATCH v3] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] To: "H.J. Lu" Cc: GNU C Library , Florian Weimer Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-24.4 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL 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: Tue, 30 Mar 2021 19:17:38 -0000 On Tue, Mar 30, 2021 at 11:17 AM H.J. Lu wrote: > > On Mon, Mar 29, 2021 at 4:24 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 variabl= e > > will not be optimized out; 'retain' prevents section garbage collection > > 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 > > --- > > config.h.in | 3 +++ > > configure | 23 +++++++++++++++++++++++ > > configure.ac | 16 ++++++++++++++++ > > include/libc-symbols.h | 14 ++++++++++---- > > 4 files changed, 52 insertions(+), 4 deletions(-) > > > > 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..6a6b8ac264 100755 > > --- a/configure > > +++ b/configure > > @@ -4105,6 +4105,29 @@ fi > > $as_echo "$libc_cv_textrel_ifunc" >&6; } > > > > > > +# Check if gcc supports attribute ifunc as it is used in libc_ifunc ma= cro. > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gcc attribute re= tain 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 ab= out alias for function with incompatible types" >&5 > > $as_echo_n "checking if compiler warns about alias for function with i= ncompatible types... " >&6; } > > diff --git a/configure.ac b/configure.ac > > index fce967f2c2..031b8c3fb3 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 ifunc as it is used in libc_ifunc ma= cro. > > +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 incomp= atible types], > > libc_cv_gcc_incompatible_alias, [dnl > > 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 &(sym= bol) > > #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 &(sym= bol) > > #endif > > > > /* Define SET as a symbol set. This may be required (it is in a.out) = to > > -- > > 2.31.0.291.g576ba9dcdaf-goog > > > > Need gc-sections tests for linkers with and without -z start-stop-gc. > > -- > H.J. I don't know how to add such a test... It needs to check flush at ext time but I cannot find a template in libio/tst-* ...