Used for initialization functions for global objects, which can only be completed with the cooperation of both compiler and linker
- generates a "static init" function for each TU
- pointers to this function into
.init_array
section
- gathers all
.init_array
s together - script defines symbols pointing at begin and end of
.init_array
__init_array_start
__init_array_end
- walks through
init_array
and calls each
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
PROVIDE_HIDDEN (__init_array_end = .);
}
(1) CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()” - YouTube