Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c++ - GCC --gc-sections and finding symbol dependencies

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.

Is there some command in the GNU toolchain I can run to find out which symbols are being used and where?

  • Toolchain: GNU arm-none-eabi
  • Platform: Cortex-M4
  • Language: C++

Here are my typical build flags:

Compilation: 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

Link: arm-none-eabi-g++.exe -static -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -Wl,-gc-sections -Wl,-T"LinkerScript.ld

Thanks for the help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...