What is a Linker?
A linker is a program in a system, also known as a link editor and binder, which combines object modules into a single object file. Generally, it is a program that performs the process of linking; it takes one or multiple object files, which are generated by compiler. And, then combines these files into an executable files. Modules are called for the different pieces of code, which are written in programming languages. Linking is a process that helps to gather and maintain a different piece of code into an executable file or single file. With the help of a linker, a specific module is also linked into the system library.
The primary function of the linker is to take objects from the assembler as input and create an executable file as output for the loader, as it helps to break down a large problem into a small module that simplifies the programming task. Usually, computer programs are made up of various modules in which all being a compiled computer programs and span separate object files. The whole program refers to these different compiled modules with the help of using symbols. These separate files are combined by linker into a single executable file. The source code is converted into machine code, and the linking is performed at the last step while compiling the program.
Source code -> compiler -> Assembler -> Object code -> Linker -> Executable file -> Loader
The objects can be collected by linker from a library or runtime library. Most of the linker only consists of files in the output that are referenced by other libraries or object files, and they do not include the whole library. The process of library linking requires additional modules to be linked with some referenced modules; thus, it may be an iterative process. Generally, one or more than one system libraries are linked by default, and libraries are available for different purposes.
In the program's address space, the arranging of the objects is also handled by the linker. The compiler often assumes a fixed base location (like zero), as it seldom knows about the object location where it will reside. The loads, store, and re-targeting of absolute jumps may be involved in the relocating machine code. When the executable output produced by the linker is finally loaded into memory, it may require other relocation pass. Usually, this pass is omitted on the hardware, hardware that offers virtual memory. There is no conflict even at the time all programs load at the same base address because each program is put into its own address space. If the executable file is a position-independent executable, this pass is also omitted on this file.
The process (assembling object files into a program) performed by the linker on the Unix kind operating system like SINTRAN III was known as loading. Furthermore, on some operating systems, the linking and loading of a program are both tasks handled by the linker, called dynamic linking.
Types of Linking
There are two types of linking, which are as follows:
These dynamic link libraries are loaded at the time a program is executed; then, it performs a final linking. Also, a linker is not needed by the dynamic linking.
Although it needs less memory space, there are more chances of error and failure chances. In the linking, the needed shared library is held in virtual memory that helps to save random access memory. This linking fix the address at run time; also, it allows the users to reposition the code in order to smooth running of code. However, it cannot be relocatable all the code. There are two benefits of using the dynamic linking approach, which is given below:
The dynamic linking also has some disadvantages that are as follows:
The compiler would be unable to take advantage of shorter or more efficient instructions, as in the final output, it does not have the information of the object on the layout. For instance, an offset or absolute address from the current location can be addressed by a jump instruction, and on the basis of the distance to the target, the offset could be expressed with different lengths. It is also known as automatic jump-sizing in order to jump optimizations.
The addresses are reassigned by the linker relaxation pass, which helps to more potential relaxations. Typically, instruction relaxation occurs when the linking is performed, but at compile-time, inner-module relaxation has the ability to take place as part of the optimizing process. Moreover, relaxation can also occur in some cases at the load-time.
Difference Between Linker and Loader
In the execution of the program, the linker and loader (two utility programs) play an important role. Before executing the program, its code passes through the compiler, assembler, linker, loader. Below is a table that contains the major difference between the linker and loader. The linker is a program in a system, also known as link editor and binder, which combines object modules into a single object file, whereas the loader is a special program that loads the executable module of a program that is generated by the linker and prepares this code for execution by a computer.