0% found this document useful (0 votes)
153 views82 pages

Spos Unit 3 PPT 2022 - Compressed

The document discusses different types of loaders and linking schemes: 1. Absolute loaders load programs without relocation at specified addresses. 2. Relocating loaders adjust addresses to avoid conflicts and wasted memory. 3. Subroutine linkage allows calling and returning from subroutines through branch and link instructions that save return addresses.

Uploaded by

sakshi kumavat
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)
153 views82 pages

Spos Unit 3 PPT 2022 - Compressed

The document discusses different types of loaders and linking schemes: 1. Absolute loaders load programs without relocation at specified addresses. 2. Relocating loaders adjust addresses to avoid conflicts and wasted memory. 3. Subroutine linkage allows calling and returning from subroutines through branch and link instructions that save return addresses.

Uploaded by

sakshi kumavat
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/ 82

“LOADER AND LINKER”

Prepared By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.

CLASS : TE COMPUTER 2019


SUBJECT : SPOS (SEM-I) 22 AUG 2021
.UNIT : III
CONTENTS :-
• Introduction, Loader schemes:
1. Compile and Go
2. General Loader Scheme
3. Absolute Loaders
4. Subroutine Linkages
5. Relocating Loaders
6. Direct linking Loaders
• Overlay structure,
• Design of an Absolute Loader,
• Design of Direct linking Loader,
• Self-relocating programs,
• Static and Dynamic linking
Unit-1

 A program which accepts object program and


prepares them for execution.

2/16/2022 3
2/16/2022 4
 object code, or an object file, is the
representation of code that a compiler or
assembler generates by processing a source
code file.
 A linker is typically used to generate an
executable file by linking object files together.
 Object files often also contain data for use by the
code at runtime, relocation information,
program symbols (names of variables and
functions) for linking and/or debugging purposes,
and other debugging information.
2/16/2022 5
 In MS-DOS, Windows 95/98 etc object
modules have extension .obj and the
executable binary programs have .exe
extension. In UNIX, object modules have .o
extension and executable programs have no
extens

2/16/2022 6
 ALLOCATION: Allocates space in memory for the
programs( calculation program size)

 RELOCATION: Adjustment of addresses of all address


sensitive entities .

 LINKING: Resolve inter-segment (inter-program)


symbolic reference in object program. Linking of
object modules with each other.

 LOADING: Physically place the machine instructions


and data into memory and initiate execution.
2/16/2022 7
Source Loader Object program
Assembler ready for execution
Program

Fig.: Role of loader Memory

2/16/2022 8
 Loader is utility program which takes object
code as input prepares it for execution and
loads the executable code into the memory.

 Thus loader is actually responsible for


initiating the executions process.

2/16/2022 9
 Relocation and linking concepts

2/16/2022 10
 User defined function and library funtion
 Example-printf() ,scanf()
 The linking process makes address of modules known to
each other so that transfer of control takes place during
execution.
 Passing of parameter is handled by the linker.
 Public variable-same address
 External variable –defined in one module and used in another
module.
 Resolving of addresses of symbolic reference is handled by the
linker.
2/16/2022 11
 Linker-
1.Handled the passing of parameter
2.Resolving of address of symbolic reference

2/16/2022 12
 It is the process of modifying the addresses used
in the address sensitive instructions of a program
 So, program Can execute correctly from any
designated area of memory.
 Ex. MOVER AREG,X
 Ex.:-Program A, call function F1();
 Program A and F1() must be linked with each
other.

2/16/2022 13
Case I: Address assigned to Prog. A and F1 () when they translated to
memory 0

Drawback- a lot of storage area


100
is wasted
A
250
Wasted area

400
F1
500
Fig.: General Loading scheme
2/16/2022 14
Case II: These two module cannot co-exist at same storage locations.
Main
Storage
0

