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
following switches passed to the GCC back end:
-ffunction-sections
-fdata-sections
.
These options are usable with C and Ada files. They cause the compiler to place each function or data in a separate section in the resulting object file.
Once you’ve created the objects and static libraries with these switches, the
linker can perform the dead code elimination. You can do this by specifying
the -Wl,--gc-sections
switch to your gcc
command or in the
-largs
section of your invocation of gnatmake
. This causes
the linker to perform a
garbage collection and remove code and data that are never referenced.
If the linker performs a partial link (-r
linker switch), then you
need to provide the entry point using the -e
/ --entry
linker switch.
Note that objects compiled without the -ffunction-sections
and
-fdata-sections
options can still be linked with the executable.
However, no dead code elimination can be performed on those objects (they will
be linked as is).
The GNAT static library is 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.