C++ 目标文件包含什么?

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

What does an object file contain?

c++ccompilation

提问by Vijay

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 上使用的编译器?

采纳答案by Roddy

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.

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

回答by kriss

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.

有几种标准化格式(Unix 上的 COFF、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).
  • 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
  • 编译产生的二进制代码(对于目标处理器)
  • 程序该部分使用的静态数据(如常量字符串等)。您可以更好地区分 BSS(导出的数据)和文本(不会被程序修改的数据)。但这对于编译器和链接器来说最为重要。请注意,与二进制代码一样,数据也依赖于目标(大端、小端、32 位、64 位)。
  • 这部分程序导出的符号表(主要是函数入口点)
  • 这部分程序使用的外部符号表

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 命令删除该信息。

回答by Daniel B?lu??

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

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

回答by Matthew Flaschen

Use the file command for things like this. It's an ELFobject 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

回答by ChrisF

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 编译)和使用的编译器。不同的编译器会从同一个源文件生成不同的机器代码。

回答by ShinTakezou

First, binary files canbe 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. readelffor ELF, objdumpfor several formats (try objdump -i) depending on how it was compiled.

其中每一个都可能有分析内容的工具;例如readelf对于ELF,objdump对于几种格式(尝试objdump -i),取决于它是如何编译的。

回答by INS

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 morefunctions (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, compilationand objects.

阅读有关链接编译对象的更多信息。

回答by soulmerge

The file contains binary data which must be run through a linkerto 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.

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