C语言 从目标文件中获取源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2346754/
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
Getting source from object file
提问by Pavunkumar
Is there any way to get the source code by using object file in C?
有什么办法可以通过在 C 中使用目标文件来获取源代码吗?
For example I have a source code simple.c
例如我有一个源代码 simple.c
cc simple.c
cc simple.c
Now I have a.out (object file). By this a.out whether can I get the source?
现在我有了 a.out(目标文件)。通过这个a.out我是否可以得到源?
code of simple.c
simple.c的代码
采纳答案by Dacav
There are many useful tools to retrieve information from your executable. None of them is able to give you back the original source code (as some other user pointed out, it's impossible for C), but you can try some reverse engineering tools. My preferred one are:
有许多有用的工具可以从可执行文件中检索信息。他们都不能给你原始的源代码(正如其他一些用户指出的那样,C 是不可能的),但你可以尝试一些逆向工程工具。我的首选是:
- Objdump (part of the "binutils" package)
- hte (debian packages it as "ht")
- Objdump(“binutils”包的一部分)
- hte(debian 将其打包为“ht”)
With the first one you can actually get all the exported symbols and their relative executable code in terms of assembly (obviously this is true only for the .textsections). The second one is aimed to work with intel architectures, but you will be able to analize every executable file and get information about the ELF sections and symbols.
使用第一个,您实际上可以在汇编方面获得所有导出的符号及其相关的可执行代码(显然这仅适用于.text部分)。第二个旨在与英特尔架构一起工作,但您将能够分析每个可执行文件并获取有关 ELF 部分和符号的信息。
回答by Jerry Coffin
No. Turning a cow into hamburger is fairly easy. Turning hamburger into a living cow, somewhat more difficult.
不。把一头牛变成汉堡包相当容易。把汉堡变成一头活牛,有点困难。
回答by bortzmeyer
The words for the programs you are looking for are "disassembler" (turning the machine language in a.out into assembly language) and "decompiler" (turning assembly language into C, as explained by Dacav, it is much more difficult and the result is never satisfying).
你要找的程序的词是“ disassembler”(把a.out中的机器语言变成汇编语言)和“ decompiler”(把汇编语言变成C,正如Dacav解释的那样,它要困难得多,结果永远不会令人满意)。
But using a Web search engine with these words may yield results.
但是使用带有这些词的网络搜索引擎可能会产生结果。
回答by kennytm
No.
不。
With reverse engineering (reading the disassembly) one could reconstruct the logic, but the variable names, comments, etc. will usually be lost.
使用逆向工程(阅读反汇编)可以重建逻辑,但变量名、注释等通常会丢失。
回答by prabhakaran9397
If you're using Linux, use gdb
如果您使用的是 Linux,请使用 gdb
$gdb a.out
$gdb a.out
(gdb) list
(gdb) list

