How does Linker Works???

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.

  • Header section????????????????--> Contains information about all sections and section flags
  • symbol table????????????????????--> Symbols and their address information
  • relocation Section???????????--> Hold information for runtime object loading
  • dynamic section??????????????--> For the dynamic library, it will be having a dynamic section

??????????????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???????????????????????

  • Combine object module into a load module
  • Relocate the object modules as they are being combined
  • Link the object module to combine
  • Search lib for external reference not defined in the object module

Creating Loadable module Steps

  1. Initialize by creating an empty Module with an empty global symbol table
  2. Read the next object module from the command line
  3. Insert the object module to the next available space in the load module. remember the address where it loaded
  4. relocate object to new load address in Newly initialized module, also relocate all the object symbol table to module global symbol table
  5. merge global symbol table if reference of the symbol is undefined in the global symbol table, linker make a note of it to fix it up while loading other object files
  6. fix symbol table when other modules are getting loaded who might have reference to undefined symbol in the global symbol table
  7. Earlier loaded undefined symbol links will be noted by Linker to fix it while loading other object files
  8. In the case of any library, the same process will be followed for mapping and loading
  9. go Back to step 2 and repeat till all object modules are not loaded

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.

No alt text provided for this image

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

Akash Walunjkar

Embedded Software Engineer, Johnson Controls,IEC.

2 年

Very Helpful Stuff...??

Shwetank Kishor Ambast

Teacher of Physics at Sant Atulanand Convent School, Varanasi

2 年

Very informative, Vinit!

要查看或添加评论,请登录

Vinit ..的更多文章

  • Cache

    Cache

    Introduction Memory is an Important resource in all embedded systems, any program executing on the core will be needing…

  • Security Attacks by Buffer overflows

    Security Attacks by Buffer overflows

    Introduction Buffer is a chunk of contiguous memory which is used to store some data. Buffers are used for data…

    2 条评论
  • Remote Procedural Call

    Remote Procedural Call

    Introduction Remote procedure calls allow a local computer (client) to remotely call procedures on a different computer…

    5 条评论
  • Virtual function/class, Where? Why? & How?

    Virtual function/class, Where? Why? & How?

    There is no doubt object-oriented languages are becoming the base for the creation of a new software stack. In this…

    1 条评论
  • CPU Isolation & CPU affinity In Linux

    CPU Isolation & CPU affinity In Linux

    In Multi-processor architecture processors is directly get compared with processing power. Common notion is more…

    13 条评论
  • Kernel Module Debugging Techniques

    Kernel Module Debugging Techniques

    There are several Debugging Techniques, few efficient Debugging techniques are listed Below. For kernel Module…

    4 条评论
  • Debugging With GDB

    Debugging With GDB

    GNU GDB Debugger No programmer is perfect, some of them do logical mistakes so some to syntactical. Syntax error can be…

    1 条评论
  • "Inline" Function and It's Use

    "Inline" Function and It's Use

    "Inline" Function is a provision or feature provided by the compiler. Inline is a request made to the compiler to…

  • Which Programming Language to learn???

    Which Programming Language to learn???

    Which Programming Language to learn???? it is always a big question for new learners or beginners. should it be C, C++,…

社区洞察

其他会员也浏览了