ld: Improve PE build-id computation efficiency
The linker now reads PE files in chunks for build-id computation, improving performance.
This patch improves the efficiency of the pecoff_checksum_contents function in the linker (ld) when computing the PE build-id. Previously, it read the file byte-by-byte, which was slow for large executables. The change implements reading the file using a 64KB buffer, falling back to a stack-allocated 64-byte buffer if memory allocation fails. This optimization significantly reduces the time spent on build-id computation.
- proposer
Submitted a patch to optimize PE build-id computation by reading files in chunks.
“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 ch…”
- reviewer
Approved the proposed change.
“Okay.”
Technical Tradeoffs
- Improves link time performance by optimizing build-id computation at the cost of slightly more complex I/O handling.
In Details
The pecoff_checksum_contents function within binutils/ld is responsible for computing the build-id for PE (Portable Executable) files. The original implementation processed the file byte-by-byte, involving frequent I/O operations and seeks, which proved to be a bottleneck, consuming up to 15% of link time for large files. This patch optimizes this by introducing buffered I/O using a heap-allocated buffer, with a stack-based fallback for robustness. This change affects the emultempl/pe.em and emultempl/pep.em files.
- PE
- Portable Executable, the file format used for executables, object code, and DLLs on 32-bit and 64-bit versions of Windows operating systems. Binutils supports generating and manipulating PE files.
- build-id
- A unique identifier embedded in an executable or library that helps in matching the binary to its debugging information. It's often generated from the file's content.
- ld
- The GNU linker, responsible for combining object files and libraries into an executable or shared library.
- emultempl
- A directory within binutils that contains template files for different executable formats (like PE, COFF, ELF), allowing the linker to be configured for various targets.
- checksum
- A value computed from a block of data to detect errors. In this context, it's part of the build-id generation process.