Weak references to __start_/__stop_ symbols
authorAlan Modra <amodra@gmail.com>
Mon, 1 Mar 2021 01:10:51 +0000 (11:40 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 1 Mar 2021 03:56:39 +0000 (14:26 +1030)
If a weak reference to a __start_foo or __stop_foo symbol ends up
having no definition due to all the foo sections being removed for
some reason, undef_start_stop currently makes the symbol strong
undefined.  That risks a linker undefined symbol error.  Fix that by
making the symbol undefweak and also undo some dynamic symbol state.

Note that saving the state of the symbol type at the time
lang_init_start_stop runs is not sufficient.  The linker may have
merged in a shared library reference by that point and made what was
an undefweak in regular objects, a strong undefined.  So it is
necessary to look at the ELF symbol flags to decide whether an
undefweak is the proper resolution.

Something probably should be done for COFF/PE too, but I'm unsure how
to do go about that.

* ldlang.c (undef_start_stop): For ELF make undefined start/stop
symbols undefweak if that was how they were referenced.  Undo
dynamic state too.

ld/ChangeLog
ld/ldlang.c

index 873928608bdbb7dd2af01ceb7d3de05757cbe3e6..2347f9ae19c2bbf089b036603ee5bae8933ad3fc 100644 (file)
@@ -1,3 +1,9 @@
+2021-03-01  Alan Modra  <amodra@gmail.com>
+
+       * ldlang.c (undef_start_stop): For ELF make undefined start/stop
+       symbols undefweak if that was how they were referenced.  Undo
+       dynamic state too.
+
 2021-03-01  Alan Modra  <amodra@gmail.com>
 
        * testsuite/ld-elf/pr25708.d: Add --with-symbol-versions to nm.
index 5ffc8444c77fa3dd0965ded3941a18bf0446b927..a77e8fabeff8fa691c16ed943a371b8b7d31bb7c 100644 (file)
@@ -6808,6 +6808,19 @@ undef_start_stop (struct bfd_link_hash_entry *h)
        }
       h->type = bfd_link_hash_undefined;
       h->u.undef.abfd = NULL;
+      if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
+       {
+         const struct elf_backend_data *bed;
+         struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
+         unsigned int was_forced = eh->forced_local;
+
+         bed = get_elf_backend_data (link_info.output_bfd);
+         (*bed->elf_backend_hide_symbol) (&link_info, eh, TRUE);
+         if (!eh->ref_regular_nonweak)
+           h->type = bfd_link_hash_undefweak;
+         eh->def_regular = 0;
+         eh->forced_local = was_forced;
+       }
     }
 }
 
This page took 0.028102 seconds and 4 git commands to generate.