0% found this document useful (0 votes)
14 views31 pages

Unit 4

This document explains the concepts of linkers and loaders in program execution, detailing the steps of translation, linking, relocation, and loading. It distinguishes between linkers, which combine object files into executables, and loaders, which prepare programs for execution in memory. Additionally, it covers types of linking (static and dynamic), self-relocating programs, overlays, and various loader schemes, highlighting their advantages and disadvantages.

Uploaded by

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

Unit 4

This document explains the concepts of linkers and loaders in program execution, detailing the steps of translation, linking, relocation, and loading. It distinguishes between linkers, which combine object files into executables, and loaders, which prepare programs for execution in memory. Additionally, it covers types of linking (static and dynamic), self-relocating programs, overlays, and various loader schemes, highlighting their advantages and disadvantages.

Uploaded by

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

Unit -4

Linkers and Loaders


Introduction
• Execution of program written in language L involves following steps:

1. Translation of program
2. Linking of program with other programs needed for its execution.
3. Relocation of the program to execute from the specific memory area allocated
to it.
4. Loading of the program in the memory for the purpose of execution.
These steps are performed by different language processors.

Step1 is performed by the translator of language L.


Step 2 and 3 are performed by Linker
step 4 is performed by Loader
Schematic of Program Execution

Data

Translator Linker Binary


Loader ---->
Program

Result
Source
Program
Object Binary
Modules Programs
Difference between linker and Loader

• Linker is a program that takes one or more objects generated


by the compiler and combines them into a single executable
program.

• Loader is a part of operating system that is responsible for


loading programs from executable files into memory, preparing
them for execution and then execute them.
Relocation and Linking Concepts
1. Program relocation is the process of modifying the addresses
used in the address sensitive instruction of the program such that
the program can execute correctly from the designated area of
memory.

• If linked origin ≠ translated origin, relocation must be


performed by the linker.
• If load origin ≠ linked origin, relocation must be performed
by loader.
• Let A be the absolute addresses - instruction or data addresses-used in the
instruction of the program P.
• A ≠ø implies that program P assumes its instructions and data to occupy
memory words with specific addresses. Such a program is called
address sensitive program - contains one or more of the following:
1. address sensitive instruction
2. address constant

An address sensitive program P can execute correctly only if the start


address of the memory area allocated to it is same as its translated
origin. To execute correctly from any other area the address used in each
address sensitive instruction of P must be ‘corrected’.

Program relocation is the process of modifying the addresses used in the


address sensitive instructions of a program such that the program can
execute correctly from the designated area of memory.
2. Linking:-
A program consist of program units Pi, Pj. A program Pi interact with anothe
unit Pj by using addresses of Pj’s instruction and data in its own instructions.
To realise such interactions, Pj and Pi must contain public definition and
external references as defined by following:
1. public definition: a symbol which may be referenced in other program units
2. External reference: a reference to a symbol, which is not defined in the
program unit containing the reference.

EXTRN and ENTRY statements


The ENTRY statement lists the public definitions of the program units.
The EXTRN statement lists the symbols to which external references are
madein the program unit.
• Example:
START 500
START 200
ENTRY TOTAL
ENTRY ALPHA
EXTRN MAX, ALPHA
--
READ A (500) +09 0 540
--
LOOP (501)
ALPHA DS 25 (231)
-
END
-
-
MOVER AREG, ALPHA (518) +04 1 000
BC ANY, MAX (519) +06 6 000
-
-
BC LT, LOOP (538) +06 1 501
STOP (539) +00 0 000
A DS 1 (540)
TOTAL DS 1 (541)
END
3. Object Module
The object module of program contains all information necessary to relocate
and link hte program with other programs. The object module of program P
consist of following components:

1. Header: It contains Translated origin, Size and execution start address of P


2. Program: It contains Machine language program corresponding to P
3. Relocation table (RELOCTAB): It contains single field.
4. Linking Table (LINKTAB): It contains info concerning public definitions and
external referencesin P. Three fields Symbol, Type, Translated address
• Example:
1. Translated origin=500, size=42, execution start address=500
2. Machine language instruction as shown in program.
3. Relocation table
500
538

Linking Table
ALPHA EXT 518
MAX EXT 519
A PD 540
Design of a Linker

Object files Shared Libraries

Normal Libraries

Linker
Control Files

Link/Load
map

Debug symbol files


Executable files
Design of a Linker
• Linker are also called link editors.
• Linking is process of collecting and maintaining piece of code and data into a single file.
• Linker also link a particular module into system library. It takes object modules from
assembler as input and forms an executable file as output for 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.
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 argument and generate fully
linked object file that can be loaded and run.

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 is more chances of error and failure chances.
• It require less memory space as multiple program can share a single copy of the library.
Self Relocating Programs
• A self relocating program is a program which can perform the relocation of its
own address sensitive instructions.

• It contains following two provisions for this purpose:


1. A table of information concerning the address sensitive instructions exists as
a part of program.

2. Code to perform the relocation of address sensitive instructions also exists


as a part of program. This is called as relocating logic.

• A self relocating programs can execute in any area of memory.


Linking for Overlays structured Programs
• An overlays is a part of program (or software package) which has the same load origin as
some other part of the program.

• Overlays are used to reduce the main memory requirement of a program.

• A program containing overlays are called as overlays structured program. Such a program
consist of
1. A permanent resident portion called the root
2. A set of overlays.

Execution of an overlays structures program proceeds as follows


