Prerequisite – Introduction of Compiler design
Linker is a program in a system which helps to link object modules of a program into a single object file. It performs the process of linking. Linkers are also called as link editors. Linking is a process of collecting and maintaining piece of code and data into a single file. Linker also links a particular module into system library. It takes object modules from assembler as input and forms an executable file as output for the loader. Linking is performed at both compile time, when the source code is translated into machine code and load time, when the program is loaded into memory by the loader. Linking is performed at the last step in compiling a program.
Source code -> compiler -> Assembler -> Object code -> Linker -> Executable file -> Loader
Linking is of two types: 1. Static Linking – It is performed during the compilation of source program. Linking is performed before execution in static linking. It takes collection of relocatable object file and command-line arguments and generates a fully linked object file that can be loaded and run. Static linker performs two major tasks:
- Symbol resolution – It associates each symbol reference with exactly one symbol definition .Every symbol has a predefined task.
- Relocation – It relocates code and data section and modifies the symbol references to the relocated memory locations.
The linker copies all library routines used in the program into executable image. As a result, it requires more memory space. As it does not require the presence of library on the system when it is run, so it is faster and more portable. No failure chance and less error chance. 2. Dynamic linking – Dynamic linking is performed during the run time. This linking is accomplished by placing the name of a shareable library in the executable image. There are more chances of errors and failures. It require less memory space as multiple programs can share a single copy of the library. Here we can perform code sharing. It means if we are using the same object a number of times in the program, instead of linking the same object again and again into the library, each module shares information of the object with other modules having the same object. The shared library needed in the linking is stored in virtual memory to save RAM. In this linking we can also relocate the code for the smooth running of code but all the code is not relocatable. It fixes the address at run time.