public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
* [binutils-gdb] Don't write zeros to a gap in the output file
@ 2023-03-02 12:01 Alan Modra
  0 siblings, 0 replies; only message in thread
From: Alan Modra @ 2023-03-02 12:01 UTC (permalink / raw)
  To: bfd-cvs

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

commit 281309f3c8c692d0246f4437ad3c635fe69dbaf7
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Mar 2 19:59:14 2023 +1030

    Don't write zeros to a gap in the output file
    
    Writing out zeros is counterproductive if a file system supports
    sparse files.  A very large gap need not take much actual disk space,
    but it usually will if zeros are written.
    
    memory_bseek also supports not writing out zeros in a gap.
    
            * elf.c (write_zeros): Delete.
            (assign_file_positions_for_load_sections): Don't call write_zeros.
            Comment.

Diff:
---
 bfd/elf.c | 26 ++++++--------------------
 1 file changed, 6 insertions(+), 20 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 37f331b98cd..409be7068ec 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5402,22 +5402,6 @@ print_segment_map (const struct elf_segment_map *m)
   fflush (stderr);
 }
 
-static bool
-write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
-{
-  void *buf;
-  bool ret;
-
-  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
-    return false;
-  buf = bfd_zmalloc (len);
-  if (buf == NULL)
-    return false;
-  ret = bfd_bwrite (buf, len, abfd) == len;
-  free (buf);
-  return ret;
-}
-
 /* Assign file positions to the sections based on the mapping from
    sections to segments.  This function also sets up some fields in
    the file header.  */
@@ -5866,11 +5850,13 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		      if (p->p_filesz + adjust < p->p_memsz)
 			{
 			  /* We have a PROGBITS section following NOBITS ones.
-			     Allocate file space for the NOBITS section(s) and
-			     zero it.  */
+			     Allocate file space for the NOBITS section(s).
+			     We don't need to write out the zeros, posix
+			     fseek past the end of data already written
+			     followed by a write at that location is
+			     guaranteed to result in zeros being read
+			     from the gap.  */
 			  adjust = p->p_memsz - p->p_filesz;
-			  if (!write_zeros (abfd, off, adjust))
-			    return false;
 			}
 		    }
 		  /* We only adjust sh_offset in SHT_NOBITS sections

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

only message in thread, other threads:[~2023-03-02 12:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-02 12:01 [binutils-gdb] Don't write zeros to a gap in the output file 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).