什么是“对象文件”中的“对象”,为什么这样叫?(复制)

时间:2022-05-26 05:07:31

This question already has an answer here:

这个问题已经有了答案:

I was asked a question: "What is an 'object file'?".

有人问我一个问题:“什么是‘对象文件’?”

After looking at Wiki, I only know that it contains objects.

在查看了Wiki之后,我只知道它包含了对象。

But what are those objects and why someone called them that way?

但是这些物体是什么?为什么有人这样称呼它们?

4 个解决方案

#1


17  

Object files (or object code) are machine code files generated by a compiler from source code.

对象文件(或对象代码)是编译器从源代码生成的机器代码文件。

The difference with an executable is that the object file isn't linked, so references to functions, symbols, etc aren't defined yet (their memory addresses is basically left blank).

与可执行文件的不同之处在于,对象文件没有链接,所以对函数、符号等的引用还没有定义(它们的内存地址基本上是空的)。

When you compile a C file with GCC:

使用GCC编译C文件时:

gcc -Wall -o test test.c

Here you are compiling AND linking. So you'll got an executable, containing all the memory addresses references for the symbols it contains (libraries, headers, etc).

这里是编译和链接。因此,您将得到一个可执行文件,其中包含了所有的内存地址,包括它所包含的符号(库、头文件等)。

But when you do this:

但是当你这样做的时候:

gcc -Wall -o test.o -c test.c

You'll produce and object file. It's also machine code, but it will need to be linked in order to produce an executable, or a library.

您将生成和对象文件。它也是机器代码,但是为了生成可执行文件或库,它需要被链接。

When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product.

当您有一个包含许多C文件的项目(例如)时,您将把每个文件编译成目标代码,然后将所有对象文件链接在一起,以生成最终的产品。

For instance:

例如:

gcc -Wall -o foo.o -c foo.c              // Object file for foo.c
gcc -Wall -o bar.o -c bar.c              // Object file for bar.c
gcc -Wall -o main.o -c main.c            // Object file for main.c
gcc -Wall -o software foo.o bar.o main.o // Executable (foo + bar + main)

The term object stands here for sequences of unlinked machine code (basically). An object file contains objects.

术语对象在这里表示未链接的机器代码序列(基本上)。对象文件包含对象。

You asked: why is this call that way. I can't really answer. Why is "blue" named "blue"? ; )

你问:为什么这样称呼?我不能回答。为什么“蓝色”被命名为“蓝色”?,)

It's just the term used since... well, decades...

这只是因为…好了,几十年……

For information, the GCC Internals documentation only defines object code as:

有关信息,GCC内部文件只将对象代码定义为:

The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.

作品的“源代码”是对作品进行修改的首选形式。“对象代码”指工作的任何非源代码形式。

Pretty vague about the historical reason...

很模糊的历史原因…

I simply hope you now understand better what is an object file. I think it's more important than knowing why it's called like that, as words are just, well, words...

我只是希望您现在能更好地理解什么是对象文件。我认为这比知道为什么要这样叫更重要,因为单词就是,嗯,单词……

#2


7  

I believe the name has something to do with making a distinction between:

我相信这个名字与区分两者有关系:

  • code for humans -- source code
  • 人类代码——源代码
  • code for machines -- object code
  • 机器代码——对象代码。

Object files contain:

对象文件包含:

  • Header information: overall information about the file, such as the size of the code, name of the source file it was translated from, and creation date.
  • 头信息:关于文件的总体信息,例如代码的大小、被翻译的源文件的名称和创建日期。
  • Object code: Binary instructions and data generated by a compiler or assembler.
  • 对象代码:编译器或汇编器生成的二进制指令和数据。
  • Relocation: A list of the places in the object code that have to be fixed up when the linker changes the addresses of the object code.
  • 重新定位:当链接器更改目标代码的地址时,必须修复的对象代码中的位置列表。
  • Symbols: Global symbols defined in this module, symbols to be imported from other modules or defined by the linker.
  • 符号:在此模块中定义的全局符号,从其他模块导入或由链接器定义的符号。
  • Debugging information: Other information about the object code not needed for linking but of use to a debugger. This includes source file and line number information, local symbols, descriptions of data structures used by the object code such as C structure definitions.
  • 调试信息:关于对象代码的其他信息,这些代码不是用来链接的,而是用于调试器的。这包括源文件和行号信息、本地符号、对象代码使用的数据结构的描述,如C结构定义。

Source: here

来源:这里

#3


3  

