Elimination of unused sections
Elimination of unused sections is the most significant optimization on image size that the linker performs.
Unused section elimination:
- Removes unreachable code and data from the final image.
- Is suppressed in cases that might result in the removal of all sections.
To control this optimization, use the --remove
, --no_remove
, --first
, --last
, and --keep
linker options.
Unused section elimination requires an entry point. Therefore, if no entry
point is specified for an image, use the --entry
linker
option to specify an entry point and permit unused section elimination to work, if it is
enabled.
Use the --info unused
linker option to instruct the linker to generate a
list of the unused sections that it eliminates.
An input section is retained in the final image when:
- It contains an entry point or an externally accessible symbol, for example, an entry function into the secure code for Arm®v8‑M Security Extensions.
- It is an
SHT_INIT_ARRAY
,SHT_FINI_ARRAY
, orSHT_PREINIT_ARRAY
section. - It is specified as the first or last input section, either by the
--first
or--last
option or by a scatter-loading equivalent. - It is marked as unremovable by the
--keep
option. - It is referred to, directly or indirectly, by a non-weak reference from an input section retained in the image.
- Its name matches the name referred to by an input section symbol, and that symbol is referenced from a section that is retained in the image.
Note
Compilers usually collect functions and data together and emit one section for each category. The linker can only eliminate a section if it is entirely unused.
You can also use the -ffunction-sections
compiler command-line option to
instruct the compiler to generate one ELF section for each function in the source
file.