Linux relocation

Linux relocation,第1张

Linux有symbol defining和symbol reference两个过程,在link阶段,需要由relocation建立两过程联系。

Elf32_Rel(对于x86_64就是Elf64_Rela)表达定义引用之间的联系。

r_info即指向symbol defining

ELF32_R_SYM(info) ((info)>>8)

ELF32_R_TYPE(info) ((unsigned char)(info))

ELF64_R_SYM(info) ((info)>>32)

ELF64_R_TYPE(info) ((Elf64_Word)(info))

ELF32_R_SYM/ELF64_R_SYM算出来的是.dynsym表格中的table offset,需要由.rel.plt sh_link字段链接至.dynsym表。

r_offset,指向symbol reference。两种情况:

当type为ET_REL 时,r_offset为 需要重定向的section offset,Section需要由.rel.plt sh_info字段决定。

当type为ET_EXEC时,r_offset为重定向VA

引用原文地址:https://www.intezer.com/executable-and-linkable-format-101-part-3-relocations/

具体解析实例:

elf64_rela 通过offset连接符号引用,在.got.plt section,最终替换各got值为具体符号地址,如puts/gets等函数libc地址。

elf64_rela通过Info连接符号定义。

.dynsym为外部符号定义,如libc gets/puts等。

.symtab为内部符号定义,如main等。

通过st_shndx寻找符号定义所在的section,寻找过程为:通过st_shndx指向section header table索引,找到对应section描述结构 elf64_shdr,通过描述结构elf64_shdr.sh_addr找到真实section,如.rodata/.data/.text等。

通过st_value,在上一步找到的section中查找值。

总结:符号定义和符号引用都得在section中。符号引用默认在.got.plt section中(对动态链接而言),符号定义得根据st_shndex===>section header table[st_shndex]===>elf64_Shdr===>section

用file命令查看:

file /usr/bin/ls

/usr/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=cf9b019522fb43db6e2f324ab63b7f9e2da6ede8, stripped


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8076721.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存