0% found this document useful (0 votes)
21 views

SPCC Module 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

SPCC Module 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

LOADERS & LINKERS

Module 4

Ms. Ankita Karia, SFIT


Topics to Cover
• Introduction

• Functions of loaders

• Relocation and Linking concept,

• Different loading schemes:

• Relocating loader,

• Direct Linking Loader

• Dynamic linking and loading.


INTRODUCTION
Introduction to LOADERS & LINKERS
LINKERS - Also known as Linkage Editors
• A linker is a software tool that combines multiple object files generated by an assembler
into a single executable program.
• An object file is a machine code representation of the compiled code for a single module
or source file. The linker takes all these object files and merges them into a single
executable file.
• The linker performs several tasks such as resolving external symbol references,
allocating memory space for program code and data, and generating executable code.
• The linker also performs the essential task of resolving dependencies between different
modules of the program. For eg., if your program uses a library function like printf()
from the standard C library, the linker needs to know where to find the function code and
link it with your program.
FUNCTION OF LINKER
• Symbol Resolution: Linkers resolve symbols or references between
different object files or modules. When a program is split into multiple
files (like source files, libraries, etc.), each file may reference symbols
(functions, variables, etc.) defined in other files. The linker ensures that all
these references are correctly resolved so that the program can execute
without errors.

• Address Binding: Linkers perform address binding, which involves


assigning final memory addresses to various program elements such as
functions, variables, and data structures. This can involve both relocatable
addresses (which are resolved at load time) and absolute addresses (which
are fixed during linking).
FUNCTION OF LINKER
• Relocation: Linkers perform relocation, which involves adjusting the
addresses of memory references within the program to reflect the final
memory layout. This is especially important when dealing with shared
libraries or when loading programs into different memory locations.

• Generating Executables: Ultimately, the main function of a linker is to


combine all the object files and libraries into a single executable file that
can be run by the operating system. The linker ensures that all
dependencies are resolved, addresses are correctly bound, and
optimizations are applied before producing the final executable.
TYPES OF LINKING
There are two types of linking, which are as follows:
STATIC LINKING: In static linking, the linker combines all necessary object files and
libraries into a single executable file before the program is executed. This includes
resolving external references, assigning absolute addresses to symbols, and copying the
required code and data into the executable. Static linker performs two major tasks:
1. Symbol resolution – It associates each symbol reference with exactly one symbol
definition .Every symbol has a predefined task.
2. Relocation – It relocates code and data section and modifies the symbol references
to the relocated memory locations.
TYPES OF LINKING
DYNAMIC LINKING: Dynamic linking involves linking the program with shared libraries
(.dll files on Windows, .so files on Unix-like systems) at runtime rather than embedding
them into the executable. The linker only includes references to the shared libraries, and
the actual linking occurs when the program is loaded into memory.
ADVANTAGES DISADVANTAGES
Reduced executable size, as shared libraries are Dependency on external libraries during runtime,
not included in the executable itself. requiring these libraries to be present on the
system where the program is executed.
Improved memory usage, as shared libraries are Potential version compatibility issues if different
loaded into memory only once and shared across versions of shared libraries are installed on
multiple programs. different systems.
Easier library updates and maintenance, as Slightly slower startup time due to the need to
changes to shared libraries can be applied without load shared libraries at runtime.
recompiling or relinking the entire program.
LOADERS
• As the program that has to be executed currently must reside in the main memory of the
computer. It is the responsibility of the loader, to load the executable file/module of a
program, generated by the linker, to the main memory for execution. It allocates the
memory space to the executable module in main memory.
• Definition: A loader is a software component in an operating system that loads
executable programs or software modules into memory for execution by the CPU. It
performs tasks such as allocating memory space, relocating program code to the
appropriate memory addresses, and resolving external references between different parts
of the program. This process is essential for preparing a program to be executed
effectively within the computer's memory space.
LOADERS

There are three kinds of loading approaches:


