public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@sourceware.org>
To: bfd-cvs@sourceware.org
Subject: [binutils-gdb] bfd_close and target free_cached_memory
Date: Thu,  1 Jun 2023 00:33:29 +0000 (GMT)	[thread overview]
Message-ID: <20230601003329.88BDE3858291@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8261abd51344cb4c454514fba1389a9292f9652b

commit 8261abd51344cb4c454514fba1389a9292f9652b
Author: Alan Modra <amodra@gmail.com>
Date:   Wed May 31 21:17:48 2023 +0930

    bfd_close and target free_cached_memory
    
    bfd_free_cached_info is used in just one place in archive.c, which
    means most times we reach bfd_close the function isn't called.  On the
    other hand, if bfd_free_cached_info is called we can't do much on the
    bfd since it loses all its obj_alloc memory.  This restricts what can
    be done in a target _close_and_cleanup.  In particular you can't look
    at sections, which leads to duplication of code in target
    close_and_cleanup and free_cached_info, eg. elfnn-aarch64.c.
    
            * opncls.c (_bfd_delete_bfd): Call bfd_free_cached_info.
            * elfnn-aarch64.c (elfNN_aarch64_close_and_cleanup): Delete.
            (bfd_elfNN_close_and_cleanup): Don't define.
            * som.c (som_bfd_free_cached_info): Don't call
            _bfd_generic_close_and_cleanup here.
            (som_close_and_cleanup): Define as _bfd_generic_close_and_cleanup.

Diff:
---
 bfd/elfnn-aarch64.c | 13 -------------
 bfd/opncls.c        |  5 +++++
 bfd/som.c           |  6 ++++--
 3 files changed, 9 insertions(+), 15 deletions(-)

diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c
index 32799b6b009..3ad5cb95b40 100644
--- a/bfd/elfnn-aarch64.c
+++ b/bfd/elfnn-aarch64.c
@@ -8735,16 +8735,6 @@ unrecord_section_via_map_over_sections (bfd *abfd ATTRIBUTE_UNUSED,
   unrecord_section_with_aarch64_elf_section_data (sec);
 }
 
-static bool
-elfNN_aarch64_close_and_cleanup (bfd *abfd)
-{
-  if (abfd->sections)
-    bfd_map_over_sections (abfd,
-			   unrecord_section_via_map_over_sections, NULL);
-
-  return _bfd_elf_close_and_cleanup (abfd);
-}
-
 static bool
 elfNN_aarch64_bfd_free_cached_info (bfd *abfd)
 {
@@ -10263,9 +10253,6 @@ const struct elf_size_info elfNN_aarch64_size_info =
 #define ELF_MAXPAGESIZE			0x10000
 #define ELF_COMMONPAGESIZE		0x1000
 
-#define bfd_elfNN_close_and_cleanup		\
-  elfNN_aarch64_close_and_cleanup
-
 #define bfd_elfNN_bfd_free_cached_info		\
   elfNN_aarch64_bfd_free_cached_info
 
diff --git a/bfd/opncls.c b/bfd/opncls.c
index f0064954bf3..b0c23b4201c 100644
--- a/bfd/opncls.c
+++ b/bfd/opncls.c
@@ -156,6 +156,11 @@ _bfd_new_bfd_contained_in (bfd *obfd)
 static void
 _bfd_delete_bfd (bfd *abfd)
 {
+  /* Give the target _bfd_free_cached_info a chance to free memory.  */
+  if (abfd->memory)
+    bfd_free_cached_info (abfd);
+
+  /* The target _bfd_free_cached_info may not have done anything..  */
   if (abfd->memory)
     {
       bfd_hash_table_free (&abfd->section_htab);
diff --git a/bfd/som.c b/bfd/som.c
index 0780b235e0e..ef1f6dc3eb6 100644
--- a/bfd/som.c
+++ b/bfd/som.c
@@ -6813,7 +6813,9 @@ som_bfd_free_cached_info (bfd *abfd)
 #undef FREE
     }
 
-  return _bfd_generic_close_and_cleanup (abfd);
+  /* Do not call _bfd_free_cached_info here.  som_write_armap needs
+     to access the bfd obj_alloc memory.  */
+  return true;
 }
 
 /* End of miscellaneous support functions.  */
@@ -6828,7 +6830,7 @@ som_bfd_link_split_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
 
 #define som_find_line				_bfd_nosymbols_find_line
 #define som_get_symbol_version_string		_bfd_nosymbols_get_symbol_version_string
-#define	som_close_and_cleanup			som_bfd_free_cached_info
+#define som_close_and_cleanup			_bfd_generic_close_and_cleanup
 #define som_read_ar_hdr				_bfd_generic_read_ar_hdr
 #define som_write_ar_hdr			_bfd_generic_write_ar_hdr
 #define som_openr_next_archived_file		bfd_generic_openr_next_archived_file

                 reply	other threads:[~2023-06-01  0:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230601003329.88BDE3858291@sourceware.org \
    --to=amodra@sourceware.org \
    --cc=bfd-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).