Format:ELF (Executable and Linkable Format)/Symbol versioning
GNU symbol versioning of ELF: .gnu.version (Versym, one uint16 per .dynsym entry), .gnu.version_r (Verneed/Vernaux: versions this file needs from each DT_NEEDED library) and .gnu.version_d (Verdef/Verdaux: versions it defines), with the structs and flag values.
Why it matters
A plain ELF import is only a name plus a library list (DT_NEEDED). Symbol versioning adds the version string a symbol was bound against, so memcpy@GLIBC_2.2.5 and memcpy@GLIBC_2.14 can coexist. For analysts the highest GLIBC_2.x in .gnu.version_r is the minimum glibc the binary runs on, and a version name attached to an import tells which library it came from.
Sections and dynamic tags
| Section (sh_type) | Dynamic tags | Content |
|---|---|---|
.gnu.version (SHT_GNU_versym 0x6fffffff) |
DT_VERSYM 0x6ffffff0 | array of uint16, entry i belongs to .dynsym[i]
|
.gnu.version_r (SHT_GNU_verneed 0x6ffffffe) |
DT_VERNEED 0x6ffffffe, DT_VERNEEDNUM 0x6fffffff | versions this file needs, grouped per library |
.gnu.version_d (SHT_GNU_verdef 0x6ffffffd) |
DT_VERDEF 0x6ffffffc, DT_VERDEFNUM 0x6ffffffd | versions this file defines (shared objects) |
In all three, sh_link is the string table (.dynstr), and sh_info of verneed/verdef is the number of entries.
Versym values
Each uint16: low 15 bits (mask 0x7fff) = version index, bit 15 (0x8000) = hidden (not the default version, written name@VERSION instead of name@@VERSION). 0 = local (VER_NDX_LOCAL), 1 = global/unversioned (VER_NDX_GLOBAL), 2 and up index the version definitions or needs; 0xff00 is VER_NDX_LORESERVE, 0xff01 VER_NDX_ELIMINATE. Index 1 in a defining library is its own base definition (VER_FLG_BASE).
Verneed / Vernaux (.gnu.version_r)
A chain of Elf_Verneed records, each with a chain of Elf_Vernaux:
Verneed(16 bytes, same in ELF32 and ELF64):uint16 vn_version(1),uint16 vn_cnt(number of vernaux),uint32 vn_file(offset in .dynstr of the library name, equal to a DT_NEEDED string),uint32 vn_aux(byte offset from this record to the first vernaux),uint32 vn_next(byte offset to the next verneed, 0 at the end).Vernaux(16 bytes):uint32 vna_hash(SysV elf_hash of the version name),uint16 vna_flags(VER_FLG_WEAK 0x2),uint16 vna_other(the version index that .gnu.version entries use for symbols bound to this version),uint32 vna_name(offset of the version string, e.g.GLIBC_2.2.5),uint32 vna_next.
To name an import: take .gnu.version[i], mask 0x7fff, find the vernaux whose vna_other equals it, then its vna_name and the parent's vn_file.
Verdef / Verdaux (.gnu.version_d)
Verdef(20 bytes):uint16 vd_version(1),uint16 vd_flags(VER_FLG_BASE 0x1 = the file itself, VER_FLG_WEAK 0x2),uint16 vd_ndx(index used in Versym),uint16 vd_cnt(verdaux count),uint32 vd_hash(elf_hash of the name),uint32 vd_aux,uint32 vd_next.Verdaux(8 bytes):uint32 vda_name(offset of the version name, or of a parent version for entries after the first),uint32 vda_next.
The first verdef (VER_FLG_BASE) names the file itself (its SONAME), the others are versions such as GLIBC_2.34 or GLIBC_PRIVATE.
Pitfalls
- Offsets (
vn_aux,vn_next, ...) are relative to the start of the current record, not the section. - Statically linked or musl programs have no version sections; the Android linker ignores them mostly.
- A symbol with Versym 1 in a binary that has a
.gnu.version_rwas not bound to a version (rare for glibc imports).
Source: glibc elf/elf.h and LLVM BinaryFormat/ELF.h at the commits cited by the facts of this subject.