Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc linker get list of unused objects

Tags:

c++

c

gcc

linker

I want to identify unused object files in a large C application with many libraries. The project has grown a lot over time and now I want to search for libraries that are not used anymore, so that I can remove them from the dependency file. Is it possible with the gcc linker to identify any object that is not used?

For example, if I compile an application with gcc and let's say none of the symbols/functions of library2 are used. Is there any way to get the info about which objects are not linked in?

gcc library1.o library2.o main.o -o main.elf

I know that gcc has the compiler and linker flags to remove unused symbols:

-fdata-sections -ffunction-sections -Wl,--gc-sections

However this way I don't know which of the objects were removed by gcc. It would be perfect if gcc has an option to get a list of objects which were not linked into the application.

Just to mention: I need it on object file basis not on function/symbol basis!

Does anyone know such an option for gcc?

like image 477
franz86 Avatar asked Jun 27 '16 08:06

franz86


2 Answers

For example, if I compile an application with gcc and let's say none of the symbols/functions of library2 are used. Is there any way to get the info about which objects are not linked in?

gcc library1.o library2.o main.o -o main.elf

With above command, library2.o will be linked in even if none of the code from it is ever used. To understand why, read this or this.

It is true that if you compile code in library2.o with -ffunction-sections -fdata-sections and link with -Wl,-gc-sections, then all of the code and data from library2.o will be GC'd out, but that is not the command you gave.

Presumably, you are more interested in what happens if you use libraries as libraries:

gcc main.o -o main.elf -lrary1 -lrary2

In that case, if none of the code from library2 is referenced, the linker will not pull it into the link.

There is no way to ask the linker for list of things it didn't use, but (if you are using GNU-ld) there is a way to ask it for a list of objects it did use: the -M or -Map option. Once you know what objects are used, it's a simple matter of subtracting used objects from all objects used while linking to get the list that is not used.

Update:

Gold linker supports --print-symbol-counts FILENAME option, which can also be helpful here. It prints defined and used symbol counts. For library2.a, it should print $num_defined 0, the 0 indicating that none of the objects from library2.a were actually used.

like image 180
Employed Russian Avatar answered Oct 24 '22 01:10

Employed Russian


Take a look at callcatcher

This compiles your program into assembly and extracts obvious references from the assembly output. I guess that is exactly what you are searching for. (Note due to the fact it analyzes assembler output it will only work on x86 platforms)

Note callcatcher ignores virtual functions (for some good reasons), so it will not directly allow you to analyse those.

like image 1
tofro Avatar answered Oct 24 '22 03:10

tofro