From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <3cBZYYAcKCgcthzryh5nvvnsl.jvtspij-hswohzv1yjl3hyl.vyn@flex--maskray.bounces.google.com> 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 2E4723858D29 for ; Mon, 22 Mar 2021 04:00:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2E4723858D29 Received: by mail-yb1-xb4a.google.com with SMTP id y7so58613706ybh.20 for ; Sun, 21 Mar 2021 21:00:49 -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=MpuA3/25RPtCPB9tGVOrpHE/r900UAnPD4O7rMx7mEM=; b=fGqHT0D3R5oeNZDJ81BSwTcrFuV7yt1VuRQiYCPqhKvPday9qiUeZRetMOJg6P3L5z Fquuy7Y9qBUjkTlLVNwhxh3JoLpvtaPFHrBSrJPgsXUkfbhqEb4FYntKgYnm5TGCv7RP CtTAGnhG01PuccDjKNsVa2kyjBKv7VYHrzprXYAnpMRyzg1P6ni6fmJjZOIVOuuk5hoo ElrJVWwF1PIBNiRGDtGl5ZZwssS4SG8OAeBiGE12uj840UPVSEpbdQbXcvsa821E4l8a KJxY5Zlz6oWds3WjCDZ5VI5unSV/J4oOBAUj9Sp0nWgzxR+DI6G3Cpl1Q1UEZRsvK0zT FkgQ== X-Gm-Message-State: AOAM532gcAgbxubwDB6o0bCKhxGcovX7PWd6fa19QkoOONOYHzCRwShI cHz1nBpZ+3x/5/bFNcl/88kNQnob2udBv3rXThv2CIiGCDJ8UjTWLWXQI/7L7nbNitWPYBwJB6f YuPr7saMeWtogi298a9AIoihC6Oh2RBVldhDw0la+88qp+jCpay6qHZVnlvTmOD/zBMXz X-Google-Smtp-Source: ABdhPJzAlE+3pPQNGjpOIAtvowTpKwi8dL8trtE0BitNqDB9dboCHolLNOX4WhtF+FTy65amRgi8yOxYHsqm X-Received: from maskray1.svl.corp.google.com ([2620:15c:2ce:0:683f:d5f2:bcd4:6d19]) (user=maskray job=sendgmr) by 2002:a25:d58:: with SMTP id 85mr17517703ybn.450.1616385648681; Sun, 21 Mar 2021 21:00:48 -0700 (PDT) Date: Sun, 21 Mar 2021 21:00:44 -0700 In-Reply-To: <87zgz4ygui.fsf@oldenburg.str.redhat.com> Message-Id: <20210322040044.2559211-1-maskray@google.com> Mime-Version: 1.0 References: <87zgz4ygui.fsf@oldenburg.str.redhat.com> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog Subject: [PATCH v2] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] From: Fangrui Song To: libc-alpha@sourceware.org, Florian Weimer Cc: Fangrui Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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-List-Received-Date: Mon, 22 Mar 2021 04:00:52 -0000 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 __ha= s_attribute(retain) is 1 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99587). --- config.h.in | 3 +++ configure | 23 +++++++++++++++++++++++ configure.ac | 16 ++++++++++++++++ include/libc-symbols.h | 30 +++++++++++++++++++++--------- 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/config.h.in b/config.h.in index f21bf04e47..cc875e90f9 100644 --- a/config.h.in +++ b/config.h.in @@ -187,6 +187,9 @@ /* Define if gcc supports attribute ifunc. */ #undef HAVE_GCC_IFUNC =20 +/* Define if gcc supports attribute retain. */ +#undef HAVE_GCC_RETAIN + /* Define if the linker defines __ehdr_start. */ #undef HAVE_EHDR_START =20 diff --git a/configure b/configure index 37cef37413..28494de748 100755 --- a/configure +++ b/configure @@ -4105,6 +4105,29 @@ fi $as_echo "$libc_cv_textrel_ifunc" >&6; } =20 =20 +# 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=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 about = alias for function with incompatible types" >&5 $as_echo_n "checking if compiler warns about alias for function with incom= patible types... " >&6; } diff --git a/configure.ac b/configure.ac index 16b15b6f90..94ad713820 100644 --- a/configure.ac +++ b/configure.ac @@ -707,6 +707,22 @@ fi rm -f conftest*]) AC_SUBST(libc_cv_textrel_ifunc) =20 +# 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=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 incompatib= le types], libc_cv_gcc_incompatible_alias, [dnl diff --git a/include/libc-symbols.h b/include/libc-symbols.h index c83e550b03..f8eafcf1f3 100644 --- a/include/libc-symbols.h +++ b/include/libc-symbols.h @@ -373,17 +373,29 @@ for linking") =20 /* These are all done the same way in ELF. There is a new section created for each set. */ -#ifdef SHARED +#ifdef HAVE_GCC_RETAIN +# ifdef SHARED /* 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) + 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, retain, section (#set))) =3D &(symbol) +# else +# define _elf_set_element(set, symbol) \ + static const void *const __elf_set_##set##_element_##symbol##__ \ + __attribute__ ((used, retain, section (#set))) =3D &(symbol) +# endif #else -# define _elf_set_element(set, symbol) \ - static const void *const __elf_set_##set##_element_##symbol##__ \ - __attribute__ ((used, section (#set))) =3D &(symbol) -#endif +# ifdef SHARED +# define _elf_set_element(set, symbol) \ + static const void *__elf_set_##set##_element_##symbol##__ \ + __attribute__ ((used, 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) +# endif +#endif /* HAVE_GCC_RETAIN */ =20 /* Define SET as a symbol set. This may be required (it is in a.out) to be able to use the set's contents. */ --=20 2.31.0.rc2.261.g7f71774620-goog