什么是“对象”?在“目标文件”中为什么这样称呼?

发布于 2024-12-17 20:37:05 字数 159 浏览 5 评论 0原文

有人问我一个问题:“什么是‘目标文件’?”。

查看Wiki后,我只知道它包含对象

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫多说 2024-12-24 20:37:05

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

与可执行文件的区别在于,目标文件没有链接,因此尚未定义对函数、符号等的引用(它们的内存地址基本上留空)。

当您使用 GCC 编译 C 文件时:

gcc -Wall -o test test.c

这里您正在编译 AND 链接。因此,您将获得一个可执行文件,其中包含其所包含符号(库、标头等)的所有内存地址引用。

但是当你这样做时:

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

你将生成目标文件。它也是机器代码,但需要链接才能生成可执行文件或库。

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

例如:

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)

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

你问:为什么这样称呼。我实在无法回答。 “蓝”为什么叫“蓝”? ; )

这只是自...好吧,数十年以来使用的术语...

仅供参考,GCC 内部文档仅将目标代码定义为:

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

关于历史原因相当模糊......

我只是希望您现在更好地理解什么是目标文件。我认为这比知道为什么这么称呼更重要,因为单词只是,嗯,单词......

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 -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.

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:

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...

风情万种。 2024-12-24 20:37:05

我相信这个名称与区分以下内容有关:

  • 人类代码 - 源代码
  • 机器代码 - 目标代码

对象文件包含:

  • 标题信息:有关文件的总体信息,例如代码的大小、翻译源文件的名称以及
    创建日期。
  • 目标代码:由编译器或汇编器生成的二进制指令和数据。
  • 重定位:当链接器更改目标代码的地址时必须修复的目标代码中的位置列表。
  • 符号:本模块中定义的全局符号、从其他模块导入的符号或由链接器定义的符号。
  • 调试信息:有关链接不需要但可用于调试器的目标代码的其他信息。这包括源文件
    和行号信息、局部符号、数据描述
    目标代码使用的结构,例如 C 结构定义。

来源:此处

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.

Source: here

归途 2024-12-24 20:37:05

目标文件是源(文本)文件的二进制表示。它是各个部分的集合,将数据类型分隔在以下位置:

  • 文本部分
  • 数据部分
  • 堆栈

根据您的编译器/环境,这些可能会有所不同。

例如在 *nix 系统上:

objdump -d a.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:

这里的各种调用命令然后被各种库调用以调用实际的函数。

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:

objdump -d a.out <--- provide we compiled 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.

入怼 2024-12-24 20:37:05

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文