• Absolute loading
• Relocatable loading
• Dynamic run-time loading
FUNCTIONS OF LOADER
The loader is responsible for several crucial functions in the execution of programs:
1. Memory Allocation:
The loader allocates memory space for the program in the computer's memory. It ensures
that the program's instructions and data have the necessary memory addresses to operate
properly.
2. Address Binding:
It performs the process of address binding, which involves associating symbolic
addresses used in the program with actual memory addresses. This can include absolute,
relative, or dynamic addressing, ensuring that the program can access its required
resources correctly.
3. Relocation:
If the program is designed to be loaded at a different memory location than its original
compilation address, the loader performs relocation. It adjusts the program's memory
references to reflect the new starting address.
FUNCTIONS OF LOADER
4. Linking and Symbol Resolution:
The loader resolves references to external symbols, such as functions or variables, by
matching them with their actual locations in other modules or libraries. This allows
different parts of the program to communicate and interact seamlessly.
5. Loading and Execution:
The loader physically loads the program's machine code and data into memory, making it
ready for execution by the CPU. Once loaded, the loader transfers control to the program's
starting point, initiating its execution.
6. Error Detection:
The loader checks for potential errors during the loading process, such as insufficient
memory, incompatible machine code formats, or unresolved symbols. It informs the user or
programmer about any issues that could prevent successful execution.
Overall, the loader plays a critical role in preparing executable programs for smooth & efficient
execution on an operating system by managing memory allocation, address binding, symbol
resolution, and error detection.
TYPES OF LOADERS / LOADER SCHEMES
• Compile and Go Loader
• General Loader Scheme
• Absolute Loader
• Relocating Loaders
• Direct linking Loaders
• Dynamic linking and loading.
ABSOLUTE LOADER
• The absolute loader is a kind of loader in which relocated object files are created, loader
accepts these files and places them at a specified location in the memory. This type of
loader is called absolute loader because no relocating information is needed, rather it is
obtained from the programmer or assembler.

• The starting address of every module is known to the programmer, this corresponding
starting address is stored in the object file then the task of loader becomes very simple
that is to simply place the executable form of the machine instructions at the locations
mentioned in the object file.
ABSOLUTE LOADER
In this scheme, the programmer or assembler should have knowledge of memory
management.
The programmer should take care of two things:
• Specification of starting address of each module to be used. If some modification is done
in some module then the length of that module may vary. This causes a change in the
starting address of immediate next modules, it's then the programmer's duty to make
necessary changes in the starting address of respective modules.

• While branching from one segment to another the absolute starting address of respective
module is to be known by the programmer so that such address can be specified at
respective JMP instruction.
ABSOLUTE LOADER

Process of Absolute Loader


ABSOLUTE LOADER
Advantages:
1. It is simple to implement.
2. This scheme allows multiple programs or the source programs written in different languages.
If there are multiple programs written in different languages then the respective language
assembler will convert it to the language and common object file can be prepared with all the
ad resolution.
3. The task of loader becomes simpler as it simply obeys the instruction regarding where to
place the object code to the main memory.
4. The process of execution is efficient.
Disadvantages:
1. In this scheme, it's the programmer's duty to adjust all the inter-segment addresses and
manually do the linking activity. For that, it is necessary for a prograSmmer to know the
memory management.
2. If at all any modification is done to some segment the starting address of immediate next
segments may get changed the programmer has to take care of this issue and he/she needs to
update the corresponding starting address on any modification in the source.
RELOCATING LOADER
• A relocating loader is a type of loader that can load the program into different regions of
memory at different times during execution. This process allows better utilization of the
system's memory.

• The term "relocating" refers to the ability of the loader to adjust, or "relocate", the
absolute addresses in a program so that it can be loaded at an address different from the
one originally specified for it.

• It achieves this by modifying the object code of a program based on the offset address,
which is the starting address where the program is loaded in main memory.
RELOCATING LOADER
• For instance, if a program has an instruction that refers to a memory location, the address
of that memory location is determined when the program is assembled or compiled.

