对象文件包含什么?

[英]What does an object file contain?


During the various stages of compilation in C or C++, I know that an object file gets generated (i.e., any_name.o file). What does this .o file contain? I can't open it since it's a binary file.

在C或c++编译的各个阶段中,我知道会生成一个对象文件(即:,any_name。o文件)。这个.o文件包含什么?我不能打开它,因为它是一个二进制文件。

Could anybody please help me? Are the contents of the object file mainly dependent on the compiler which we use on Unix?

谁能帮帮我吗?对象文件的内容主要依赖于我们在Unix上使用的编译器吗?

8 个解决方案

#1


45  

Object files can contain a bunch of stuff: Basically it's some or all of the list below:

对象文件可以包含很多东西:基本上就是下面列表的一部分或全部:

  • Symbol Names
  • 符号名称
  • Compiled code
  • 编译后的代码
  • Constant data, eg. strings
  • 常数数据,例如。字符串
  • Imports - which symbols the compiled code references (gets fixed up by linker)
  • 导入——将编译后的代码引用符号(由链接器修复)
  • Exports - which symbols the object file makes available to OTHER object files.
  • 导出—对象文件使其他对象文件可用的符号。

The linker turns a bunch of object files into an executable, by matching up all the imports and exports, and modifying the compiled code so the correct functions get called.

链接器通过匹配所有导入和导出,修改编译后的代码,从而调用正确的函数,将一堆对象文件转换为可执行文件。

#2


7  

There is several standardized formats (COFF, ELF on Unix), basically they are variants of the same formats that those used for executables but missing some informations. These missing informations will be completed when linking.

有几种标准化的格式(COFF, Unix上的ELF),基本上它们是用于可执行文件的格式的变体,但是缺少一些信息。这些丢失的信息将在链接时完成。

Objects files formats basically contains the same informations:

对象文件格式基本上包含相同的信息:

  • binary code resulting of compilation (for a target processor)
  • 编译产生的二进制代码(用于目标处理器)
  • static data used by that part of the program (like constant strings, etc). You can make a finer distinction between BSS (exported data) and Text (data that won't be modified by the program). But that is mostly important for compiler and linker. Note that like binary code, data are also dependant on target (big-endian, little-endian, 32bits, 64bits).
  • 程序中该部分使用的静态数据(如常量字符串等)。您可以更好地区分BSS(导出数据)和Text(程序不会修改的数据)。但这对编译器和链接器来说是最重要的。注意,与二进制代码一样,数据也依赖于目标(big-endian, little-endian, 32bit, 64bit)。
  • tables of symbols exported by this part of the program (mostly functions entry points)
  • 程序这一部分导出的符号表(主要是函数入口点)
  • tables of external symbols used by this part of the program
  • 程序这一部分使用的外部符号的表

When objects will be linked together the parts of the code that refers to external symbols will be replaced by actual values (well, that is still oversimplified, there is a last part that will be done at loading time when running the program, but that's the idea).

当对象被连接在一起时,引用外部符号的代码部分将被实际值所取代(好吧,这仍然是过度简化的,在运行程序时加载时将完成最后一部分,但这就是我们的想法)。

The object file may also contain more symbols information that strictly necessary for resolving imports and export (useful for debug). That information can be removed using the strip command.

对象文件还可以包含更多的符号信息,这些信息对于解析导入和导出是非常必要的(对于调试非常有用)。可以使用strip命令删除这些信息。

#3


5  

First read the wiki page. You can use objdump to examine such a file :)

首先阅读维基页面。您可以使用objdump来检查这样的文件:)

#4


3  

The object file is the compiled source.

对象文件是编译后的源文件。

This means that it's machine code, which is dependent on the target platform (you can compile for Unix on Windows if you really want to) and the compiler used. Different compilers will produce different machine code from the same source file.

这意味着它的机器代码依赖于目标平台(如果你真的想的话,你可以在Windows上编译Unix)和编译器。不同的编译器将从相同的源文件生成不同的机器代码。

#5


3  

Use the file command for things like this. It's an ELF object file on a modern Linux system. E.g. if compiled for 32-bit x86.

使用file命令处理类似的事情。它是一个现代Linux系统上的ELF对象文件。例如,如果编译为32位x86。

ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

In contrast, a dynamically linked executable might look like:

相反,动态链接的可执行文件可能看起来像:

ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

To see headers, including section names, you can use:

要查看标题,包括部分名称,您可以使用:

objdump -x any_name.o

To disassemble:

拆卸:

objdump -d any_name.o

#6


3  

