Unused section elimination is the most significant optimization on image size that is performed by the linker. It removes unreachable code and data from the final image.
Unused section elimination is suppressed in cases that might result in the removal of all sections.
To control this optimization use the
--keep linker options.
Unused section elimination requires an entry point. Therefore,
if there is no entry point specified for an image, use the
option to specify an entry point and permit unused section elimination
to work, if it is enabled.
--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 under the following conditions:
If it contains an entry point.
If it is referred to, directly or indirectly, by a non-weak reference from an input section containing an entry point.
If it is specified as the first or last input section by the
--lastoption (or a scatter-loading equivalent).
If it is marked as unremovable by the
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
command-line option to instruct the compiler to generate one ELF
section for each function in the source file.