diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 2fef817d73..818f580e35 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 7b74f2653c..74b54c2c0c 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 1fb57876c3..7e0415cbed 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 616b923b53..16c14d9b7a 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 95728b6f03..0871a0c025 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 2347f9ae19..d72d69af12 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 4bb4ba3f46..d1aa3b0700 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 5e59f3853e..cea89e57e2 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -760,6 +760,10 @@ fragment <