Ensure deterministic ordering of global symbols in PDB output
Sorts colliding global symbol hashes by offset in PDB output for reproducible results.
The linker’s PDB (Program Database) generation could produce non-deterministic ordering for global symbols that had hash collisions. This was because the global_compare_hash function treated symbols with identical hashes as equal, relying on the host system’s qsort implementation for their relative order. This commit modifies global_compare_hash to further sort colliding symbols by their record offset, ensuring that the PDB output is reproducible across different build environments.
In Details
In ld/pdb.c, the global_compare_hash function was responsible for ordering global symbols for PDB output. When two global symbols had the same hash value, global_compare_hash returned 0, allowing the underlying qsort to interleave them arbitrarily. This commit enhances global_compare_hash by adding a secondary comparison criterion: the symbol's offset. If hashes collide, symbols are now sorted by their offsets, making the PDB generation deterministic.
- PDB
- Program Database, a file format used by Microsoft Windows to store information about executables, such as debugging symbols and type information.
- global symbols
- Symbols (like function or variable names) that are visible across different compilation units (object files) within a program.
- hash collision
- When two different inputs produce the same output hash value, often requiring secondary comparison methods for ordering or lookup.
- deterministic
- An operation or process that always produces the same output given the same input, regardless of external factors or system state.
- offset
- A numerical value representing the distance in bytes from the beginning of a file or memory region to a specific location.