public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* _bfd_elf_munmap_section_contents
@ 2025-01-17  9:35 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2025-01-17  9:35 UTC (permalink / raw)
  To: binutils

Do unmap/free cached contents to avoid some memory leaks we'd
otherwise see.

I'll wait until after the branch for this one too.

	* elf.c (_bfd_elf_munmap_section_contents): Clear pointers to
	contents that we unmap/free rather than not unmapping/freeing.

diff --git a/bfd/elf.c b/bfd/elf.c
index 1f2b82bfe92..1aecb4786a2 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -14270,15 +14270,15 @@ _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
   if (contents == NULL)
     return;
 
+  /* Don't leave pointers to data we are about to munmap or free.  */
+  if (sec->contents == contents)
+    sec->contents = NULL;
+  if (elf_section_data (sec)->this_hdr.contents == contents)
+    elf_section_data (sec)->this_hdr.contents = NULL;
+
 #ifdef USE_MMAP
   if (sec->mmapped_p)
     {
-      /* _bfd_elf_mmap_section_contents may return the previously
-	 mapped section contents.  Munmap the section contents only
-	 if they haven't been cached.  */
-      if (elf_section_data (sec)->this_hdr.contents == contents)
-	return;
-
       /* When _bfd_elf_mmap_section_contents returns CONTENTS as
 	 malloced, CONTENTS_ADDR is set to NULL.  */
       if (elf_section_data (sec)->contents_addr != NULL)
@@ -14288,7 +14288,6 @@ _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
 		      elf_section_data (sec)->contents_size) != 0)
 	    abort ();
 	  sec->mmapped_p = 0;
-	  sec->contents = NULL;
 	  elf_section_data (sec)->contents_addr = NULL;
 	  elf_section_data (sec)->contents_size = 0;
 	  return;

-- 
Alan Modra

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-01-17  9:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-17  9:35 _bfd_elf_munmap_section_contents Alan Modra

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).