Close elements of output archive
Checks
Commit Message
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.
@@ -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;
@@ -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 ();
@@ -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;
@@ -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;