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...
-
1Good question, poor answers (to date). Also see [How can I get the source lines inline with the assembly output using GCC?](https://stackoverflow.com/q/2412816/608639) – jww Sep 01 '17 at 21:09
3 Answers
objdump
uses the DWARF debugging information compiled into the binary, which references the source file name. objdump
tries to open the named source file to load the source and display it in the output. 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.

- 233,326
- 40
- 323
- 462
-
Are you saying we have to compile with dwarf debugging enabled and then use `objdump` (with some unknown options) to display the source interleaved with the disassembly? Could you provide the actual steps with the actual commands? – jww Sep 01 '17 at 21:08
-
2@jww: The `-g` option to `gcc` enables debugging information, and the `-S` option to objdump dissambles with source interleaved. But the question wasn't asking how to do this; the questioner already knew that, they were instead asking how this is implemented (how `objdump` finds the source to interleave). – caf Sep 02 '17 at 02:39
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,956
- 1
- 19
- 34

- 155
- 1
- 5
-
2Perhaps you should provide a live example. I can't get `objdump` to provide both the disassembly with the source code line numbers interleaved. – jww Sep 01 '17 at 21:08
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

- 191
- 4
- 15
-
Are you saying we have to compile with dwarf debugging enabled and then use `objdump` (with some unknown options) to display the source interleaved with the disassembly? Could you provide the actual steps with the actual commands? – jww Sep 01 '17 at 21:05
-
Suppose you have a source file with name: bufoverflow.c and you choose a name bufoverflow for binary, the steps for generating the binary and seeing the interleaved source code-disassembly will be (considering x86_64): gcc -g -o bufoverflow bufoverflow.c ; objdump --target=elf64-x86-64 -d -S bufoverflow Then lookout for the
section in output (comes on console) – a.saurabh May 19 '23 at 21:29