Linking and loading are two instruments that play a pivotal role in program execution. Linking intends to generate an executable module of a program by combining the object codes generated by the assembler. A loader, on the other hand, loads these executable modules to the main memory for execution.
In this tutorial, we’ll explore those concepts and their variants.
Linking is the process of connecting all the modules or the function of a program for program execution. It takes more than one object module and combines it into a single object file. The linker, also known as the link editor, takes object modules from the assembler and forms an executable file for the loader. Thus, as the name suggests, linking is the process of collecting and maintaining pieces of data and code into a single file.
Linking of object modules is done at both compile as well as load time. Compile-time linking is done when the source code is translated to machine code. The load-time linking is done while the program is loaded into memory by the loader.
Linking is classified into two types based on the time when it is done – static linking and dynamic linking:
For instance, in the above diagram, in the static linking, each program binds to its dependent libraries at compile time. Whereas, in the case of dynamic linking, programs use shared libraries, and these libraries are linked to the programs at run time.
2.1. Static Linking
Static linking is done at the time of compilation of the program. It takes the collection of object files and the command-line arguments and generates the fully-linked object file that is loaded and executed.
2.2. Dynamic Linking
Dynamic linking is another technique that intends to reduce the shortcomings of static linking. With static linking, the user ends up copying functions or routines that are repetitive across various executables. Thus, static linking becomes inefficient. For instance, nearly every program needs printf() function. Thus, a copy of it is present in all executables which wastes space in both virtual memory and in the file system.
In the dynamic linking approach, the linker does not copy the routines into the executables. It takes note that the program has a dependency on the library. When the program actually executes, the linker binds the function calls in the program to the shared library present in the disk.
Loading is the process of loading the program from secondary memory to the main memory for execution.
Dynamic loading is the technique through which a computer program at runtime load a library into memory, retrieve the variable and function addresses, executes the functions, and unloads the program from memory. It is often used to implement software plugins. For instance, Apache Web Server loads the dynamic shared object (*.dso) files at runtime using this technique.
4. Loading vs. Linking
Let’s first discuss the relation between linking and loading:
First, an assembler or compiler translates the source program to an object module. A linker then uses this object module and other modules to prepare the linked module. A loader uses the linked modules along with other system libraries to generate the binary memory image. Thus, the process of program execution uses the offerings of both linker and loader.
Generally, a program can consist of multiple modules, and based on the program execution, not all modules can be present in the memory at the same time. Dynamic loading provides us the capability to load a module on demand.
For instance, in Java, we can use an instance of a ClassLoader to dynamically load a class if we need access to it. Besides, for larger applications, it is not possible to load all modules to the memory at the same time. Thus, dynamic loading is a convenient technique in such cases.
Dynamic linking works in a much similar manner but with a subtle difference. An application module consists of several functions, and together these provide the functionality the module offers. With dynamic linking, we use references to the library in the system where function definitions are available. The information about the library in which a function definition exists is stored in stubs.
In this quick article, we’ve discussed various aspects of linking and loading in a computer application.
We introduced the process of linking and their various types. Subsequently, we discussed the process of dynamic loading and its purpose.
Finally, we provided a comparison of dynamic loading and dynamic linking.