100 150
A
A
250 151
F1
100
251
F1
200
Relocated address given
Translation time by the loader
address
Fig.: Relocation to avoid address conflict or storage waste
2/16/2022 15
 A loader must relocate A and F1 to avoid address
conflict or storage waste.
 Relocation refers to adjustment of address field
not to movement of a program.

2/16/2022 16
 Various types of loader ,based on various functionalities
1. Compile and go loader
2. General loader scheme
3. Absolute loader
4. subroutine linkage
5. Relocating loader
6. Direct linking loader

2/16/2022 17
 Performing the loader function is to have the
assembler run in one part of memory and
place the assembled machine instructions
and data, as they are assembled,direcltly into
their assigned memory location.
 Diagram.
 Also called “assemble and go”

2/16/2022 18
1. COMPILE AND GO LOADER
Advantages:
 It is very simple to understand.

 It is simple to implement because loader is just an


extension to the assembler design.

2/16/2022 20
 Portion of the memory is wasted because the assembler
resides in the memory.
 It is necessary to assemble every time even though no
modifications are being done.
 Can not handle multiple program segments especially if
they are written in different languages (e.g. assembly
lang. and FORTRAN or PL/I)
 Scheme is good for small program not for bigger one
 Execution time = assemble time + load time
2/16/2022 21
Program modules A and B are loaded in memory after linking. It is
ready for execution

Object
Module
A
A
Loader

B
Object
Module
B

2/16/2022 Fig.: General Loading scheme 22


 An Absolute loader is the simplest of all other
loaders.
 It takes the output of Assembler and load into
memory without relocation.
 The output of the assembler can be stored on
any machine readable form of storage, But
most commonly it is stored on puched cards or
magnetic tape, disk, or drum.
 It loads a binary program in memory for
Execution .
2/16/2022 23
2/16/2022 25
 Binary program is stored in a file that
contains:
 Header records: Contains load Origin ,Length
of code, load time execution starting address
of program.
 Transfer record: contains entry point of
execution

2/16/2022 26
Advantages:
 Simple, easy to design and implement
 Since more core memory is available the user, as an
assembler is not memory at load time.
 No linking or relocation is efficient.
 Execution time = load time

2/16/2022 27
Disadvantages:
• Programmer must specify the starting address to the assembler for
the program where it should be loaded.
•so programmer must know memory management as well as memory
status at any time.
•It is very difficult to relocate in case of multiple subroutine.

2/16/2022 28
• In absolute loader 4 loader function are
performed by,
 Allocation: by programmer
 Linking: by programmer
 Relocation: by assembler
 Loading-by loader

2/16/2022 29
4. SUBROUTINE LINKAGE
5. RELOCATING LOADER
 Subroutine: In given program ,it is often
needed to perform a particular subtask many
times on different data values. such a
subtask is usually called a subroutine.
 Subroutine linkage method: The way in
which a machine makes it possible to call and
return from subroutine is referred to as its
Subroutine linkage method

2/16/2022 32
Subroutine Linkage: Instructions
There are two forms of the Branch and Link
instruction.
 BAL Branch and Link
 BALR Branch and Link, Register.

2/16/2022 33
 Calling the subroutine
 The format of the instruction is BAL
Reg,Address
 An example of such an instruction is BAL
8,P10PAGE

 Returning from the subroutine


 jump to an address contained in the register.
 The format of the instruction is: BR Reg
 An example of such an instruction is: BR 8

2/16/2022 34
MAIN START
SUBROUT START
EXTRN SUBROUT
USING *,15
-------------
-------------
--------------
--------------
L 15,=A(SUBROUT)
BR 14
BALR 14,15
END
….
…..
END

2/16/2022 35
5. RELOCATING LOADER
Relocation is the process of modifying the address used by program such that
program can execute correctly.

E.g. assume that a program A calls a function F1. The program A and the
function F1 must be linked with each other. But where we have to load in main
storage? A possible solution would be to load them according to address
assigned when they were translated.

