Beruflich Dokumente
Kultur Dokumente
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
GTU Questions
What is program relocation? How relocation is performed by
linker? Explain with example. OR Explain Self relocating program
Explain Absolute Loader with example.
What is overlay? Explain linking of overlay structured program.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction - Linker
The linker is a system program that combines the code of a target
program with codes of other programs and library routines.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction - Linker
The Linker
• Extracts this information from the object module
• Locates the needed programs and routines
• Combines them with the target code of the program
• Produces an executable program in machine (binary) language
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 55 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Schematic of program’s execution
Execution of a program is achieved through four steps:
(1) Translation (2) Linking (3) Relocation and (4) Loading
Source Data
Program
Binary
Translator Linker Loader Result
Program
Object Binary
module Programs
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 66 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction - Linker
Translation time address: Translation time address is used at the
translation time. This address is assigned by translator.
Linked time address: Link time address is used at the link time.
This address is assigned by linker.
Load time address: Load time address is used at the load time.
This address is assigned by loader
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 77 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Introduction - Linker
Translated origin: Address of origin assumed by the translator
(address specified in ORIGIN or START or default).
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 88 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Relocation and Linking Concept
Let AA be the set of absolute addresses - instruction or data
addresses – used in the instruction of a program P.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 99 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Relocation and Linking Concept
An address sensitive program P can be executed correctly only if
the start address of the memory area allocated to it is the same as
its translated origin.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 10
10 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Program Relocation
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.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 11
11 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Performing relocation
Let the translated and linked origins of program P be t_originp and
l_originp, respectively.
Let its translation time address be tsymb and link time address be
lsymb.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 13
13 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
RELOCATION EXAMPLE
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 14
14 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking
Consider an application program AP consisting of a set of program
units SP = {Pi}.
A program unit Pi interacts with another program unit Pj by using
addresses of Pj’s instructions and data in its own instructions.
To realize such interactions, Pj and Pi must contain public
definitions and external references as defined in the following:
• Public definition: a symbol pub_symb defined in a program unit which may
be referenced in other program units.
• External reference: a reference to a symbol ext_symb which is not defined
in the program unit.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 15
15 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking
EXTRN AND ENTRY statements
• An ENTRY statement in a program unit lists the public definitions of the
program unit.
• An EXTRN statement lists the symbols to which external references are
made in the program unit.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 16
16 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking
Linking is the action of putting the correct linked addresses in those
instructions of a program that contain external references.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 17
17 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Design of a Linker
The design of linker is divided into two parts:
1. Relocation: allocate space in memory for the programs
2. Linking:
• Resolve symbolic references between object files
• combines two or more separate object programs
• supplies the information needed to allow references between them
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 18
18 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Program Relocation
1. program_linked_origin := <link origin> from the linker command;
2. For each object module mentioned in the linker command
a. t_origin := translated origin of the object module;
b. OMsize := size of the object module;
c. relocation_factor := program_linked_origin – t_origin;
d. Read the machine language program contained in the program component
of the object module into the work-area.
e. Read RELOCTAB of the object module.
f. For each entry in RELOCTAB,
i. translated_address := address found in the RELOCTAB entry;
ii. address_in_work_area := address of work_area + translated_address –
t_origin;,
iii. Add relocation-factor to the operand address found in the word that has the
address address_in_work_area.
g. Program_linked_origin := program_linked_origin + OM_size;
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 19
19 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Program Linking
1. program_linked_origin := <link origin> from the linker command.
2. For each object module mentioned in the linker command
a. t_origin := translated origin of the object module;
b. OM_size := size of the object module;
c. relocation_factor := program_linked_origin – t_origin;
d. Read the machine language program contained in the program
component of the object module into the work_area.
e. Read LINKTAB of the object module.
f. Enter (object module name, program_linked_origin) in NTAB.
g. For each LINKTAB entry with type = PD
i. name := symbol field of the LINKTAB entry;
ii. linked_address := translated_address + relocation_factor;
iii. Enter (name, linked_address) in a new entry of the NTAB.
h. program_linked_origin := program_linked_origin + OM_size;
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 20
20 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Algorithm: Program Linking
3. For each object module mentioned in the linker command
a. t_origin := translated origin of the object module;
b. program_linked_origin := linked_adress from NTAB;
c. For each LINKTAB entry with type = EXT
i. address_in_work_area := address of work_area + program_linked_origin -
<link origin> in linker command + translated address – t_origin;
ii. Search the symbol found in the symbol field of the LINKTAB entry in NTAB and
note its linked address. Copy this address into the operand address field in the
word that has the address address_in_work_area.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 21
21 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
STATEMENTS
Example
ADD M/C CODE
ORIGIN 500
READ A 500 09 0 540
LOOP …. 501
. . . .
. . . .
A DS 1 540 SYMBOL LINKED
ADDRESS
541
P 900
A 940
STATEMENTS ADD M/C CODE
Q 942
START 200 ALPHA 973
ENTRY ALPHA
. . . .
ALPHA DS 25 231
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 22
22 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Self-Relocating Programs
Programs can be classified as,
1. A non-relocatable program
2. A relocatable program
3. A Self-relocatable program
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 23
23 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Self-Relocating Programs
The start address of the relocating logic is specified as the execution start address of
the program.
Thus the relocating logic gains control when the program is loaded in memory for the
execution.
It uses the load address and the information concerning address sensitive instructions
to perform its own relocation.
This is very important in time sharing operating systems where the load address of a
program is likely to be different for different executions.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 24
24 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking of Overlay Structured Programs
An overlay is part of a program (or software package) which has
the same load origin as some other part of the program.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 25
25 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Execution of Overlay structured programs
Execution of an overlay structured program proceeds as follows:
• To start with, the root is loaded in memory and given control for the
purpose of execution.
• Other overlays are loaded as and when needed.
• The loading of an overlay overwrites a previously loaded overlay with the
same load origin.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 26
26 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Execution of Overlay structured programs
The overlay structure of a program is designed by identifying
mutually exclusive modules that is, modules which do not call
each other.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 27
27 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Execution of an overlay structured program
For linking and execution of an overlay structured program in MS
DOS, the linker produces a single executable file at the output,
which contains two provisions to support overlays.
• First, an overlay manager module is included in the executable file.
• This module is responsible for loading the overlays when needed.
• Second, all calls that cross overlay boundaries are replaced by an interrupt
producing instruction.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 28
28 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Execution of an overlay structured program
The overlay manager receives control and loads the root.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 29
29 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Static Linking
In static linking, the liner links all modules of a program before its
execution begins; it produces a binary program that does not
contain any unresolved external references.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 30
30 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Dynamic Linking
Dynamic linking is performed during execution of a binary program.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 31
31 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Dynamic Linking
To facilitate dynamic linking, each program is first processed by
the static linker.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 32
32 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Loader Introduction
The loader is responsible for the activities such as allocation,
linking, relocation and loading.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 33
33 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Different Loading Schemes
Different Loading Schemes are,
• Compile-and-Go Loaders
• General Loader Schemes
• Absolute Loaders
• Relocating Loaders
• Linking Loaders
• Relocating Linking Loaders
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 34
34 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
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 directly put into their
assigned memory locations.
Advantages
• No additional routines are required to load the compiled code into memory.
• Execution speed is generally much superior to interpreted systems.
• They are simple and easier to implement.
Disadvantages
• There is wastage in memory space due to the presence of the assembler.
• The code must be re-assembled every time it is run.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 36
36 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Compile-and-Go Loaders
Compile & go
Loader
Source Compiler & go
program assembler
Executable code
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 37
37 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
General Loader Schemes
The general loading scheme improves the compile/assemble-and-
go scheme by allowing different source programs (or modules of
the same program) to be translated separately into their
respective object programs.
The loader usually combines the object codes and executes them
by loading them into the memory, including the space where the
assembler had been in the assemble-and-go scheme.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 38
38 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
General Loader Schemes
Rather than the entire assembler sitting in the memory, a small
utility component called loader does the job.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 39
39 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
General Loader Schemes
Disadvantages of the general loading scheme:
• The loader is more complicated and needs to manage multiple object files.
• Secondary storage is required to store object files, and they cannot be
directly placed into the memory by assemblers.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 40
40 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Absolute Loaders
An absolute loader loads a binary program in memory for
execution.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 41
41 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Absolute Loaders
The absolute loader notes the load origin and the length of the
program mentioned in the header record.
It then enters a loop that reads a binary image record and moves
the code contained in it to the memory area starting on the
address mentioned in the binary image record.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 42
42 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Absolute Loaders
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 43
43 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Absolute Loaders
Advantages Disadvantages
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 44
44 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Absolute Loaders
Advantages Disadvantages
The loader is simpler and just obeys If the subroutine is modified, the
the instruction regarding where to program has to be assembled again
place the object code in the main from first to last.
memory.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 45
45 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Relocating Loaders
A relocating loader loads a program in a designated area of
memory, relocates it so that it can execute correctly in that area of
memory and passes control to it for execution.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 46
46 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Practical Relocating Loaders
To avoid possible assembling of all subroutine when a single
subroutine is changed and to perform task of allocation and
linking for the programmer, the general class of relocating loader
was introduced.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 47
47 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking Loaders
A modern program comprises several procedures or subroutines together with
the main program module.
Special system program called linking loader gathers various object modules,
links them together to produce single executable binary program and loads
them into the memory.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 48
48 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Linking Loaders
This category of loaders leads to a popular class of loaders called
direct-linking loaders.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 49
49 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Relocating Linking Loaders
Relocating linking loaders combines together the relocating capabilities of
relocating loaders and the advanced linking features of linking loaders and
presents a more robust loading scheme.
This necessarily eliminates the need to use two separate programs for linking and
loading respectively.
These loaders can work efficiently with support from the operating system and
utilize the memory and other resources efficiently.
Unit
Unit –– 5:
5: Linkers
Linkers and
and Loaders
Loaders 50
50 Darshan
Darshan Institute
Institute of
of Engineering
Engineering &
& Technology
Technology