ld: Read the output in chunks when computing the PE build-id

Message ID 20261102225947.1725-8-oleg.tolmatcev@gmail.com
State New
Headers
Series ld: Read the output in chunks when computing the PE build-id |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Oleg Tolmatcev Nov. 2, 2026, 10:59 p.m. UTC
  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 in 64 KB chunks instead.

ld/
	* emultempl/pe.em (pecoff_checksum_contents): Read the file in
	64 KB chunks.
	* emultempl/pep.em (pecoff_checksum_contents): Likewise.
---
 ld/emultempl/pe.em  | 27 ++++++++++-----------------
 ld/emultempl/pep.em | 27 ++++++++++-----------------
 2 files changed, 20 insertions(+), 34 deletions(-)
  

Comments

Jan Beulich Nov. 6, 2026, 10:13 a.m. UTC | #1
On 02.11.2026 23:59, Oleg Tolmatcev wrote:
> 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 in 64 KB chunks instead.

Hmm. 64k may not seem a lot nowadays, but what if the buffer allocation
fails nevertheless? I'm again wondering whether some fallback wouldn't
be wanted. Surely that doesn't need to go down to single-byte reads
again, but having an on-stack 16-, 32-, or 64-byte buffer to cope with
very low memory may already help.

Jan
  

Patch

diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em
index 0326137ff1d..9db08002b7b 100644
--- a/ld/emultempl/pe.em
+++ b/ld/emultempl/pe.em
@@ -1269,25 +1269,18 @@  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 *buf;
+  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 = xmalloc (bufsize);
+  while ((n = bfd_read (buf, bufsize, abfd)) != 0
+	 && n != (bfd_size_type) -1)
+    (*process) (buf, n, arg);
+  free (buf);
 
   return true;
 }
diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em
index f78701b5f6b..469eb1b2232 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1145,25 +1145,18 @@  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 *buf;
+  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 = xmalloc (bufsize);
+  while ((n = bfd_read (buf, bufsize, abfd)) != 0
+	 && n != (bfd_size_type) -1)
+    (*process) (buf, n, arg);
+  free (buf);
 
   return true;
 }