[binutils-gdb] elf: Renumber local dynamic symbols only if needed

H.J. Lu hjl@sourceware.org
Sun Nov 30 07:34:05 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=54fbc12d6404b645363f901110a21f4410067df5

commit 54fbc12d6404b645363f901110a21f4410067df5
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Nov 28 16:07:57 2025 +0800

    elf: Renumber local dynamic symbols only if needed
    
    Only hppa, ia64 and sparc ELF targets use local dynamic symbols.  But
    elf_link_renumber_local_hash_table_dynsyms is always called to renumber
    local dynamic symbols even if there is none.  Add has_local_dynsyms to
    elf_link_hash_table and set it to true only if there are local dynamic
    symbols.  Renumber local dynamic symbols only if there are local dynamic
    symbols.
    
            * elf-bfd.h (elf_link_hash_table): Add has_local_dynsyms.
            * elflink.c (_bfd_elf_link_renumber_dynsyms): Renumber local
            dynamic symbols only if there are local dynamic symbols.
            (_bfd_elf_adjust_dynamic_symbol): Set has_local_dynsyms if
            there are local dynamic symbols.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>

Diff:
---
 bfd/elf-bfd.h |  3 +++
 bfd/elflink.c | 12 ++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 5f08d1a4dfb..1a0d476c37d 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -637,6 +637,9 @@ struct elf_link_hash_table
   /* Whether dynamic relocations are present.  */
   bool dynamic_relocs;
 
+  /* TRUE if there are local dynamic symbols.  */
+  bool has_local_dynsyms;
+
   /* True if this target has relocatable executables, so needs dynamic
      section symbols.  */
   bool is_relocatable_executable;
diff --git a/bfd/elflink.c b/bfd/elflink.c
index adc21d7d821..529f81159e3 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -1064,9 +1064,10 @@ _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
   if (do_sec)
     *section_sym_count = dynsymcount;
 
-  elf_link_hash_traverse (elf_hash_table (info),
-			  elf_link_renumber_local_hash_table_dynsyms,
-			  &dynsymcount);
+  if (elf_hash_table (info)->has_local_dynsyms)
+    elf_link_hash_traverse (elf_hash_table (info),
+			    elf_link_renumber_local_hash_table_dynsyms,
+			    &dynsymcount);
 
   if (elf_hash_table (info)->dynlocal)
     {
@@ -3269,6 +3270,10 @@ _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   if (! is_elf_hash_table (eif->info->hash))
     return false;
 
+  htab = elf_hash_table (eif->info);
+  if (h->forced_local && h->dynindx != -1)
+    htab->has_local_dynsyms = true;
+
   /* Ignore indirect symbols.  These are added by the versioning code.  */
   if (h->root.type == bfd_link_hash_indirect)
     return true;
@@ -3277,7 +3282,6 @@ _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
   if (! _bfd_elf_fix_symbol_flags (h, eif))
     return false;
 
-  htab = elf_hash_table (eif->info);
   bed = get_elf_backend_data (htab->dynobj);
 
   if (h->root.type == bfd_link_hash_undefweak)


More information about the Binutils-cvs mailing list