• If the program is later loaded into a different part of memory, the addresses referred to by
those instructions will be incorrect. The relocating loader adjusts these addresses based
on the new location of the program in memory.
DIRECT LINKING LOADER
• The direct linking loader is the most common type of loader. The loader cannot have the
direct access to the source code. The assembler should give the following information to
the loader
✓ The length of the object code segment
✓ The list of all the symbols which are not defined in the current segment but can be
used in the current segment.
✓ The list of all the symbols which are defined in the current segment but can be
referred by the other segments.
• The list of symbols which are not defined in the current segment but can be used in the
current segment are stored in a data structure called USE table. The list of symbols which
are defined in the current segment and can be referred by the other segments are stored in
a data structure called DEFINITION table.
DIRECT LINKING LOADER
• There are 4 types of cards available in the direct linking loader. They are
i. ESD-External symbol dictionary
ii. TXT-card
iii. RLD-Relocation and linking dictionary
iv. END-card
Advantages of Direct Linking Loader:

Simplicity: Direct linking loaders are simpler to implement because they do not require any relocation or
address modification during loading. This simplicity can make them suitable for resource-constrained
environments or for simple programs where complexity is not a major concern.

Faster Loading: Since direct linking loaders do not perform any address adjustments, they can load
programs into memory more quickly compared to relocating loaders. This can be advantageous for
programs that need to start execution promptly.
DIRECT LINKING LOADER
Disadvantages of Direct Linking Loader:
1. Lack of Flexibility: One of the significant drawbacks of direct linking loaders is their lack of
flexibility in terms of memory layout. Programs loaded using direct linking loaders are typically
bound to specific memory addresses, making it challenging to load them at different locations
without modification.
2. Memory Fragmentation: Direct linking loaders can contribute to memory fragmentation because
they load programs into fixed memory locations. This can lead to inefficient memory usage over
time, especially if programs are loaded and unloaded frequently.
3. Inability to Handle Overlays: Direct linking loaders are not well-suited for handling overlay
techniques, which involve dynamically swapping portions of a program in and out of memory. This
limitation can be problematic for large programs that need to manage memory usage efficiently.
4. Limited Portability: Programs loaded by direct linking loaders may have limited portability across
different systems or memory configurations. They are often tied to specific memory layouts and may
require modifications to run on systems with different memory architectures.
DYNAMIC LINKING AND LOADING
• Definition: Dynamic loading refers to the capability of a program to load and execute
modules (code or data) at run time, as opposed to being loaded statically during
compilation or linking.
• Usage: This feature allows programs to be more flexible and efficient by loading only
the necessary modules or libraries when they are required, rather than loading everything
at startup.
• Benefits:
• Reduced memory usage: Only necessary modules are loaded, saving memory.
• Faster startup times: Only essential components are loaded initially, improving startup
speed.
• Flexibility: Modules can be loaded and unloaded dynamically, enabling features like
plug-ins and modular architecture.
DYNAMIC LINKING AND LOADING
• Definition: Dynamic linking is a mechanism where a program is linked to libraries (code or
data) at run time, allowing multiple programs to share a single copy of a library in memory.
• Usage: This feature promotes code reusability and reduces memory overhead by allowing
multiple programs to use common libraries without each program having its own copy of
the library.
• Benefits:
• Code reuse: Multiple programs can use the same shared libraries, reducing duplication.
• Memory efficiency: Shared libraries are loaded into memory once and shared among
programs, saving memory.
• Updates: Libraries can be updated independently without requiring changes to every
program using them.
• Example: On a Unix-like system, dynamic linking is commonly used with shared libraries
(e.g., .so files), where programs dynamically link to these libraries at runtime.
UNIVERSITY QUESTIONS
• What are the main functions of Loader? Enlist the loader schemes [05]
• Explain Direct Linking Loader in detail [10]
• What are the functions of Loader [05]
• What is Relocation and Linking Concept in Loaders [05]
• Explain different types of Loaders in detail [10]
• Explain absolute loader. State its advantages and disadvantages [05]
• Explain Dynamic Linking Loader in detail [10]

You might also like