是“.o”。文件“可加载”?

发布于 2025-01-07 17:23:19 字数 645 浏览 4 评论 0原文

我一直在阅读 John R. Levine 的链接器和加载器,并且我读到目标文件的属性将包括以下一项或多项。

  1. 文件应该是可链接的
  2. 文件应该是可加载的
  3. 文件应该是可执行的

现在,考虑这个例子:

#include<stdio.h>
int main() {
  printf("testing\n");
  return 0;
}

我将编译并链接它:

$ gcc -c t.c
$ gcc -o t t.o

我尝试使用 objdump 检查 to ,其类型显示为RELto 满足哪些所有属性?我相信它是可链接的,不可执行的。我本以为它是不可加载的(除非您从 .o 文件创建 .so 文件);然而类型REL意味着它应该被重定位,并且重定位只会在加载的上下文中发生,所以我在这里感到困惑。 我的疑问总结如下:-

  1. “.o”文件可加载吗?
  2. 阅读有关“.o”、“.so”文件中存在的部分的资源 - 差异等?

I have been reading John R. Levine's Linkers and Loaders and I read that the properties of an object file will include one or more of the following.

  1. file should be linkable
  2. file should be loadable
  3. file should be executable

Now, considering this example:

#include<stdio.h>
int main() {
  printf("testing\n");
  return 0;
}

Which I would compile and link with:

$ gcc -c t.c
$ gcc -o t t.o

I tried inspecting t.o using objdump and its type shows up as REL. What all properties does t.o satisfy? I believe that its linkable, non-executable. I would have believed that its not loadable(unless you create an .so file from the .o file); however the type REL means that its supposed to be relocated, and relocation would occur only in the context of loading, so I'm having a confusion here.
My doubts summarized :-

  1. Are ".o" files loadable?
  2. Reading resources regarding the sections present in a ".o", ".so" file - differences etc?

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

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

发布评论

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

评论(2

情绪 2025-01-14 17:23:19

目标文件(即扩展名为 .o 的文件)不可加载。这是因为它缺乏有关如何解析其中所有符号的关键信息:在这种情况下,println 符号尤其需要附加信息。 (C 编译器不会将库标识绑定到它们创建的目标文件中,这有时甚至很有用。)

当您将目标文件链接到共享库 (.so) 时,您就添加了该绑定。通常,您还将许多对象文件分组在一起并解析它们之间的引用(以及一些更深奥的东西)。这样就可以加载结果,因为加载器可以解析引用并加载它还不知道的依赖项。

从那里到可执行文件通常只需添加操作系统定义的程序引导程序即可。这是操作系统将通过调用启动程序运行的一小段代码,它通常通过加载程序的其余部分和依赖项,然后使用有关参数的信息调用 main() 来工作。 (如果 main 返回,它还负责干净地退出。)

An object file (i.e., a file with the .o extension) is not loadable. This is because it lacks critical information about how to resolve all the symbols within it: in this case, the println symbol in particular would need additional information. (C compilers do not bind library identities into the object files they create, which is occasionally even useful.)

When you link the object file into a shared library (.so), you are adding that binding. Typically, you're also grouping a number of object files together and resolving references between them (plus a few more esoteric things). That then makes the result possible to load, since the loader can then just do resolution of references and loading of dependencies that it doesn't already know about.

Going from there to executable is typically just a matter of adding on the OS-defined program bootstrap. This is a small piece of code that the OS will start the program running by calling, and it typically works by loading the rest of the program and dependencies and then calling main() with information about the arguments. (It's also responsible for exiting cleanly if main returns.)

柠檬色的秋千 2025-01-14 17:23:19

只是为了设置上下文此链接声明类似的内容(强调仅供阅读);

文件可以可链接,用作链接编辑器或链接的输入
装载机。它是可执行,能够被加载到
内存并作为程序运行,可加载,能够被加载
作为库和程序一起进入内存,或者它们的任意组合
三者。

.o 文件是链接器目标文件,根据此定义,它不可执行并且绝对可链接。可加载是一个更艰难的选择,但由于 .o 文件在没有一些绝对不是跨平台的技巧的情况下是不可加载的,我想说的精神是它是不可加载

Just to set the context this link states somethings similar (emphasis for readability only);

A file may be linkable, used as input by a link editor or linking
loader. It my be executable, capable of being loaded into
memory and run as a program, loadable, capable of being loaded
into memory as a library along with a program, or any combination of
the three.

A .o file is a linker object file, which is according to this definition not executable and definitely linkable. Loadable is a tougher call, but since .o files are not loadable without some definitely not cross platform trickery, I'd say the spirit is that it's not loadable.

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