S represents the value of the symbol whose index resides in the relocation entry. A represents the addend used to compute the value of the relocatable field.
不妨将 S 理解成符号在虚存中的地址,A 理解成相对于符号的偏移量;
计算 S 需要在所有动态链接库中搜索符号,因此重定位 R_X86_64_64 表项会用到符号绑定。
1 2 3
# LD_DEBUG=bindings ./main 2>&1 | grep -E "\<a\>|\<b\>" 656: binding file /root/talk/relocation_types/libfoo.so [0] to /root/talk/relocation_types/libfoo.so [0]: normal symbol `a' 656: binding file /root/talk/relocation_types/libfoo.so [0] to ./main [0]: normal symbol
无论符号是否在同一个动态链接库内,重定位 R_X86_64_64 表项都会发生符号绑定;
Symbol value of pa 是 a 的地址,symbol value of pb 是 0 ,两者对符号查找有什么影响?
R_X86_64_RELATIVE
1 2 3 4 5 6 7
# readelf --relocs libfoo.so Relocation section '.rela.dyn' at offset 0x5e0 contains 15 entries: Offset Info Type Sym. Value Sym. Name + Addend 000000003db0 000000000008 R_X86_64_RELATIVE 1130 000000003db8 000000000008 R_X86_64_RELATIVE 11c5 000000003dc0 000000000008 R_X86_64_RELATIVE 10f0 000000004038 000000000008 R_X86_64_RELATIVE 4038
So what about R_X86_64_GLOB_DAT relocation type in ld.so? First, RESOLVE_MAP (a macro defined within elf/dl-reloc.c) is called (with r_type = R_X86_64_GLOB_DAT) to find out which ELF binary (could be the user's program or its dependent dynamic libraries) contains this symbol. Then R_X86_64_GLOB_DAT relocation type is calculated as Base Address + Symbol Value + Addend where Base Address is the base address of ELF binary which contains the symbol, and Symbol Value is the symbol value from the symbol table of ELF binary which contains the symbol.