链接中的重定位条目(C编程) [英] relocation entry in linking (C programming)

查看:376
本文介绍了链接中的重定位条目(C编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在努力理解可重定位目标文件中的重定位条目,假设我有一个简单的C程序:

still struggling to understand the relocation entry in Relocatable Object Files, let's say I have this simple C program:

//main1.c

void functionTest();

functionTest(){
   ...
}

int main()
{
   functionTest();
   return 0;
}

我的问题是:

Q1.由于main1知道一切,因此main1.o的.rel.text或.rel.data节中没有重定位条目,我的理解正确吗?

Q1. since everything is known to main1, so there is no relocation entry in .rel.text or .rel.data section of main1.o, is my understanding correct?

Q2.下面的图片说明了DLL的工作原理,

Q2. below is a picture illustrates how DLL works,

对于libc.so来说,一切都是已知的(就像main1一样具有所有定义),那么为什么libc.so中仍然有重定位条目?我了解符号表信息需要复制,因为它们存在,请问如何复制不存在的信息?

for libc.so, everything is known(it has all definitions just like main1), so why there is still relocation entries in libc.so? I can understand the symbol table information needs to be copied because they exist, how can you copy something that doesn't exist?

Q3.让我们在下面说一下重定位条目的结构;

Q3. lets say below is the relocation entry structure;

typedef struct {
   int offset; /* Offset of the reference to relocate */
   int symbol:24, /* Symbol the reference should point to */
   type:8; /* Relocation type */
} Elf32_Rel;

所以我的理解是main2.o中已经有一个针对printf()的重定位条目,因此偏移量将是调用方函数的偏移量,例如8或9个字节,符号将为'printf',类型为R_386_PC32,因此如果需要从libc.so复制另一个文件到main2.o,那么该重定位条目的结构是什么?

so my understanding is there is already a relocation entry in main2.o for printf(), so the offset will be something like 8 or 9 bytes offset from caller function, symbol will be 'printf', type is R_386_PC32, so if there is another one needs to be copied from libc.so to main2.o, what's the structure of that relocation entry?

推荐答案

问题1:是的,如果您编译问题中的main1.c,它将构建 without 不需要链接任何东西,因为它没有使用在其他地方定义的函数.

Q1: Yes, if you compile the main1.c in your question, it will build without the need to link in anything, because it's not using functions that are defined elsewhere.

第二季度:该图不适用于构建main1.c,因为main1.c不使用外部功能.但是,在确实调用了例如printf()的程序中,发生了这样的情况:该图显示了放置了 about libc.so的重定位条目. 插入 main2.o.您问为什么libc.so中仍然有重定位条目?"但将重定位条目 not 放入 libc.so;它们被放入main2.o,并且它们引用libc.so中.

Q2: That diagram won't apply to building main1.c, because main1.c does not use external functions. But, in a program that does have a call to, say, printf(), here's what's going on: the diagram shows that relocation entries about libc.so are being placed into main2.o. You ask "why there is still relocation entries in libc.so?" but the relocation entries are not being put into libc.so; they are being put into main2.o, and they refer to things in libc.so.

第二季度后续活动#1:当您说对于libc.so,一切都已知"时,这是正确的,仅在 libc.so之内.使用libc.so中定义的功能的任何人都会知道该功能的定义, 直到链接发生 .这就是ld的功能:将参考信息库复制到正在构建的程序 中,如图main2所示.参考信息允许执行main2的内核还将也加载 libc.so到内存中,以便执行可以从main2代码流到libc.so代码,然后又回到main2main2处调用其定义/代码位于libc.so中的函数.

Q2 follow-up #1: When you say "for libc.so, everything is known", that is true only within libc.so. Anything that uses a function defined in libc.so will not know how that function is defined, until linking takes place. That's the function of ld: to copy reference info from a library like libc.so into a program being built, like main2 in the diagram. The reference info allows the kernel executing main2 to also load libc.so into memory in such a way that execution can flow from the main2 code over to the libc.so code and back to main2 wherever main2 calls a function whose definition / code resides in libc.so.

第3季度:我认为最好的表达方式是:用于填充 main2.o 中的重定位结构的信息来自 libc.so.我说重定位条目是从libc.so复制的,这就是我的意思:关于目标(例如printf())的信息是从libc.so获取的,用于为main2.o中的重定位条目提供值,其目的是告诉加载程序从何处加载printf()的代码.

Q3: I think the best way to put it is this: The information that is used to populate the relocation structure within main2.o comes from libc.so. Where I say that relocation entries are copied from libc.so, that's what I mean: information about the target (e.g. printf()) is taken from libc.so and used to provide values for the relocation entry in main2.o whose purpose is to tell the loader where to load the code for printf() from.

第3季度后续活动#1:还有另一种含义,其中libc.so具有重定位条目:构建libc.so的事物将重定位条目添加到了libc.so,因此任何想要的东西使用其(可导出的)函数和变量可以这样做. 这些不需要复制到任何地方.构建目标文件的一部分是为其他程序可能使用的内部内容创建信息.并且,构建程序的一部分是填充有关外部所使用事物的信息.但是在我看来,该图仅表示要向main2.o添加关于 libc.solibvector.so的信息,以便当被执行.

Q3 follow-up #1: There is another sense in which libc.so has relocation entries: the thing that built libc.so added relocation entries to libc.so, so that anything that wants to use its (exportable) functions and variables can do so. These don't need to be copied anywhere. Part of building an object file is to create information for internal things that other programs might use; and, part of building a program is to populate the information about external things that it makes use of. But the diagram looks to me like it's only meant to show that information about libc.so and libvector.so are added to main2.o so that the loader can load all the needed code into memory when main2 is executed.

这篇关于链接中的重定位条目(C编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