GNU ld's output section layout is determined by a linker script,
which can be either internal (default) or external (specified with
-T
or -dT
). Within the linker script,
SECTIONS
commands define how input sections are mapped into
output sections.
Input sections not explicitly placed by SECTIONS
commands are termed "orphan
sections".
Orphan sections are sections present in the input files which are not explicitly placed into the output file by the linker script. The linker will still copy these sections into the output file by either finding, or creating a suitable output section in which to place the orphaned input section.
GNU ld's default behavior is to create output sections to hold these orphan sections and insert these output sections into appropriate places.
Orphan section placement is crucial because GNU ld's built-in linker
scripts, while understanding common sections like
.text
/.rodata
/.data
, are unaware
of custom sections. These custom sections should still be included in
the final output file.
- Grouping: Orphan input sections are grouped into orphan output sections that share the same name.
- Placement: These grouped orphan output sections are then inserted
into the output sections defined in the linker script. They are placed
near similar sections to minimize the number of
PT_LOAD
segments needed.