PR27451, -z start_stop_gc
authorAlan Modra <amodra@gmail.com>
Sun, 28 Feb 2021 21:52:49 +0000 (08:22 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 1 Mar 2021 06:58:03 +0000 (17:28 +1030)
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.

14 files changed:
bfd/ChangeLog
bfd/elflink.c
bfd/linker.c
include/ChangeLog
include/bfdlink.h
ld/ChangeLog
ld/NEWS
ld/emultempl/elf.em
ld/ld.texi
ld/ldmain.c
ld/lexsup.c
ld/testsuite/ld-gc/gc.exp
ld/testsuite/ld-gc/start2.d [new file with mode: 0644]
ld/testsuite/ld-gc/start2.s [new file with mode: 0644]

index 2fef817d734670e9331ac747d160fe3e36f19433..818f580e359cd92d4bbaac4b2bc3556c6b07de68 100644 (file)
@@ -1,3 +1,12 @@
+2021-03-01  Alan Modra  <amodra@gmail.com>
+           Fangrui Song <maskray@google.com>
+
+       * 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  <amodra@gmail.com>
 
        PR 27441
index 7b74f2653c227afb6439337030edf8c787e4afa7..74b54c2c0c0887a34652fa7ab9089229ea0f0301 100644 (file)
@@ -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)
index 1fb57876c318b4151f6e746d97c36e5f3af272e1..7e0415cbed276016dc844f8e38bcebe0a4238631 100644 (file)
@@ -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))
     {
index 616b923b53b15efd45ec8aa397e01d8bf0dfef1d..16c14d9b7a617f2bd41452f2e299c3eaf373c8e7 100644 (file)
@@ -1,3 +1,8 @@
+2021-03-01  Alan Modra  <amodra@gmail.com>
+           Fangrui Song <maskray@google.com>
+
+       * bfdlink.h (struct bfd_link_info): Add start_stop_gc.
+
 2021-02-21  Alan Modra  <amodra@gmail.com>
 
        * bfdlink.h (struct bfd_link_info): Add warn_multiple_definition.
index 95728b6f031ca5b6eb6306fdd497859cc5dad360..0871a0c025acf82d4af71b409c6ad914e9afabc5 100644 (file)
@@ -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;
 
index 2347f9ae19c2bbf089b036603ee5bae8933ad3fc..d72d69af1204de0941220fdb439d43039a97bce2 100644 (file)
@@ -1,3 +1,16 @@
+2021-03-01  Alan Modra  <amodra@gmail.com>
+           Fangrui Song <maskray@google.com>
+
+       * 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  <amodra@gmail.com>
 
        * ldlang.c (undef_start_stop): For ELF make undefined start/stop
diff --git a/ld/NEWS b/ld/NEWS
index 4bb4ba3f466a5555e887a7be2019b10150dfa0b1..d1aa3b07000b4d08a53758549ae6c2c6ab343a85 100644 (file)
--- 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
index 5e59f3853ee3b3bc30f28c9829fbd66ca0105df9..cea89e57e255b06fb4930dc7c3b91aeaf482e434 100644 (file)
@@ -760,6 +760,10 @@ fragment <<EOF
        {
          link_info.flags_1 |= DF_1_GLOBAUDIT;
        }
+      else if (CONST_STRNEQ (optarg, "start-stop-gc"))
+       link_info.start_stop_gc = TRUE;
+      else if (CONST_STRNEQ (optarg, "nostart-stop-gc"))
+       link_info.start_stop_gc = FALSE;
       else if (CONST_STRNEQ (optarg, "start-stop-visibility="))
        {
          if (strcmp (optarg, "start-stop-visibility=default") == 0)
index 9643818e3ed3a745d085549cc0a4b873cce90ecf..6d016ecc347d7ec32260f61c50bc5dadd74b8632 100644 (file)
@@ -1453,6 +1453,23 @@ Specify a stack size for an ELF @code{PT_GNU_STACK} segment.
 Specifying zero will override any default non-zero sized
 @code{PT_GNU_STACK} segment creation.
 
+@item start-stop-gc
+@itemx nostart-stop-gc
+@cindex start-stop-gc
+When @samp{--gc-sections} is in effect, a reference from a retained
+section to @code{__start_SECNAME} or @code{__stop_SECNAME} causes all
+input sections named @code{SECNAME} to also be retained, if
+@code{SECNAME} is representable as a C identifier and either
+@code{__start_SECNAME} or @code{__stop_SECNAME} is synthesized by the
+linker.  @samp{-z start-stop-gc} disables this effect, allowing
+sections to be garbage collected as if the special synthesized symbols
+were not defined.  @samp{-z start-stop-gc} has no effect on a
+definition of @code{__start_SECNAME} or @code{__stop_SECNAME} in an
+object file or linker script.  Such a definition will prevent the
+linker providing a synthesized @code{__start_SECNAME} or
+@code{__stop_SECNAME} respectively, and therefore the special
+treatment by garbage collection for those references.
+
 @item start-stop-visibility=@var{value}
 @cindex visibility
 @cindex ELF symbol visibility
index 5c88ee744f81ccd61c20246efd5675f6a2052beb..7a3c02aeaa62fd9d579fc9c13e8bf26fc0e1d973 100644 (file)
@@ -357,6 +357,7 @@ main (int argc, char **argv)
 #ifdef DEFAULT_NEW_DTAGS
   link_info.new_dtags = DEFAULT_NEW_DTAGS;
 #endif
+  link_info.start_stop_gc = FALSE;
   link_info.start_stop_visibility = STV_PROTECTED;
 
   ldfile_add_arch ("");
index f005a58a04579f0273088b16249625f5411825bf..36492abb405c95a84bd5f743534afb02e5a1db7f 100644 (file)
@@ -2070,8 +2070,6 @@ elf_shlib_list_options (FILE *file)
   -z common                   Generate common symbols with STT_COMMON type\n"));
   fprintf (file, _("\
   -z nocommon                 Generate common symbols with STT_OBJECT type\n"));
-  fprintf (file, _("\
-  -z stack-size=SIZE          Set size of stack segment\n"));
   if (link_info.textrel_check == textrel_check_error)
     fprintf (file, _("\
   -z text                     Treat DT_TEXTREL in output as error (default)\n"));
@@ -2116,8 +2114,12 @@ elf_static_list_options (FILE *file)
   fprintf (file, _("\
   -z defs                     Report unresolved symbols in object files\n"));
   fprintf (file, _("\
+  -z undefs                   Ignore unresolved symbols in object files\n"));
+  fprintf (file, _("\
   -z muldefs                  Allow multiple definitions\n"));
   fprintf (file, _("\
+  -z stack-size=SIZE          Set size of stack segment\n"));
+  fprintf (file, _("\
   -z execstack                Mark executable as requiring executable stack\n"));
   fprintf (file, _("\
   -z noexecstack              Mark executable as not requiring executable stack\n"));
@@ -2127,6 +2129,13 @@ elf_static_list_options (FILE *file)
   -z nounique-symbol          Keep duplicated local symbol names (default)\n"));
   fprintf (file, _("\
   -z globalaudit              Mark executable requiring global auditing\n"));
+  fprintf (file, _("\
+  -z start-stop-gc            Enable garbage collection on __start/__stop\n"));
+  fprintf (file, _("\
+  -z nostart-stop-gc          Don't garbage collect __start/__stop (default)\n"));
+  fprintf (file, _("\
+  -z start-stop-visibility=V  Set visibility of built-in __start/__stop symbols\n\
+                                to DEFAULT, PROTECTED, HIDDEN or INTERNAL\n"));
 }
 
 static void
index b3245bb915ea834ef7a910dd6b20c6407298ce02..ad3bc2e641e456a62c6f365df708cb45601fa42c 100644 (file)
@@ -89,6 +89,7 @@ test_gc "Check --gc-section/-r/-u" "gcrel" $ld "-r --gc-sections -u used_func"
 run_dump_test "noent"
 run_dump_test "abi-note"
 run_dump_test "start"
+run_dump_test "start2"
 run_dump_test "stop"
 run_dump_test "pr19167"
 if { [is_elf_format] } then {
diff --git a/ld/testsuite/ld-gc/start2.d b/ld/testsuite/ld-gc/start2.d
new file mode 100644 (file)
index 0000000..480dc74
--- /dev/null
@@ -0,0 +1,10 @@
+#name: --gc-sections with -z start-stop-gc
+#ld: --gc-sections -e _start -z start-stop-gc
+#nm: -n
+#target: *-*-linux* *-*-gnu* arm*-*-uclinuxfdpiceabi
+#xfail: bfin-*-*linux* frv-*-*
+
+#failif
+#...
+[0-9a-f]+ D +__start__foo
+#...
diff --git a/ld/testsuite/ld-gc/start2.s b/ld/testsuite/ld-gc/start2.s
new file mode 100644 (file)
index 0000000..b0084a1
--- /dev/null
@@ -0,0 +1,7 @@
+.globl _start
+_start:
+       .weak   __start__foo
+       .dc.a   __start__foo
+       .section        _foo,"aw",%progbits
+foo:
+       .long   1
This page took 0.035312 seconds and 4 git commands to generate.