简体   繁体   English

了解Linux中的动态库加载

[英]Understanding Dynamic Library loading in Linux

I am trying to understand Dynamic Library loading in Linux from here [1] and want to clarify the concept. 我试图从这里[1]了解Linux中的动态库加载,并想澄清一下这个概念。 Concretely, when a dynamic library is loaded in a process in a Linux environment, it is loaded at any point in the address space. 具体而言,在Linux环境中的进程中加载​​动态库时,它会在地址空间中的任意位置加载。 Now, a library has a code segment, and a data segment. 现在,一个库具有一个代码段和一个数据段。 The code segment's address is not defined pre-linking so it is 0x0000000 while for data segment, some number is defined to be an address. 未预先定义代码段的地址,因此它是0x0000000,而对于数据段,则将某个数字定义为地址。

But here is the trick, this address of data segment is not actually the true address. 但这是技巧,数据段的此地址实际上不是真实地址。 Actually, at whatever position code segment is loaded, data segment's pre-defined address is added to it. 实际上,无论在什么位置加载代码段,都会将数据段的预定义地址添加到其中。

Am I correct here? 我在这里正确吗?

One more thing from the referenced article. 被引用的文章还有另外一件事。 What does this statement mean? 这句话是什么意思?

However, we have the constraint that the shared library must still have a unqiue data instance in each process. 但是,我们有一个限制,那就是共享库在每个进程中仍然必须有一个unueue数据实例。 While it would be possible to put the library data anywhere we want at runtime, this would require leaving behind relocations to patch the code and inform it where to actually find the data — destroying the always read-only property of the code and thus sharability. 尽管可以在运行时将库数据放置在我们想要的任何位置,但这将需要留下重定位以修补代码并告知其实际查找数据的位置—破坏了代码的始终只读属性,从而破坏了可共享性。

[1] http://www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html [1] http://www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html

Actually, at whatever position code segment is loaded, data segment's pre-defined address is added to it. 实际上,无论在什么位置加载代码段,都会将数据段的预定义地址添加到其中。

Yes. 是。 The "VirtAddr" of the data segment will be added to base address. 数据段的“ VirtAddr”将被添加到基址。

What does this statement mean? 这句话是什么意思?

It means that when library accesses its own static data, we should not use relocations in the library code. 这意味着当库访问其自身的静态数据时,我们不应在库代码中使用重定位。 Otherwise linker may need to patch the binary code, which leads to unsharing some parts of library codes between processes (if process1 loads library lib1 at 0x40000000, and process2 loads lib1 at 0x50000000, their data relocations will be different). 否则,链接程序可能需要修补二进制代码,从而导致在进程之间取消共享库代码的某些部分(如果process1在0x40000000加载库lib1,而process2在0x50000000加载lib1,则它们的数据重定位将有所不同)。

So, different solution is used in real life. 因此,现实生活中使用了不同的解决方案。 Both library code and data are loaded together, and the offset between code and data is fixed for all cases. 库代码和数据都一起加载,并且代码和数据之间的偏移量对于所有情况都是固定的。 There is the "solution" after text you cited: http://www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html 您引用的文字后有“解决方案”: http : //www.technovelty.org/linux/plt-and-got-the-key-to-code-sharing-and-dynamic-libraries.html

As you can see from the above headers, the solution is that the read-write data section is always put at a known offset from the code section of the library. 从上面的标头中可以看到,解决方案是将读写数据节始终放在库的代码节的已知偏移处。 This way, via the magic of virtual-memory, every process sees its own data section but can share the unmodified code. 这样,通过虚拟内存的魔力,每个进程都可以看到自己的数据部分,但是可以共享未修改的代码。 All that is needed to access data is some simple maths; 访问数据所需要做的只是一些简单的数学运算。 address of thing I want = my current address + known fixed offset. 我想要的东西的地址=我的当前地址+已知的固定偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM