How does objdump manage to display source code wit

2020-05-25 07:13发布

问题:

Is there a reference to the source file in the binary? I tried running strings on the binary and couldn't find any reference to the source file listed...

回答1:

objdump uses the DWARF debugging information compiled into the binary, which references the source file name. If the binary isn't compiled with debugging information, or objdump can't find the source file, then you don't get source code in your output - only assembly.

You don't see the source file name when you use strings on the binary, because DWARF uses compression.



回答2:

The dwarf information in a binary stores the mapping between instructions(the instruction pointer or IP actually) and the source file and line number. The source file is specified using the complete path so it can be found even if the binary is moved around. To see this information you can use objdump --dwarf=decodedline <binary> (the binary ofcourse has to be compiled with -g).

Once you say objdump -S <binary> it use this dwarf info to give you source code along with the disassembly.



回答3:

My understanding is that for objdump to display source code from the binary code, there is a precondition: the DWARF debugging information must be compiled into the binary. (by gcc -g sourcefile or gcc -gdwarf-2 sourcefile) And by processing this DWARF information objdump is able to get the source code as @vlcekmi3 and @vkrnt answered