2ND YEAR (B.TECH)
COMPUTER SCIENCE AND ENGINEERING
RTMNU , NAGPUR
SUB- SYATEM PROGRAMMING
Unit :- 4
Linker and Loaders
Introduction
·
The Source
Program written in assembly language or high level language will be converted
to object program, which is in the machine language form for execution.
·
This conversion
either from assembler or from compiler, contains translated instructions and
data values from the source program, or specifies addresses in primary memory
where these items are to be loaded for execution.
·
This contains
the following three processes, and they are,
Loading - which
allocates memory location and brings the object program into memory for
execution – (Loader)
Linking- which
combines two or more separate object programs and supplies the information
needed to allow references between them
- (Linker)
Relocation -
which modifies the object program so that it can be loaded at an address
different from the location
originally specified - (Linking Loader)
Basic
Loader Functions/ Tasks of Loader
·
A loader is a system program that
performs the loading function. It brings object program into memory and starts
its execution.
·
Translators may be assembler/complier,
which generates the object program and later loaded to the memory by the loader
for execution
·
Loader perform the four basic functions
Allocation:
·
Allocates the space
in the memory where the object program would be loaded for Execution.
·
It allocates the space for program in the
memory, by calculating the size of the program. This activity is called
allocation.
·
In
reloadable loader allocation is done by the loader hence the assembler must
supply the loader the size of the program
Linking:
·
It links two or more
object codes and provides the information needed to allow references between
them.
·
It resolves the
symbolic references (code/data) between the object modules by assigning all the
user subroutine and library subroutine addresses. This activity is called
linking.
·
In absolute loader linking is done by the programmer as the
programmer is aware about the runtime address of the symbols.
·
In reloadable loader, linking is done by the loader and hence
the assembler must supply to the loader, the locations at which the loading is
to be done.
Relocation:
·
It modifies the object program by changing the
certain instructions so that it can be loaded at different address from
location originally specified.
·
There are some address dependent locations in
the program, such address constants must be adjusted according to allocated space;
such activity done by loader is called relocation.
·
In absolute Loader relocation is done by the
assembler as the assembler is aware of the starting address of the program.
·
In
reloadable loader, relocation is done by the loader and hence assembler must
supply to the loader the location at which relocation is to be done.
Loading:
·
It
brings the object program into the memory for execution.
·
Finally
it places all the machine instructions and data of corresponding programs and
subroutines into the memory. Thus program now becomes ready for execution, this
activity is called loading.
·
In both the loaders
(absolute, reloadable) Loading is done by the loader and hence the assembler
must supply to the loader the object program.
Concept of relocations:
· Relocation is the process of updating the addresses used in the address sensitive instructions of a program.
· It is necessary that such a modification should help to execute the program from designated area of the memory.
· The assembler generates the object code. This object code gets executed after loading at storage locations.
· There are two types of addresses being generated: Absolute address and, relative address.
· The absolute address can be directly used to map the object code in the main memory. Whereas the relative address is only after the addition of relocation constant to the object code address.
· For example LOADA(X) +5 the expression A(X) means the address of variable X. The meaning of the above instruction is that loading of the contents of memory location which is 5 more than the address of variable X. Suppose if the address of X is 50 then by above command we try to get the memory location 50+5=55. Therefore as the address of variable X is relative A(X) + 5 is also relative.
Concept of Linking:
· The execution of program can be done with the help of following steps 1. Translation of the program (done by assembler or compiler) 2. Linking of the program with all other programs which are needed for execution. This also involves preparation of a program called load module. 3. Loading of the load module prepared by linker to some specified memory location.
· The output of translator is a program called object module. The linker processes these object modules binds with necessary library routines and prepares a ready to execute program. Such a program is called binary program.
· The "binary program also contains some necessary information about allocation and relocation. The loader then load s this program into memory for execution purpose.
Various tasks of linker are –
1. Prepare a single load module and adjust all the addresses and subroutine references with respect to the offset location.
2. To prepare a load module concatenate all the object modules and adjust all the operand address references as well as external references to the offset location.
3. At correct locations in the load module, copy the binary machine instructions and constant data in order to prepare ready to execute module.
Loader Schemes:
· Based on the various functionalities of loader, there are various types of loaders:
1)
“compile and go” 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.
· The assembler runs in one part of memory and the assembled machine instructions and data is directly put into their assigned memory locations.
· This loading scheme is also called as “assemble and go”.
Advantages:
• This scheme is simple to implement. Because assembler is placed at one part of the memory and loader simply loads assembled machine instructions into the memory.
Disadvantages:
• In this scheme some portion of memory is occupied by assembler which is simply a wastage of memory.
• There is no production of .obj file; the source code is directly converted to executable form.
• 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:
· 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.
Advantages:
• The program need not be retranslated each time while running it. This is because initially when source program gets executed an object program gets generated. Of program is not modified, then loader can make use of this object program to convert it to executable form.
• There is no wastage of memory, because assembler is not placed in the memory, instead of it, loader occupies some portion of the memory. And size of loader is smaller than assembler, so more memory is available to the user.
• It is possible to write source program with multiple programs and multiple languages, because the source programs are first converted to object programs always, and loader accepts these object modules to convert it to executable form.
3) Absolute Loader:
· 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.
·
Thus
the absolute loader is simple to implement in this scheme
1)
Allocation is done by either programmer or assembler
2)
Linking is done by the programmer or assembler
3)
Resolution is done by assembler
4) Simply loading is done by the loader As the
name suggests, no relocation information is needed, if at all it is required
then that task can be done by either a programmer or assembler.
· For example
Line number
1 MAIN START 1000
.
.
.
.
.
.
1 JMP 5000
16 STORE; instruction at location 2000
END
1 SUM START 5000
2
20 JMP 2000
21 END
Advantages:
1.
It is simple to implement
2.
This scheme allows multiple programs or the source programs written different
languages. If there are multiple programs written in different languages then
the respective language assembler will convert it to the language and a 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 in the
main memory.
4. The process of execution is efficient.
Disadvantages:
1.
In
this scheme it is the programmer's duty to adjust all the inter segment
addresses and manually do the linking activity.
Relocating
Loader:-
·
To
avoid possible reassembling of all subroutine when a single subroutine is
changed and to perform the task of
allocation and linking for the programmer the relocating address introduce
·
The
BSS loader allows many procedure segments but only one data segment
·
The
output of the relocating assembler using a BSS scheme is the object program and
information about all programs its references.
·
The
statement calling SQRT would be translated into transfer instruction.
·
The
BSS Loader scheme is often used on computer with a fixed-length direct address
instruction format.
For. Ex:-
·
A2-16
bit Direct address
·
Work
for 216 Memory
Direct
Linking Loaders
·
The
direct linking loader is the most common type of loader. This type of loader is
a reloadable loader. The loader cannot have the direct access to the source
code.
·
And to place the object code in the memory
there are two situations: either the address of the object code could be
absolute which then can be directly placed at the specified location or the
address can be relative.
·
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 111 the current segment but can be used in the current
segment.
3) The list of all the symbols which are defined in the current segment but can be referred by the other segments.
Design
of direct linking loader
pass1:-
1.
Input object deck
2. Initial Program Load Address (IPLA)
3.
Program Load Address (PLA) counter
4.
Global External Symbol Table (GEST)
5.
A copy of the input (TXT card)
6. RLD (Relocation & Linkage Directory)
7.
END card
pass2:-
1.
copy of object
program
2.
IPLA parameter
(Initial Program Load Address) supplied by the OS or programmer to specify the
address to load the first segment
3.
PLA counter (Program Load address ) to keep
track of each segment location
4.
GEST (Global external symbol table) to store
each external symbol and its corresponding assigned core address
5.
Local External
Symbol Array(LESA) to establish correspondence between ESD ID used in ESD &
RLD cards
6.
ESD (external
symbol dictionary)
- External Symbol Dictionary (ESD)
•
ESD record combines
information about the entire symbol that is defined in this program.
•
But may be referenced
in the program but defined elsewhere.
2.
Text Cards (TXT)
•
Text card record
control the actual object code translated version of the source program.
3.
Relocation and Linkage Directory (RLD):
•
The RLD records
contains information about location in the program
•
Whose contents depend
on the address at which the program is placed?
4.
END CARD:
•
The END card records
indicate the end of the object File and specify the start address for
Execution.
Differences between Linker
and Loader are as follows:
LINKER |
LOADER |
The main
function of Linker is to generate executable files. |
Whereas
main objective of Loader is to load executable files to main memory. |
The
linker takes input of object code generated by compiler/assembler. |
And the
loader takes input of executable files generated by linker. |
Linking
can be defined as process of combining various pieces of codes and source
code to obtain executable code. |
Loading
can be defined as process of loading executable codes to main memory for
further execution. |
Linkers
are of 2 types: Linkage Editor and Dynamic Linker. |
Loaders
are of 4 types: Absolute, Relocating, Direct Linking, Bootstrap. |
Another
use of linker is to combine all object modules. |
It helps
in allocating the address to executable codes/files. |
Linker
is also responsible for arranging objects in program’s address space. |
Loader
is also responsible for adjusting references which are used within the
program. |
0 Comments