0

When I run gcc with the -g option, this enables me to see some c code when I run the executable with objdump.

I have a program that has a sections of code in #if blocks. When I run gcc with -g and run objdump it displays c code from the #ifs that should not be included.

For example with the following c code:

#define ENABLED 0

#if ENABLED
    startSomething();
#endif

When compiled with gcc -g, the call to the startSomething function is included in the objdump output.

Section of objdump output:

    261c:       1c19            adds    r1, r3, #0
    261e:       f7fe f9a1       bl      964 <init>
    }

    startSomething();
#else

Is this expected behaviour? I would have thought that if the call to startSomething() and the actual function definition of startSomething is wrapped in #if ENABLED then it shouldn't be included and gcc should just cut that out.

Is there a way for gcc/objdump to pay more attention to the preprocessor #ifs so I don't get code in the objdump that will never get executed?

Thanks for your help.

EDIT: Additionally I know that this function is not getting executed when actually running the code. So I know I haven't set ENABLED somewhere else.

EDIT 2: A good observation is that the objdump isn't actually outputting any asm instructions related to the function call. So it seems to just be incorrectly including the function call c block there related to the function call.

Dave_Peachy
  • 498
  • 3
  • 12
  • 2
    As shown in your sample, gcc will not compile the call of `startSomething()`. (Definitely not.) However, what you might see in objdump is the prototyping or implementation of this function (in opposition to its call). To prove this, a full sample would be necessary. – Scheff's Cat Feb 21 '17 at 16:37
  • @Scheff Thanks for the reply. The implementation is also wrapped in a hash if, in addition it is the call that I can see in the objdump. – Dave_Peachy Feb 21 '17 at 16:40
  • I do not know anything about objdump. However, if you want to see how the compiler compiles your code after macro invocation, please, "compile" with `-E`. This shall show you the C source code after macro invocation. – Scheff's Cat Feb 21 '17 at 16:46
  • Is it possible that you see simply something like a comment which shall give you a hint about the associated source code for assembler commands (which is in this case, of course, a little bit irritating...) – Scheff's Cat Feb 21 '17 at 16:50
  • @Scheff Yeah i think that is exactly what it is. Except there is no assembler commands under it. (I updated the question to show the ouput). It's pretty irritating indeed. – Dave_Peachy Feb 21 '17 at 16:56
  • The debugger shows you the source code, from the source file. I am not sure why you would expect it to hide some parts of it? The out-#ifdeffed parts will not have been compiled, so you won't be able to execute them, but they are there in the source file, and to me it would be pretty confusing if they had disappeared when I tried to look at the source. Should the lines be deleted, messing up the line numbers, or should they be replaced with whitespace, or what? – Thomas Padron-McCarthy Feb 21 '17 at 17:15
  • It's mapping assembly instructions to c source code. There are no assembly instructions for the source code that is shown as it's not part of the compiled program. Why would it be useful to have source code that isn't part of the assembly file? – Dave_Peachy Feb 22 '17 at 01:57

1 Answers1

4

Objdump doesn't have a slightest idea about C or gcc or preprocessor. It has information of the form "instruction at address range S-E come from a statement on line L". Given two adjacent instruction blocks that correspond to lines L1 and L2, it may show you all lines between L1 and L2.

This is just like a debugger would work. You don't expect gdb to skip comments, or ifdef'd portions of your code, or empty lines for that matter, in the listing.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Hmm, maybe it's how i'm using objdump. To me this was pretty confusing to see code that wasn't part of the executable. Thanks for the explanation though :) – Dave_Peachy Feb 22 '17 at 01:59