ld/pdb: Make globals hash ordering deterministic
Ensures deterministic ordering of global symbols in PDB files by sorting colliding hashes by symbol offset.
Oleg Tolmatcev’s patch modifies the global_compare_hash function in ld/pdb.c to ensure deterministic ordering of global symbols in PDB files. Previously, symbols with colliding hash values were ordered by the host’s qsort implementation, leading to non-reproducible output. The change now sorts these collisions by their symbol record offset, guaranteeing consistent output across different hosts.
- proposer
Modified global_compare_hash to sort colliding hashes by symbol-record offset for deterministic PDB output.
“global_compare_hash treated entries with colliding hashes as equal, leaving their relative order to the host qsort implementation. Sort collisions by symbol-record offset so PDB output is reproducible across hosts.”
- reviewer
Acknowledges the change and approves it.
“Okay.”
Technical Tradeoffs
- Improves PDB file reproducibility, beneficial for debugging and build automation.
- Slightly increases the complexity of the sorting logic in `pdb.c`.
- No negative performance impact is expected; sorting is done during the linking phase.
In Details
This change addresses non-determinism in the PDB writer within the GNU linker (ld). Specifically, when multiple global symbols hash to the same value, their relative order in the PDB file was previously determined by the unstable qsort implementation of the host system. By adding a secondary sort key based on the symbol's offset within the object file, this patch ensures that the PDB output is consistent regardless of the host environment, which is crucial for reproducible builds and reliable debugging.
- PDB
- Program Database. A file format used by Microsoft's compilers and debuggers to store information about program symbols and types, often used for debugging C/C++ code.
- ld
- The GNU linker, responsible for combining object files and libraries into an executable or shared library.
- global symbols
- Symbols (like function names or global variables) that are visible across different object files and libraries.
- hash collision
- When two different inputs produce the same hash output. In this context, two different symbols have the same hash value.
- qsort
- The standard C library function for sorting arrays. Its behavior for equal elements can be implementation-defined.
- symbol record offset
- The position of a symbol's definition or reference within the symbol table of an object file.