From cd6ae7ea5431c2b8f16201fb0e2c413bf8d2df06 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 16 Apr 2021 11:26:39 -0700 Subject: 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 --- libio/Makefile | 28 ++++++++++++++++++++++++ libio/tst-cleanup-default-static.c | 1 + libio/tst-cleanup-default.c | 1 + libio/tst-cleanup-nostart-stop-gc-static.c | 1 + libio/tst-cleanup-nostart-stop-gc.c | 1 + libio/tst-cleanup-start-stop-gc-static.c | 1 + libio/tst-cleanup-start-stop-gc.c | 1 + libio/tst-cleanup.c | 34 ++++++++++++++++++++++++++++++ libio/tst-cleanup.exp | 1 + 9 files changed, 69 insertions(+) create mode 100644 libio/tst-cleanup-default-static.c create mode 100644 libio/tst-cleanup-default.c create mode 100644 libio/tst-cleanup-nostart-stop-gc-static.c create mode 100644 libio/tst-cleanup-nostart-stop-gc.c create mode 100644 libio/tst-cleanup-start-stop-gc-static.c create mode 100644 libio/tst-cleanup-start-stop-gc.c create mode 100644 libio/tst-cleanup.c create mode 100644 libio/tst-cleanup.exp (limited to 'libio') 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 + . */ + +/* Test that stdout is flushed after atexit callbacks were run, even if the + * executable is linked with --gc-sections. */ + +#include +#include + +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 -- cgit 1.4.1