From patchwork Mon Mar 29 23:24:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 42804 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7BA9F3857023; Mon, 29 Mar 2021 23:24:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7BA9F3857023 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1617060287; bh=UXlKSFc/gydAaQK0mOvzoMJUt7bBuwEt/v9PhkW5qPo=; 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=VKfMJk9urLZgEnju5pYLgta9PCtSI1XVVLQEl+F5SOhmX6dT2AX+yGn5fwrHxQIHW fvioemdq64TCwU76w6sx/cWZNYyduc+/Vgk7gaZH0qPnU3tAz2mqLSvWxLCkwu/1Ep ptO6CK5fW7Gbh8aCz76qJ5n0aaRYHnYe+QjESU44= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id 324123858002 for ; Mon, 29 Mar 2021 23:24:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 324123858002 Received: by mail-yb1-xb4a.google.com with SMTP id t5so10852080ybc.18 for ; Mon, 29 Mar 2021 16:24:44 -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=UXlKSFc/gydAaQK0mOvzoMJUt7bBuwEt/v9PhkW5qPo=; b=KCyioJfMgG7QzdFLhZtGa4BDBGpYYNStA47NPP6NxwC09z/OS2O5eC2P78biXCfZRl naUbxOLQsusz8Von4GmazsLaRshxuGMng7Fpgkahj38Y1HXddC3saJy0lJtOTmgr6K2r D6B6mH/enZn4iZPsR1u+F/FCy7pooX0rDDwsyA/DqYs9+g5V90OTo2P5XpQhUx8Iecd0 M19/uB3T47mH56lIJ/U57EY98OdRxRboACHsJbXGyJ9+m12tpzulc/LRdU6pcWI06TaU kGuPXcE6xCgvDf22S1nKPT1ohBIud+GuSv8i9Z1Nd93xmtz1q++oAfeNWS11DKU9gbny sKEw== X-Gm-Message-State: AOAM530WRb9hIl49G2TZL+vS9K1q+3mOVdgfkYj83M1SfO3/4X8uqgm4 ejiVRIjdOCJ/ahP5Q+AUY+AEeW8FtCPVi4Jk/bdlsrvfHl0fS75azxiQFhA6AvmtdB3BSBVt+o8 ObxAF9g+52g7D9UcPFt73RNC9Du5hej7980kZ8oJPWMK+9GJqDwDOxUpqml76sUqexLa1 X-Google-Smtp-Source: ABdhPJw0I4PWInRKhptDLUwU9KWkD0yARNMOTFqI4o9L6iRq4gS9NSvj+LRAGzLl6FnFl4PROGfN4Gt5rLwH X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:0:cdf0:a0fb:148f:863]) (user=maskray job=sendgmr) by 2002:a25:56c2:: with SMTP id k185mr41377105ybb.131.1617060283709; Mon, 29 Mar 2021 16:24:43 -0700 (PDT) Date: Mon, 29 Mar 2021 16:24:40 -0700 In-Reply-To: Message-Id: <20210329232440.3083493-1-maskray@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.31.0.291.g576ba9dcdaf-goog Subject: [PATCH v3] 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=-19.5 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 --- 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 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; } 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 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 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