What is the difference between Shared object file and Relocatable file?

https://linux-audit.com/elf-binaries-on-linux-understanding-and-analysis/
says

The type field tells us what the purpose of the file is. There are a
few common file types.

CORE (value 4)
DYN (Shared object file), for libraries (value 3)
EXEC (Executable file), for binaries (value 2)
REL (Relocatable file), before linked into an executable file (value 1)

https://unix.stackexchange.com/a/476157/674 shows that a kernel module is REL. Why is it REL not DYN?

What is the difference between DYN and REL?

Thanks.

Answer

See the System V ABI, which contains the specifications of the ELF format. It says

Relocation entries for different object files have slightly different interpretations for the r_offset member.

  • In relocatable files, r_offset holds a section offset. That is, the relocation section itself describes how to modify another section in the file; relocation offsets designate a storage unit within the second section.

  • In executable and shared object files, r_offset holds a virtual address. To make these files’ relocation entries more useful for the dynamic linker, the section offset (file interpretation) gives way to a virtual address (memory interpretation).

Relocatable files are still fully relocatable, whereas shared objects are one step further along the linking process and have been largely relocated. Shared objects are only relocatable if their code is position-independent (e.g. it was built with GCC’s -fPIC option).

Kernel modules need to be relocatable without being position-independent, so they are shipped as relocatable files.

Attribution
Source : Link , Question Author : Tim , Answer Author : Community

Leave a Comment