Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the relocation information in the ELF format?

Tags:

linux

linker

elf

Quoting "Linkers and Loaders" in the Loaders part

"load-time relocation is far simpler than link-time relocation, because the entire program is relocated as a unit. [...] After reading the program into memory, the loader consults the relocation items in the object file and fixes up the memory locations to which the items point"

Maybe I misunderstood this point and this is only in some architectures, but my question is: where in the ELF format is specified which items need relocation at load time? how can I inquire for this list?

like image 217
Stefano Borini Avatar asked Mar 19 '23 04:03

Stefano Borini


1 Answers

Relocations are to be found in special relocation sections in the ELF file. You can use the readelf --sections command to find out what sections are there in an executable or a shared library and those of type REL contain relocation instructions. The content of those relocation sections can be displayed using readelf --relocs. For example:

$ readelf --relocs /bin/ls

Relocation section '.rela.dyn' at offset 0x16c8 contains 5 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000061afd8  000c00000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
00000061b540  006d00000005 R_X86_64_COPY     000000000061b540 optind + 0
00000061b548  006e00000005 R_X86_64_COPY     000000000061b548 optarg + 0
00000061b550  006a00000005 R_X86_64_COPY     000000000061b550 stderr + 0
00000061b560  006600000005 R_X86_64_COPY     000000000061b560 stdout + 0

Relocation section '.rela.plt' at offset 0x1740 contains 99 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
00000061b000  000100000007 R_X86_64_JUMP_SLO 0000000000000000 strcoll + 0
00000061b008  000200000007 R_X86_64_JUMP_SLO 0000000000000000 mktime + 0
...

The .rela.dyn section contains references to references in the program's code of code or data symbols that have to be relocated at load time while .rela.plt contains mostly jump slots that are used to call functions in shared objects. Note that usually only shared objects are compiled as position-independent code while the usual executables are not. This is due to the fact that PIC code is a bit slower than non-PIC code.

like image 129
Hristo Iliev Avatar answered Apr 01 '23 10:04

Hristo Iliev