First, binary files can be opened! Don't be scared of it, you need just the right tools! Being binary data, a text editor is not the right tool of course; a right tool could be a hex editor, or an advanced editor like emacs, or a tool that instead of simply "outputting" bytes in their "hex" representation and letting you alone with your interpretation of the data, knows that particular format and "interprets" the data properly, at some level (e.g. GIMP interprets a PNG file as an image and shows it, a PNG analyser will "decompose" the data inside PNG sections showing telling you the flags in certain bytes, ...etc).

首先,可以打开二进制文件!不要害怕,你需要正确的工具!作为二进制数据,文本编辑器当然不是正确的工具;合适的工具可以是十六进制编辑器,或一个高级编辑像emacs,或一个工具,而不是简单地“输出”字节的十六进制表示和让你单独与数据的解释,知道特定的格式和“解释”正确的数据,在某种程度上(如GIMP解释一个PNG文件作为图像显示,PNG分析器将“分解”里面的数据显示PNG部分告诉你的旗帜在某些字节,…等)。

In your case, the general answer is that the object file contains your compiled code (and data), plus all extra informations needed by the linker, and eventually more.

在您的示例中,一般的答案是,对象文件包含编译后的代码(和数据),以及链接器所需的所有额外信息,最后还有更多信息。

How these informantions are "organized" and in some case in what the "eventually more" consists, it depends on the specific object format. Some wikipedia links listing some of the possibilities are this, this, this, this ...

这些信息是如何“组织”的,在某些情况下,“最终更多”是什么,这取决于具体的对象格式。一些维基百科的链接列出了一些可能性,比如这个,这个,这个…

Each of these may have its tools to analyse the content; e.g. readelf for ELF, objdump for several formats (try objdump -i) depending on how it was compiled.

每一个都可能有自己的工具来分析内容;例如:readelf, objdump用于多种格式(尝试dump -i),这取决于它是如何编译的。

#7


1  

The file contains binary data which must be run through a linker to generate an executable. It is essentially a bunch of machine code instructions with named sections (corresponding to your functions). From wikipedia's 'Object File' article:

该文件包含二进制数据,必须通过链接器运行才能生成可执行文件。它本质上是一堆带有命名部分的机器代码指令(对应于您的函数)。从维基百科的“目标文件”文章:

In computer science, an object file is an organized collection of separate, named sequences of machine code[citation needed]. Each sequence, or object, typically contains instructions for the host machine to accomplish some task, possibly accompanied by related data and metadata (e.g. relocation information, stack unwinding information, comments, program symbols, debugging or profiling information). A linker is typically used to generate an executable or library by combining parts of object files.

在计算机科学中,目标文件是一组单独的、命名的机器代码序列的有组织的集合。每个序列或对象通常包含主机完成某些任务的指令,可能伴随着相关数据和元数据(例如,重新定位信息、堆栈展开信息、注释、程序符号、调试或分析信息)。链接器通常用于通过合并对象文件的部分来生成可执行文件或库。

#8


1  

In the GNU compilation environment you can look with objdump both in the executable and in the object file.

在GNU编译环境中,您可以在可执行文件和对象文件中查找objdump。

As you can see the object contains only the code of functions declared/referenced within the compiled file (the file contains only the main function with a scanf call and a printf call).

如您所见,该对象只包含已编译文件中声明/引用的函数代码(该文件仅包含主函数,其中包含一个scanf调用和一个printf调用)。

$ objdump -t scanf_sample.o

scanf_sample.o:     file format pe-i386

SYMBOL TABLE:
[  0](sec -2)(fl 0x00)(ty   0)(scl 103) (nx 1) 0x00000000 scanf_sample.c
File
[  2](sec  1)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x00000000 _main
[  3](sec  1)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .text
AUX scnlen 0x91 nreloc 9 nlnno 0
[  5](sec  2)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .data
AUX scnlen 0x0 nreloc 0 nlnno 0
[  7](sec  3)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .bss
AUX scnlen 0x0 nreloc 0 nlnno 0
[  9](sec  4)(fl 0x00)(ty   0)(scl   3) (nx 1) 0x00000000 .rdata
AUX scnlen 0x54 nreloc 0 nlnno 0
[ 11](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 1) 0x00000000 ___main
AUX tagndx 0 ttlsiz 0x0 lnnos 0 next 0
[ 13](sec  0)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 __alloca
[ 14](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x00000000 _memset
[ 15](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x00000000 _scanf
[ 16](sec  0)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x00000000 _printf

If you use objdump on an executable you can see a lot more functions (besides those found inside the object). This proves that the object file contains only the functions defined in the source file with references to other functions. Those references will be resolved at linking phase.

如果您在可执行文件上使用objdump,您可以看到更多的函数(除了在对象中发现的)。这证明对象文件只包含源文件中定义的函数,并引用其他函数。这些引用将在链接阶段解析。

Read more about linking, compilation and objects.

阅读更多关于链接,编译和对象。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/06/15/a8d24d176b44885ca9d25c521993abbc.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com