about summary refs log tree commit diff
path: root/libio/tst-cleanup.c
diff options
context:
space:
mode:
authorFangrui Song <maskray@google.com>2021-04-16 11:26:39 -0700
committerFangrui Song <maskray@google.com>2021-04-16 11:26:39 -0700
commitcd6ae7ea5431c2b8f16201fb0e2c413bf8d2df06 (patch)
tree68d5b5402097696b3e3898fad40a143720da0438 /libio/tst-cleanup.c
parent1a8605b6cd257e8a74e29b5b71c057211f5fb847 (diff)
downloadglibc-cd6ae7ea5431c2b8f16201fb0e2c413bf8d2df06.tar.gz
glibc-cd6ae7ea5431c2b8f16201fb0e2c413bf8d2df06.tar.xz
glibc-cd6ae7ea5431c2b8f16201fb0e2c413bf8d2df06.zip
Set the retain attribute on _elf_set_element if CC supports [BZ #27492]
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).

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
Diffstat (limited to 'libio/tst-cleanup.c')
-rw-r--r--libio/tst-cleanup.c34
1 files changed, 34 insertions, 0 deletions
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
+   <https://www.gnu.org/licenses/>.  */
+
+/* Test that stdout is flushed after atexit callbacks were run, even if the
+ * executable is linked with --gc-sections.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+void
+hook (void)
+{
+  puts ("hello");
+}
+
+int
+main (void)
+{
+  atexit (hook);
+}