Correct empty DT_GNU_HASH symoffset
Fixes an incorrect symoffset value in Binutils' handling of empty DT_GNU_HASH tables for ELF dynamic symbols.
This patch corrects an issue in Binutils’ bfd_elf_size_dynsym_hash_dynstr function related to the DT_GNU_HASH table. When an object file has no exported dynamic symbols, an ‘empty’ hash table is generated with fixed values. However, the symoffset field was incorrectly set to 1, which is only appropriate if there’s exactly one non-hashed symbol. The patch corrects this to report the actual count of non-hashed symbols (dynsymcount) when the hash table is empty, addressing PR 34736.
- proposer
Submits a patch to correct the `symoffset` value in the 'empty' `DT_GNU_HASH` table generated by Binutils.
“However, in a special case where there are no hashed (exported) symbols, a special "empty" table is generated with fixed values - 1 empty bucket, 1 zeroed word in the bloom filter and symoffset always set to 1 (to point past the always-present zeroed out first symbol). This is wrong because there may be other non-hashed dynamic symbols.”
In Details
This patch addresses a subtle bug in the DT_GNU_HASH table generation within Binutils' ELF linking code (bfd/elflink.c). The DT_GNU_HASH structure's symoffset field indicates the index of the first symbol that is part of the hash table (i.e., exported symbols). In the edge case where there are no exported symbols, Binutils generates a minimal hash table but incorrectly sets symoffset to 1. This patch corrects it to dynsymcount, which represents the total number of non-hashed (non-exported) dynamic symbols, aligning with the expected semantics where symoffset points past all non-…
- DT_GNU_HASH
- An ELF hash table used for efficient dynamic symbol lookup. It hashes symbol names to quickly find their corresponding entries in the dynamic symbol table.
- symoffset
- A field within the DT_GNU_HASH table header that indicates the index of the first symbol included in the hash table. Symbols before this index are not hashed (e.g., local or imported symbols).
- ELF
- Executable and Linkable Format. A standard file format for executables, object code, shared libraries, and core dumps on many Unix-like systems.
- dynamic symbol table
- A symbol table within an ELF object that contains information about symbols (functions, variables) that are needed for dynamic linking at runtime.
- PR 34736
- Bug report number 34736, indicating a specific issue that this patch aims to fix.