[binutils-gdb] nm: fall back to heuristic when ELF symbol has zero size

Jan Beulich jbeulich@sourceware.org
Mon Apr 7 10:48:13 GMT 2025


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

commit e71f5f09ce75ed54c75fd5a7ca47f0452081e26e
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Apr 7 12:45:30 2025 +0200

    nm: fall back to heuristic when ELF symbol has zero size
    
    Size being set for a symbol isn't a strict requirement in ELF. For ones
    not having their size set, fall back to the same logic as used for non-
    ELF, non-COFF symbols.
    
    While there switch to using elf_symbol_from() instead of kind of open-
    coding it.

Diff:
---
 binutils/nm.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/binutils/nm.c b/binutils/nm.c
index 1a8615e86e0..7ef5d61306b 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -80,6 +80,7 @@ struct extended_symbol_info
 #define SYM_STAB_OTHER(sym)  (sym->sinfo->stab_other)
 #define SYM_SIZE(sym) \
   (sym->elfinfo \
+   && sym->elfinfo->internal_elf_sym.st_size \
    ? sym->elfinfo->internal_elf_sym.st_size \
    : sym->coffinfo \
      && ISFCN (sym->coffinfo->native->u.syment.n_type) \
@@ -1088,6 +1089,7 @@ sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
       asection *sec;
       bfd_vma sz;
       asymbol *temp;
+      const elf_symbol_type *elfsym;
       const coff_symbol_type *coffsym;
 
       if (from + size < fromend)
@@ -1108,8 +1110,9 @@ sort_symbols_by_size (bfd *abfd, bool is_dynamic, void *minisyms,
 	 we can't rely on that information for the symbol size.  Ditto for
 	 bfd/section.c:global_syms like *ABS*.  */
       if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
-	  && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
-	sz = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
+	  && (elfsym = elf_symbol_from (sym)) != NULL
+	  && elfsym->internal_elf_sym.st_size != 0)
+	sz = elfsym->internal_elf_sym.st_size;
       else if ((sym->flags & (BSF_SECTION_SYM | BSF_SYNTHETIC)) == 0
 	       && (coffsym = coff_symbol_from (sym)) != NULL
 	       && ISFCN (coffsym->native->u.syment.n_type)


More information about the Binutils-cvs mailing list