From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 431993860C3F for ; Wed, 31 Mar 2021 16:17:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 431993860C3F Received: by mail-ot1-x333.google.com with SMTP id g8-20020a9d6c480000b02901b65ca2432cso19466986otq.3 for ; Wed, 31 Mar 2021 09:17: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=Tk20BEr0646Ky+jc4Ll2w5nM8vsY+Ral4QITs+aVNs4=; b=sY8G8NYzcC2qpH8oR8uKW9rr6hlaWBITwUxDegMONmsPDjThTRH5qqcJ6jH+vRg+nN D/NzwP7YBES0vyg7tkwVkrkIDJJHcPrw4ceBuYYt05RJEz4ZdPGSe6B8uEenYjbSNOLp qNUodym0d071/zEZ/sLu4kDbhdZUHlGmyIXevzNNK/ExgoyK+WBB55PZ+UJKxVoI7VI4 ZIEe2vB4nzhknzaUzy1BmNHqNxoX17Rb84Qg/15cvu/2+dK77dRI2q5nrVmx8OM4yMds xMuRBeib/QTUyq3TGCyzxX4OZyKVYLOByKvQlmBW4I6BnZ5UjTMUo8E0IMzoOKg7CfDv iWMg== X-Gm-Message-State: AOAM531J3pBjtSJa85UfKN/IR0t2zINLJUU2h5fKXdnZYdosmVUCUxB0 pp+eVDzqbNFrA+6g0AKMZ1yFEzESGMhMx+WjB8s= X-Google-Smtp-Source: ABdhPJwptC6//y6bvdMX7Y9DlvzKbvmj3wfMIsygjhfkduucEvEpy8vPRc7ux5W8bBeOt3dcr47/Csb9WvfcXn48e30= X-Received: by 2002:a9d:600a:: with SMTP id h10mr3353444otj.90.1617207466611; Wed, 31 Mar 2021 09:17:46 -0700 (PDT) MIME-Version: 1.0 References: <20210329232440.3083493-1-maskray@google.com> <20210331043413.havjfvkjw3ljhsfl@google.com> In-Reply-To: <20210331043413.havjfvkjw3ljhsfl@google.com> From: "H.J. Lu" Date: Wed, 31 Mar 2021 09:17:09 -0700 Message-ID: Subject: Re: [PATCH v3] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] To: =?UTF-8?B?RsSBbmctcnXDrCBTw7JuZw==?= Cc: GNU C Library , Florian Weimer Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3034.9 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: Wed, 31 Mar 2021 16:17:49 -0000 On Tue, Mar 30, 2021 at 9:34 PM F=C4=81ng-ru=C3=AC S=C3=B2ng wrote: > > On 2021-03-30, H.J. Lu wrote: > >On Tue, Mar 30, 2021 at 12:17 PM F=C4=81ng-ru=C3=AC S=C3=B2ng wrote: > >> > >> On Tue, Mar 30, 2021 at 11:17 AM H.J. Lu wrote: > >> > > >> > On Mon, Mar 29, 2021 at 4:24 PM Fangrui Song wr= ote: > >> > > > >> > > 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 va= riable > >> > > will not be optimized out; 'retain' prevents section garbage colle= ction > >> > > 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 w= hile __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_ifu= nc macro. > >> > > +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gcc attribu= te retain 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_at= exit"))); > >> > > +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_ret= ain" >&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 t= ypes. > >> > > { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler war= ns about alias for function with incompatible types" >&5 > >> > > $as_echo_n "checking if compiler warns about alias for function w= ith incompatible 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_ifu= nc 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_at= exit"))); > >> > > +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 t= ypes. > >> > > AC_CACHE_CHECK([if compiler warns about alias for function with i= ncompatible 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 = &(symbol) > >> > > #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 = &(symbol) > >> > > #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-* ... > > > >You can add a special rule to check for linker output with > >--print-gc-sections. > > I can come up runtime tests. ( https://github.com/MaskRay/glibc/tree/reta= in) > Runtime tests are direct. > (Unsure how to test --print-gc-sections with tests-static...) > > > diff --git a/libio/Makefile b/libio/Makefile > index 12ce41038f..5e0e7f00cd 100644 > --- a/libio/Makefile > +++ b/libio/Makefile > @@ -195,6 +195,13 @@ ifeq (yes,$(build-shared)) > tests-special +=3D $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-= mem.out \ > $(objpfx)tst-bz24228-mem.out > endif > + > +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.out \ > + $(objpfx)tst-cleanup-nostart-stop-gc.out > +LDFLAGS-tst-cleanup-start-stop-gc =3D -Wl,--gc-sections,-z,start-stop-gc > +LDFLAGS-tst-cleanup-nostart-stop-gc =3D -Wl,--gc-sections,-z,nostart-sto= p-gc 1. Need to check if linker supports -z start-stop-gc. 2. Need to test with only -Wl,--gc-sections. > endif > > include ../Rules > @@ -224,6 +231,20 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) > $(objpfx)tst-wfile-sync.out: $(gen-locales) > endif > > +$(objpfx)tst-cleanup-start-stop-gc.out: $(objpfx)tst-cleanup-start-stop-= gc > + $< > $@T > + grep hello $@T; $(evaluate-test) > + rm -f $@T tst-cleanup-start-stop-gc.out isn't generated. You should add a ".exp" fil= e and compare the .out file against it. > +$(objpfx)tst-cleanup-start-stop-gc.o: tst-cleanup.c > + $(compile.c) -o $@ > + > +$(objpfx)tst-cleanup-nostart-stop-gc.out: $(objpfx)tst-cleanup-nostart-s= top-gc > + $< > $@T > + grep hello $@T; $(evaluate-test) > + rm -f $@T > +$(objpfx)tst-cleanup-nostart-stop-gc.o: tst-cleanup.c > + $(compile.c) -o $@ > + > $(objpfx)test-freopen.out: test-freopen.sh $(objpfx)test-freopen > $(SHELL) $< $(common-objpfx) '$(test-program-prefix)' \ > $(common-objpfx)libio/; \ > diff --git a/libio/tst-cleanup.c b/libio/tst-cleanup.c > new file mode 100644 > index 0000000000..de8130a482 > --- /dev/null > +++ b/libio/tst-cleanup.c > @@ -0,0 +1,14 @@ Missing comments and copyright notice. > +#include > +#include > + > +void > +hook (void) > +{ > + puts ("hello"); > +} > + > +int > +main (void) > +{ > + atexit (hook); > +} -- H.J.