commit:     380d983472c3292f2276aecee685768a12fd92e9
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Fri Oct 10 05:27:54 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Fri Oct 10 05:28:14 2025 +0000
URL:        
https://gitweb.gentoo.org/proj/toolchain/binutils-patches.git/commit/?id=380d9834

9999: try patch from branch

Signed-off-by: Sam James <sam <AT> gentoo.org>

 ...-internal-symbol-table-in-relocatable-BFD.patch | 166 +++++++++++++++++----
 1 file changed, 138 insertions(+), 28 deletions(-)

diff --git a/9999/0005-elf-Cache-internal-symbol-table-in-relocatable-BFD.patch 
b/9999/0005-elf-Cache-internal-symbol-table-in-relocatable-BFD.patch
index dbf42d5..acae3cf 100644
--- a/9999/0005-elf-Cache-internal-symbol-table-in-relocatable-BFD.patch
+++ b/9999/0005-elf-Cache-internal-symbol-table-in-relocatable-BFD.patch
@@ -1,27 +1,128 @@
-From a724f5c582f8df93eee54d43cf7766bc47d30f44 Mon Sep 17 00:00:00 2001
+From 13058a8b8feb26ddafb62995f164d81dce02979c Mon Sep 17 00:00:00 2001
 From: "H.J. Lu" <[email protected]>
 Date: Thu, 9 Oct 2025 13:16:19 +0800
-Subject: [PATCH] elf: Cache internal symbol table in relocatable BFD
+Subject: [PATCH] elf: Cache full internal symbol table for relocatable input
 
-Don't cache internal symbol table in symtab_hdr->contents.
+Cache internal symbol table for relocatable input and use the internal
+symbol table cache for both local and global symbols to avoid swapping in
+the same symbol table repeatedly for --gc-sections.  This improves linker
+--gc-sections speed by ~20x.
+
+Most backends cache the local symbol table in symtab_hdr->contents. But
+some backends cache something else in it.  Since symtab_hdr->contents may
+point to the middle of the internal symbol table cache, we can release
+symtab_hdr->contents only when no backends cache the local symbol table
+in it.
 
 bfd/
 
        PR ld/33530
-       * elf-bfd.h (elf_obj_tdata): Add symtab.
+       * elf-bfd.h (elf_obj_tdata): Add symtab and keep_symtab.
+       (bfd_elf_get_elf_syms): Remove the Elf_External_Sym_Shndx pointer
+       argument.
        * elf-eh-frame.c (adjust_eh_frame_local_symbols): Changed to
        return void.
        (_bfd_elf_discard_section_eh_frame): Updated.  Don't cache
        internal symbol table in symtab_hdr.
