From 8ee10e86093150c70360d9e26b29e6d9b6398f33 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Mon, 1 Mar 2021 08:22:49 +1030 Subject: [PATCH] PR27451, -z start_stop_gc When --gc-sections is in effect, a reference from a retained section to __start_SECNAME or __stop_SECNAME causes all input sections named SECNAME to also be retained, if SECNAME is representable as a C identifier and either __start_SECNAME or __stop_SECNAME is synthesized by the linker. Add an option to disable that feature, effectively ignoring any relocation that references a synthesized linker defined __start_ or __stop_ symbol. PR 27451 include/ * bfdlink.h (struct bfd_link_info): Add start_stop_gc. bfd/ * elflink.c (_bfd_elf_gc_mark_rsec): Ignore synthesized linker defined start/stop symbols when start_stop_gc. (bfd_elf_gc_mark_dynamic_ref_symbol): Likewise. (bfd_elf_define_start_stop): Don't modify ldscript_def syms. * linker.c (bfd_generic_define_start_stop): Likewise. ld/ * emultempl/elf.em: Handle -z start-stop-gc and -z nostart-stop-gc. * lexsup.c (elf_static_list_options): Display help for them. Move help for -z stack-size to here from elf_shlib_list_options. Add help for -z start-stop-visibility and -z undefs. * ld.texi: Document -z start-stop-gc and -z nostart-stop-gc. * NEWS: Mention -z start-stop-gc. * testsuite/ld-gc/start2.s, * testsuite/ld-gc/start2.d: New test. * testsuite/ld-gc/gc.exp: Run it. --- bfd/ChangeLog | 9 +++++++++ bfd/elflink.c | 11 +++++++++-- bfd/linker.c | 1 + include/ChangeLog | 5 +++++ include/bfdlink.h | 4 ++++ ld/ChangeLog | 13 +++++++++++++ ld/NEWS | 3 +++ ld/emultempl/elf.em | 4 ++++ ld/ld.texi | 17 +++++++++++++++++ ld/ldmain.c | 1 + ld/lexsup.c | 13 +++++++++++-- ld/testsuite/ld-gc/gc.exp | 1 + ld/testsuite/ld-gc/start2.d | 10 ++++++++++ ld/testsuite/ld-gc/start2.s | 7 +++++++ 14 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 ld/testsuite/ld-gc/start2.d create mode 100644 ld/testsuite/ld-gc/start2.s diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 2fef817d734..818f580e359 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,12 @@ +2021-03-01 Alan Modra + Fangrui Song + + * elflink.c (_bfd_elf_gc_mark_rsec): Ignore synthesized linker + defined start/stop symbols when start_stop_gc. + (bfd_elf_gc_mark_dynamic_ref_symbol): Likewise. + (bfd_elf_define_start_stop): Don't modify ldscript_def syms. + * linker.c (bfd_generic_define_start_stop): Likewise. + 2021-02-25 Alan Modra PR 27441 diff --git a/bfd/elflink.c b/bfd/elflink.c index 7b74f2653c2..74b54c2c0c0 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -13444,12 +13444,15 @@ _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, hw->mark = 1; } - if (start_stop != NULL) + if (h->start_stop && !h->root.ldscript_def) { + if (info->start_stop_gc) + return NULL; + /* To work around a glibc bug, mark XXX input sections when there is a reference to __start_XXX or __stop_XXX symbols. */ - if (h->start_stop) + else if (start_stop != NULL) { asection *s = h->u2.start_stop_section; *start_stop = !s->gc_mark; @@ -13912,6 +13915,9 @@ bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) if ((h->root.type == bfd_link_hash_defined || h->root.type == bfd_link_hash_defweak) + && (!h->start_stop + || h->root.ldscript_def + || !info->start_stop_gc) && ((h->ref_dynamic && !h->forced_local) || ((h->def_regular || ELF_COMMON_DEF_P (h)) && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL @@ -14984,6 +14990,7 @@ bfd_elf_define_start_stop (struct bfd_link_info *info, FALSE, FALSE, TRUE); /* NB: Common symbols will be turned into definition later. */ if (h != NULL + && !h->root.ldscript_def && (h->root.type == bfd_link_hash_undefined || h->root.type == bfd_link_hash_undefweak || ((h->ref_regular || h->def_dynamic) diff --git a/bfd/linker.c b/bfd/linker.c index 1fb57876c31..7e0415cbed2 100644 --- a/bfd/linker.c +++ b/bfd/linker.c @@ -3188,6 +3188,7 @@ bfd_generic_define_start_stop (struct bfd_link_info *info, h = bfd_link_hash_lookup (info->hash, symbol, FALSE, FALSE, TRUE); if (h != NULL + && !h->ldscript_def && (h->type == bfd_link_hash_undefined || h->type == bfd_link_hash_undefweak)) { diff --git a/include/ChangeLog b/include/ChangeLog index 616b923b53b..16c14d9b7a6 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,8 @@ +2021-03-01 Alan Modra + Fangrui Song + + * bfdlink.h (struct bfd_link_info): Add start_stop_gc. + 2021-02-21 Alan Modra * bfdlink.h (struct bfd_link_info): Add warn_multiple_definition. diff --git a/include/bfdlink.h b/include/bfdlink.h index 95728b6f031..0871a0c025a 100644 --- a/include/bfdlink.h +++ b/include/bfdlink.h @@ -662,6 +662,10 @@ struct bfd_link_info /* May be used to set DT_GNU_FLAGS_1 for ELF. */ bfd_vma gnu_flags_1; + /* TRUE if references to __start_/__stop_ synthesized symbols do not + specially retain C identifier named sections. */ + int start_stop_gc; + /* May be used to set ELF visibility for __start_* / __stop_. */ unsigned int start_stop_visibility; diff --git a/ld/ChangeLog b/ld/ChangeLog index 2347f9ae19c..d72d69af120 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,16 @@ +2021-03-01 Alan Modra + Fangrui Song + + * emultempl/elf.em: Handle -z start-stop-gc and -z nostart-stop-gc. + * lexsup.c (elf_static_list_options): Display help for them. Move + help for -z stack-size to here from elf_shlib_list_options. Add + help for -z start-stop-visibility and -z undefs. + * ld.texi: Document -z start-stop-gc and -z nostart-stop-gc. + * NEWS: Mention -z start-stop-gc. + * testsuite/ld-gc/start2.s, + * testsuite/ld-gc/start2.d: New test. + * testsuite/ld-gc/gc.exp: Run it. + 2021-03-01 Alan Modra * ldlang.c (undef_start_stop): For ELF make undefined start/stop diff --git a/ld/NEWS b/ld/NEWS index 4bb4ba3f466..d1aa3b07000 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -5,6 +5,9 @@ * Add -z report-relative-reloc to x86 ELF linker to report dynamic relative relocations. +* Add -z start-stop-gc to disable special treatment of __start_*/__stop_* + references when --gc-sections. + Changes in 2.36: * Add libdep plugin, for linking dependencies of static libraries that diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index 5e59f3853ee..cea89e57e25 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -760,6 +760,10 @@ fragment <