windows 可以读取目标文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8599693/
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-15 18:42:55  来源:igfitidea点击:

It is possible to read an object file?

c++windowscompilationcompiler-constructionobject-files

提问by Leandro

I was curious about .objfiles: I pretty much don't know what they are (or what they contain), so I opened them with Vim text editor and what I found inside was an Alien like language...

我对.obj文件很好奇:我几乎不知道它们是什么(或它们包含什么),所以我用 Vim 文本编辑器打开它们,我发现里面是一种类似 Alien 的语言......

Is there any way to understand what they represent and what is their content Also, for what are they being used ?

有什么方法可以理解它们代表什么以及它们的内容是什么另外,它们的用途是什么?

Thanks.

谢谢。

回答by paulsm4

Sure.

当然。

But every different platform has a different object format. On Windows, you could use a tool like dumpbin(dumpbin comes with Visual Studio). On Linux, you could use "dumpobj", or disassemble the program.

但是每个不同的平台都有不同的对象格式。在 Windows 上,您可以使用像dumpbin这样的工具(dumpbin 随 Visual Studio 一起提供)。在 Linux 上,您可以使用“dumpobj”,或反汇编程序。

Here's a good link for Linux:

这是一个很好的 Linux 链接:

http://www.linuxjournal.com/article/1060

http://www.linuxjournal.com/article/1060

PS: objdump also lets you disassemble the object. Like you used to be able to do with "debug" on DOS PCs...

PS:objdump 还可以让你反汇编对象。就像您过去能够在 DOS PC 上进行“调试”一样……

回答by Abyx

The .objfiles used by link.exehas MS COFF format.

使用的.obj文件link.exe具有 MS COFF 格式。

You can find "Microsoft PE and COFF Specification" here, and parse .objfile according to it.

你可以在这里找到“Microsoft PE and COFF Specification” ,并.obj根据它解析文件。

Or, you can use existing tool like dumpbin.

或者,您可以使用现有工具,例如dumpbin.

回答by sarnold

The readelftool is good at showing you somedetails on the data:

readelf工具擅长向您展示数据的一些细节:

$ readelf -a /usr/bin/readelf
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
...

Some of its abilities to inspect specific sections of the executable can come in handy too:

它检查可执行文件特定部分的一些功能也可以派上用场:

$ readelf -p .rodata /usr/bin/readelf | more

String dump of section '.rodata':
  [     4]  R_IA64_IMM14
  [    11]  R_IA64_NONE
  ...
  [  1f58]    Personality routine: 
  [  1f70]  __gcc_personality_v0
  [  1f85]  __gxx_personality_v0
  [  1f9a]  __gcj_personality_v0
  [  1faf]  __gnu_objc_personality_v0
  ...

Actually disassembling the code is a bit of a stretch; if you compile your code with -gfor debugging symbols, you can use readelf --debug-dumpto read the program source, type information, etc.

实际上反汇编代码有点费劲;如果你用-g调试符号编译你的代码,你可以readelf --debug-dump用来读取程序源、类型信息等。