-       * elf.c (bfd_elf_get_elf_syms): Cache internal symbol table in
-       BFD.
-       (_bfd_elf_free_cached_info): Free internal symbol table in BFD.
+       * elf-m10200.c (mn10200_elf_relax_section): Drop the last argument
+       to bfd_elf_get_elf_syms.
+       (mn10200_elf_get_relocated_section_contents): Likewise.
+       * elf-m10300.c (mn10300_elf_check_relocs): Likewise.
+       (mn10300_elf_relax_section): Likewise.
+       (mn10300_elf_get_relocated_section_contents): Likewise.
+       * elf32-arc.c (arc_elf_relax_section): Likewise.
+       * elf32-arm.c (cmse_scan): Likewise.
+       (elf32_arm_size_stubs): Likewise.
+       (bfd_elf32_arm_init_maps): Likewise.
+       * elf32-avr.c (elf32_avr_relax_delete_bytes): Likewise.
+       (retrieve_local_syms): Likewise.
+       (elf32_avr_relax_section): Likewise.
+       (elf32_avr_get_relocated_section_contents): Likewise.
+       (get_local_syms): Likewise.
+       * elf32-bfin.c (bfd_bfin_elf32_create_embedded_relocs): Likewise.
+       * elf32-cr16.c (elf32_cr16_get_relocated_section_contents):
+       Likewise.
+       (elf32_cr16_relax_section): Likewise.
+       (bfd_cr16_elf32_create_embedded_relocs): Likewise.
+       * elf32-crx.c (elf32_crx_get_relocated_section_contents):
+       Likewise.
+       (elf32_crx_relax_section): Likewise.
+       * elf32-csky.c (elf32_csky_size_stubs): Likewise.
+       * elf32-epiphany.c (epiphany_elf_relax_section): Likewise.
+       * elf32-ft32.c (elf32_ft32_relax_delete_bytes): Likewise.
+       (elf32_ft32_relax_is_branch_target): Likewise.
+       (ft32_elf_relax_section): Likewise.
+       * elf32-h8300.c (elf32_h8_relax_section): Likewise.
+       (elf32_h8_get_relocated_section_contents): Likewise.
+       * elf32-hppa.c (get_local_syms): Likewise.
+       * elf32-ip2k.c (ip2k_elf_relax_section): Likewise.
+       * elf32-m32c.c (dump_symtab): Likewise.
+       (m32c_elf_relax_plt_section): Likewise.
+       (m32c_elf_relax_section): Likewise.
+       * elf32-m68hc11.c (m68hc11_elf_relax_section): Likewise.
+       * elf32-m68hc1x.c (elf32_m68hc11_size_stubs): Likewise.
+       * elf32-m68k.c (bfd_m68k_elf32_create_embedded_relocs): Likewise.
+       * elf32-metag.c (get_local_syms): Likewise.
+       * elf32-microblaze.c (microblaze_elf_relax_section): Likewise.
+       * elf32-msp430.c (msp430_elf_relax_section): Likewise.
+       * elf32-nds32.c (nds32_elf_relax_delete_blanks): Likewise.
+       (nds32_get_local_syms): Likewise.
+       * elf32-ppc.c (get_sym_h): Likewise.
+       (ppc_elf_late_size_sections): Likewise.
+       * elf32-pru.c (pru_elf_relax_delete_bytes): Likewise.
+       (pru_elf32_relax_section): Likewise.
+       * elf32-rl78.c (rl78_elf_relax_plt_section): Likewise.
+       (rl78_elf_relax_section): Likewise.
+       * elf32-rx.c (elf32_rx_relax_section): Likewise.
+       (rx_dump_symtab): Likewise.
+       * elf32-sh.c (sh_elf_relax_section): Likewise.
+       (sh_elf_get_relocated_section_contents): Likewise.
+       * elf32-spu.c (get_sym_h): Likewise.
+       (discover_functions): Likewise.
+       * elf32-v850.c (v850_elf_relax_section): Likewise.
+       * elf32-xstormy16.c (xstormy16_elf_relax_section): Likewise.
+       * elf32-xtensa.c (retrieve_local_syms): Likewise.
+       * elf64-alpha.c (elf64_alpha_relax_section): Likewise.
+       * elf64-hppa.c (elf64_hppa_check_relocs): Likewise.
+       * elf64-ia64-vms.c (elf64_ia64_relax_section): Likewise.
+       (elf64_vms_link_add_object_symbols): Likewise.
+       * elf64-mmix.c (mmix_elf_relax_section): Likewise.
+       * elf64-ppc.c (opd_entry_value): Likewise.
+       (get_sym_h): Likewise.
+       (ppc64_elf_late_size_sections): Likewise.
+       (ppc64_elf_layout_multitoc): Likewise.
+       (got_and_plt_relr_for_local_syms): Likewise.
+       * elfnn-aarch64.c (_bfd_aarch64_add_call_stub_entries): Likewise.
+       (bfd_elfNN_aarch64_init_maps): Likewise.
+       * elfnn-ia64.c (elfNN_ia64_relax_section): Likewise.
+       * elfnn-kvx.c (elfNN_kvx_size_stubs): Likewise.
+       * elfnn-loongarch.c (_bfd_riscv_relax_section): Likewise.
+       * elfxx-mips.c (_bfd_mips_elf_relax_section): Likewise.
+       * elf.c (bfd_elf_get_elf_syms): Drop the Elf_External_Sym_Shndx
+       pointer argument.  Only allow the full external symbol table.
+       Cache internal symbol table.
+       (group_signature): Update the bfd_elf_get_elf_syms call.
+       (bfd_sym_from_r_symndx): Likewise.
+       (_bfd_elf_free_cached_info): Free internal symbol table cache.
        * elfcode.h (elf_slurp_symbol_table): Don't free internal symbol
        buffer.
-       * elflink.c (elf_link_is_defined_archive_symbol): Likewise.
-       (elf_link_add_object_symbols): Free cached internal symbol buffer.
-       (elf_link_input_bfd): Don't use symtab_hdr->contents for internal
-       symbol table.
+       * elflink.c (bfd_elf_link_record_local_dynamic_symbol): Update
+       the bfd_elf_get_elf_syms call.
+       (elf_link_is_defined_archive_symbol): Likewise.  Free cached
+       internal symbol buffer unless it should be kept.
+       (elf_link_add_object_symbols): Update the bfd_elf_get_elf_syms
+       call.  Free cached internal symbol buffer.
+       (bfd_elf_match_symbols_in_sections): Update the
+       bfd_elf_get_elf_syms call.  Don't free the internal symbol table.
+       (elf_final_link_info): Remove external_syms, locsym_shndx and
+       internal_syms.
+       (elf_link_check_versioned_symbol): Update the bfd_elf_get_elf_syms
+       call.
+       (elf_link_input_bfd): Likewise.  Don't use symtab_hdr->contents
+       for internal symbol table.
+       (elf_final_link_free): Updated.
+       (bfd_elf_final_link): Don't set flinfo.external_syms,
+       flinfo.internal_syms nor flinfo.locsym_shndx.
        (init_reloc_cookie): Remove the keep_memory argument.  Don't
        cache internal symbol table in symtab_hdr.
        (fini_reloc_cookie): Removed.
