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.
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:
- Static
Linking: Static linking is a kind of linking that is performed during
the compilation of a source program in which linking is performed before
the execution of the file or object. On the other hand, the linker
produces a result at the time of copying all library routines into the
executable image, which is known as static linking. As compared to
dynamic linking, it may need more memory storage and disk space.
However, when it runs on the system, it does not need the presence of
the library that makes it more portable. It generates a fully linked
object file that would be able to load and run and takes a collection of
the relocatable object file and command-line argument. Two major tasks
are performed by the static linker, which is discussed below:
- Symbol resolution: In this, each symbol has a predefined task, and it associates each symbol exactly with one symbol definition from which they belong to.
- Relocation: Its function is to modify symbol references to the relocated memory location and relocate the code and data section.
- Dynamic linking: Another type of linking is dynamic linking, which is performed at the run time, in which multiple programs can share a single copy of the library. It means, each module having the same object can share information of an object with other modules rather than linking the same object repeatedly into the library.
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:
- In dynamic linking, the often-used libraries do not need to store in every single executable file; they only need to store in only one location that helps to save memory and disk space.
- In the library function, if a bug is corrected with the help of replacing the library, all problems using it dynamically, after restarting them will get benefit from the correction. Otherwise, programs would have to be re-linked first if they include this function by static linking.
The dynamic linking also has some disadvantages that are as follows:
- An incompatible updated library is known as "DLL hell" on the Windows platform. If the newer version is incorrectly not backward compatible, this library will break executables on the basis of the performance of the earlier form of the library.
- A program used with the libraries might be certified in the form of documentation requirements, correctness, performance, and package, but if components can be replaced, they may not be certified.
Relocation
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.
LINKER |
LOADER |
The linker generates executable files of a source program. |
The primary function of the loader is to load the executable module to the main memory. |
An assembler generates the object code, which is taken as an input by the linker. |
The linker creates the executable module, which is taken by the loader. |
To generate an executable code, the linker combines all the object modules and source code. |
In the main memory, it loads executable codes for further execution. |
Linkage Editor and Dynamic linker are the two types of linker. |
Absolute loading, Dynamic Run-time loading, and Relocatable loading are three kinds of loader. |
Combining all object modules is another use of a linker. |
The loader allocates the address to executable files. |
Furthermore, in the program's address space, it is also responsible for arranging objects. |
The references used within the program are handled by the loader. |
0 Comments