An object file is binary representation of source(text) file. It's a collection of various sections segragating type of data in:

对象文件是源(文本)文件的二进制表示形式。它是数据的不同部分的集合。

  • text section
  • 文本部分
  • data section
  • 数据部分
  • stack
  • 堆栈
  • heap

Depending on your compiler/environment these may differ.

根据编译器/环境的不同,它们可能会有所不同。

E.g. on *nix systems:

例如在* nix系统:

objdump -d a.out <--- provide we compiled a.cpp

objdump - d。out <---提供我们编译a.cpp

disassembly of section .init:

08048278 <_init>:
 8048278:       55                      push   %ebp
 8048279:       89 e5                   mov    %esp,%ebp
 804827b:       83 ec 08                sub    $0x8,%esp
 804827e:       e8 61 00 00 00          call   80482e4 <call_gmon_start>
 8048283:       e8 b3 00 00 00          call   804833b <frame_dummy>
 8048288:       e8 9f 01 00 00          call   804842c <__do_global_ctors_aux>
 804828d:       c9                      leave
 804828e:       c3                      ret
Disassembly of section .plt:

08048290 <puts@plt-0x10>:
 8048290:       ff 35 78 95 04 08       pushl  0x8049578
 8048296:       ff 25 7c 95 04 08       jmp    *0x804957c
 804829c:       00 00                   add    %al,(%eax)
        ...

080482a0 <puts@plt>:
 80482a0:       ff 25 80 95 04 08       jmp    *0x8049580
 80482a6:       68 00 00 00 00          push   $0x0
 80482ab:       e9 e0 ff ff ff          jmp    8048290 <_init+0x18>

080482b0 <__libc_start_main@plt>:
 80482b0:       ff 25 84 95 04 08       jmp    *0x8049584
 80482b6:       68 08 00 00 00          push   $0x8
 80482bb:       e9 d0 ff ff ff          jmp    8048290 <_init+0x18>
Disassembly of section .text:

The various call commands here are then liked to the various libraries to call the actual functions.

这里的各种调用命令被喜欢到各种库中,以调用实际的函数。

#4


-4  

According to the page you linked, 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).

根据所链接的页面,每个序列或对象通常包含主机完成某些任务的指令,可能伴随着相关数据和元数据(例如,重新定位信息、堆栈展开信息、注释、程序符号、调试或分析信息)。

Basically, each object in the object file is a function, and the relevant info for the linker to include it into the full program.

基本上,对象文件中的每个对象都是一个函数,链接器将其包含到整个程序中的相关信息。

#1


17  

Object files (or object code) are machine code files generated by a compiler from source code.

对象文件(或对象代码)是编译器从源代码生成的机器代码文件。

The difference with an executable is that the object file isn't linked, so references to functions, symbols, etc aren't defined yet (their memory addresses is basically left blank).

与可执行文件的不同之处在于,对象文件没有链接,所以对函数、符号等的引用还没有定义(它们的内存地址基本上是空的)。

When you compile a C file with GCC:

使用GCC编译C文件时:

gcc -Wall -o test test.c

Here you are compiling AND linking. So you'll got an executable, containing all the memory addresses references for the symbols it contains (libraries, headers, etc).

这里是编译和链接。因此,您将得到一个可执行文件,其中包含了所有的内存地址,包括它所包含的符号(库、头文件等)。

But when you do this:

但是当你这样做的时候:

gcc -Wall -o test.o -c test.c

You'll produce and object file. It's also machine code, but it will need to be linked in order to produce an executable, or a library.

您将生成和对象文件。它也是机器代码,但是为了生成可执行文件或库,它需要被链接。

When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product.

当您有一个包含许多C文件的项目(例如)时,您将把每个文件编译成目标代码,然后将所有对象文件链接在一起,以生成最终的产品。

For instance:

例如:

gcc -Wall -o foo.o -c foo.c              // Object file for foo.c
gcc -Wall -o bar.o -c bar.c              // Object file for bar.c
gcc -Wall -o main.o -c main.c            // Object file for main.c
gcc -Wall -o software foo.o bar.o main.o // Executable (foo + bar + main)

The term object stands here for sequences of unlinked machine code (basically). An object file contains objects.

术语对象在这里表示未链接的机器代码序列(基本上)。对象文件包含对象。

You asked: why is this call that way. I can't really answer. Why is "blue" named "blue"? ; )

你问:为什么这样称呼?我不能回答。为什么“蓝色”被命名为“蓝色”?,)

It's just the term used since... well, decades...

这只是因为…好了,几十年……

