8.3.7 Gc-sections Option

The --gc-sections option enables garbage collection of unused input sections. This option is not compatible with the -r option. The default behavior (of not performing this garbage collection) can be restored by specifying --no-gc-sections on the command line.

This option can sometimes interfere with the debugging experience associated with a project, because sections containing functions can be removed from the linker while the debugging information for these deleted functions remains in the ELF file.

When link-time garbage collection is in use, marking sections that should not be eliminated is often useful. Mark the section by surrounding an input section's wildcard entry with KEEP(), as in KEEP(*(.init)) or KEEP(SORT_BY_NAME(*)(.ctors)).