elf-by-example

Experiment and learn how compilers, linkers and C runtime work cooperatively

View the Project on GitHub sivachandra/elf-by-example

Why are relocations present in the ELF of a statically linked executable?

ELF file of a statically linked non-PIE will have relocations to support ifuncs. ELF file of a statically linked PIE will additional have one specific kind of dynamic relocations. These relocations are required to adjust for the load address (a PIE is loaded at a different address everyone time it runs). See this example for more elaborate illustration.

What are linker generated relocations?

A coworker once asked, “Aren’t relocations generated by the compiler/assembler? What are linker generated relocations?” It is true that the compiler and/or the assembler generate the relocations. The static linker makes use of these relocations to performing the linking. Some values, like global variable addresses in a PIE, can only be known after the program starts. For such values, the linker typically replaces the compiler generated relocations with its own relocations which are most suitable for the linking mode used (for example, -pie or -static-pie). The relocations generated by the linker are applied by the CRT or the dynamic linker. This example illustrates one such relocation relevant for static-pie linking.