about summary refs log tree commit diff
path: root/libio
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
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')
-rw-r--r--libio/Makefile28
-rw-r--r--libio/tst-cleanup-default-static.c1
-rw-r--r--libio/tst-cleanup-default.c1
-rw-r--r--libio/tst-cleanup-nostart-stop-gc-static.c1
-rw-r--r--libio/tst-cleanup-nostart-stop-gc.c1
-rw-r--r--libio/tst-cleanup-start-stop-gc-static.c1
-rw-r--r--libio/tst-cleanup-start-stop-gc.c1
-rw-r--r--libio/tst-cleanup.c34
-rw-r--r--libio/tst-cleanup.exp1
9 files changed, 69 insertions, 0 deletions
diff --git a/libio/Makefile b/libio/Makefile
index 12ce41038f..ed0ce4ee81 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -195,6 +195,26 @@ 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 tst-cleanup-default-static
+tests-static += tst-cleanup-default-static
+tests-special += $(objpfx)tst-cleanup-default-cmp.out $(objpfx)tst-cleanup-default-static-cmp.out
+LDFLAGS-tst-cleanup-default = -Wl,--gc-sections
+LDFLAGS-tst-cleanup-default-static = -Wl,--gc-sections
+
+ifeq ($(have-gnu-retain)$(have-z-start-stop-gc),yesyes)
+tests += tst-cleanup-start-stop-gc tst-cleanup-start-stop-gc-static \
+		tst-cleanup-nostart-stop-gc tst-cleanup-nostart-stop-gc-static
+tests-static += tst-cleanup-start-stop-gc-static tst-cleanup-nostart-stop-gc-static
+tests-special += $(objpfx)tst-cleanup-start-stop-gc-cmp.out \
+		$(objpfx)tst-cleanup-start-stop-gc-static-cmp.out \
+		$(objpfx)tst-cleanup-nostart-stop-gc-cmp.out \
+		$(objpfx)tst-cleanup-nostart-stop-gc-static-cmp.out
+LDFLAGS-tst-cleanup-start-stop-gc := -Wl,--gc-sections,-z,start-stop-gc
+LDFLAGS-tst-cleanup-start-stop-gc-static := -Wl,--gc-sections,-z,start-stop-gc
+LDFLAGS-tst-cleanup-nostart-stop-gc := -Wl,--gc-sections,-z,nostart-stop-gc
+LDFLAGS-tst-cleanup-nostart-stop-gc-static := -Wl,--gc-sections,-z,nostart-stop-gc
+endif
 endif
 
 include ../Rules
@@ -224,6 +244,14 @@ $(objpfx)tst_wprintf2.out: $(gen-locales)
 $(objpfx)tst-wfile-sync.out: $(gen-locales)
 endif
 
+define gen-tst-cleanup
+$(objpfx)tst-cleanup-$1-cmp.out: tst-cleanup.exp $(objpfx)tst-cleanup-$1.out
+	cmp $$^ > $$@; $$(evaluate-test)
+endef
+
+$(foreach t,default default-static start-stop-gc start-stop-gc-static nostart-stop-gc nostart-stop-gc-static, \
+  $(eval $(call gen-tst-cleanup,$(t))))
+
 $(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-default-static.c b/libio/tst-cleanup-default-static.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-default-static.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
diff --git a/libio/tst-cleanup-default.c b/libio/tst-cleanup-default.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-default.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
diff --git a/libio/tst-cleanup-nostart-stop-gc-static.c b/libio/tst-cleanup-nostart-stop-gc-static.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-nostart-stop-gc-static.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
diff --git a/libio/tst-cleanup-nostart-stop-gc.c b/libio/tst-cleanup-nostart-stop-gc.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-nostart-stop-gc.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
diff --git a/libio/tst-cleanup-start-stop-gc-static.c b/libio/tst-cleanup-start-stop-gc-static.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-start-stop-gc-static.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
diff --git a/libio/tst-cleanup-start-stop-gc.c b/libio/tst-cleanup-start-stop-gc.c
new file mode 100644
index 0000000000..c39956e2a8
--- /dev/null
+++ b/libio/tst-cleanup-start-stop-gc.c
@@ -0,0 +1 @@
+#include "tst-cleanup.c"
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);
+}
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