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?

franz86
  • 103
  • 1
  • 7
  • 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... – R. Joiny Jun 27 '16 at 08:22
  • 1
    Take a look at [this SO answer](http://stackoverflow.com/a/4449936/3436922). It could help. – LPs Jun 27 '16 at 08: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 – franz86 Jun 27 '16 at 08:28
  • @LPs: I already read this post, but I thought of an gcc option to get the list automatically... – franz86 Jun 27 '16 at 08:33
  • AFAIK it is not possible with [tag:gcc]. Another interesting post is [this one](http://stackoverflow.com/questions/9091397/is-there-a-way-to-get-warned-about-unused-functions), but probably you already know it. – LPs Jun 27 '16 at 08:35

2 Answers2

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 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. – franz86 Jun 29 '16 at 14:45
  • 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 May 01 '18 at 14:40
  • @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. – Employed Russian May 01 '18 at 14:59
  • 1
    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... – K.Mulier May 01 '18 at 15:15
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.

tofro
  • 5,640
  • 14
  • 31
  • Thanks for the hint. I didn't know this tool, but it looks interesting. I will give it a try. – franz86 Jun 27 '16 at 08:43
  • Hi @tofro. Looks very interesting. It would be great if it could work on code written for ARM microcontrollers... – K.Mulier May 04 '18 at 19:56