Case (I) At the time of translation A has been given storage area from 100 to
250 while F occupies area between 400 to 500. If we were to load these
programs at their translated address , a lot of storage will be wasted.
5. RELOCATING LOADER
Case (II) at the time of translated, both A and F1 may have been translated
with the identical start address 100. A goes from 100 to 250 and F1 goes
from 100 to 200. These two modules cannot co-exist at same. The linker
must relocate A and F1 to avoid address conflict or storage waste A possible
relocation is shown in figure.

It may be noted that relocation more than simply moving a program from one are to
another in the storage. It refers to adjustment of address fields and not to movement of a
program.
 Relocation bits are used to solve the problem
of relocation.
 Transfer vector is used to solve the problem
of linking & Program length info to solve
allocation.

2/16/2022 38
 No suited for loading external data.
 Transfer vector increase the size.
 Does not facilitate access to data segment
that can be shared.

2/16/2022 39
6. DIRECT LINKING LOADER
 It is type of Re-locatable Loader.
 It is most common type of loader.
 It allows the programmer to use multiple
procedure segments and multiple data
segments.
 The assembler should give the following
information to the loader:
1.The length of the object code segment.
2.A list of all symbols which are not defined in the
current segment but can be used in the current
2/16/2022 42
3.A list of all symbols which are defined in the
current segment but can be referred in the
current segment.
4.Information about address constants.
5.Machine code translation of the source program
and relative address.
 To place the object code in the memory there
are two situations:1. Address of the object code
could be absolute.2. The address of object code
can be relative.
2/16/2022 43
 The list of symbols not defined in the current
segment but used in the current segment are
stored in a data structure called USE table.
 The lists of symbols defined in the current
segment and referred by the other segments
are stored in a data structure called
DEFINITION table.

2/16/2022 44
 Assembler generates 4 types of cards in the
object desk:
 ESD:External Symbol Dictionary (ESD) record:
 TXT: (TXT) records.
 RLD: Relocation and Linkage Directory (RLD):
 END :End of object deck

2/16/2022 45
1.ESD:External Symbol Dictionary (ESD) record:
Card contain information about all symbol that are
defined in this program, but that may reference
elsewhere, and all symbol referenced in this
program but defined elsewhere.
Source object symbol Type Relative Length
record Location
no./card no.
1 MAIN SD 0 36
2 RESULT LD 32 -
3 2/16/2022
SUM ER - - 46
 Type:
 SD: segment definition: symbol in segment
 LD: Local definition: symbol is defined in this
program but it can be referenced by other
program.
 ER: External symbol: defined in some external
program.

2/16/2022 47
 2. TXT: (TEXT) records.
 Text card contains actual object code.(translated
source code).
Source object Relative Location Object code
record no.
1 0

2 32

3 -

2/16/2022 48
3. RLD: Relocation and Linkage Directory (RLD):
Card contain information about those locations in
the program whose content depend on the
address at which the program is placed
Source ESD_ID Length(in Flag + or - Relative
object Byte) address
record no.

+ sign denote that something must be added to the constant 49


4.END: card indicate the end of object deck and
specifies the starting address for execution if
assembled routine is the main program.

2/16/2022 50
Allow multiple procedure and data segment.
Allow independent translation of the program.
Relocation Faclility

2/16/2022 51
 Not suitable in multitasking.
 It is necessary to allocate, relocate, link, and
load all of the subroutines each time in order
to execute a program
 loading process can be extremely time
consuming.

2/16/2022 52
 Design of direct linking loader is more complicated
than absolute loader.
 Input to loader is object module and this is divide into
4 section ESD,TXT,RLD,END
 It requires two passes to complete the linking process
 Pass I: assigns addresses to all external symbols
(Uses Global EST: GEST)
 Pass II: performs actual loading, relocation and
linking.
2/16/2022 53
 In Pass I ,a Global External Symbol table(GEST) is
