Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 960 Bytes

sections.md

File metadata and controls

30 lines (24 loc) · 960 Bytes

Sections

.init_array

Used for initialization functions for global objects, which can only be completed with the cooperation of both compiler and linker

Compiler

  • generates a "static init" function for each TU
  • pointers to this function into .init_array section

Linker

  • gathers all .init_arrays together
  • script defines symbols pointing at begin and end of .init_array
    • __init_array_start
    • __init_array_end

C runtime

  • 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 = .);
  }

Reference

(1) CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()” - YouTube