[PATCH v2 08/16] BFD: Prevent a crash on freeing a BFD's uninitialized section hash

Maciej W. Rozycki macro@orcam.me.uk
Thu Nov 6 21:19:34 GMT 2025


From: Maciej W. Rozycki <macro@redhat.com>

Ensure memory has been allocated for the section hash before freeing it 
in `bfd_preserve_restore' and `bfd_preserve_finish'.  It is not required 
in principle to only use these internal interfaces on BFDs whose section 
hash has been initialized and in particular building archive symbol maps 
on the fly would otherwise cause a crash in the latter function where a
BFD is passed whose data has been freed by `_bfd_free_cached_info'.
---
No change from v1 (5/8), 
<https://inbox.sourceware.org/binutils/alpine.DEB.2.21.2509261817430.63399@angie.orcam.me.uk/>.
---
 bfd/format.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

binutils-bfd-preserve-restore-hash-table-free.diff
Index: binutils-gdb/bfd/format.c
===================================================================
--- binutils-gdb.orig/bfd/format.c
+++ binutils-gdb/bfd/format.c
@@ -215,7 +215,8 @@ bfd_reinit (bfd *abfd, unsigned int sect
 static bfd_cleanup
 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
 {
-  bfd_hash_table_free (&abfd->section_htab);
+  if (abfd->section_htab.memory)
+    bfd_hash_table_free (&abfd->section_htab);
 
   abfd->tdata.any = preserve->tdata;
   abfd->arch_info = preserve->arch_info;
@@ -256,7 +257,8 @@ bfd_preserve_finish (bfd *abfd ATTRIBUTE
      tdata, but that's not possible since these blocks are sitting
      inside bfd_alloc'd memory.  The section hash is on a separate
      objalloc.  */
-  bfd_hash_table_free (&preserve->section_htab);
+  if (preserve->section_htab.memory)
+    bfd_hash_table_free (&preserve->section_htab);
   preserve->marker = NULL;
 }
 


More information about the Binutils mailing list