prepared.
 It contain every external symbol and corresponding
absolute address value.

2/16/2022 54
Copy of
Object object
Module records

Pass I Pass II
Main
Memory

Object GEST
Module

2/16/2022 55
 Write entries of ESD,TXT,RLD,& Global
External Symbol Table(GEST) for PG1,PG2
given below

2/16/2022 56
Object Relative Source
Record Address Program
no.
1. 0 PG1 START
2. ENTRY PG1EN1,PG1EN2
3. EXTRN PG2EN1, PG2
4. 20 PG1EN1 =
5. 30 PG1EN2 =
6. 40 DC A (PG1EN1)
7. 44 DC A (PG1EN2+15)
8. 48 DC A (PG1EN2-PG1EN1-3)

9. 52 DC A (PG2)
10. 56 DC A
2/16/2022
(PG2EN1+PG1EN1+4) 57
Object Relative Source
Record Address Program
no.
12. 0 PG2 START

13. ENTRY PG2EN2

14. EXTRN PG1EN1, PG1EN2

15. 16 PG2EN1 =

16. 24 DC A (PG1EN1)

17. 28 DC A (PG1EN2+15)

18. 32 DC A (PG1EN2-
PG1EN1-3)
19. 36 END
2/16/2022 58
Source Name Type ID Relative length
object Address
record
no.
1. PG1 SD 01 0 60
2. PG1EN1 LD - 20
3. PG1EN2 LD - 30
4. PG2 ER 02 -
5. PG2EN1 ER 03 -

2/16/2022 59
 SD: Segment definition
 LD: Local definition (declared in this seg, can
be used by others)
 ER: External definition (declared in other , can
be used by this)
 Each SD and ER symbol is given unique
numeber which is to used in RLD

2/16/2022 60
Source Relative Contents Comments
object Address
record no.

6 40-43 20 ADDr PG1EN1=20


7 44-47 45 Addr PG1EN2+15=45
8 48-51 7 Addr PG1EN2-PG1EN1-3= 7
9 52-55 0 Addr PG2 is not known
10 56-59 -16 Addr of PG2EN1 +PG2-
PG1EN1+4
=0+0-20+4 = -16

2/16/2022 61
Source ESD_ID Length(in Flag + or - Relative
object Byte) Address
record
no.
6 01 4 + 40

7 01 4 + 44

9 02 4 + 52

10 03 4 + 56

10 02 4 + 56

10
2/16/2022 01 4 - 56 62
Source Name Type ID Relative length
object Address
record
no.
12. PG2 SD 01 0 36
13. PG2ENT1 LD - 16 -
14 PG1EN1 ER 02 - -
14 PG1ENT2 ER 03 - -

2/16/2022 63
Source object Relative Address Contents
record no.

16 24-27 0

17 28-31 15

18 32-35 -3

2/16/2022 64
Source ESD_ID Length(in Flag + or - Relative
object Byte) Address
record
no.
16 02 4 + 24

17 03 4 + 28

18 03 4 + 32

18 02 4 - 32

2/16/2022 65
 Contain external symbol and its corresponding
address.
 If we assume that PG2 is loaded at location 268.
External Symbol Assigned Memory Address

PG1 208

PG1ENT1 228

PG1ENT2 238

PG2 268

PG2ENT1 284

2/16/2022 66
 LESA is created for each segment.it contains
SD & LD type entries of each segment.
Assigned Memory
 LESA for PG1 Address
PG1 208
PG1ENT1 228
PG1ENT2 238
 LESA for PG2
Assigned Memory
Address
PG2 268
PG2ENT1 284

2/16/2022 67
OVERLAY STRUCTURE
• Program execution need not be achieved by loading all the
parts of large program in memory.
• So,that we can reduce memory requirement of such program
by loading required parts of program in memory.
• Hence some parts of program are given the same load
address during linking.
• Therefore,at any time,only one of these parts of program can
be loaded in memory because loading of other parts that has
same load address will overwrite it.

