是“.o”。文件“可加载”?
我一直在阅读 John R. Levine 的链接器和加载器,并且我读到目标文件的属性将包括以下一项或多项。
- 文件应该是可链接的
- 文件应该是可加载的
- 文件应该是可执行的
现在,考虑这个例子:
#include<stdio.h>
int main() {
printf("testing\n");
return 0;
}
我将编译并链接它:
$ gcc -c t.c $ gcc -o t t.o
我尝试使用 objdump 检查 to
,其类型显示为REL
。 to
满足哪些所有属性?我相信它是可链接的,不可执行的。我本以为它是不可加载的(除非您从 .o 文件创建 .so 文件);然而类型REL
意味着它应该被重定位,并且重定位只会在加载的上下文中发生,所以我在这里感到困惑。 我的疑问总结如下:-
- “.o”文件
可加载
吗? - 阅读有关“.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.
- file should be linkable
- file should be loadable
- 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 :-
- Are ".o" files
loadable
? - Reading resources regarding the sections present in a ".o", ".so" file - differences etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目标文件(即扩展名为 .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.)只是为了设置上下文此链接声明类似的内容(强调仅供阅读);
.o 文件是链接器目标文件,根据此定义,它不可执行并且绝对可链接。可加载是一个更艰难的选择,但由于 .o 文件在没有一些绝对不是跨平台的技巧的情况下是不可加载的,我想说的精神是它是不可加载。
Just to set the context this link states somethings similar (emphasis for readability only);
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.