From patchwork Wed Mar 31 19:39:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 1460685 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=cDJmNyr5; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4F9c8P6pLbz9sVS for ; Thu, 1 Apr 2021 06:39:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7212B3857C74; Wed, 31 Mar 2021 19:39:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7212B3857C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1617219571; bh=BGBgFe3iPx+vMrlGuqMYqKZI1Dxo0aHorBdMwQehAdA=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=cDJmNyr5Ep1A9oVOzpVCwJnjSCTvH573K03ZQ9m3gPuEH/Td2MK8KbS1JSlg/Shxw iLjkC3802DW87ys7ZRubCDN7/q9ass5KYLBascDCzMfrNwzilAW5+F1pRN7URhle5X obeCTdqp907jlhQXlSh4Sp4Wuqb/NtqXyKrCIIW8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by sourceware.org (Postfix) with ESMTPS id 8E0C53857C6F for ; Wed, 31 Mar 2021 19:39:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8E0C53857C6F Received: by mail-pf1-x44a.google.com with SMTP id a6so1984933pfv.9 for ; Wed, 31 Mar 2021 12:39:28 -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:in-reply-to:message-id:mime-version :references:subject:from:to:cc:content-transfer-encoding; bh=BGBgFe3iPx+vMrlGuqMYqKZI1Dxo0aHorBdMwQehAdA=; b=OYSc18QQ4aQbfuN0c0rHt8sIakjLsr3Y/Bx5h6ijUtelJ8vpPOtRcmKdjLttB+0Kn+ Tx05AabOkVc/HouMnHlOsCEyBlewIJge5R9CZrwJQHSwBaSbNPCRkCKosocBb8J4bfF4 kov4h4+d2SD+YnCH0SRv6ubqd2SuuMki4PvDcmbFcYUyKR1RoDJsKofbdWpdYrqhXubp DfZQaxvVsBEf/44EZsLfRtc7vSSD3csaob0DQ/3n86yzjQYv5vQz6UR/STOR+uu4vp3v dIsp4FX6qapn3aZJEvY9Vbs8g4u1IER/aICBaejUhea2GfcJhAFgu4r2No12SPyLi7zV wwZw== X-Gm-Message-State: AOAM532liMDsN4roGYKMFxkwaKD9OHYho8/95h+1zCIRZLMIjy6I6hpn 8XNUNcSNk1P8a8XELqRk2kTZOVqNPb900hZFlkdQ916LtN/lNOSA8eCO2fN5wqkbDIn0HRaqWJD N1TYMk0zydiDNVXo+tMRDeyhtn9gOY0CleRDZj62P4L0J6nPaXsJBQKbthdWDiUFa1kog X-Google-Smtp-Source: ABdhPJxD86/p1kyzWnrwdvtRscIsWmgmnG1nVb8FMBChV0WKk9WSrlj6AfpCve24BpxN6JxCB6nO7cL2Kytu X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:0:9106:8d2e:6ec7:9708]) (user=maskray job=sendgmr) by 2002:a17:902:b60b:b029:e6:ef44:5231 with SMTP id b11-20020a170902b60bb02900e6ef445231mr4603200pls.79.1617219567471; Wed, 31 Mar 2021 12:39:27 -0700 (PDT) Date: Wed, 31 Mar 2021 12:39:23 -0700 In-Reply-To: Message-Id: <20210331193923.2602174-1-maskray@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog Subject: [PATCH v4] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] To: libc-alpha@sourceware.org, Florian Weimer , "H . J . Lu " X-Spam-Status: No, score=-18.4 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_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-Patchwork-Original-From: Fangrui Song via Libc-alpha From: Fangrui Song Reply-To: Fangrui Song Cc: Fangrui Song Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 --- 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_used_retain macro. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for gcc attribute 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 <&5 \ + 2>&5 ; then + libc_cv_gcc_retain=yes +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 = 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 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 +5895,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..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_used_retain macro. +AC_CACHE_CHECK([for gcc attribute retain support], + libc_cv_gcc_retain, [dnl +cat > conftest.c <&AS_MESSAGE_LOG_FD \ + 2>&AS_MESSAGE_LOG_FD ; then + libc_cv_gcc_retain=yes +fi +rm -f conftest*]) +if test $libc_cv_gcc_retain = 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 incompatible types], libc_cv_gcc_incompatible_alias, [dnl @@ -1317,6 +1333,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..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))) = &(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..439d22e057 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 +LDFLAGS-tst-cleanup-default = -Wl,--gc-sections + +ifeq ($(have-z-start-stop-gc),yes) +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 +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 @@ +/* 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