2/16/2022 68
OVERLAY STRUCTURE

• Overlay is a part of program that has same load origin as


some other parts of the program .and the program which
contains overlays is called as overlay structured program.
• It consist of Permanently resident portion known as
ROOT.
• Set of overlays that will e loaded in memory as per the
requirements.
• Overlay manager is linked with the root.
• Root is loaded in memory .
2/16/2022 69
OVERLAY STRUCTURE
EXAMPLE

• Suppose a program consisting of five subprograms (A{20k},B{20k},


C{30k}, D{10k}, and E{20k}) that require 100K bytes of core.

• Subprogram A only calls B, D and E;

• subprogram B only calls C and E;

• subprogram D only calls E

• subprogram C and E do not call any other routines

• Note that procedures B and D are never in used the same time;
neither are C and E.

2/16/2022 70
OVERLAY STRUCTURE

2/16/2022 71
What is linker? Why program relocation is required and
how is it performed?
Any usable program written in any language has to use functions /
subroutines. These functions could be either user defined functions
or they can be library functions.

For example, consider a program written in C language such a


program may contain calls to functions like printf( ). During
program execution main program calls the function
1) The linking process makes address of modules known to each
other so that transfer of control takes place.
2) Passing of parameters is handled by the linker.
3) An external variable can be defined in one module and can be
used in another module.
SELF RELOCATING PROGRAM
Program relocatibility refers to the ability to load and execute a
given program into an arbitrary place in memory as opposed to a
fixed set of locations specified at program translation time
depending on how and when the mapping from virtual address
space to the physical address space takes place in given relocation:
(a) Static (b) Dynamic .

A self relocating program is a program which can perform the


relocation itself. Self relocating program contain the relocating
logic, so no need of a linker in that.
STATIC & DYNAMIC LINK LIBRARIES
1. Static Linking :
A static linker takes object files produced by the compiler
including library functions and produces and executable file.
The executable file contains a copy of every subroutine (user
defined or library function.) The biggest disadvantage of the
static linking is that each executable file contains its own
copy of the library routines. If many programs containing
same library routines are executed then memory is wasted.
STATIC & DYNAMIC LINK LIBRARIES
2. Dynamic Linking :
Dynamic linking defers much of the linking process until a
program starts running. Dynamic linking involves the
following steps:
1) A reference to an external module during run time causes
the loader to find the target module and load it.

2) Perform relocation during run time

Dynamic linking permits a program to load and unload


routines at run time.
STATIC & DYNAMIC LINK LIBRARIES
BASIS FOR
COMPARISON LINKER LOADER
Basic It generates the executable It loads the executable module to
module of a source 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/Approac Linkage Editor, Dynamic Absolute loading, Relocatable


h linker. loading and Dynamic Run-time
loading.
STATIC & DYNAMIC LINK LIBRARIES
S. LINKER LOADER
No.
1 A linker is an important utility program A loader is a vital component of an
that takes the object files, produced by operating system that is accountable for
the assembler and compiler, and other loading programs and libraries.
code to join them into a single
executable file.

2 It uses an input of object code produced It uses an input of executable files produced
by the assembler and compiler. by the linker.

3 The foremost purpose of a linker is to The foremost purpose of a loader is to load


produce executable files. executable files to memory.

4 Linker is used to join all the modules. Loader is used to allocate the address to
executable files.
5 It is accountable for managing objects in It is accountable for setting up references
the program’s space. that are utilized in the program.
STATIC & DYNAMIC LINK LIBRARIES
STATIC & DYNAMIC LINK LIBRARIES
DYNAMIC LINK LIBRARIES
CALLBACK FUNCTION
COMPARE CALLBACK &
NORMAL FUNCTION
THANK YOU!!!
My Blog : https://anandgharu.wordpress.com/

Email : [email protected]

You might also like