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

夙愿已清 提交于 2020-03-17 06:52:11

问题


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



来源:https://stackoverflow.com/questions/2511018/how-does-objdump-manage-to-display-source-code-with-the-s-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!