@@ -29,17 +130,20 @@ bfd/
        error.
        (_bfd_elf_gc_mark): Replace fini_reloc_cookie_for_section with
        fini_reloc_cookie_rels.
-       (bfd_elf_gc_sections): Likewise.
-       (bfd_elf_discard_info): Likewise.  Don't call fini_reloc_cookie.
+       (bfd_elf_gc_sections): Likewise.  Free the internal symbol table
+       cache.
+       (bfd_elf_discard_info): Replace fini_reloc_cookie_for_section with
+       fini_reloc_cookie_rels.  Don't call fini_reloc_cookie.
        (bfd_elf_parse_eh_frame_entries): Updated.
        * elfxx-x86.c (_bfd_x86_elf_link_relax_section): Don't use
-       use symtab_hdr->contents for internal symbol table.
+       use symtab_hdr->contents for internal symbol table.  Set
+       keep_symtab if the symbol table has been updated.
 
 libctf/
 
        PR ld/33530
        * ctf-open-bfd.c (ctf_bfdopen_ctfsect): Free internal symbol
-       table in BFD.
+       table cache.
 
 Signed-off-by: H.J. Lu <[email protected]>
 ---
@@ -47,7 +151,7 @@ Signed-off-by: H.J. Lu <[email protected]>
  bfd/elf-eh-frame.c     |  17 ++---
  bfd/elf-m10200.c       |   4 +-
  bfd/elf-m10300.c       |  10 +--
- bfd/elf.c              | 127 ++++++++++++++++++++++++----------
+ bfd/elf.c              | 131 ++++++++++++++++++++++++++---------
  bfd/elf32-arc.c        |   2 +-
  bfd/elf32-arm.c        |   7 +-
  bfd/elf32-avr.c        |  12 ++--
@@ -92,7 +196,7 @@ Signed-off-by: H.J. Lu <[email protected]>
  bfd/elfxx-mips.c       |   2 +-
  bfd/elfxx-x86.c        |  24 +++----
  libctf/ctf-open-bfd.c  |   5 +-
- 49 files changed, 237 insertions(+), 264 deletions(-)
+ 49 files changed, 243 insertions(+), 262 deletions(-)
 
 diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
 index 5d19529d972..b7061e55b59 100644
@@ -249,7 +353,7 @@ index b381bb9037c..373290cbe4a 100644
            goto error_return;
        }
 diff --git a/bfd/elf.c b/bfd/elf.c
-index bde7414ee21..edce3d3b95f 100644
+index bde7414ee21..9e220f0a0d7 100644
 --- a/bfd/elf.c
 +++ b/bfd/elf.c
 @@ -428,21 +428,20 @@ bfd_elf_string_from_elf_section (bfd *abfd,
@@ -372,17 +476,17 @@ index bde7414ee21..edce3d3b95f 100644
 -      goto out1;
 +      bfd_set_error (bfd_error_file_too_big);
 +      goto out1;
-+    }
+     }
 +  alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
 +  if (alloc_intsym == NULL)
 +    {
 +      intsym_buf = NULL;
 +      goto out1;
-     }
- 
++    }
++
 +  if (intsym_buf == NULL)
 +    intsym_buf = alloc_intsym;
-+
+ 
    /* Convert the symbols to internal form.  */
 -  isymend = intsym_buf + symcount;
 -  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
@@ -479,14 +583,20 @@ index bde7414ee21..edce3d3b95f 100644
        return NULL;
  
        if (cache->abfd != abfd)
-@@ -10239,8 +10294,8 @@ _bfd_elf_free_cached_info (bfd *abfd)
+@@ -10239,8 +10294,16 @@ _bfd_elf_free_cached_info (bfd *abfd)
              free (sec_info->cies);
            }
        }
--      free (tdata->symtab_hdr.contents);
--      tdata->symtab_hdr.contents = NULL;
-+      free (elf_tdata (abfd)->symtab);
-+      elf_tdata (abfd)->symtab = NULL;
++#if 0
++      /* NB: tdata->symtab_hdr.contents may point to the middle of
++       tdata->symtab.  Need to update all backends only to cache
++       the malloced memory in tdata->symtab_hdr.contents before
++       tdata->symtab_hdr.contents can be freed.  */
+       free (tdata->symtab_hdr.contents);
+       tdata->symtab_hdr.contents = NULL;
++#endif
++      free (tdata->symtab);
++      tdata->symtab = NULL;
      }
  
    return _bfd_generic_bfd_free_cached_info (abfd);
@@ -1771,5 +1881,5 @@ index 7241de70709..205347c4e22 100644
        {
          bfderrstr = N_("cannot read symbol table");
 -- 
-2.51.0
+GitLab
 

Reply via email to