I need to optimize the size of my executable severely (ARM
development) and
I noticed that in my current build scheme (gcc
+ ld
) unused symbols are not getting stripped.
The usage of the arm-strip --strip-unneeded
for the resulting executables / libraries doesn't change the output size of the executable (I have no idea why, maybe it simply can't).
What would be the way (if it exists) to modify my building pipeline, so that the unused symbols are stripped from the resulting file?
I wouldn't even think of this, but my current embedded environment isn't very "powerful" and
saving even 500K
out of 2M
results in a very nice loading performance boost.
Update:
Unfortunately the current gcc
version I use doesn't have the -dead-strip
option and the -ffunction-sections... + --gc-sections
for ld
doesn't give any significant difference for the resulting output.
I'm shocked that this even became a problem, because I was sure that gcc + ld
should automatically strip unused symbols (why do they even have to keep them?).
You'll want to check your docs for your version of gcc & ld:
However for me (OS X gcc 4.0.1) I find these for ld
And this helpful option
There's also a note in the gcc/g++ man that certain kinds of dead code elimination are only performed if optimization is enabled when compiling.
While these options/conditions may not hold for your compiler, I suggest you look for something similar in your docs.
It seems to me that the answer provided by Nemo is the correct one. If those instructions do not work, the issue may be related to the version of gcc/ld you're using, as an exercise I compiled an example program using instructions detailed here
Then I compiled the code using progressively more aggressive dead-code removal switches:
These compilation and linking parameters produced executables of size 8457, 8164 and 6160 bytes, respectively, the most substantial contribution coming from the 'strip-all' declaration. If you cannot produce similar reductions on your platform,then maybe your version of gcc does not support this functionality. I'm using gcc(4.5.2-8ubuntu4), ld(2.21.0.20110327) on Linux Mint 2.6.38-8-generic x86_64
From the GCC 4.2.1 manual, section
-fwhole-program
:Programming habits could help too; e.g. add
static
to functions that are not accessed outside a specific file; use shorter names for symbols (can help a bit, likely not too much); useconst char x[]
where possible; ... this paper, though it talks about dynamic shared objects, can contain suggestions that, if followed, can help to make your final binary output size smaller (if your target is ELF).For GCC, this is accomplished in two stages:
First compile the data but tell the compiler to separate the code into separate sections within the translation unit. This will be done for functions, classes, and external variables by using the following two compiler flags:
Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):
So if you had one file called test.cpp that had two functions declared in it, but one of them was unused, you could omit the unused one with the following command to gcc(g++):
(Note that -Os is an additional compiler flag that tells GCC to optimize for size)