The operation of eliminating the unused code and data from the final executable is directly performed by the linker.
In order to do this, it has to work with objects compiled with the
These options are usable with C and Ada files. They will place respectively each function or data in a separate section in the resulting object file.
Once the objects and static libraries are created with these options, the
linker can perform the dead code elimination. You can do this by setting
-Wl,--gc-sections option to gcc command or in the
-largs section of
gnatmake. This will perform a
garbage collection of code and data never referenced.
If the linker performs a partial link (
-r linker option), then you
will need to provide the entry point using the
Note that objects compiled without the
-fdata-sections options can still be linked with the executable.
However, no dead code elimination will be performed on those objects (they will
be linked as is).
The GNAT static library is now compiled with -ffunction-sections and -fdata-sections on some platforms. This allows you to eliminate the unused code and data of the GNAT library from your executable.