ld: Optimize PE build-id computation by reading chunks
Improves linker performance when computing PE build-ids by reading the output file in larger chunks instead of byte-by-byte.
This commit significantly optimizes the process of computing the PE build-id by changing how the linker’s pecoff_checksum_contents function reads the output file. Previously, it read the file one byte at a time, involving frequent seeks and function calls, which was a performance bottleneck for large executables. The update now reads the file in larger, 64KB chunks using a heap buffer, falling back to a smaller stack buffer if allocation fails. This reduces I/O overhead and speeds up linking.
In Details
The pecoff_checksum_contents function in ld/emultempl/pe.em (and pep.em) previously iterated byte-by-byte, calling bfd_seek before each bfd_read and processing each byte individually. For large PE executables, this byte-at-a-time I/O was excessively slow. This change refactors the function to use a 64KB buffer for reading file content in chunks, significantly reducing the number of I/O operations and improving performance. A stack-allocated fallback buffer is provided for robustness.
- ld
- The GNU linker, responsible for combining object files and libraries into an executable or shared library.
- PE
- Portable Executable, a file format for executables, object code, DLLs, and system components used in 32-bit and 64-bit versions of Windows.
- Build-id
- A unique identifier for a build of an executable or library, often used for debugging and crash report analysis. It's typically computed from the file's content.
- Checksum
- A value calculated from a block of data for error detection. In this context, it's part of the process to generate the build-id.