Free symtab_hdr.contents and a cache_size correction

Alan Modra amodra@gmail.com
Wed Jan 15 12:05:03 GMT 2025


symtab_hdr.contents looks to be malloc'd memory, except in one case.
Change that one case to also be malloc'd and free when we are done.

	* elf.c (swap_out_syms): bfd_malloc outbound_syms.
	(_bfd_elf_free_cached_info): Free symtab_hdr.contents.
	* elflink.c (init_reloc_cookie): Correct cache_size.  locsyms
	is an array of Elf_Internal_Sym.

diff --git a/bfd/elf.c b/bfd/elf.c
index a7d23bbd83c..d37f16e974b 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -8684,7 +8684,7 @@ swap_out_syms (bfd *abfd,
     }
 
   if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
-      || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL)
+      || (outbound_syms = bfd_malloc (amt)) == NULL)
     {
     error_no_mem:
       bfd_set_error (bfd_error_no_memory);
@@ -10125,6 +10125,8 @@ _bfd_elf_free_cached_info (bfd *abfd)
       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
       _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
       _bfd_stab_cleanup (abfd, &tdata->line_info);
+      free (tdata->symtab_hdr.contents);
+      tdata->symtab_hdr.contents = NULL;
     }
 
   return _bfd_generic_bfd_free_cached_info (abfd);
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 8c130dc6474..91c77c211ef 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -13829,7 +13829,7 @@ init_reloc_cookie (struct elf_reloc_cookie *cookie,
 	{
 	  symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
 	  info->cache_size += (cookie->locsymcount
-			       * sizeof (Elf_External_Sym_Shndx));
+			       * sizeof (Elf_Internal_Sym));
 	}
     }
   return true;

-- 
Alan Modra


More information about the Binutils mailing list