From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32b.google.com (mail-ot1-x32b.google.com [IPv6:2607:f8b0:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id A36D73858020 for ; Tue, 30 Mar 2021 18:17:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A36D73858020 Received: by mail-ot1-x32b.google.com with SMTP id t23-20020a0568301e37b02901b65ab30024so16480909otr.4 for ; Tue, 30 Mar 2021 11:17:57 -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=o0a7DdXcEzxzcl+iaKtm5J6Lc3wuL/bwGBDNtgdvDX0=; b=Yt+hIq6GfCjpuJkvlY5k1gMvKWJaoSonVaAU/PYi1G6K7v09EZUkctZbWOUMwWvYWo 7fVdwLWsqWSl+gJNIR1zMgg/OgFoxG1DTGXPt4rAGet8L3ADWk+a58TTIqfPj84CkuA7 6TqbJWQ0ve8tg+TI4pPbJj644+lM6N/gMIY2Uso1d/4F21hF1ohk36mDBpFQkv5zaMJI 5QWJjHfmU0I3VWcVltZu5NWWjuuMXANUpxqjjN2k5HQ/pqA9SU++ZO7BFzERyT5sn0qV QN/afh8EKpgn988n3pVIrNDx73kd8tQL1+XnFWZtGSFzx9o8tYChfuSZM8oL7xC5UrSJ Xv4A== X-Gm-Message-State: AOAM532G/7aeH8NtG/bkwYAmA82iwv2385AaEcwS7yVAfpePt4q1HCce 8PYgTQ7ijkAIju5lYZlCA4V8d6x8gV4wQPZwhnc= X-Google-Smtp-Source: ABdhPJxTeppu5psDVGrTOPUiN5mjBnHd5PmFuEWbf8ba557Ve4dYPYgSO3JauKMWznPPBctHc8CGQBJmMDh6VTFLWEs= X-Received: by 2002:a9d:600a:: with SMTP id h10mr27953985otj.90.1617128276990; Tue, 30 Mar 2021 11:17:56 -0700 (PDT) MIME-Version: 1.0 References: <20210329232440.3083493-1-maskray@google.com> In-Reply-To: <20210329232440.3083493-1-maskray@google.com> From: "H.J. Lu" Date: Tue, 30 Mar 2021 11:17:21 -0700 Message-ID: Subject: Re: [PATCH v3] 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: Tue, 30 Mar 2021 18:18:00 -0000 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 variable > 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 macr= o. > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gcc attribute reta= in 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 abou= t alias for function with incompatible types" >&5 > $as_echo_n "checking if compiler warns about alias for function with inc= ompatible 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 macr= o. > +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 incompat= ible 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 &(symbo= l) > #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 &(symbo= l) > #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. --=20 H.J.