GCC --gc-sections和查找符号依赖 [英] GCC --gc-sections and finding symbol dependencies
问题描述
我想减小我的elf可执行文件的大小。我正在编译 -ffunction-sections
-fdata-sections
并链接到 -gc
I'm trying to reduce the size of my elf executable. I'm compiling with -ffunction-sections
-fdata-sections
and linking with -gc-sections
, but it appears some of the symbols that I believe are unused are not being discarded.
在GNU工具链中有一些命令我可以运行以找出正在使用的符号和位置?
Is there some command in the GNU toolchain I can run to find out which symbols are being used and where?
- 工具链:GNU arm-none-eabi
- 平台:Cortex-M4
- 语言:C ++
典型构建标志:
编译: arm-none-eabi-g ++。exe -Wall -O3 -mthumb -std = c ++ 11 - mcpu = cortex-m4 -mfpu = fpv4-sp-d16 -mfloat-abi = softfp -fsingle-precision-constant -ffunction-sections -fdata-sections
链接: arm-none-eabi-g ++。exe -static -mthumb -mcpu = cortex-m4 -mfpu = fpv4-sp-d16 -mfloat-abi = softfp -Wl, gc-sections -Wl,-TLinkerScript.ld
感谢您的帮助。
推荐答案
我找不到一个显示符号依赖的命令。但是,我能够通过使用以下技术获取我需要的信息:
I wasn't able to find a command that showed the symbol dependencies. However, I was able to get the information I needed by using the following technique:
- 将问题中的符号添加到
/ DISCARD /
部分。这将输出一个错误消息,显示哪个符号正在使用它。它看起来像这样:在< lib0.a文件路径>(< object0文件路径>)的'<符号1>部分中引用的< symbol0& ;
(object1文件路径>)的符号0>' - 继续将这些消息中的符号添加到调用堆栈
/ DISCARD /
部分,直到找到问题的根源。
- Add the symbol in question to the
/DISCARD/
section of the linker script. This will output an error message revealing which symbol is using it. It looks something like this:<symbol0>' referenced in section '<symbol1>' of <lib0.a file path>(<object0 file path>): defined in discarded section '<symbol0>' of <lib1.a file path>(object1 file path>)
- Continue to add symbols from these messages up the call stack to the
/DISCARD/
section until you find the root of the problem.
根的问题对我来说是一个继承自另一个类。这创建了一个虚拟表,编译器不能删除虚拟表中引用的死代码。
The root of the problem for me was having a class that inherited from another. This created a virtual table, and the compiler cannot remove dead code that is referenced in a virtual table.
学到的经验教训:如果你想减少代码大小并仍然使用C ++,不要使用继承。 GNU工具链以前有一个 -fvtable-gc
切换来帮助这个,但它被删除了一段时间前。我将重构我的代码来解决我的具体问题。
Lesson learned: if you want to reduce code size and still use C++, don't use inheritance. The GNU toolchain used to have a -fvtable-gc
switch to help with this, but it was removed some time ago. I will refactor my code to address my specific problem.
这篇关于GCC --gc-sections和查找符号依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!