5

I want to remove dead code from a largish project and would like to start with unused symbols. Is there anyway to get the linker to list unused symbols that it has optimized out? I am using the GNU linker (LD) together with GCC.

Failing that, can any of the Binutils (readelf or objdump) perform the same function?

doron
  • 27,972
  • 12
  • 65
  • 103
  • possible duplicate of [Finding "dead code" in a large C++ legacy application](http://stackoverflow.com/questions/2380153/finding-dead-code-in-a-large-c-legacy-application) – icecrime Dec 15 '10 at 10:46
  • This question is specific to the Binutils – doron Dec 15 '10 at 11:00
  • 1
    possible duplicate of [Is there a way to get gcc to warn about unused functions?](http://stackoverflow.com/questions/9091397/is-there-a-way-to-get-gcc-to-warn-about-unused-functions) – Greg Hewgill Mar 02 '12 at 02:56

2 Answers2

7

Most compilers/linkers optimise out unused symbols. If you're running on a *nix system, you can try using the command "nm" on all the object files, filter it and sort it, to produce a list of all exported functions defined by those object files.

nm *.o | grep "^[0-9a-f]* T " | sed 's/^[0-9a-f]* T //' | sort -u > symbols_in.txt

I believe you can do the same on the final binaries.

If you then diff the two sets of results you should get a list of all unused exported functions.

Beware though that some functions may be used by code that is excluded as a result of conditional compilation. E.g. a #ifdef switch to say that on platform A, use such and such built in functionality and on another platform use your own version of the function because there is no built in or standard library equivalent, or it doesn't work properly.

AlastairG
  • 4,119
  • 5
  • 26
  • 41
  • I tried something like this approach first, but had trouble because symbols declared in a module and *only* called from that module (and not declared `static`) look like they're unused. Also, by default gcc doesn't do function level linking anyway so unused functions in a module alongside used functions don't get eliminated. This question has a much better solution: [Is there a way to get gcc to warn about unused functions?](http://stackoverflow.com/questions/9091397/is-there-a-way-to-get-gcc-to-warn-about-unused-functions) – Greg Hewgill Mar 02 '12 at 02:56
5

GCC can generate a compiler warning when it encounters functions, labels and function parameters that are unused. The compiler flags -Wunused -Wunused-parameter will do this.

I'd personally recommend turning on all warnings and extra warnings when developing. The flags are -Wall -Wextra and the dead code warnings are implied by these flags, together with a whole host of other warnings that I've found useful.

CadentOrange
  • 3,263
  • 1
  • 34
  • 52
  • 8
    That only helps with non-relocatable symbols. If the symbol is relocatable, the we must wait for the linker to decide whether the symbol is needed or not so the compiler will be unable to help here. – doron Dec 15 '10 at 11:39