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 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'libio/Makefile') 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/; \ -- cgit v1.2.3