gas: replace bfd_alloc with notes_alloc

Alan Modra amodra@gmail.com
Thu Feb 13 22:45:25 GMT 2025


bfd_alloc can return NULL on out-of-memory so code needs to check the
return value and print an error.  That check was missing in write.c.
notes_alloc won't return NULL, instead the underlying obstack_alloc
prints an OOM message and the process exits.  This is more convenient,
and when the bfd_alloc memory is attached to the gas output bfd it is
released only slightly before the notes obstack.

	* config/obj-macho.c (obj_mach_o_set_indirect_symbols): Use
	notes_calloc rather than bfd_zalloc.
	* write.c (set_symtab): Use notes_alloc.

diff --git a/gas/config/obj-macho.c b/gas/config/obj-macho.c
index 249791f9081..89c2e0494db 100644
--- a/gas/config/obj-macho.c
+++ b/gas/config/obj-macho.c
@@ -1818,13 +1818,8 @@ obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
 	      if (nactual < bfd_get_symcount (abfd))
 		nactual = bfd_get_symcount (abfd);
 
-	      ms->indirect_syms =
-			bfd_zalloc (abfd,
-				    nactual * sizeof (bfd_mach_o_asymbol *));
-
-	      if (ms->indirect_syms == NULL)
-		as_fatal (_("internal error: failed to allocate %d indirect"
-			    "symbol pointers"), nactual);
+	      ms->indirect_syms = notes_calloc (nactual,
+						sizeof (*ms->indirect_syms));
 
 	      for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
 		{
diff --git a/gas/write.c b/gas/write.c
index 439f6a6913a..325c4402061 100644
--- a/gas/write.c
+++ b/gas/write.c
@@ -1804,9 +1804,8 @@ set_symtab (void)
   if (nsyms)
     {
       int i;
-      bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
 
-      asympp = (asymbol **) bfd_alloc (stdoutput, amt);
+      asympp = notes_alloc (nsyms * sizeof (asymbol *));
       symp = symbol_rootP;
       for (i = 0; i < nsyms; symp = symbol_next (symp))
 	if (!symbol_removed_p (symp)

-- 
Alan Modra


More information about the Binutils mailing list