]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Close elements of output archive
authorAlan Modra <amodra@gmail.com>
Wed, 1 Jan 2025 12:22:06 +0000 (22:52 +1030)
committerAlan Modra <amodra@gmail.com>
Wed, 1 Jan 2025 12:22:06 +0000 (22:52 +1030)
When cleaning up an archive, close all its elements.  This fixes a
number of ar memory leaks.

bfd/
* archive.c (_bfd_archive_close_and_cleanup): Close elements
of an archive open for writing.
binutils/
* objcopy.c (copy_archive): Don't close output archive
elements here.
* dlltool.c (gen_lib_file): Likewise.
ld/
* pe-dll.c (pe_dll_generate_implib): Don't close output
archive elements here.

bfd/archive.c
binutils/dlltool.c
binutils/objcopy.c
ld/pe-dll.c

index b3019e21059d1dab99bfeae6c81c45f355fead61..ef0109599e15d5eaaae0d0ab81ad2f124b21f6e7 100644 (file)
@@ -2882,6 +2882,15 @@ _bfd_unlink_from_archive_parent (bfd *abfd)
 bool
 _bfd_archive_close_and_cleanup (bfd *abfd)
 {
+  if (bfd_write_p (abfd) && abfd->format == bfd_archive)
+    {
+      bfd *current;
+      while ((current = abfd->archive_head) != NULL)
+       {
+         abfd->archive_head = current->archive_next;
+         bfd_close_all_done (current);
+       }
+    }
   if (bfd_read_p (abfd) && abfd->format == bfd_archive)
     {
       bfd *nbfd;
index 4426aae713eda5d7eeaa0c7b7ce11836ef3ad299..d32bf2062b4830d68e8348e27baeef33a852ac35 100644 (file)
@@ -3009,13 +3009,6 @@ gen_lib_file (int delay)
   if (! bfd_close (outarch))
     bfd_fatal (imp_name);
 
-  while (head != NULL)
-    {
-      bfd *n = head->archive_next;
-      bfd_close (head);
-      head = n;
-    }
-
   /* Delete all the temp files.  */
   unlink_temp_files ();
 
index 10539ed054b5e42825f9166ffb07dcebb2f45417..f64417697cb8c98a9e5d5e01d6b28957a4002f13 100644 (file)
@@ -3845,10 +3845,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
          if (l->obfd == NULL)
            rmdir (l->name);
          else
-           {
-             bfd_close (l->obfd);
-             unlink (l->name);
-           }
+           unlink (l->name);
          free (l->name);
        }
       next = l->next;
index 2b2ef68b31c7b92108b532ba98b4c1ccaacfba99..d888a0b6373a9249a6bfa88abaaa6ba02d469af0 100644 (file)
@@ -3058,13 +3058,6 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
 
   if (! bfd_close (outarch))
     einfo ("%X%P: bfd_close %s: %E\n", impfilename);
-
-  while (head != NULL)
-    {
-      bfd *n = head->archive_next;
-      bfd_close (head);
-      head = n;
-    }
 }
 
 static int undef_count = 0;