From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 890983858C54; Thu, 2 Mar 2023 12:01:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 890983858C54 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] Don't write zeros to a gap in the output file X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 0d5adb56c85da38a0f95e872fda05cc6446010c3 X-Git-Newrev: 281309f3c8c692d0246f4437ad3c635fe69dbaf7 Message-Id: <20230302120159.890983858C54@sourceware.org> Date: Thu, 2 Mar 2023 12:01:59 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Mar 2023 12:01:59 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D281309f3c8c6= 92d0246f4437ad3c635fe69dbaf7 commit 281309f3c8c692d0246f4437ad3c635fe69dbaf7 Author: Alan Modra Date: Thu Mar 2 19:59:14 2023 +1030 Don't write zeros to a gap in the output file =20 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. =20 memory_bseek also supports not writing out zeros in a gap. =20 * elf.c (write_zeros): Delete. (assign_file_positions_for_load_sections): Don't call write_zer= os. 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); } =20 -static bool -write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) -{ - void *buf; - bool ret; - - if (bfd_seek (abfd, pos, SEEK_SET) !=3D 0) - return false; - buf =3D bfd_zmalloc (len); - if (buf =3D=3D NULL) - return false; - ret =3D bfd_bwrite (buf, len, abfd) =3D=3D 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 =3D p->p_memsz - p->p_filesz; - if (!write_zeros (abfd, off, adjust)) - return false; } } /* We only adjust sh_offset in SHT_NOBITS sections