1. The root is loaded in memory and given control for the purpose of execution.
2. Other overlays are are loaded as needed.
The loading of overlays overwrites previously loaded overlays with the same origin.
This reduces the memory requirement of the program.
Example:
Consider the program with 6 sections init, read, trans_a, trans_b, trans_c,
print.

init performs some initializations and passes control to read.

read reads one set of data and invokes one of trans_a, trans_b, trans_c
depending on the value of data.

print is called to print the result.

trans_a, trans_b, trans_c are mutually exclusive. Hence they are made into
seperate overlays.

read and print are put in the root of the program.


an overlay tree
Dynamic Linking
• A dynamic linker is the part of an operating system that loads and links the shared
libraries needed by an executable when it is executed.
• This approach is also called dynamic linking or late linking.
• Advantage of dynamic linking is memory requirements of the program are reduced.
Loaders
• The loader is a program which accepts the object program, prepares these programs for execution
by the computer and initiates the execution.
• Loader must perform 4 functions:
1. Allocate space in memory for the programs (allocation)
2. Resolve symbolic references between objects (linking)
3. Adjust all address dependent locations, such as address constants, to correspond to allocated
space (relocation)
4. Physically place the machine instructions and data
≈ into memory (loading)

A
Loader
A
| Program loaded
|
|
in memory,
| ready B
B for execution
Data bases
≈ ≈
Different Loading schemes
• Various schemes for accomplishing the four functions of a loader.

• New term segment is introduced, which is a unit of information that is treated as an entity, be
it a program or data.

1. “Complie-and-Go” loaders
2. General Loader Scheme
3. Absolute Loaders
4. Direct linking Loaders
5. Relocating Loaders
6. Linking Loaders
1. “Compile-and-Go” loaders
• In this type of loader, the instruction is read line by line, its machine code is obtained and it is
directly put in the main memory at some known address.
• That means the assembler runs in one part of memory and the assembled machine instructions
and data is directly put into their assigned memory locations.
• After completion of assembly process, assign starting address of the program to the location
counter.

User Program “Compile and


“Compile and
Go” Loader
Go” Loader
Executable code of
user program

|
|
|
Advantages and Disadvantages of “Compile and Go” loaders

Advantages
• Easy to implement.

Disadvantages
• Portion of memory is wasted because combination of assembler and loader activities, this
combination program occupies large block of memory
• There is no production of .obj file
• It cannot handle multiple source programs or multiple programs written in different languages
• The execution time will be more in this scheme as every time program is assembled and then
executed
2. General Loader Scheme
• In this loader scheme, the source program is converted to object program by some translator
(assembler).
• The loader accepts these object modules and puts machine instruction and data in an
executable form at their assigned memory.
• The loader occupies some portion of main memory.

Source Object
Translator |
Program Program 1 |
|

Executable
object code
|
Loader |
|

Loader Program
Source Object
Translator Program 2 |
Program |
|
Advantages of General Loader Scheme

• The program need not be retranslated each time while running it


• There is no wastage of memory, because assembler is not placed in the
memory
• It is possible to write source program with multiple programs and multiple
languages
3. Absolute Loaders
• Absolute loader is a kind of loader in which relocated object files are created, loader accepts
these files and places them at specified locations in the memory.
• This type of loader is called absolute because no relocation 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 task of loader becomes very simple and that
is to simply place the executable form of the machine instructions at the locations mentioned
in the object file.

• Thus the absolute loader is simple to implement in this scheme


i. Allocation is done by either programmer or assembler
ii. Linking is done by the programmer or assembler
iii. Resolution is done by assembler
iv. Simply loading is done by the loader
Absolute Loaders

Object
Code
|
Segment 1 Translator Starting |
|
address
Object code for
segment 1
|
|
Object Code |

Translator Loader Object code for


Segment 2
Starting segment 2
Address
|
|
| | |
|
| | |
| | | Object code for
segment N
Object
Code
Segment N Translator
Starting
Address
4. Direct linking Loaders

• 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


1. The length of the object code segment
2. The list of all the symbols which are not defined in the current segment but can
be used in the current segment.(USE table)
3. The list of all the symbols which are defined in the current segment but can be
referred by the other segments.(DEFINITION table)

One disadvantage of a direct linking loader is it is necessary to allocate, relocate, link and
load all of the subroutines each time in order to execute a program.
5. Relocating Loaders
• Another function commonly performed by a loader is that of program re – location.
• Relocation is simply moving a program from one area to another in the storage.
• It referred to adjustment of address field and not to movement of a program.
• The task of relocation is to add some constant value to each relative address in the
segment the part of a loader which performed relocation is called re – location
loader.
• To avoid possible reassembling of all subroutines , When a single subroutines is
changed and to performed the task of allocation and linking of the programmer.
• Output of a relocating loader is the object program and information about all other
programs its references.
6. Linking Loaders

• Performs all linking and relocation operations, including automatic library


search, and loads the linked program into memory for execution.

• Suitable when a program is reassembled for nearly every execution.

• Resolution of external reference and library searching is performed more


than once.

• Linking loaders perform linking operations at load time.

• When program is in development stage then at that time the linking loader
can be used.
Linkers v/s Loaders

Basis for comparison Linkers Loaders

Basic Basic It generates the It loads the executable module


executable module of a source to the main memory.
program.

Input It takes as input, the object It takes executable module


code generated by an generated by a linker.
assembler.

Function It combines all the object It allocates the addresses to an


modules of a source code to executable module in main
generate an executable memory for execution.
module.
Type/Approach Linkage Editor, Dynamic linker. Absolute loading, Relocatable
loading and Dynamic Run-time
loading.
End of Chapter

You might also like