| |||
| Home > Using linker optimizations > Example of using linker feedback | |||
To see how linker feedback works:
Create
a file fb.c containing the code shown in this
example:
Example 1. Feedback example
#include <stdio.h>
void legacy()
{
printf("This is a legacy function that is no longer used.\n");
}
int cubed(int i)
{
return i*i*i;
}
void main()
{
int n = 3;
printf("%d cubed = %d\n",n,cubed(n));
}
Compile the program, and ignore the warning that the feedback file does not exist:
armcc --asm -c --feedback fb.txt fb.c
This inlines the cubed() function by
default, and creates an assembler file fb.s and
an object file fb.o. In the assembler file,
the code for legacy() and cubed() is
still present. Because of the inlining, there is no call to cubed() from main.
An out-of-line copy of cubed() is kept
because it is not declared as static.
Link the object file to create the linker feedback file with the command line:
armlink --info sizes --list fbout1.txt --feedback fb.txt fb.o -o fb.axf
Linker diagnostics are output to the file fbout1.txt.
The linker feedback file identifies the source file that contains
the unused functions in a comment (not used by the compiler) and
includes entries for the legacy() and cubed() functions:
;#<FEEDBACK># ARM Linker, 5.01 [Buildnum]: Last Updated:Date;VERSION 0.2 ;FILE fb.o cubed <= USED 0 legacy <= USED 0
This shows that the functions are not used.
Repeat the compile and link stages with a different diagnostics file:
armcc --asm -c --feedback fb.txt fb.c
armlink --info sizes --list fbout2.txt fb.o -o fb.axf
Compare the two diagnostics files, fbout1.txt and fbout2.txt,
to see the sizes of the image components (for example, Code, RO
Data, RW Data, and ZI Data). The Code component is smaller.
In the assembler file, fb.s, the legacy() and cubed() functions
are no longer in the same area as the main() function. They are
compiled into their own ELF sections. Therefore, armlink can
remove the legacy() and cubed() functions
from the final image.
To get the maximum benefit from linker feedback you have to do a full compile and link at least twice. However, a single compile and link using feedback from a previous build is usually sufficient.