public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Set the retain attribute on _elf_set_element if CC supports [BZ #27492]
@ 2021-03-15  1:10 Fangrui Song
  2021-03-15  7:28 ` Florian Weimer
  0 siblings, 1 reply; 40+ messages in thread
From: Fangrui Song @ 2021-03-15  1:10 UTC (permalink / raw)
  To: libc-alpha; +Cc: Fangrui Song

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.36 and LLD 13 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: `#if defined (__has_attribute) && __has_attribute (retain)`
does not work in GCC assembly mode:

    error: missing binary operator before token "("
---
 include/libc-symbols.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/include/libc-symbols.h b/include/libc-symbols.h
index c83e550b03..291a8d8f08 100644
--- a/include/libc-symbols.h
+++ b/include/libc-symbols.h
@@ -373,16 +373,32 @@ for linking")
 
 /* These are all done the same way in ELF.
    There is a new section created for each set.  */
-#ifdef SHARED
+#ifdef __has_attribute
+#if __has_attribute (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) \
+#  define _elf_set_element(set, symbol) \
   static const void *__elf_set_##set##_element_##symbol##__ \
-    __attribute__ ((used, section (#set))) = &(symbol)
+    __attribute__ ((used, retain, section (#set))) = &(symbol)
+# else
+#  define _elf_set_element(set, symbol) \
+  static const void *const __elf_set_##set##_element_##symbol##__ \
+    __attribute__ ((used, retain, section (#set))) = &(symbol)
+# endif
 #else
-# define _elf_set_element(set, symbol) \
+# 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))) = &(symbol)
+# else
+#  define _elf_set_element(set, symbol) \
   static const void *const __elf_set_##set##_element_##symbol##__ \
     __attribute__ ((used, section (#set))) = &(symbol)
+# endif
+#endif
 #endif
 
 /* Define SET as a symbol set.  This may be required (it is in a.out) to
-- 
2.31.0.rc2.261.g7f71774620-goog


^ permalink raw reply	[flat|nested] 40+ messages in thread

end of thread, other threads:[~2021-04-16 13:01 UTC | newest]

Thread overview: 40+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-15  1:10 [PATCH] Set the retain attribute on _elf_set_element if CC supports [BZ #27492] Fangrui Song
2021-03-15  7:28 ` Florian Weimer
2021-03-15  7:45   ` Fāng-ruì Sòng
2021-03-15  8:13     ` Florian Weimer
2021-03-22  4:00       ` [PATCH v2] " Fangrui Song
2021-03-22  4:24         ` H.J. Lu
2021-03-22  4:40           ` Fangrui Song
2021-03-22 20:28             ` H.J. Lu
2021-03-23  4:14               ` Fangrui Song
2021-03-29 23:11                 ` H.J. Lu
2021-03-29 23:24                   ` [PATCH v3] " Fangrui Song
2021-03-30 18:17                     ` H.J. Lu
2021-03-30 19:17                       ` Fāng-ruì Sòng
2021-03-30 19:30                         ` H.J. Lu
2021-03-31  4:34                           ` Fāng-ruì Sòng
2021-03-31 16:17                             ` H.J. Lu
2021-03-31 18:09                               ` Fāng-ruì Sòng
2021-03-31 18:57                                 ` H.J. Lu
2021-03-31 19:39                                   ` [PATCH v4] " Fangrui Song
2021-03-31 23:38                                     ` H.J. Lu
2021-04-01  0:02                                       ` Fangrui Song
2021-04-01  1:00                                         ` H.J. Lu
2021-04-01  1:06                                           ` [PATCH v5] " Fangrui Song
2021-04-01 12:52                                             ` H.J. Lu
2021-04-02  3:23                                               ` Fangrui Song
2021-04-02 14:14                                                 ` H.J. Lu
2021-04-02 17:09                                                   ` Fangrui Song
2021-04-02 17:33                                                     ` H.J. Lu
2021-04-03 18:02                                                   ` Fangrui Song
2021-04-03 20:47                                                     ` H.J. Lu
2021-04-03 21:57                                                       ` Fangrui Song
2021-04-05  1:55                                                         ` H.J. Lu
2021-04-05 18:17                                                           ` Fangrui Song
2021-04-05 20:35                                                             ` H.J. Lu
2021-04-05 21:03                                                               ` Fangrui Song
2021-04-05 21:58                                                                 ` H.J. Lu
2021-04-06 21:46                                                                   ` Fangrui Song
2021-04-09 22:36                                                                     ` Fāng-ruì Sòng
2021-04-16  3:51                                                                       ` Fāng-ruì Sòng
2021-04-16 13:00                                                                     ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).