For information, the GCC Internals documentation only defines object code as:

有关信息,GCC内部文件只将对象代码定义为:

The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.

作品的“源代码”是对作品进行修改的首选形式。“对象代码”指工作的任何非源代码形式。

Pretty vague about the historical reason...

很模糊的历史原因…

I simply hope you now understand better what is an object file. I think it's more important than knowing why it's called like that, as words are just, well, words...

我只是希望您现在能更好地理解什么是对象文件。我认为这比知道为什么要这样叫更重要,因为单词就是,嗯,单词……

#2


7  

I believe the name has something to do with making a distinction between:

我相信这个名字与区分两者有关系:

  • code for humans -- source code
  • 人类代码——源代码
  • code for machines -- object code
  • 机器代码——对象代码。

Object files contain:

对象文件包含:

  • Header information: overall information about the file, such as the size of the code, name of the source file it was translated from, and creation date.
  • 头信息:关于文件的总体信息,例如代码的大小、被翻译的源文件的名称和创建日期。
  • Object code: Binary instructions and data generated by a compiler or assembler.
  • 对象代码:编译器或汇编器生成的二进制指令和数据。
  • Relocation: A list of the places in the object code that have to be fixed up when the linker changes the addresses of the object code.
  • 重新定位:当链接器更改目标代码的地址时,必须修复的对象代码中的位置列表。
  • Symbols: Global symbols defined in this module, symbols to be imported from other modules or defined by the linker.
  • 符号:在此模块中定义的全局符号,从其他模块导入或由链接器定义的符号。
  • Debugging information: Other information about the object code not needed for linking but of use to a debugger. This includes source file and line number information, local symbols, descriptions of data structures used by the object code such as C structure definitions.
  • 调试信息:关于对象代码的其他信息,这些代码不是用来链接的,而是用于调试器的。这包括源文件和行号信息、本地符号、对象代码使用的数据结构的描述,如C结构定义。

Source: here

来源:这里

#3


3  

An object file is binary representation of source(text) file. It's a collection of various sections segragating type of data in:

对象文件是源(文本)文件的二进制表示形式。它是数据的不同部分的集合。

  • text section
  • 文本部分
  • data section
  • 数据部分
  • stack
  • 堆栈
  • heap

Depending on your compiler/environment these may differ.

根据编译器/环境的不同,它们可能会有所不同。

E.g. on *nix systems:

例如在* nix系统:

objdump -d a.out <--- provide we compiled a.cpp

objdump - d。out <---提供我们编译a.cpp

disassembly of section .init:

08048278 <_init>:
 8048278:       55                      push   %ebp
 8048279:       89 e5                   mov    %esp,%ebp
 804827b:       83 ec 08                sub    $0x8,%esp
 804827e:       e8 61 00 00 00          call   80482e4 <call_gmon_start>
 8048283:       e8 b3 00 00 00          call   804833b <frame_dummy>
 8048288:       e8 9f 01 00 00          call   804842c <__do_global_ctors_aux>
 804828d:       c9                      leave
 804828e:       c3                      ret
Disassembly of section .plt:

08048290 <puts@plt-0x10>:
 8048290:       ff 35 78 95 04 08       pushl  0x8049578
 8048296:       ff 25 7c 95 04 08       jmp    *0x804957c
 804829c:       00 00                   add    %al,(%eax)
        ...

080482a0 <puts@plt>:
 80482a0:       ff 25 80 95 04 08       jmp    *0x8049580
 80482a6:       68 00 00 00 00          push   $0x0
 80482ab:       e9 e0 ff ff ff          jmp    8048290 <_init+0x18>

080482b0 <__libc_start_main@plt>:
 80482b0:       ff 25 84 95 04 08       jmp    *0x8049584
 80482b6:       68 08 00 00 00          push   $0x8
 80482bb:       e9 d0 ff ff ff          jmp    8048290 <_init+0x18>
Disassembly of section .text:

The various call commands here are then liked to the various libraries to call the actual functions.

这里的各种调用命令被喜欢到各种库中,以调用实际的函数。

#4


-4  

According to the page you linked, 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).

根据所链接的页面,每个序列或对象通常包含主机完成某些任务的指令,可能伴随着相关数据和元数据(例如,重新定位信息、堆栈展开信息、注释、程序符号、调试或分析信息)。

Basically, each object in the object file is a function, and the relevant info for the linker to include it into the full program.

基本上,对象文件中的每个对象都是一个函数,链接器将其包含到整个程序中的相关信息。