From c0419c024bf922128131671e40de0aed736e38ed Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Thu, 4 Apr 2024 18:46:40 -0700 Subject: elf: Always honor the first definition in shared object and archive GCC doesn't put builtin function symbol references, which are defined in the shared C library, in the IR symbol table. When linker rescans shared objects and archives for newly added symbol references generated from the IR inputs, it skips definitions of the builtin functions in shared objects and archives. Add first_hash to elf_link_hash_table to track unreferenced definitions defined first in shared objects and archives. Always use them to resolve any references. bfd/ PR ld/31482 PR ld/31489 * elf-bfd.h (elf_link_hash_table): Add first_hash. * elflink.c (elf_link_add_to_first_hash): New function. (elf_link_add_object_symbols): Initialize first_hash for an IR input. Always use the first definition in shared object. Add the first unreferenced dynamic definition to first_hash. (_bfd_elf_archive_symbol_lookup): Add the first unreferenced definition to first_hash.. (elf_link_add_archive_symbols): Use the symbol definition in archive if symbol is defined first in this archive. (_bfd_elf_link_hash_table_free): Also free first_hash. ld/ PR ld/31482 PR ld/31489 * testsuite/ld-plugin/lto.exp: Add PR ld/31482 and PR ld/31489 tests. * testsuite/ld-elf/pr31482a-no-lto.c: New file. * testsuite/ld-elf/pr31482b-no-lto.c: Likewise. * testsuite/ld-elf/pr31482c-no-lto.c: Likewise. * testsuite/ld-elf/pr31482d-no-lto.c: Likewise. * testsuite/ld-plugin/pass1.out: Likewise. * testsuite/ld-plugin/pr31482a.c: Likewise. * testsuite/ld-plugin/pr31482b.c: Likewise. * testsuite/ld-plugin/pr31482c.c: Likewise. --- bfd/elf-bfd.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bfd/elf-bfd.h') diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index f31244f..cf043d6 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -737,6 +737,10 @@ struct elf_link_hash_table /* Small local sym cache. */ struct sym_cache sym_cache; + /* Hash table of symbols which are first defined in archives or shared + objects when there are any IR inputs. */ + struct bfd_link_hash_table *first_hash; + /* Short-cuts to get to dynamic linker sections. */ asection *sgot; asection *sgotplt; -- cgit v1.1