C语言 objdump 如何使用 -S 选项显示源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2511018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:56:25  来源:igfitidea点击:

How does objdump manage to display source code with the -S option?

clinuxgccdisassemblyobjdump

提问by anon

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...

二进制文件中是否有对源文件的引用?我尝试在二进制文件上运行字符串,但找不到对列出的源文件的任何引用...

回答by caf

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

objdump使用编译成二进制文件的 DWARF 调试信息,它引用源文件名。如果二进制文件没有使用调试信息编译,或者objdump找不到源文件,那么您不会在输出中获得源代码 - 只有程序集。

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

strings在二进制文件上使用时,您看不到源文件名,因为 DWARF 使用压缩。

回答by vkrnt

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).

二进制文件中的矮信息存储指令(实际上是指令指针或IP)与源文件和行号之间的映射。源文件是使用完整路径指定的,因此即使二进制文件四处移动也可以找到它。要查看此信息,您可以使用objdump --dwarf=decodedline <binary>(当然必须使用 编译二进制文件-g)。

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

一旦你说它objdump -S <binary>使用这个侏儒信息为你提供源代码以及反汇编。

回答by JerseyGood

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

我的理解是,objdump要从二进制代码中显示源代码,有一个前提:DWARF调试信息必须编译成二进制文件。(by gcc -g sourcefileor gcc -gdwarf-2 sourcefile) 并且通过处理此 DWARF 信息objdump能够获得源代码,如@vlcekmi3 和@vkrnt 的回答