Using GCC to find unreachable functions ("dead code")
Asked Answered
S

2

10

I was looking for a way of finding statically unreachable functions in a (very) big C++ project. I had tried using doxygen and other static analysis tools suggested here but it seemed that the project is too complicated for them to handle. Finally i decided that using GCC tools (g++, gprof, gcov, etc.) is the safest option, although I couldn't figure out how to do it.

I think the g++ optimizations eliminate statically unreachable functions but I'm not sure how to get the names of the functions it eliminates.

Do you have any suggestions?

Scotch answered 16/11, 2010 at 15:0 Comment(1)
#229569Fawkes
Q
9

Dead code optimization is typically done by the linker - the compiler doesn't have the overview. However, the compiler might have eliminated unused static functions (as they have internal linkage).

Therefore, you shouldn't be looking at GCC options, but at ld options. It seems you want --print-gc-sections. However, note that you probably want GCC to put each function in its own section, -ffunction-sections. By default GCC will put all functions in the same section, and ld isn't smart enough to eliminate unused functions - it can only eliminate unused sections.

Quartus answered 16/11, 2010 at 15:11 Comment(6)
The --print-gc-sections option prints result like this: Removing unused section '.text._DriverUnloadHandler' in file '/tmp/ccLoxO8q.ltrans0.ltrans.o'. So how to find the original C file name?Eng
And the -ffunction-sections doesn't work with assembly code since no compilation is involved. So all functions in assembly code will be in the .text section, which renders the --print-gc-sections less useful to detect unused functions.Eng
@smwikipedia: Check objdump -W /tmp/ccLoxO8q.ltrans0.ltrans.oQuartus
@smwikipedia: As for assembly, it can be structured in ways which are not expressible as functions. For instance, you cannot call into the middle of a C function; C functions have a single entry point. Assembly is just a sequence of instructiosn, and you can jump to any point in that sequence. In x86, rhere's not even a restriction that you jump to an actual instruction boundary !Quartus
Yes. I agree with you. So these options are kind of useful to C only. But that's good enough for most scenarios. Thanks.Eng
@smwikipedia: Well, you can of course use .segment .text.MyAssemblyFoo to manually create sections in your assembly. That's the double-edged nature of assembly: you can do everything manually, and you must do everythign manually.Quartus
P
-1

gcov is what you're looking for. You have that listed in the question, have you not looked at it?

Pickax answered 16/11, 2010 at 15:9 Comment(1)
Not really, "gcov creates a logfile called sourcefile.gcov which indicates how many times each line of a source file sourcefile.c has executed." This is dynamic analysis not static.Scotch

© 2022 - 2024 — McMap. All rights reserved.