[binutils-gdb] ld: Read the output in chunks when computing the PE build-id

Jan Beulich jbeulich@sourceware.org
Fri Nov 13 13:17:14 GMT 2026


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

commit 6edb577a7aa703885b804da0b96b0150c70df350
Author: Oleg Tolmatcev <oleg.tolmatcev@gmail.com>
Date:   Fri Nov 13 14:16:48 2026 +0100

    ld: Read the output in chunks when computing the PE build-id
    
    pecoff_checksum_contents read the output file back one byte at a time,
    seeking before every byte and passing each byte to the hash function
    separately.  For a 235 MB executable this took about 15% of the link
    time.
    
    Read the file using a 64 KB heap buffer.  If its allocation fails,
    fall back to a 64-byte buffer on the stack.
    
    ld/
            * emultempl/pe.em (pecoff_checksum_contents): Read the file in
            chunks, with a stack buffer fallback.
            * emultempl/pep.em (pecoff_checksum_contents): Likewise.

Diff:
---
 ld/emultempl/pe.em  | 33 +++++++++++++++++----------------
 ld/emultempl/pep.em | 33 +++++++++++++++++----------------
 2 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 0326137ff1d..d6e2ae11822 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1269,25 +1269,26 @@ pecoff_checksum_contents (bfd *abfd,
 			  void (*process) (const void *, size_t, void *),
 			  void *arg)
 {
-  file_ptr filepos = (file_ptr) 0;
+  const bfd_size_type bufsize = 65536;
+  unsigned char fallback_buf[64];
+  unsigned char *buf;
+  bfd_size_type read_size = bufsize;
+  bfd_size_type n;
 
-  while (1)
-    {
-      unsigned char b;
-      int status;
-
-      if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
-	return 0;
-
-      status = bfd_read (&b, 1, abfd);
-      if (status < 1)
-	{
-	  break;
-	}
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
+    return false;
 
-      (*process) (&b, 1, arg);
-      filepos += 1;
+  buf = malloc (bufsize);
+  if (buf == NULL)
+    {
+      buf = fallback_buf;
+      read_size = sizeof (fallback_buf);
     }
+  while ((n = bfd_read (buf, read_size, abfd)) != 0
+	 && n != (bfd_size_type) -1)
+    (*process) (buf, n, arg);
+  if (buf != fallback_buf)
+    free (buf);
 
   return true;
 }
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index f78701b5f6b..62d6d6b87a9 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1145,25 +1145,26 @@ pecoff_checksum_contents (bfd *abfd,
 			  void (*process) (const void *, size_t, void *),
 			  void *arg)
 {
-  file_ptr filepos = (file_ptr) 0;
+  const bfd_size_type bufsize = 65536;
+  unsigned char fallback_buf[64];
+  unsigned char *buf;
+  bfd_size_type read_size = bufsize;
+  bfd_size_type n;
 
-  while (1)
-    {
-      unsigned char b;
-      int status;
-
-      if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
-	return 0;
-
-      status = bfd_read (&b, 1, abfd);
-      if (status < 1)
-	{
-	  break;
-	}
+  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
+    return false;
 
-      (*process) (&b, 1, arg);
-      filepos += 1;
+  buf = malloc (bufsize);
+  if (buf == NULL)
+    {
+      buf = fallback_buf;
+      read_size = sizeof (fallback_buf);
     }
+  while ((n = bfd_read (buf, read_size, abfd)) != 0
+	 && n != (bfd_size_type) -1)
+    (*process) (buf, n, arg);
+  if (buf != fallback_buf)
+    free (buf);
 
   return true;
 }


More information about the Binutils-cvs mailing list