Remove dead code
0赞
[问题] 当一个大程序中,有一些函数从来没有被调用过,用GCC编译仍然会把他们保留在最终的输出elf中。怎样去除这些没有被调用过的函数呢?
[Question] Sometimes some functions are never used in one program, but gcc will retain them in the produced elf file even with -O2 option. How to remove these dead code?
[GCC Doc]
引用
In order to do this, it has to work with objects compiled with the following options: -ffunction-sections -fdata-sections. 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 the -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.
[解决]
XPS --> Software --> Software Platform Settings --> Software Platform --> extra_compiler_flags = -g -ffunction-sections -fdata-sections
Project --> Right Click --> Set Compiler Settings --> Paths and options --> Other compiler options to append --> -ffunction-sections -fdata-sections -Wl,--gc-sections
[注意]
如果是一个有interrupt的系统,用了以上方法会导致interrupt vector和interrupt handler也被remove掉。
暂时没有解决办法。(是不是可以手动加禁止优化的属性?)
[Ref]
http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gnat_ugn_unw/Compilation-options.html
http://www.xilinx.com/support/answers/21533.htm
