Linker: A linker is a program that joins the object files produced by the assembler/ compiler and other blocks of code to generate an executable file with a .exe extension. Linker has the functionality to search and add all the libraries needed for the executable file. The main aim of the linker is to produce executable files.
The linker controls the memory for the blocks of code and builds a link between different object programs. It organizes the objects in the memory space.
What does a linker do?
The compiler produces the object code of a program, and the code is passed onto the linker. The linker generates the executable code using the code obtained from the assembler/compiler. The executable code then passes to the loader.
Many large programs are split into sub-programs. These sub-programs are known as modules. When the modules are compiled, and object code is generated. The object codes from all these sub-programs are combined and form a single executable file of the main program.
There are two types of linkers
- Linkage Editor
Linkage Editor: This linker helps in producing relocatable (portable) executable modules. The linking done by the linkage editor is known as static linking. The linkage by a linkage editor is made during program compilation. Static linking helps relocate code and modifies symbol references to the relocated memory location. Every symbol has a specific task.
Dynamic Linker: This linker delays the linkage of modules from an outside source(external modules) until an executable module is generated. The linking is done during run-time.
The linking by the dynamic linker is referred to as dynamic linking, performed during run time. It is based on the memory sharing concept. The linking is achieved by allocating space for the shareable libraries in the executable image.
What is a Loader?
A loader is a unit of an operating system that loads programs and libraries. It takes executable files produced from the linker as an input. The main objective of loader is to load the executable files into the main memory. It also helps in assigning addresses to the executable files. The embedded computer system executes the code through ROM.
The loader has an essential role in the execution of the program. It reads the executable file and stores the instructions which help the executable file to run.
What does a loader do?
The loader initiates the registers, and the validation of the program is done for memory permissions, etc. It loads the files with the required libraries from the disk into the memory. Executable files are executed when the loader loads them into the memory.
There are three types of loading:
- Absolute loading
- Relocatable loading
- Dynamic run-time loading
Absolute loading: All the executable files are assigned the same memory location in this loading. The programmer should know the assignment strategy for loading the modules.
Relocatable loading: Relative addresses are produced by the compiler instead of the main memory address.
Dynamic Run-Time loading: This loading generates the address when the executable module is run. A different main memory address is given when the executing module is interrupted, removed from the disk, and loaded again.
Differences between Linker and Loader
|Definition||It takes object files from the compiler /assembler and generates executable files by joining them.||It loads executable files into the main program.|
|Input||Object code is taken as input from the compiler/assembler.||Executable files are taken as input from the linker.|
|Function||Links the object and header files to get executable files.||The address is assigned in the main memory for the executable modules.|
|Types||Dynamic linker and linker editor are the types of linkers.||Relocatable loading, Absolute loading, and Dynamic Run-time loading.|
|Role||organizing objects in the address space.||Adjusting references that are used in the program.|
Representation of linker and loader
The linker is used to generate executable files by taking object modules from the compiler/assembler. The linker links the other library functions in the object module to the built-in libraries. The loader loads the executable modules into the main memory for executing the program.