How does Linker Works???
We know the general compiler steps, which the compiler follows for the generation of the executable file.
Code --> compiler --> object file --> Linker --> executable
First of let's understand exactly what Linking is (Loading Object and Relocating Object For execution)
When the compiler compiles individual files it creates an independent object file. While creating the file it assumes the start address as 0x00. After the compilation step, multiple object files need to be merged together to create one executable with all correctly mapped addresses.
To merge(Link) object files and create an executable linker does two jobs.
1.?????Creates Loadable module
2.?????Relocate the loadable module
Before going much into linking first we need to know the object file, what it is and what it contains. Every object file has the below section, which will be used by the linker while creating a loadable process.
??????????????The compiler compiles each object file with the start address as Zero, for each individual section it maintains an offset size for relocation.
Relocation section in elf file:
The relocation section describes how to modify another section. Relocation offsets designate a storage unit within the second section. For an executable or shared object, the value indicates the virtual address of the storage unit affected by the relocation. This information makes the relocation entries more useful for the runtime linker.
this value will be changed at the time of loading, based on the header offset value and operating system Virtual address offset point.
领英推荐
BUT “_start” function address will be considered as an Entry point for the elf file (Linker output file).
The program must go through various steps to be executable and loaded into memory. the compiler compiles a program and creates an object module .so or .o file. linker's job is to combine one or more objects into a load module. which is a program ready to be executed.
Linker Jobs???????????????????????
Creating Loadable module Steps
This is not completely accurate there is a catch, we are seeing modules loading sequentially but each module has its own code, data, and uninitialized segment. the segments of each type are loaded sequentially together which means there will be three segments growing at the same time which makes linking complicated because each section has its own load address.
Relocation of Loadable module
while the file is getting compiled, the compiler assumes absolute addresses. relocation records the place where symbols need to relocate by the linker. The relocation section describes how to modify another section in the file. Relocation offsets designate a storage unit within the second section.
??????????????For an executable or shared object, the value indicates the virtual address of the storage unit affected by the relocation. This information makes the relocation entries more useful for the runtime linker. when the linker combines several object modules only one of them can be loaded at actual address 0. the rest are relocated to a higher address.
Now linker knows only one object module can get address 0 for the start of execution, the correct one. All other addresses of other object Modules needed to change for the continuous address Map. Changing references and address of object module to get a loadable Map is called relocating Loadable Module relocation involves modifying each code and data location assuming the object module starts at 0. Relocation consists of adding the actual starting address of the object module in the load module of each of these locations Linker combines address spaces into a single address space, in doing so it must change some of the address space into the object module
Embedded Software Engineer, Johnson Controls,IEC.
2 年Very Helpful Stuff...??
Teacher of Physics at Sant Atulanand Convent School, Varanasi
2 年Very informative, Vinit!