GCC --gc-sections and finding symbol dependencies

痴心易碎 提交于 2019-11-29 04:50:17

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:

  • 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.

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.

You can try the linker option -Wl,--trace-symbol=<symbol_name>. It will display in the output where the symbol is defined, and where the symbol is used.

Create a linker map file with cross-reference output:

-Wl,-Map=output.map -Wl,--cref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!