public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: binutils@sourceware.org
Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com
Subject: [PATCH v8 6/6] elf: Add _bfd_elf_link_m[un]map_section_contents
Date: Wed, 13 Mar 2024 08:52:27 -0700	[thread overview]
Message-ID: <20240313155227.513873-7-hjl.tools@gmail.com> (raw)
In-Reply-To: <20240313155227.513873-1-hjl.tools@gmail.com>

To copy input section contents, add _bfd_elf_link_mmap_section_contents
and _bfd_elf_link_munmap_section_contents to mmap in the input sections.
_bfd_elf_link_free_section_contents is added for flinfo.external_relocs,
which is nop when mmap is used since flinfo.external_relocs is unused.
When mmap isn't used, _bfd_elf_link_munmap_section_contents is also nop.

	* elf-bfd.h (_bfd_elf_link_mmap_section_contents): New.
	(_bfd_elf_link_munmap_section_contents): Likewise.
	(_bfd_elf_link_free_section_contents): Likewise.
	* elf.c (elf_mmap_section_contents): New.
	(_bfd_elf_mmap_section_contents): Use it.
	(_bfd_elf_link_mmap_section_contents): New.
	(_bfd_elf_munmap_section_contents): Never free the cached
	section contents.
	* elflink.c (elf_link_input_bfd): Call
	_bfd_elf_link_mmap_section_contents instead of
	bfd_get_full_section_contents.  Call
	_bfd_elf_link_munmap_section_contents to munmap the section
	contents.
	(elf_final_link_free): Use _bfd_elf_link_free_section_contents,
	instead of free, on flinfo->contents.
	(bfd_elf_final_link): Don't allocate memory for section contents.
---
 bfd/elf-bfd.h | 10 ++++++++++
 bfd/elf.c     | 43 +++++++++++++++++++++++++++++++++----------
 bfd/elflink.c | 10 +++++++---
 3 files changed, 50 insertions(+), 13 deletions(-)

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index b9ec590cd4e..4b9dc1c72b8 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -3143,6 +3143,16 @@ extern bool _bfd_elf_mmap_section_contents
   (bfd *abfd, asection *section, bfd_byte **buf);
 extern void _bfd_elf_munmap_section_contents
   (asection *, void *);
+extern bool _bfd_elf_link_mmap_section_contents
+  (bfd *abfd, asection *section, bfd_byte **buf);
+#ifdef USE_MMAP
+#define _bfd_elf_link_munmap_section_contents(sec, mem) \
+  _bfd_elf_munmap_section_contents (sec, mem)
+#define _bfd_elf_link_free_section_contents(ptr)
+#else
+#define _bfd_elf_link_munmap_section_contents(sec, mem)
+#define _bfd_elf_link_free_section_contents(ptr) free (ptr)
+#endif
 
 extern bool _bfd_elf_link_keep_memory
   (struct bfd_link_info *);
diff --git a/bfd/elf.c b/bfd/elf.c
index 3a6c07af6c1..65d01aabfb1 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -14416,10 +14416,12 @@ _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
   return result;
 }
 
-/* Mmap in section contents.  */
+/* Mmap in section contents.  If LINK is false, set *BUF to NULL before
+   calling bfd_get_full_section_contents.  */
 
-bool
-_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
+static bool
+elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf,
+			   bool link)
 {
 #ifdef USE_MMAP
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
@@ -14446,13 +14448,34 @@ _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
 	}
     }
 #endif
-  *buf = NULL;
+  /* NB: When this is called from elf_link_input_bfd, LINK is true,
+     *BUF is set to the preallocated buffer if USE_MMAP is undefined
+     and *BUF is set to NULL if USE_MMAP is defined.  */
+  if (!link)
+    *buf = NULL;
   bool ret = bfd_get_full_section_contents (abfd, sec, buf);
   if (ret && sec->mmapped_p)
     *buf = sec->contents;
   return ret;
 }
 
+/* Mmap in section contents.  */
+
+bool
+_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
+{
+  return elf_mmap_section_contents (abfd, sec, buf, false);
+}
+
+/* Mmap in the full section contents for linking.  */
+
+bool
+_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
+				     bfd_byte **buf)
+{
+  return elf_mmap_section_contents (abfd, sec, buf, true);
+}
+
 /* Munmap section contents.  */
 
 void
@@ -14464,15 +14487,15 @@ _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
   if (contents == NULL)
     return;
 
+  /* 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;
+
 #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)
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 7cb9269f101..7b781cb79ab 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -11463,7 +11463,8 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
       else
 	{
 	  contents = flinfo->contents;
-	  if (! bfd_get_full_section_contents (input_bfd, o, &contents))
+	  if (! _bfd_elf_link_mmap_section_contents (input_bfd, o,
+						     &contents))
 	    return false;
 	}
 
@@ -12021,6 +12022,9 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 	  }
 	  break;
 	}
+
+      /* Munmap the section contents for each input section.  */
+      _bfd_elf_link_munmap_section_contents (o, contents);
     }
 
   return true;
@@ -12316,7 +12320,7 @@ elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
 
   if (flinfo->symstrtab != NULL)
     _bfd_elf_strtab_free (flinfo->symstrtab);
-  free (flinfo->contents);
+  _bfd_elf_link_free_section_contents (flinfo->contents);
   free (flinfo->external_relocs);
   free (flinfo->internal_relocs);
   free (flinfo->external_syms);
@@ -12748,6 +12752,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
       && bed->elf_backend_elfsym_local_is_section (abfd))
     symtab_hdr->sh_info = bfd_get_symcount (abfd);
 
+#ifndef USE_MMAP
   /* Allocate some memory to hold information read in from the input
      files.  */
   if (max_contents_size != 0)
@@ -12757,7 +12762,6 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
 	goto error_return;
     }
 
-#ifndef USE_MMAP
   if (max_external_reloc_size != 0)
     {
       flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
-- 
2.44.0


      parent reply	other threads:[~2024-03-13 15:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 15:52 [PATCH v8 0/6] elf: Use mmap to map in section contents H.J. Lu
2024-03-13 15:52 ` [PATCH v8 1/6] elf: Use mmap to map in read-only sections H.J. Lu
2024-03-13 15:52 ` [PATCH v8 2/6] elf: Add _bfd_elf_m[un]map_section_contents H.J. Lu
2024-03-13 15:52 ` [PATCH v8 3/6] elf: Use mmap to map in symbol and relocation tables H.J. Lu
2024-03-13 15:52 ` [PATCH v8 4/6] elf: Don't cache symbol nor relocation tables with mmap H.J. Lu
2024-03-13 15:52 ` [PATCH v8 5/6] elf: Always keep symbol table and relocation info for eh_frame H.J. Lu
2024-03-13 15:52 ` H.J. Lu [this message]

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=20240313155227.513873-7-hjl.tools@gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=goldstein.w.n@gmail.com \
    --cc=sam@gentoo.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).