Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: how to find why an object file is not discarded

Tags:

c++

c

gcc

linker

ld

I have an executable which links to a big .a archive that contains lots of functions. The executable only uses a small fraction of the functions in this archive, but for some reason it pulls everything from it and ends up being very big.
My suspicion is that some of the functionality that the executable is using somehow references something it shouldn't and that causes everything else to be pulled.
Is it possible to make gcc tell me what reference causes a specific symbol to be added in the executable? Why else can this happen?

I've tried using --gc-sections with no effect.
I've tried using --version-script to make all the symbols in the executable local with no effect
I'm not interested in -ffunction-sections and -fdata-sections since it is while object files I want to discard, not functions.
Other answers mention -why_live but that seem to be implemented only for darwin and I am in linux x86_64

like image 400
shoosh Avatar asked Jan 18 '15 14:01

shoosh


1 Answers

Use -Wl,-M to pass -M to the linker, causing it to print a link trace. This will show you the reasons (or at least the first-found reason) for every object file that gets linked from an archive.

like image 157
R.. GitHub STOP HELPING ICE Avatar answered Nov 14 '22 06:11

R.. GitHub STOP HELPING ICE