Linker and loader are the two system programs that play a vital role in the execution of a program. A linker combines the target program generated by the language translator with all the external references mentioned in the program. Linker produces a machine language program that does not contain any unresolved external references. This machine language code is an executable code.
On the other hand, the loader is responsible for placing the executable code in the main memory for execution. In this section, we will be discussing both of these system programs in detail. Also, we will discuss the types and differences between them.
Content: Linker and Loader
- What is Linker?
- Types of Linker
- What is Loader?
- What is Relocation in System Programming?
- Types of Loader
- What is Bootstrap Loader?
- Difference Between Loader and Linker
What is Linker?
Every programming language has libraries that have some prewritten codes. The purpose of these prewritten codes is to perform a specific task. The programmer uses these codes to optimize their task while designing code.
For example, printf(), and scanf() functions that a programmer directly uses in a C program are defined in the standard header file <stdio.h> of the C library. The programmer does not define this printf(), scanf() function; he can directly use them from the standard C library.
Usually, the codes in libraries are designed to perform functions such as:
- Writing to files
- Displaying results
- Evaluating mathematical expressions
- Other functions
Now when a programmer writes a program that includes these library functions – each time a library function occurs then, the language translator (compiler, assembler) has to make a call to the corresponding library function.
This is because your CPU cannot execute the target code produced by the compiler all alone. Even if the compiler directly produces the machine code, the CPU can not execute it directly.
Thus, before executing the target code, it must be combined with library functions’ codes.
It is similar to macros as wherever in the code we use the macro name; it is replaced with the macro definition.
Similarly, wherever the library function is used in the code, the target code has to combine with the library functions. For this, the language translator generates an object module with the target code and the information of all the library routines and other programs that need to be invoked during the execution of that program.
Function of Linker
Linker accepts the object module and extracts all the information out of it. The linker then locates the required library routine and other programs and combines them with the target code.
Types of Linker
There are two forms of linking a target program produced by the translator with the library routines and other external references:
- Static Linking
- Dynamic Linking
In static linking, all the external references present in the object module are resolved before the execution of the program. Static linking might create an overhead. Suppose several programs refer to the same library routine.
Now, if these programs are linked statically, then all of these programs will have a private copy of that library routine. Many of these programs may be in execution simultaneously. If so, those many copies of the same library routine will be in the memory, occupying the memory unnecessarily. So, we came up with the concept of dynamic linking.
Dynamic linking is where all the external references are resolved at the time of execution of the machine language program. This kind of linker is invoked at the time of program execution.
Here, the linker combines the machine language program with external references, such as library routines and other referred programs, as they occur in the machine language program and resumes the execution of the program.
What is Loader?
A loader is a system program that loads or places the machine language program or the binary program in the main memory for its execution. The linker generates this binary program and places it on the hard disk. The loader locates this executable binary program from the hard disk and loads it on the main memory. The loader is even responsible for performing relocation. Now you must be thinking, what is relocation?
What is Relocation in System Programming?
Sometimes a programmer may code a program with the intention of executing the program from a specific area of the memory. But it may happen that the operating system may occupy the same specific area of memory for some other purpose. In such a case, the loader has to allocate a different memory area to the program for its execution.
In short, relocation is a process where the loader changes the memory addresses mentioned in the program with the available memory addresses. So that program can correctly execute in the allocated area.
Types of Loader
Loading can be performed in three different ways such as:
- Compile and Go Loader
- Absolute Loading
- Relocatable Loading
- Dynamic Run-time Loading
Compile and Go Loader
In this kind of loading, the assembler is placed in one part of the memory. It reads the compiled program line by line and generates the machine program that is directly placed in the assigned memory location. A large portion of memory is wasted as it holds the combination of assembler and loader activities.
Program execution with this scheme takes more time as the program must be assembled and executed each time.
In this scheme, the language translator generates a relocated object file. The absolute loader accepts this file and places it in the specified memory location. In this scenario, the loader does not require relocation information as it is already provided by the programmer itself in the program or by the assembler.
The language translators do not generate a relocated object file in this scheme. Instead, the loader can allocate the space to executable code anywhere in the main memory for its correct execution.
Dynamic Run-time Loader
What is Bootstrap Loader?
The bootstrap loader is also a system program in the computer’s read-only memory (ROM). This loader gets activated as soon as you switch on your computer. It reads your computer’s hard disk and loads the operating system onto the main memory.
Difference Between Linker and Loader
|Basis for Differentiation||Linker||Loader|
|Basic||Responsible for generating the binary (machine language) program||Responsible for allocating memory to binary programs for their execution|
|Provided Input||It accepts the object module generated by the language translator||It accepts the binary program generated by the linker|
|Working||It combines the object module with the library routines and other programs.||It allocates a specific area to the binary program for its execution.|
|Types||Static linking and dynamic linking||Absolute Loading, Relocatable Loading, Dynamic Run-time Loading|
|Position||A linker is positioned after the language translator, such as the compiler and assembler.||A loader is positioned after the linker; it is the last language processor in the line the program has to pass through before execution.|
So, this is all about the two system programs that are essential for the execution of the program. We require the linker to resolve all the external references present in the source program, such as library routines and references to other programs and generate a machine language program. We require the loader to allocate the space to the machine language program in the main memory for its execution.