section_by_target_index memory leak
authorAlan Modra <amodra@gmail.com>
Tue, 30 May 2023 10:35:38 +0000 (20:05 +0930)
committerAlan Modra <amodra@gmail.com>
Thu, 1 Jun 2023 00:21:59 +0000 (09:51 +0930)
The rs6000 backend can call coff_section_from_bfd_index from its
object_p function via coff_set_alignment_hook.  If the object doesn't
match, or another target matches too, then the hash table needs to be
freed via a cleanup.

* coffgen.c (coff_object_cleanup): New function.
(coff_real_object_p): Return coff_object_cleanup, and call on
failure path.  Move declaration to..
* libcoff-in.h: ..here.
(coff_object_cleanup): Declare.
* coff-stgo32.c (go32exe_cleanup): Call coff_object_cleanup.
(go32exe_check_format): Adjust assertion.
* libcoff.h: Regenerate.

bfd/coff-stgo32.c
bfd/coffgen.c
bfd/libcoff-in.h
bfd/libcoff.h

index f443a30c7b1647bb2c3003d20da509f4125fe2ea..707b80963da554328fe128151a46f5cf2db59d00 100644 (file)
@@ -248,6 +248,7 @@ static void
 go32exe_cleanup (bfd *abfd)
 {
   abfd->origin = 0;
+  coff_object_cleanup (abfd);
 
   free (go32exe_temp_stub);
   go32exe_temp_stub = NULL;
@@ -323,7 +324,7 @@ go32exe_check_format (bfd *abfd)
   bfd_cleanup cleanup = coff_object_p (abfd);
   if (cleanup == NULL)
     goto fail;
-  BFD_ASSERT (cleanup == _bfd_no_cleanup);
+  BFD_ASSERT (cleanup == coff_object_cleanup);
 
   return go32exe_cleanup;
 
index c81f67c1711e201083eeff4a2aba18329c6c0e8b..afc663c056f821d385ecd92b4c8df2be5f81b1e1 100644 (file)
@@ -281,14 +281,25 @@ make_a_section_from_file (bfd *abfd,
   return result;
 }
 
+void
+coff_object_cleanup (bfd *abfd)
+{
+  if (bfd_family_coff (abfd) && bfd_get_format (abfd) == bfd_object)
+    {
+      struct coff_tdata *td = coff_data (abfd);
+      if (td != NULL)
+       {
+         if (td->section_by_index)
+           htab_delete (td->section_by_index);
+         if (td->section_by_target_index)
+           htab_delete (td->section_by_target_index);
+       }
+    }
+}
+
 /* Read in a COFF object and make it into a BFD.  This is used by
    ECOFF as well.  */
 bfd_cleanup
-coff_real_object_p (bfd *,
-                   unsigned,
-                   struct internal_filehdr *,
-                   struct internal_aouthdr *);
-bfd_cleanup
 coff_real_object_p (bfd *abfd,
                    unsigned nscns,
                    struct internal_filehdr *internal_f,
@@ -358,9 +369,10 @@ coff_real_object_p (bfd *abfd,
     }
 
   _bfd_coff_free_symbols (abfd);
-  return _bfd_no_cleanup;
+  return coff_object_cleanup;
 
  fail:
+  coff_object_cleanup (abfd);
   _bfd_coff_free_symbols (abfd);
   bfd_release (abfd, tdata);
  fail2:
index 24a950dedd41e96d99325a216a92145edc1c04b2..96f44512ac57776e1be6f0335c8f2624383729ce 100644 (file)
@@ -331,6 +331,10 @@ struct decoration_hash_entry
 };
 
 /* Functions in coffgen.c.  */
+extern void coff_object_cleanup
+  (bfd *);
+extern bfd_cleanup coff_real_object_p
+  (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
 extern bfd_cleanup coff_object_p
   (bfd *);
 extern struct bfd_section *coff_section_from_bfd_index
index c5d15a13f7bcf528534970a31660e1b91f8d24bc..61561c5a4714ea2c94e386e726912ee761005532 100644 (file)
@@ -335,6 +335,10 @@ struct decoration_hash_entry
 };
 
 /* Functions in coffgen.c.  */
+extern void coff_object_cleanup
+  (bfd *);
+extern bfd_cleanup coff_real_object_p
+  (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
 extern bfd_cleanup coff_object_p
   (bfd *);
 extern struct bfd_section *coff_section_from_bfd_index