List of unused characters

I want to remove dead code from a fairly large project and would like to start with unused characters. In any case, to get the linker to display unused characters that it optimized? I am using the GNU Linker (LD) with GCC.

Otherwise, can any of the binutils (readelf or objdump) perform the same function?

+4
c ++ c linker
Dec 15 '10 at 10:43
source share
2 answers

Most compilers / linkers optimize unused characters. If you are working on a * nix system, you can try using the "nm" command for all object files, filtering and sorting it to create a list of all exported functions defined by these object files.

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

I believe that you can do the same in the final binaries.

If you then separate the two result sets, you should get a list of all unused exported functions.

Remember that some functions may be used by code that is excluded due to conditional compilation. For example. #ifdef, to say that on platform A use such built-in functions, and on another platform use your own version of the function, because there is no built-in or standard library equivalent or it does not work properly.

+6
Dec 15 '10 at 12:38
source share

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

I would recommend including all warnings and additional warnings during development. The flags are -Wall -Wextra , and warnings about dead code are implied by these flags, as well as a number of other warnings that I found useful.

+4
Dec 15 '10 at 11:17
source share



All Articles