[PATCH v2] ld: Read the output in chunks when computing the PE build-id
Oleg Tolmatcev
oleg.tolmatcev@gmail.com
Fri Nov 6 20:33:39 GMT 2026
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.
---
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 5a8c2c1011c..85018577405 100644
--- a/ld/emultempl/pep.em
+++ b/ld/emultempl/pep.em
@@ -1165,25 +1165,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;
}
--
2.56.0.windows.4
More information about the Binutils
mailing list