From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id DD4263858012 for ; Tue, 6 Apr 2021 21:46:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DD4263858012 Received: by mail-pg1-x52b.google.com with SMTP id z16so4004174pga.1 for ; Tue, 06 Apr 2021 14:46:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=odfweoKS/ECYq4DW5RsBjpMSqFuqEpcLQbyqbu1E7K0=; b=HQaTMRgt5cWZ+YLRM4HK356uNMuTojQ/mt6jJT+8jOojkvMkGNRKWzY+E38VJxc3tn h6PS3Kp1UrbnVUSqQXFwB3OxGDr8TGQbAY2ziNPtBS7+vkrDFJAwgshhiyy9+6fML6Iu VVpWwufclHaU7uXRgqP3TSpnBmfpl4xpr8o5w5uN+dD1gOihyNLFEfkoLn+Nl+g6c/SX zHApbIwVLv5ODSW2Gu+T8714WY4tMe9PKfUShO/ndy/xspqQR4UuaBrco/tTUMLdrx2N Bl0cZuyVBrzn5VXJ6vvNDKSVyfHsIUy/XARGJfy8m2/mAjk2uvKFLSaSvtQRdBZnxJr/ qXSQ== X-Gm-Message-State: AOAM530YXLRMuL4ZgQZ+r/nWfLM9i21JCDTfFFaC2fxXzwmv8rGXKs10 v8d5U+yVip82XFnee7pXpFwBCg== X-Google-Smtp-Source: ABdhPJytnyIesCLt4UjFtz3R6Ts26smrYLncPd7XCJXAm5F7lEqKYmTmkw6imVfMapeM7iOw5cpomQ== X-Received: by 2002:a62:187:0:b029:241:fc67:d41f with SMTP id 129-20020a6201870000b0290241fc67d41fmr17083pfb.55.1617745596532; Tue, 06 Apr 2021 14:46:36 -0700 (PDT) Received: from google.com ([2620:15c:2ce:0:c4dd:ad3:31cc:bdf2]) by smtp.gmail.com with ESMTPSA id c129sm20295142pfb.141.2021.04.06.14.46.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Apr 2021 14:46:36 -0700 (PDT) Date: Tue, 6 Apr 2021 14:46:32 -0700 From: Fangrui Song To: "H.J. Lu" Cc: GNU C Library , Florian Weimer Subject: Re: [PATCH v5] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] Message-ID: <20210406214632.2i4zjs7hsk5mwybb@google.com> References: <20210402032329.c5d3zzlrtm3d3do3@google.com> <20210403180245.el7x24zpg4icg26k@google.com> <20210403215730.3hjzutts6fty7jji@google.com> <20210405181702.46i742yevcjiazgn@google.com> <20210405210300.wh72lzmga6a2qw4t@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-27.3 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, 06 Apr 2021 21:46:43 -0000 On 2021-04-05, H.J. Lu wrote: >On Mon, Apr 5, 2021 at 2:03 PM Fangrui Song wrote: >> >> On 2021-04-05, H.J. Lu wrote: >> >On Mon, Apr 5, 2021 at 11:17 AM Fangrui Song wrote: >> >> >> >> On 2021-04-04, H.J. Lu wrote: >> >> >On Sat, Apr 3, 2021 at 2:57 PM Fangrui Song wrote: >> >> >> >> >> >> On 2021-04-03, H.J. Lu wrote: >> >> >> >On Sat, Apr 3, 2021 at 11:02 AM Fangrui Song wrote: >> >> >> >> >> >> >> >> >> >> >> >> On 2021-04-02, H.J. Lu wrote: >> >> >> >> >On Thu, Apr 1, 2021 at 8:23 PM Fangrui Song wrote: >> >> >> >> >> >> >> >> >> >> On 2021-04-01, H.J. Lu wrote: >> >> >> >> >> >On Wed, Mar 31, 2021 at 6:07 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 ‘retain’ attribute ignored while __has_attribute(retain) is 1 >> >> >> >> >> >> (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587). >> >> >> >> >> >> --- >> >> >> >> >> >> 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-cleanup* tests >> >> >> >> >> >> Changes in v4 -> v5: >> >> >> >> >> >> * Enable -z start-stop-gc tests if both retain and -z start-stop-gc are supported >> >> >> >> >> >> * Rename *gcc_retain* to *gnu_retain* >> >> >> >> >> >> --- >> >> >> >> >> >> config.h.in | 3 +++ >> >> >> >> >> >> configure | 59 ++++++++++++++++++++++++++++++++++++++++++ >> >> >> >> >> >> configure.ac | 21 +++++++++++++++ >> >> >> >> >> >> include/libc-symbols.h | 14 +++++++--- >> >> >> >> >> >> libio/Makefile | 32 +++++++++++++++++++++++ >> >> >> >> >> >> libio/tst-cleanup.c | 33 +++++++++++++++++++++++ >> >> >> >> >> >> libio/tst-cleanup.exp | 1 + >> >> >> >> >> >> 7 files changed, 159 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..96a08c7757 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 CC supports attribute retain. */ >> >> >> >> >> >> +#undef HAVE_GNU_RETAIN >> >> >> >> >> >> + >> >> >> >> >> >> /* Define if the linker defines __ehdr_start. */ >> >> >> >> >> >> #undef HAVE_EHDR_START >> >> >> >> >> >> >> >> >> >> >> >> diff --git a/configure b/configure >> >> >> >> >> >> index fcf43bf7de..e64b7f8efe 100755 >> >> >> >> >> >> --- a/configure >> >> >> >> >> >> +++ b/configure >> >> >> >> >> >> @@ -4105,6 +4105,31 @@ fi >> >> >> >> >> >> $as_echo "$libc_cv_textrel_ifunc" >&6; } >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. >> >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU attribute retain support" >&5 >> >> >> >> >> >> +$as_echo_n "checking for GNU attribute retain support... " >&6; } >> >> >> >> >> >> +if ${libc_cv_gnu_retain+:} false; then : >> >> >> >> >> >> + $as_echo_n "(cached) " >&6 >> >> >> >> >> >> +else >> >> >> >> >> >> + cat > conftest.c <> >> >> >> >> >> +static int var __attribute__ ((used, retain, section ("__libc_atexit"))); >> >> >> >> >> >> +EOF >> >> >> >> >> >> +libc_cv_gnu_retain=no >> >> >> >> >> >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&5 \ >> >> >> >> >> >> + 2>&5 ; then >> >> >> >> >> >> + libc_cv_gnu_retain=yes >> >> >> >> >> >> +fi >> >> >> >> >> >> +rm -f conftest* >> >> >> >> >> >> +fi >> >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_gnu_retain" >&5 >> >> >> >> >> >> +$as_echo "$libc_cv_gnu_retain" >&6; } >> >> >> >> >> >> +if test $libc_cv_gnu_retain = yes; then >> >> >> >> >> >> + $as_echo "#define HAVE_GNU_RETAIN 1" >>confdefs.h >> >> >> >> >> >> + >> >> >> >> >> >> +fi >> >> >> >> >> >> +config_vars="$config_vars >> >> >> >> >> >> +have-gnu-retain = $libc_cv_gnu_retain" >> >> >> >> >> >> + >> >> >> >> >> >> # Check if gcc warns about alias for function with incompatible types. >> >> >> >> >> >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler warns about alias for function with incompatible types" >&5 >> >> >> >> >> >> $as_echo_n "checking if compiler warns about alias for function with incompatible types... " >&6; } >> >> >> >> >> >> @@ -5871,6 +5896,40 @@ fi >> >> >> >> >> >> $as_echo "$libc_linker_feature" >&6; } >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports -z start-stop-gc" >&5 >> >> >> >> >> >> +$as_echo_n "checking for linker that supports -z start-stop-gc... " >&6; } >> >> >> >> >> >> +libc_linker_feature=no >> >> >> >> >> >> +if test x"$gnu_ld" = x"yes"; then >> >> >> >> >> >> + libc_linker_check=`$LD -v --help 2>/dev/null | grep "\-z start-stop-gc"` >> >> >> >> >> >> + if test -n "$libc_linker_check"; then >> >> >> >> >> >> + cat > conftest.c <> >> >> >> >> >> +int _start (void) { return 42; } >> >> >> >> >> >> +EOF >> >> >> >> >> >> + if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp >> >> >> >> >> >> + -Wl,-z,start-stop-gc -nostdlib -nostartfiles >> >> >> >> >> >> + -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=$? >> >> >> >> >> >> + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 >> >> >> >> >> >> + test $ac_status = 0; }; } >> >> >> >> >> >> + then >> >> >> >> >> >> + libc_linker_feature=yes >> >> >> >> >> >> + fi >> >> >> >> >> >> + rm -f conftest* >> >> >> >> >> >> + fi >> >> >> >> >> >> +fi >> >> >> >> >> >> +if test $libc_linker_feature = yes; then >> >> >> >> >> >> + libc_cv_z_start_stop_gc=yes >> >> >> >> >> >> +else >> >> >> >> >> >> + libc_cv_z_start_stop_gc=no >> >> >> >> >> >> +fi >> >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5 >> >> >> >> >> >> +$as_echo "$libc_linker_feature" >&6; } >> >> >> >> >> >> +config_vars="$config_vars >> >> >> >> >> >> +have-z-start-stop-gc = $libc_cv_z_start_stop_gc" >> >> >> >> >> >> + >> >> >> >> >> >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports --no-dynamic-linker" >&5 >> >> >> >> >> >> $as_echo_n "checking for linker that supports --no-dynamic-linker... " >&6; } >> >> >> >> >> >> libc_linker_feature=no >> >> >> >> >> >> diff --git a/configure.ac b/configure.ac >> >> >> >> >> >> index fce967f2c2..cc47e56e82 100644 >> >> >> >> >> >> --- a/configure.ac >> >> >> >> >> >> +++ b/configure.ac >> >> >> >> >> >> @@ -707,6 +707,23 @@ fi >> >> >> >> >> >> rm -f conftest*]) >> >> >> >> >> >> AC_SUBST(libc_cv_textrel_ifunc) >> >> >> >> >> >> >> >> >> >> >> >> +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. >> >> >> >> >> >> +AC_CACHE_CHECK([for GNU attribute retain support], >> >> >> >> >> >> + libc_cv_gnu_retain, [dnl >> >> >> >> >> >> +cat > conftest.c <> >> >> >> >> >> +static int var __attribute__ ((used, retain, section ("__libc_atexit"))); >> >> >> >> >> >> +EOF >> >> >> >> >> >> +libc_cv_gnu_retain=no >> >> >> >> >> >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&AS_MESSAGE_LOG_FD \ >> >> >> >> >> >> + 2>&AS_MESSAGE_LOG_FD ; then >> >> >> >> >> >> + libc_cv_gnu_retain=yes >> >> >> >> >> >> +fi >> >> >> >> >> >> +rm -f conftest*]) >> >> >> >> >> >> +if test $libc_cv_gnu_retain = yes; then >> >> >> >> >> >> + AC_DEFINE(HAVE_GNU_RETAIN) >> >> >> >> >> >> +fi >> >> >> >> >> >> +LIBC_CONFIG_VAR([have-gnu-retain], [$libc_cv_gnu_retain]) >> >> >> >> >> >> + >> >> >> >> >> >> # Check if gcc warns about alias for function with incompatible types. >> >> >> >> >> >> AC_CACHE_CHECK([if compiler warns about alias for function with incompatible types], >> >> >> >> >> >> libc_cv_gcc_incompatible_alias, [dnl >> >> >> >> >> >> @@ -1317,6 +1334,10 @@ LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,execstack], >> >> >> >> >> >> [libc_cv_z_execstack=yes], [libc_cv_z_execstack=no]) >> >> >> >> >> >> AC_SUBST(libc_cv_z_execstack) >> >> >> >> >> >> >> >> >> >> >> >> +LIBC_LINKER_FEATURE([-z start-stop-gc], [-Wl,-z,start-stop-gc], >> >> >> >> >> >> + [libc_cv_z_start_stop_gc=yes], [libc_cv_z_start_stop_gc=no]) >> >> >> >> >> >> +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=yes], >> >> >> >> >> >> diff --git a/include/libc-symbols.h b/include/libc-symbols.h >> >> >> >> >> >> index 546fc26a7b..127ea656c2 100644 >> >> >> >> >> >> --- a/include/libc-symbols.h >> >> >> >> >> >> +++ b/include/libc-symbols.h >> >> >> >> >> >> @@ -352,6 +352,12 @@ for linking") >> >> >> >> >> >> >> >> >> >> >> >> */ >> >> >> >> >> >> >> >> >> >> >> >> +#ifdef HAVE_GNU_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))) = &(symbol) >> >> >> >> >> >> + static const void *__elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> >> + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) >> >> >> >> >> >> #else >> >> >> >> >> >> # define _elf_set_element(set, symbol) \ >> >> >> >> >> >> - static const void *const __elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> >> - __attribute__ ((used, section (#set))) = &(symbol) >> >> >> >> >> >> + static const void *const __elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> >> + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) >> >> >> >> >> >> #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..c9c232ebc2 100644 >> >> >> >> >> >> --- a/libio/Makefile >> >> >> >> >> >> +++ b/libio/Makefile >> >> >> >> >> >> @@ -195,6 +195,20 @@ ifeq (yes,$(build-shared)) >> >> >> >> >> >> tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \ >> >> >> >> >> >> $(objpfx)tst-bz24228-mem.out >> >> >> >> >> >> endif >> >> >> >> >> >> + >> >> >> >> >> >> +tests += tst-cleanup-default >> >> >> >> >> >> +tests-static += tst-cleanup-default >> >> >> >> >> >> +tests-special += $(objpfx)tst-cleanup-default-cmp.out >> >> >> >> >> > >> >> >> >> >> >Please make 2 tests, tst-cleanup-default and tst-cleanup-default-static. >> >> >> >> >> >> >> >> >> >> Added. >> >> >> >> >> >> >> >> >> >> >> +LDFLAGS-tst-cleanup-default = -Wl,--gc-sections >> >> >> >> >> >> + >> >> >> >> >> >> +ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes) >> >> >> >> >> >> +tests += tst-cleanup-start-stop-gc tst-cleanup-nostart-stop-gc >> >> >> >> >> >> +tests-static += tst-cleanup-start-stop-gc tst-cleanup-nostart-stop-gc >> >> >> >> >> >> +tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \ >> >> >> >> >> >> + $(objpfx)tst-cleanup-nostart-stop-gc-cmp.out >> >> >> >> >> > >> >> >> >> >> >Same here. >> >> >> >> >> >> >> >> >> >> Added. >> >> >> >> >> >> >> >> >> >> >> +LDFLAGS-tst-cleanup-start-stop-gc = -Wl,--gc-sections,-z,start-stop-gc >> >> >> >> >> >> +LDFLAGS-tst-cleanup-nostart-stop-gc = -Wl,--gc-sections,-z,nostart-stop-gc >> >> >> >> >> >> +endif >> >> >> >> >> >> endif >> >> >> >> >> >> >> >> >> >> >> >> include ../Rules >> >> >> >> >> >> @@ -224,6 +238,24 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) >> >> >> >> >> >> $(objpfx)tst-wfile-sync.out: $(gen-locales) >> >> >> >> >> >> endif >> >> >> >> >> >> >> >> >> >> >> >> +$(objpfx)tst-cleanup-default-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-default.out >> >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> >> +$(objpfx)tst-cleanup-default.o: tst-cleanup.c >> >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> >> >> + >> >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-start-stop-gc.out >> >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc.o: tst-cleanup.c >> >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> >> >> + >> >> >> >> >> >> +$(objpfx)tst-cleanup-nostart-stop-gc-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-nostart-stop-gc.out >> >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> >> +$(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..7f0a34a91e >> >> >> >> >> >> --- /dev/null >> >> >> >> >> >> +++ b/libio/tst-cleanup.c >> >> >> >> >> >> @@ -0,0 +1,33 @@ >> >> >> >> >> > >> >> >> >> >> >Please add a comment saying that test --gc-sections. >> >> >> >> >> >> >> >> >> >> OK, added. >> >> >> >> >> >> >> >> >> >> >> +/* Copyright (C) 2021 Free Software Foundation, Inc. >> >> >> >> >> >> + This file is part of the GNU C Library. >> >> >> >> >> >> + >> >> >> >> >> >> + The GNU C Library is free software; you can redistribute it and/or >> >> >> >> >> >> + modify it under the terms of the GNU Lesser General Public >> >> >> >> >> >> + License as published by the Free Software Foundation; either >> >> >> >> >> >> + version 2.1 of the License, or (at your option) any later version. >> >> >> >> >> >> + >> >> >> >> >> >> + The GNU C Library is distributed in the hope that it will be useful, >> >> >> >> >> >> + but WITHOUT ANY WARRANTY; without even the implied warranty of >> >> >> >> >> >> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU >> >> >> >> >> >> + Lesser General Public License for more details. >> >> >> >> >> >> + >> >> >> >> >> >> + You should have received a copy of the GNU Lesser General Public >> >> >> >> >> >> + License along with the GNU C Library; if not, see >> >> >> >> >> >> + . */ >> >> >> >> >> >> + >> >> >> >> >> >> +/* Test that stdout is flushed after atexit callbacks were run. */ >> >> >> >> >> >> + >> >> >> >> >> >> +#include >> >> >> >> >> >> +#include >> >> >> >> >> >> + >> >> >> >> >> >> +void >> >> >> >> >> >> +hook (void) >> >> >> >> >> >> +{ >> >> >> >> >> >> + puts ("hello"); >> >> >> >> >> >> +} >> >> >> >> >> >> + >> >> >> >> >> >> +int >> >> >> >> >> >> +main (void) >> >> >> >> >> >> +{ >> >> >> >> >> >> + atexit (hook); >> >> >> >> >> >> +} >> >> >> >> >> >> diff --git a/libio/tst-cleanup.exp b/libio/tst-cleanup.exp >> >> >> >> >> >> new file mode 100644 >> >> >> >> >> >> index 0000000000..ce01362503 >> >> >> >> >> >> --- /dev/null >> >> >> >> >> >> +++ b/libio/tst-cleanup.exp >> >> >> >> >> >> @@ -0,0 +1 @@ >> >> >> >> >> >> +hello >> >> >> >> >> >> -- >> >> >> >> >> >> 2.31.0.291.g576ba9dcdaf-goog >> >> >> >> >> >> >> >> >> >> >> > >> >> >> >> >> >> >> >> >> >> diff --git a/config.h.in b/config.h.in >> >> >> >> >> index ca1547ae67..96a08c7757 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 CC supports attribute retain. */ >> >> >> >> >> +#undef HAVE_GNU_RETAIN >> >> >> >> >> + >> >> >> >> >> /* Define if the linker defines __ehdr_start. */ >> >> >> >> >> #undef HAVE_EHDR_START >> >> >> >> >> >> >> >> >> >> diff --git a/configure b/configure >> >> >> >> >> index fcf43bf7de..e64b7f8efe 100755 >> >> >> >> >> --- a/configure >> >> >> >> >> +++ b/configure >> >> >> >> >> @@ -4105,6 +4105,31 @@ fi >> >> >> >> >> $as_echo "$libc_cv_textrel_ifunc" >&6; } >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU attribute retain support" >&5 >> >> >> >> >> +$as_echo_n "checking for GNU attribute retain support... " >&6; } >> >> >> >> >> +if ${libc_cv_gnu_retain+:} false; then : >> >> >> >> >> + $as_echo_n "(cached) " >&6 >> >> >> >> >> +else >> >> >> >> >> + cat > conftest.c <> >> >> >> >> +static int var __attribute__ ((used, retain, section ("__libc_atexit"))); >> >> >> >> >> +EOF >> >> >> >> >> +libc_cv_gnu_retain=no >> >> >> >> >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&5 \ >> >> >> >> >> + 2>&5 ; then >> >> >> >> >> + libc_cv_gnu_retain=yes >> >> >> >> >> +fi >> >> >> >> >> +rm -f conftest* >> >> >> >> >> +fi >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_gnu_retain" >&5 >> >> >> >> >> +$as_echo "$libc_cv_gnu_retain" >&6; } >> >> >> >> >> +if test $libc_cv_gnu_retain = yes; then >> >> >> >> >> + $as_echo "#define HAVE_GNU_RETAIN 1" >>confdefs.h >> >> >> >> >> + >> >> >> >> >> +fi >> >> >> >> >> +config_vars="$config_vars >> >> >> >> >> +have-gnu-retain = $libc_cv_gnu_retain" >> >> >> >> >> + >> >> >> >> >> # Check if gcc warns about alias for function with incompatible types. >> >> >> >> >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler warns about alias for function with incompatible types" >&5 >> >> >> >> >> $as_echo_n "checking if compiler warns about alias for function with incompatible types... " >&6; } >> >> >> >> >> @@ -5871,6 +5896,40 @@ fi >> >> >> >> >> $as_echo "$libc_linker_feature" >&6; } >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports -z start-stop-gc" >&5 >> >> >> >> >> +$as_echo_n "checking for linker that supports -z start-stop-gc... " >&6; } >> >> >> >> >> +libc_linker_feature=no >> >> >> >> >> +if test x"$gnu_ld" = x"yes"; then >> >> >> >> >> + libc_linker_check=`$LD -v --help 2>/dev/null | grep "\-z start-stop-gc"` >> >> >> >> >> + if test -n "$libc_linker_check"; then >> >> >> >> >> + cat > conftest.c <> >> >> >> >> +int _start (void) { return 42; } >> >> >> >> >> +EOF >> >> >> >> >> + if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp >> >> >> >> >> + -Wl,-z,start-stop-gc -nostdlib -nostartfiles >> >> >> >> >> + -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=$? >> >> >> >> >> + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 >> >> >> >> >> + test $ac_status = 0; }; } >> >> >> >> >> + then >> >> >> >> >> + libc_linker_feature=yes >> >> >> >> >> + fi >> >> >> >> >> + rm -f conftest* >> >> >> >> >> + fi >> >> >> >> >> +fi >> >> >> >> >> +if test $libc_linker_feature = yes; then >> >> >> >> >> + libc_cv_z_start_stop_gc=yes >> >> >> >> >> +else >> >> >> >> >> + libc_cv_z_start_stop_gc=no >> >> >> >> >> +fi >> >> >> >> >> +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5 >> >> >> >> >> +$as_echo "$libc_linker_feature" >&6; } >> >> >> >> >> +config_vars="$config_vars >> >> >> >> >> +have-z-start-stop-gc = $libc_cv_z_start_stop_gc" >> >> >> >> >> + >> >> >> >> >> { $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports --no-dynamic-linker" >&5 >> >> >> >> >> $as_echo_n "checking for linker that supports --no-dynamic-linker... " >&6; } >> >> >> >> >> libc_linker_feature=no >> >> >> >> >> diff --git a/configure.ac b/configure.ac >> >> >> >> >> index fce967f2c2..cc47e56e82 100644 >> >> >> >> >> --- a/configure.ac >> >> >> >> >> +++ b/configure.ac >> >> >> >> >> @@ -707,6 +707,23 @@ fi >> >> >> >> >> rm -f conftest*]) >> >> >> >> >> AC_SUBST(libc_cv_textrel_ifunc) >> >> >> >> >> >> >> >> >> >> +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. >> >> >> >> >> +AC_CACHE_CHECK([for GNU attribute retain support], >> >> >> >> >> + libc_cv_gnu_retain, [dnl >> >> >> >> >> +cat > conftest.c <> >> >> >> >> +static int var __attribute__ ((used, retain, section ("__libc_atexit"))); >> >> >> >> >> +EOF >> >> >> >> >> +libc_cv_gnu_retain=no >> >> >> >> >> +if ${CC-cc} -Werror -c conftest.c -o /dev/null 1>&AS_MESSAGE_LOG_FD \ >> >> >> >> >> + 2>&AS_MESSAGE_LOG_FD ; then >> >> >> >> >> + libc_cv_gnu_retain=yes >> >> >> >> >> +fi >> >> >> >> >> +rm -f conftest*]) >> >> >> >> >> +if test $libc_cv_gnu_retain = yes; then >> >> >> >> >> + AC_DEFINE(HAVE_GNU_RETAIN) >> >> >> >> >> +fi >> >> >> >> >> +LIBC_CONFIG_VAR([have-gnu-retain], [$libc_cv_gnu_retain]) >> >> >> >> >> + >> >> >> >> >> # Check if gcc warns about alias for function with incompatible types. >> >> >> >> >> AC_CACHE_CHECK([if compiler warns about alias for function with incompatible types], >> >> >> >> >> libc_cv_gcc_incompatible_alias, [dnl >> >> >> >> >> @@ -1317,6 +1334,10 @@ LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,execstack], >> >> >> >> >> [libc_cv_z_execstack=yes], [libc_cv_z_execstack=no]) >> >> >> >> >> AC_SUBST(libc_cv_z_execstack) >> >> >> >> >> >> >> >> >> >> +LIBC_LINKER_FEATURE([-z start-stop-gc], [-Wl,-z,start-stop-gc], >> >> >> >> >> + [libc_cv_z_start_stop_gc=yes], [libc_cv_z_start_stop_gc=no]) >> >> >> >> >> +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=yes], >> >> >> >> >> diff --git a/include/libc-symbols.h b/include/libc-symbols.h >> >> >> >> >> index 546fc26a7b..127ea656c2 100644 >> >> >> >> >> --- a/include/libc-symbols.h >> >> >> >> >> +++ b/include/libc-symbols.h >> >> >> >> >> @@ -352,6 +352,12 @@ for linking") >> >> >> >> >> >> >> >> >> >> */ >> >> >> >> >> >> >> >> >> >> +#ifdef HAVE_GNU_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))) = &(symbol) >> >> >> >> >> + static const void *__elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) >> >> >> >> >> #else >> >> >> >> >> # define _elf_set_element(set, symbol) \ >> >> >> >> >> - static const void *const __elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> - __attribute__ ((used, section (#set))) = &(symbol) >> >> >> >> >> + static const void *const __elf_set_##set##_element_##symbol##__ \ >> >> >> >> >> + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) >> >> >> >> >> #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..ad0d53bb49 100644 >> >> >> >> >> --- a/libio/Makefile >> >> >> >> >> +++ b/libio/Makefile >> >> >> >> >> @@ -195,6 +195,26 @@ ifeq (yes,$(build-shared)) >> >> >> >> >> tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \ >> >> >> >> >> $(objpfx)tst-bz24228-mem.out >> >> >> >> >> endif >> >> >> >> >> + >> >> >> >> >> +tests += tst-cleanup-default tst-cleanup-default-static >> >> >> >> >> +tests-static += tst-cleanup-default-static >> >> >> >> >> +tests-special += $(objpfx)tst-cleanup-default-cmp.out $(objpfx)tst-cleanup-default-static-cmp.out >> >> >> >> >> +LDFLAGS-tst-cleanup-default = -Wl,--gc-sections >> >> >> >> >> +LDFLAGS-tst-cleanup-default-static = -Wl,--gc-sections >> >> >> >> >> + >> >> >> >> >> +ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes) >> >> >> >> >> +tests += tst-cleanup-start-stop-gc tst-cleanup-start-stop-gc-static \ >> >> >> >> >> + tst-cleanup-nostart-stop-gc tst-cleanup-nostart-stop-gc-static >> >> >> >> >> +tests-static += tst-cleanup-start-stop-gc-static tst-cleanup-nostart-stop-gc-static >> >> >> >> >> +tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \ >> >> >> >> >> + $(objpfx)tst-cleanup-start-stop-gc-static-cmp.out \ >> >> >> >> >> + $(objpfx)tst-cleanup-nostart-stop-gc-cmp.out \ >> >> >> >> >> + $(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out >> >> >> >> >> +LDFLAGS-tst-cleanup-start-stop-gc := -Wl,--gc-sections,-z,start-stop-gc >> >> >> >> >> +LDFLAGS-tst-cleanup-start-stop-gc-static := -Wl,--gc-sections,-z,start-stop-gc >> >> >> >> >> +LDFLAGS-tst-cleanup-nostart-stop-gc := -Wl,--gc-sections,-z,nostart-stop-gc >> >> >> >> >> +LDFLAGS-tst-cleanup-nostart-stop-gc-static := -Wl,--gc-sections,-z,nostart-stop-gc >> >> >> >> >> +endif >> >> >> >> >> endif >> >> >> >> >> >> >> >> >> >> include ../Rules >> >> >> >> >> @@ -224,6 +244,39 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) >> >> >> >> >> $(objpfx)tst-wfile-sync.out: $(gen-locales) >> >> >> >> >> endif >> >> >> >> >> >> >> >> >> >> +$(objpfx)tst-cleanup-default-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-default.out >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> +$(objpfx)tst-cleanup-default.o: tst-cleanup.c >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> >> +$(objpfx)tst-cleanup-default-static-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-default-static.out >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> +$(objpfx)tst-cleanup-default-static.o: tst-cleanup.c >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> >> + >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-start-stop-gc.out >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc.o: tst-cleanup.c >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-static-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-start-stop-gc-static.out >> >> >> >> >> + cmp $^ > $@; \ >> >> >> >> >> + $(evaluate-test) >> >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-static.o: tst-cleanup.c >> >> >> >> >> + $(compile.c) -o $@ >> >> >> >> > >> >> >> >> >Please add a separate .c file for each test to get the correct dependency. >> >> >> >> >> >> >> >> Changed this block to: >> >> >> >> >> >> >> >> @@ -224,6 +244,39 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) >> >> >> >> $(objpfx)tst-wfile-sync.out: $(gen-locales) >> >> >> >> endif >> >> >> >> >> >> >> >> +$(objpfx)tst-cleanup-default-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-default.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-default.c: tst-cleanup.c >> >> >> >> + cp $< $@ >> >> >> >> +$(objpfx)tst-cleanup-default-static-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-default-static.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-default-static.c: tst-cleanup.c >> >> >> >> + cp $< $@ >> >> >> >> + >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-start-stop-gc.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc.o: tst-cleanup.c >> >> >> >> + $(compile.c) -o $@ >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-static-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-start-stop-gc-static.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-start-stop-gc-static.c: tst-cleanup.c >> >> >> >> + cp $< $@ >> >> >> >> + >> >> >> >> +$(objpfx)tst-cleanup-nostart-stop-gc-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-nostart-stop-gc.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-nostart-stop-gc.c: tst-cleanup.c >> >> >> >> + cp $< $@ >> >> >> >> +$(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-nostart-stop-gc-static.out >> >> >> >> + cmp $^ > $@; \ >> >> >> >> + $(evaluate-test) >> >> >> >> +$(objpfx)tst-cleanup-nostart-stop-gc-static.c: tst-cleanup.c >> >> >> >> + cp $< $@ >> >> >> >> + >> >> >> >> $(objpfx)test-freopen.out: test-freopen.sh $(objpfx)test-freopen >> >> >> >> $(SHELL) $< $(common-objpfx) '$(test-program-prefix)' \ >> >> >> >> $(common-objpfx)libio/; \ >> >> >> >> >> >> >> > >> >> >> >"make clean" doesn't remove the generated files. The standard way in glibc >> >> >> >is to add a *-static.c source. >> >> >> >> >> >> I'll use .INTERMEDIATE and $(eval $(call ..)) >> >> >> >> >> >> diff --git a/libio/Makefile b/libio/Makefile >> >> >> index 12ce41038f..a9b44f04df 100644 >> >> >> --- a/libio/Makefile >> >> >> +++ b/libio/Makefile >> >> >> @@ -195,6 +195,26 @@ ifeq (yes,$(build-shared)) >> >> >> tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \ >> >> >> $(objpfx)tst-bz24228-mem.out >> >> >> endif >> >> >> + >> >> >> +tests += tst-cleanup-default tst-cleanup-default-static >> >> >> +tests-static += tst-cleanup-default-static >> >> >> +tests-special += $(objpfx)tst-cleanup-default-cmp.out $(objpfx)tst-cleanup-default-static-cmp.out >> >> >> +LDFLAGS-tst-cleanup-default = -Wl,--gc-sections >> >> >> +LDFLAGS-tst-cleanup-default-static = -Wl,--gc-sections >> >> >> + >> >> >> +ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes) >> >> >> +tests += tst-cleanup-start-stop-gc tst-cleanup-start-stop-gc-static \ >> >> >> + tst-cleanup-nostart-stop-gc tst-cleanup-nostart-stop-gc-static >> >> >> +tests-static += tst-cleanup-start-stop-gc-static tst-cleanup-nostart-stop-gc-static >> >> >> +tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \ >> >> >> + $(objpfx)tst-cleanup-start-stop-gc-static-cmp.out \ >> >> >> + $(objpfx)tst-cleanup-nostart-stop-gc-cmp.out \ >> >> >> + $(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out >> >> >> +LDFLAGS-tst-cleanup-start-stop-gc := -Wl,--gc-sections,-z,start-stop-gc >> >> >> +LDFLAGS-tst-cleanup-start-stop-gc-static := -Wl,--gc-sections,-z,start-stop-gc >> >> >> +LDFLAGS-tst-cleanup-nostart-stop-gc := -Wl,--gc-sections,-z,nostart-stop-gc >> >> >> +LDFLAGS-tst-cleanup-nostart-stop-gc-static := -Wl,--gc-sections,-z,nostart-stop-gc >> >> >> +endif >> >> >> endif >> >> >> >> >> >> include ../Rules >> >> >> @@ -224,6 +244,17 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) >> >> >> $(objpfx)tst-wfile-sync.out: $(gen-locales) >> >> >> endif >> >> >> >> >> >> +define gen-tst-cleanup >> >> >> +$(objpfx)tst-cleanup-$1-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-$1.out >> >> >> + cmp $$^ > $$@; $$(evaluate-test) >> >> >> +$(objpfx)tst-cleanup-$1.c: tst-cleanup.c >> >> >> + cp $$< $$@ >> >> >> +.INTERMEDIATE: $(objpfx)tst-cleanup-$1.c >> >> >> +endef >> >> >> + >> >> >> +$(foreach t, default default-static start-stop-gc start-stop-gc-static nostart-stop-gc nostart-stop-gc-static, \ >> >> >> + $(eval $(call gen-tst-cleanup,$(t)))) >> >> >> + >> >> >> $(objpfx)test-freopen.out: test-freopen.sh $(objpfx)test-freopen >> >> >> $(SHELL) $< $(common-objpfx) '$(test-program-prefix)' \ >> >> >> $(common-objpfx)libio/; \ >> >> > >> >> >There are 2 issues with static tests: >> >> > >> >> >1. A separate static source is needed. >> >> >> >> .INTERMEDIATE perfectly solves the lingering copied source issue. >> >> I don't see lingering libio/tst-cleanup-*.c >> > >> >I was referring to >> > >> >elf/tst-array1-static.c >> >elf/tst-array5-static.c >> >elf/tst-dl-iter-static.c >> >elf/tst-dst-static.c >> >elf/tst-leaks1-static.c >> >elf/tst-libc_dlvsym-static.c >> >elf/tst-linkall-static.c >> >elf/tst-ptrguard1-static.c >> >elf/tst-single_threaded-pthread-static.c >> >elf/tst-single_threaded-static.c >> >elf/tst-stackguard1-static.c >> >elf/tst-tls1-static.c >> >elf/tst-tls2-static.c >> >elf/tst-tls9-static.c >> >elf/tst-tlsalign-extern-static.c >> >elf/tst-tlsalign-static.c >> >gmon/tst-gmon-static.c >> >gmon/tst-profile-static.c >> >localedata/bug-setlocale1-static.c >> >localedata/tst-langinfo-newlocale-static.c >> >localedata/tst-langinfo-setlocale-static.c >> >localedata/tst-langinfo-static.c >> >malloc/tst-malloc-usable-static.c >> >math/test-fpucw-ieee-static.c >> >math/test-fpucw-static.c >> >math/test-signgam-uchar-init-static.c >> >math/test-signgam-uchar-static.c >> >math/test-signgam-uint-init-static.c >> >math/test-signgam-uint-static.c >> >math/test-signgam-ullong-init-static.c >> >math/test-signgam-ullong-static.c >> >nptl/tst-mutex8-static.c >> >nptl/tst-mutexpi8-static.c >> >nptl/tst-sem11-static.c >> >nptl/tst-sem12-static.c >> >nptl/tst-setuid1-static.c >> >nptl/tst-stackguard1-static.c >> >nss/tst-nss-static.c >> >posix/tst-exec-static.c >> >posix/tst-spawn-static.c >> >setjmp/tst-setjmp-static.c >> > >> >> >2. We need to add the static test to both tests and tests-static. >> >> >> >> tests-static suffices. I have checked some tests-static usage in other >> >> directories. >> > >> >There are >> > >> >tests := tst-setjmp jmpbug bug269-setjmp tst-setjmp-fp \ >> > tst-sigsetjmp tst-setjmp-static >> >tests-static := tst-setjmp-static >> > >> >They should be replaced by something like >> > >> >tests := tst-setjmp jmpbug bug269-setjmp tst-setjmp-fp \ >> > tst-sigsetjmp >> >tests-static-add := tst-setjmp-static >> >> Re-checking, the tst-cleanup* targets are in tests-special so `make tests` will build them. >> If a tests-static target is not in tests-internal/tests-special, looks like it needs to be in tests. >> >> As is, I think my usage is correct. >> > >The current glibc convention is to use > >[hjl@gnu-cfl-2 x86-glibc]$ cat elf/tst-tls9-static.c >#include "tst-tls9.c" >[hjl@gnu-cfl-2 x86-glibc]$ > >to test both dynamic and static libc. If we want to use a single >source, we should create >a framework to cover all such tests. OK. Dropped cp $< $@ and .INTERMEDIATE From e937ae26a4a9a8ff6b1198f347f0b141a9285305 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 6 Apr 2021 14:45:01 -0700 Subject: [PATCH] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 ‘retain’ attribute ignored while __has_attribute(retain) is 1 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587). --- config.h.in | 3 ++ configure | 59 ++++++++++++++++++++++ configure.ac | 21 ++++++++ include/libc-symbols.h | 14 +++-- libio/Makefile | 28 ++++++++++ libio/tst-cleanup-default-static.c | 1 + libio/tst-cleanup-default.c | 1 + libio/tst-cleanup-nostart-stop-gc-static.c | 1 + libio/tst-cleanup-nostart-stop-gc.c | 1 + libio/tst-cleanup-start-stop-gc-static.c | 1 + libio/tst-cleanup-start-stop-gc.c | 1 + libio/tst-cleanup.c | 34 +++++++++++++ libio/tst-cleanup.exp | 1 + 13 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 libio/tst-cleanup-default-static.c create mode 100644 libio/tst-cleanup-default.c create mode 100644 libio/tst-cleanup-nostart-stop-gc-static.c create mode 100644 libio/tst-cleanup-nostart-stop-gc.c create mode 100644 libio/tst-cleanup-start-stop-gc-static.c create mode 100644 libio/tst-cleanup-start-stop-gc.c 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..96a08c7757 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 CC supports attribute retain. */ +#undef HAVE_GNU_RETAIN + /* Define if the linker defines __ehdr_start. */ #undef HAVE_EHDR_START diff --git a/configure b/configure index fcf43bf7de..e64b7f8efe 100755 --- a/configure +++ b/configure @@ -4105,6 +4105,31 @@ fi $as_echo "$libc_cv_textrel_ifunc" >&6; } +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for GNU attribute retain support" >&5 +$as_echo_n "checking for GNU attribute retain support... " >&6; } +if ${libc_cv_gnu_retain+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat > conftest.c <&5 \ + 2>&5 ; then + libc_cv_gnu_retain=yes +fi +rm -f conftest* +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_gnu_retain" >&5 +$as_echo "$libc_cv_gnu_retain" >&6; } +if test $libc_cv_gnu_retain = yes; then + $as_echo "#define HAVE_GNU_RETAIN 1" >>confdefs.h + +fi +config_vars="$config_vars +have-gnu-retain = $libc_cv_gnu_retain" + # Check if gcc warns about alias for function with incompatible types. { $as_echo "$as_me:${as_lineno-$LINENO}: checking if compiler warns about alias for function with incompatible types" >&5 $as_echo_n "checking if compiler warns about alias for function with incompatible types... " >&6; } @@ -5871,6 +5896,40 @@ fi $as_echo "$libc_linker_feature" >&6; } +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports -z start-stop-gc" >&5 +$as_echo_n "checking for linker that supports -z start-stop-gc... " >&6; } +libc_linker_feature=no +if test x"$gnu_ld" = x"yes"; then + libc_linker_check=`$LD -v --help 2>/dev/null | grep "\-z start-stop-gc"` + if test -n "$libc_linker_check"; then + cat > conftest.c <&5 + (eval $ac_try) 2>&5 + ac_status=$? + $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } + then + libc_linker_feature=yes + fi + rm -f conftest* + fi +fi +if test $libc_linker_feature = yes; then + libc_cv_z_start_stop_gc=yes +else + libc_cv_z_start_stop_gc=no +fi +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5 +$as_echo "$libc_linker_feature" >&6; } +config_vars="$config_vars +have-z-start-stop-gc = $libc_cv_z_start_stop_gc" + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for linker that supports --no-dynamic-linker" >&5 $as_echo_n "checking for linker that supports --no-dynamic-linker... " >&6; } libc_linker_feature=no diff --git a/configure.ac b/configure.ac index fce967f2c2..cc47e56e82 100644 --- a/configure.ac +++ b/configure.ac @@ -707,6 +707,23 @@ fi rm -f conftest*]) AC_SUBST(libc_cv_textrel_ifunc) +# Check if CC supports attribute retain as it is used in attribute_used_retain macro. +AC_CACHE_CHECK([for GNU attribute retain support], + libc_cv_gnu_retain, [dnl +cat > conftest.c <&AS_MESSAGE_LOG_FD \ + 2>&AS_MESSAGE_LOG_FD ; then + libc_cv_gnu_retain=yes +fi +rm -f conftest*]) +if test $libc_cv_gnu_retain = yes; then + AC_DEFINE(HAVE_GNU_RETAIN) +fi +LIBC_CONFIG_VAR([have-gnu-retain], [$libc_cv_gnu_retain]) + # Check if gcc warns about alias for function with incompatible types. AC_CACHE_CHECK([if compiler warns about alias for function with incompatible types], libc_cv_gcc_incompatible_alias, [dnl @@ -1317,6 +1334,10 @@ LIBC_LINKER_FEATURE([-z execstack], [-Wl,-z,execstack], [libc_cv_z_execstack=yes], [libc_cv_z_execstack=no]) AC_SUBST(libc_cv_z_execstack) +LIBC_LINKER_FEATURE([-z start-stop-gc], [-Wl,-z,start-stop-gc], + [libc_cv_z_start_stop_gc=yes], [libc_cv_z_start_stop_gc=no]) +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=yes], diff --git a/include/libc-symbols.h b/include/libc-symbols.h index 546fc26a7b..127ea656c2 100644 --- a/include/libc-symbols.h +++ b/include/libc-symbols.h @@ -352,6 +352,12 @@ for linking") */ +#ifdef HAVE_GNU_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))) = &(symbol) + static const void *__elf_set_##set##_element_##symbol##__ \ + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) #else # define _elf_set_element(set, symbol) \ - static const void *const __elf_set_##set##_element_##symbol##__ \ - __attribute__ ((used, section (#set))) = &(symbol) + static const void *const __elf_set_##set##_element_##symbol##__ \ + attribute_used_retain __attribute__ ((section (#set))) = &(symbol) #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..ed0ce4ee81 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -195,6 +195,26 @@ ifeq (yes,$(build-shared)) tests-special += $(objpfx)tst-fopenloc-cmp.out $(objpfx)tst-fopenloc-mem.out \ $(objpfx)tst-bz24228-mem.out endif + +tests += tst-cleanup-default tst-cleanup-default-static +tests-static += tst-cleanup-default-static +tests-special += $(objpfx)tst-cleanup-default-cmp.out $(objpfx)tst-cleanup-default-static-cmp.out +LDFLAGS-tst-cleanup-default = -Wl,--gc-sections +LDFLAGS-tst-cleanup-default-static = -Wl,--gc-sections + +ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes) +tests += tst-cleanup-start-stop-gc tst-cleanup-start-stop-gc-static \ + tst-cleanup-nostart-stop-gc tst-cleanup-nostart-stop-gc-static +tests-static += tst-cleanup-start-stop-gc-static tst-cleanup-nostart-stop-gc-static +tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \ + $(objpfx)tst-cleanup-start-stop-gc-static-cmp.out \ + $(objpfx)tst-cleanup-nostart-stop-gc-cmp.out \ + $(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out +LDFLAGS-tst-cleanup-start-stop-gc := -Wl,--gc-sections,-z,start-stop-gc +LDFLAGS-tst-cleanup-start-stop-gc-static := -Wl,--gc-sections,-z,start-stop-gc +LDFLAGS-tst-cleanup-nostart-stop-gc := -Wl,--gc-sections,-z,nostart-stop-gc +LDFLAGS-tst-cleanup-nostart-stop-gc-static := -Wl,--gc-sections,-z,nostart-stop-gc +endif endif include ../Rules @@ -224,6 +244,14 @@ $(objpfx)tst_wprintf2.out: $(gen-locales) $(objpfx)tst-wfile-sync.out: $(gen-locales) endif +define gen-tst-cleanup +$(objpfx)tst-cleanup-$1-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-$1.out + cmp $$^ > $$@; $$(evaluate-test) +endef + +$(foreach t,default default-static start-stop-gc start-stop-gc-static nostart-stop-gc nostart-stop-gc-static, \ + $(eval $(call gen-tst-cleanup,$(t)))) + $(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-default-static.c b/libio/tst-cleanup-default-static.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-default-static.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup-default.c b/libio/tst-cleanup-default.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-default.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup-nostart-stop-gc-static.c b/libio/tst-cleanup-nostart-stop-gc-static.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-nostart-stop-gc-static.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup-nostart-stop-gc.c b/libio/tst-cleanup-nostart-stop-gc.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-nostart-stop-gc.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup-start-stop-gc-static.c b/libio/tst-cleanup-start-stop-gc-static.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-start-stop-gc-static.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup-start-stop-gc.c b/libio/tst-cleanup-start-stop-gc.c new file mode 100644 index 0000000000..c39956e2a8 --- /dev/null +++ b/libio/tst-cleanup-start-stop-gc.c @@ -0,0 +1 @@ +#include "tst-cleanup.c" diff --git a/libio/tst-cleanup.c b/libio/tst-cleanup.c new file mode 100644 index 0000000000..837feac164 --- /dev/null +++ b/libio/tst-cleanup.c @@ -0,0 +1,34 @@ +/* Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* Test that stdout is flushed after atexit callbacks were run, even if the + * executable is linked with --gc-sections. */ + +#include +#include + +void +hook (void) +{ + puts ("hello"); +} + +int +main (void) +{ + atexit (hook); +} diff --git a/libio/tst-cleanup.exp b/libio/tst-cleanup.exp new file mode 100644 index 0000000000..ce01362503 --- /dev/null +++ b/libio/tst-cleanup.exp @@ -0,0 +1 @@ +hello -- 2.31.0.208.g409f899ff0-goog