Format:ELF (Executable and Linkable Format)/Initialization arrays and IFUNC
How ELF code runs before main and how IFUNC symbols pick an implementation: DT_PREINIT_ARRAY, DT_INIT, DT_INIT_ARRAY, DT_FINI_ARRAY (and legacy .ctors), STT_GNU_IFUNC resolvers and R_*_IRELATIVE relocations.
Constructors and destructors
Dynamic tags (all pointer-array tags are followed by a byte-size tag):
| Tag | Value | Section | Meaning |
|---|---|---|---|
| DT_INIT / DT_FINI | 12 / 13 | .init / .fini |
one function each (_init, _fini)
|
| DT_INIT_ARRAY / DT_INIT_ARRAYSZ | 25 / 27 | .init_array (SHT_INIT_ARRAY 14) |
pointers to constructors |
| DT_FINI_ARRAY / DT_FINI_ARRAYSZ | 26 / 28 | .fini_array (SHT_FINI_ARRAY 15) |
pointers to destructors |
| DT_PREINIT_ARRAY / DT_PREINIT_ARRAYSZ | 32 / 33 | .preinit_array (SHT_PREINIT_ARRAY 16) |
executables only, run before the init of any library |
Order in the loader: dependencies first; for each object DT_INIT then the DT_INIT_ARRAY entries in index order; DT_FINI_ARRAY entries run in reverse index order. Entries are function pointers, so in a PIE they are R_*_RELATIVE targets: on disk the 64-bit words of .init_array may contain the link-time address (RELA files often keep it, but linkers may also leave 0 with the value only in the addend), so decode them through the relocation table when static tools show zeros. glibc and bionic call constructors as void(int argc, char **argv, char **envp).
GCC attributes __attribute__((constructor(N))) put pointers into .init_array.NNNNN sections which the linker sorts by N; C++ dynamic initialisers of globals appear as _GLOBAL__sub_I_<file> in .init_array. Old toolchains use .ctors and .dtors (also run at startup, in reverse order for ctors) instead. _start calls __libc_start_main(main, ...) and, in static glibc, __libc_csu_init before glibc 2.34, which walks the __init_array_start .. __init_array_end range.
The constructors are where many packers, anti-debug checks and runtime initialisation hide: they run before main and before a breakpoint on main is hit. Check .init_array and DT_INIT first.
IFUNC (STT_GNU_IFUNC)
A symbol of type 10 STT_GNU_IFUNC is not the function but a resolver: its st_value points to code that returns the address of the implementation (glibc uses this for memcpy, strlen, strchr, ... picking an SSE/AVX/AVX512 variant by CPU features).
- Calls from code go through the PLT as usual; the matching
.rela.pltentry isR_X86_64_IRELATIVE(37) orR_AARCH64_IRELATIVE(1032) with no symbol: the addend is the address of the resolver. The loader calls the resolver once and stores the result in the GOT slot. In static executables these relocations live in.rela.iplt(also named.rela.pltwith__rela_iplt_start/__rela_iplt_endsymbols) and__libc_start_mainapplies them viaapply_irel. - Because the target depends on the CPU, the function you land in under a debugger (
__memcpy_avx_unaligned_erms) differs between machines; statically, the interesting code is the resolver. - Bionic applies IRELATIVE relocations early so that string functions work, and does not call a resolver twice (comment in its linker_relocate.cpp).
Sources: glibc elf.h (tags, symbol type), bionic linker/linker_relocate.cpp (R_GENERIC_IRELATIVE handling), LLVM lld. The init order and .ctors notes are standard System V behaviour; unverified on this wiki for each libc version.