How To Write Shared Libraries(8)

1.5.2 Symbol Relocations (1)

The dynamic linker has to perform a relocation for all symbols which are used at run-time and which are not known at link-time to be defined in the same object as the reference. Due to the way code is generated on some ar- chitectures it is possible to delay the processing of some relocations until the references in question are actually used. This is on many architectures the case for calls to functions. All other kinds of relocations always have to be processed before the object can be used. We will ignore the lazy relocation processing since this is just a method to delay the work. It eventually has to be done and so we will include it in our cost analysis. To actu- ally perform all the relocations before using the object is used by setting the environment variable LD BIND NOW to a non-empty value. Lazy relocation can be disabled for an individual object by adding the -z now option to the linker command line. The linker will set the DF BIND NOW flag in the DT FLAGS entry of the dynamic section to mark the DSO. This setting cannot be undone without relinking the DSOs or editing the binary, though, so this option should only be used if it is really wanted.
动态链接器必须完成运行是使用但是链接时无法在所在文件中获取的标记重定位。根据倒霉生成方式,一些会延迟重定位的过程知道真正使用。这是许多架构函数实现的方式。其他的内容需要运行前完成重定位。我们忽略延迟重定位的的内容,这只是延迟工作执行。最终还是需要执行,我们会包含这部分耗时分析。通过设置LD BIND NOW是的所有的重定位过程在使用前完成。通过设置链接参数-z可以禁用延迟绑定。链接器在DSO的dynamic设置DT FLAGS中的DF BIND NOW标记。这个标记不重新链接无法清除,所以慎用。

The actual lookup process is repeated from start for each symbol relocation in each loaded object. Note that there can be many references to the same symbol in differ- ent objects.
查询进程重复的从每个加载对象的开始位置到结束为止执行重定向过程。注意有许多不同对象中重复引用相同的内容。
The result of the lookup can be different for each of the objects so there can be no short cuts except for caching results for a symbol in each object in case more than one relocation references the same symbol. The lookup scope mentioned in the steps below is an ordered list of a subset of the loaded objects which can be different for each object itself. The way the scope is computed is quite complex and not really relevant here so we refer the interested reader to the ELF specification and section 1.5.4. Important is that the length of the scope is normally directly dependent on the number of loaded ob- jects. This is another factor where reducing the number of loaded objects is increasing performance.
每个对象的查找结果可以是不同的,因此除了缓存每个对象中的一个符号的结果(以防多个重定位引用相同的符号)外,不可能有任何捷径。下面步骤中提到的查找范围是已加载对象子集的有序列表,每个对象本身可以不同。计算作用域的方式非常复杂,在这里并不是很相关,所以我们建议感兴趣的读者参考ELF规范和第1.5.4节。重要的是作用域的长度通常直接依赖于加载对象的数量。这是降低加载对象数量可以提高性能的另一个因素。(有道翻译)

There are today two different methods for the lookup process for a symbol. The traditional ELF method proceeds in the following steps:
现在还有两种不同的查找方式。传统方法步骤如下:

  1. Determine the hash value for the symbol name.
  2. In the first/next object in the lookup scope:
    2.a Determine the hash bucket for the symbol using the hash value and the hash table size in the object.
    2.b Get the name offset of the symbol and using it as the NUL-terminated name.
    2.c Compare the symbol name with the reloca- tion name.
    2.d If the names match,compare the version names as well. This only has to happen if both, the reference and the definition, are versioned. It requires a string comparison, too. If the ver- sion name matches or no such comparison is performed, we found the definition we are looking for.
    2.e If the definition does not match,retry with the next element in the chain for the hash bucket.
    2.f If the chain does not contain any further element there is no definition in the current object and we proceed with the next object in the lookup scope.
  3. If there is no further object in the lookup scope the lookup failed.
    1.根据名字获取hash值。
    2.在查找范围的第一个/下一个对象中执行:
    2.a 通过hash值和表的大小确定桶的位置。
    2.b 获取名字的偏移位置当前NULL结尾的字符处理。
    2.c 比较标识名字和重加载的名字。
    2.d 如果名称匹配,也比较版本名称。只有在引用和定义都有版本控制的情况下才需要这样做。它还需要进行字符串比较。如果版本名称匹配,或者没有进行这样的比较,则找到了要查找的定义。(有道翻译)
    2.e 如果不匹配,尝试下一个元素。
    2.f 如果链不包含任何其他元素,则当前对象中没有定义,我们继续查找范围中的下一个对象。(有道翻译)
  4. 如果查找范围中没有其他对象,则查找失败。(有道翻译)

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