6

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?

5
  • So you could just look, which .o files you can find after compiling and compare to your sourcefiles. I don't know for sure, just an idea... Commented Jun 27, 2016 at 8:22
  • 1
    Take a look at this SO answer. It could help. Commented Jun 27, 2016 at 8:26
  • @R. Joiny Yes it is. I am already using the flags -fdata-sections -ffunction-sections -Wl,--gc-sections which removes unused code. However this way I cannot identify the objects to remove and them and tidy up my buildsystem Commented Jun 27, 2016 at 8:28
  • @LPs: I already read this post, but I thought of an gcc option to get the list automatically... Commented Jun 27, 2016 at 8:33
  • AFAIK it is not possible with gcc. Another interesting post is this one, but probably you already know it. Commented Jun 27, 2016 at 8:35

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right, my command was wrong, sorry that it was not accurate. I also thought of doing a diff between the built objects and linked objects, I only thought that gcc already has some builtin flags helping me doing that.
Hi @Employed Russian, can you explain how to extract the list of used object files from the generated output.map file? Thank you so much :-)
@K.Mulier Unfortunately the output.map is written for human consumption, not machine post-processing. You can use grep, awk or any other text processing tool to get the list from it.
Thank you for the answer. My output.map file is 30.000 lines long... All I am looking for is a list of object files that are used (and a list of object files that is unused). But I don't know where to start looking in the file...
1

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.

2 Comments

Thanks for the hint. I didn't know this tool, but it looks interesting. I will give it a try.
Hi @tofro. Looks very interesting. It would be great if it could work on code written for ARM microcontrollers...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.