How To Write Shared Libraries(7)

1.5.1 The Relocation Process

Relocation in this context means adjusting the application and the DSOs, which are loaded as the dependencies, to their own and all other load addresses. There are two kinds of dependencies:
重定位意味着调整应用和DSO的的内容到自己或者其他地址。两种依赖:

• Dependencies to locations which are known to be in the own object. These are not associated with a specific symbol since the linker knows the relative position of the location in the object.
依赖自身包含的容。由于知道相对位置没有特殊内容。

Note that applications do not have relative reloca-tions since the load address of the code is known at link-time and therefore the static linker is able to perform the relocation.
应用没有重定位的内容,所有链接时可以使用静态连接完成。

• Dependencies based on symbols. The reference of the definition is generally, but not necessarily, in a different object than the definition.
依赖符号的内容。一般是定义的引用,但在不同的对象中不是必须的。

The implementation of relative relocations is easy. The linker can compute the offset of the target destination in the object file at link-time. To this value the dynamic linker only has to add the load address of the object and store the result in the place indicated by the relocation. At runtime the dynamic linker has to spend only a very small and constant amount of time which does not increase if more DSOs are used.
重定位的实现很简单。链接器可以在链接时计算目标的偏移。
对于动态链接器只需要添加对象的加载地址存储地址到相应标记的地方。运行时动态链接器不得不增加一点常量时间基本不会增加时间。

The relocation based on a symbol is much more complicated. The ELF symbol resolution process was designed very powerful so that it can handle many different prob- lems. All this powerful functionality adds to the com- plexity and run-time costs, though. Readers of the fol- lowing description might question the decisions which led to this process. We cannot argue about this here; read- ers are referred to discussions of ELF. Fact is that symbol relocation is a costly process and the more DSOs participate or the more symbols are defined in the DSOs, the longer the symbol lookup takes.
基于标记的重定位很复杂。ELF语法解析设计的很强可以处理多种不同问题。这些强大的功能增加了复杂性和运行时开销。阅读后面的内容可能会疑问什么导致这样处理。这里不讨论。读者可以参考ELF讨论内容。实际是更多的DSO和更多的DSO内容标记导致更大的查找表,从而导致重定位耗时更多。

The result of any relocation will be stored somewhere in the object with the reference. Ideally and generally the target location is in the data segment. If code is in- correctly generated by the user, compiler, or linker relo- cations might modify text or read-only segments. The dynamic linker will handle this correctly if the object is marked, as required by the ELF specification, with the DF TEXTREL set in the DT FLAGS entry of the dynamic section (or the existence of the DT TEXTREL flag in old binaries). But the result is that the modified page can- not be shared with other processes using the same object. The modification process itself is also quite slow since the kernel has to reorganize the memory handling data structures quite a bit.
结果就是所有的重定位都会存储。理想的和一般的目标是自爱数据段。如果代码被用户正确使用,编译和链接只需要修改代码段或者只读段。动态链接器会处理这些被ELF内部标记识别的内容。这导致修改页无法共享。这也导致程序本身由于内核需要处理内存数据而变得很慢。

你可能感兴趣的:(How To Write Shared Libraries(7))