[binutils-gdb] filter_implib_symbols

Alan Modra amodra@sourceware.org
Sat Jun 6 01:39:29 GMT 2026


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

commit 0a6aadacb2e9a2695c82635dd8b2f7fccf537adf
Author: Alan Modra <amodra@gmail.com>
Date:   Sat Jun 6 11:08:11 2026 +0930

    filter_implib_symbols
    
    This tidies filter_implib_symbols implementations, which I noticed
    returned unsigned int but took long symbol counts.  While unsigned int
    is very likely sufficient, size_t is better.
    
    _bfd_elf_filter_global_symbols is a linker-only function so really
    should be moved to elflink.c, but I left it in elf.c as it calls
    sym_is_global, a static function in elf.c.
    
            * elf-bfd.h (struct elf_backend_data): Take size_t symcount
            and return a size_t from elf_backend_filter_implib_symbols.
            (_bfd_elf_filter_implib_symbols): Likewise.  Rename from
            _bfd_elf_filter_global_symbols.
            * elf.c (_bfd_elf_filter_implib_symbols): Likewise.  Use
            size_t vars too, and avoid unnecessary casts.
            * elf32-arm.c (elf32_arm_filter_cmse_symbols): Likewise.
            (elf32_arm_filter_implib_symbols): Likewise.
            * elflink.c (elf_output_implib): Avoid unneccesary casts.
            Call elf_backend_filter_implib_symbols uncontitionally.
            * elfxx-target.h (elf_backend_filter_implib_symbols): Define
            fallback as _bfd_elf_filter_implib_symbols.

Diff:
---
 bfd/elf-bfd.h      |  8 ++++----
 bfd/elf.c          | 10 +++++-----
 bfd/elf32-arm.c    | 21 ++++++++++-----------
 bfd/elflink.c      | 15 ++++-----------
 bfd/elfxx-target.h |  2 +-
 5 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 879dd62dbd6..88d35925987 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -1400,8 +1400,8 @@ struct elf_backend_data
 
   /* Filter what symbols of the output file to include in the import
      library if one is created.  */
-  unsigned int (*elf_backend_filter_implib_symbols)
-    (struct bfd_link_info *, asymbol **, long);
+  size_t (*elf_backend_filter_implib_symbols)
+    (struct bfd_link_info *, asymbol **, size_t);
 
   /* Copy any information related to dynamic linking from a pre-existing
      symbol to a newly created symbol.  Also called to copy flags and
@@ -2459,8 +2459,8 @@ extern bool _bfd_elf_section_already_linked
   (bfd *, asection *, struct bfd_link_info *) ATTRIBUTE_HIDDEN;
 extern void bfd_elf_set_group_contents
   (bfd *, asection *, void *);
-extern unsigned int _bfd_elf_filter_global_symbols
-  (struct bfd_link_info *, asymbol **, long) ATTRIBUTE_HIDDEN;
+extern size_t _bfd_elf_filter_implib_symbols
+  (struct bfd_link_info *, asymbol **, size_t) ATTRIBUTE_HIDDEN;
 extern asection *_bfd_elf_check_kept_section
   (asection *, struct bfd_link_info *) ATTRIBUTE_HIDDEN;
 #define _bfd_elf_link_just_syms _bfd_generic_link_just_syms
diff --git a/bfd/elf.c b/bfd/elf.c
index 1e1f09c8bbc..c9a68404e9d 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4462,16 +4462,16 @@ sym_is_global (bfd *abfd, asymbol *sym)
 
    Returns the number of symbols to keep.  */
 
-unsigned int
-_bfd_elf_filter_global_symbols (struct bfd_link_info *info,
-				asymbol **syms, long symcount)
+size_t
+_bfd_elf_filter_implib_symbols (struct bfd_link_info *info,
+				asymbol **syms, size_t symcount)
 {
-  long src_count, dst_count = 0;
+  size_t src_count, dst_count = 0;
 
   for (src_count = 0; src_count < symcount; src_count++)
     {
       asymbol *sym = syms[src_count];
-      char *name = (char *) bfd_asymbol_name (sym);
+      const char *name = bfd_asymbol_name (sym);
       struct bfd_link_hash_entry *h;
 
       if (!sym_is_global (info->output_bfd, sym))
diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c
index 63b908462b1..e27aff48411 100644
--- a/bfd/elf32-arm.c
+++ b/bfd/elf32-arm.c
@@ -18250,13 +18250,13 @@ elf32_arm_output_arch_local_syms (struct bfd_link_info *info,
 
    Returns the number of symbols to keep.  */
 
-static unsigned int
+static size_t
 elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
-			       asymbol **syms, long symcount)
+			       asymbol **syms, size_t symcount)
 {
   size_t maxnamelen;
   char *cmse_name;
-  long src_count, dst_count = 0;
+  size_t src_count, dst_count = 0;
   struct elf32_arm_link_hash_table *htab;
 
   htab = elf32_arm_hash_table (info);
@@ -18264,7 +18264,7 @@ elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
     symcount = 0;
 
   maxnamelen = 128;
-  cmse_name = (char *) bfd_malloc (maxnamelen);
+  cmse_name = bfd_malloc (maxnamelen);
   BFD_ASSERT (cmse_name);
 
   for (src_count = 0; src_count < symcount; src_count++)
@@ -18272,12 +18272,12 @@ elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
       struct elf32_arm_link_hash_entry *cmse_hash;
       asymbol *sym;
       flagword flags;
-      char *name;
+      const char *name;
       size_t namelen;
 
       sym = syms[src_count];
       flags = sym->flags;
-      name = (char *) bfd_asymbol_name (sym);
+      name = bfd_asymbol_name (sym);
 
       if ((flags & BSF_FUNCTION) != BSF_FUNCTION)
 	continue;
@@ -18287,8 +18287,7 @@ elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
       namelen = strlen (name) + sizeof (CMSE_PREFIX) + 1;
       if (namelen > maxnamelen)
 	{
-	  cmse_name = (char *)
-	    bfd_realloc (cmse_name, namelen);
+	  cmse_name = bfd_realloc (cmse_name, namelen);
 	  maxnamelen = namelen;
 	}
       snprintf (cmse_name, maxnamelen, "%s%s", CMSE_PREFIX, name);
@@ -18317,9 +18316,9 @@ elf32_arm_filter_cmse_symbols (struct bfd_link_info *info,
 
    Returns the number of symbols to keep.  */
 
-static unsigned int
+static size_t
 elf32_arm_filter_implib_symbols (struct bfd_link_info *info,
-				 asymbol **syms, long symcount)
+				 asymbol **syms, size_t symcount)
 {
   struct elf32_arm_link_hash_table *globals = elf32_arm_hash_table (info);
 
@@ -18330,7 +18329,7 @@ elf32_arm_filter_implib_symbols (struct bfd_link_info *info,
   if (globals->cmse_implib)
     return elf32_arm_filter_cmse_symbols (info, syms, symcount);
   else
-    return _bfd_elf_filter_global_symbols (info, syms, symcount);
+    return _bfd_elf_filter_implib_symbols (info, syms, symcount);
 }
 
 /* Allocate target specific section data.  */
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 502c8068847..3c4332b549a 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -12525,7 +12525,6 @@ elf_output_implib (bfd *obfd, struct bfd_link_info *info)
   long symcount;
   long src_count;
   elf_symbol_type *osymbuf;
-  size_t amt;
 
   implib_bfd = info->out_implib_bfd;
   obed = get_elf_backend_data (obfd);
@@ -12554,7 +12553,7 @@ elf_output_implib (bfd *obfd, struct bfd_link_info *info)
     return false;
 
   /* Read in the symbol table.  */
-  sympp = (asymbol **) bfd_malloc (symsize);
+  sympp = bfd_malloc (symsize);
   if (sympp == NULL)
     return false;
 
@@ -12568,10 +12567,7 @@ elf_output_implib (bfd *obfd, struct bfd_link_info *info)
     goto free_sym_buf;
 
   /* Filter symbols to appear in the import library.  */
-  if (obed->elf_backend_filter_implib_symbols)
-    symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
-  else
-    symcount = _bfd_elf_filter_global_symbols (info, sympp, symcount);
+  symcount = obed->elf_backend_filter_implib_symbols (info, sympp, symcount);
   if (symcount == 0)
     {
       bfd_set_error (bfd_error_no_symbols);
@@ -12580,17 +12576,14 @@ elf_output_implib (bfd *obfd, struct bfd_link_info *info)
       goto free_sym_buf;
     }
 
-
   /* Make symbols absolute.  */
-  amt = symcount * sizeof (*osymbuf);
-  osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
+  osymbuf = bfd_alloc (implib_bfd, symcount * sizeof (*osymbuf));
   if (osymbuf == NULL)
     goto free_sym_buf;
 
   for (src_count = 0; src_count < symcount; src_count++)
     {
-      memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
-	      sizeof (*osymbuf));
+      memcpy (&osymbuf[src_count], sympp[src_count], sizeof (*osymbuf));
       osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
       osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
       osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
diff --git a/bfd/elfxx-target.h b/bfd/elfxx-target.h
index b6dfc108f27..03689f68ea3 100644
--- a/bfd/elfxx-target.h
+++ b/bfd/elfxx-target.h
@@ -620,7 +620,7 @@
 #define elf_backend_output_arch_syms		NULL
 #endif
 #ifndef elf_backend_filter_implib_symbols
-#define elf_backend_filter_implib_symbols	NULL
+#define elf_backend_filter_implib_symbols	_bfd_elf_filter_implib_symbols
 #endif
 #ifndef elf_backend_copy_indirect_symbol
 #define elf_backend_copy_indirect_symbol	_bfd_elf_link_hash_copy_indirect


More information about the Binutils-cvs mailing list