0% found this document useful (0 votes)
118 views22 pages

System Software Unit 3

The document discusses loaders and linkers. It describes the functions of loading, relocation and linking. Loading brings the object program into memory for execution. Relocation modifies the program so it can be loaded at different addresses. Linking combines multiple object programs and allows references between them. Absolute loaders load programs without relocation or linking at specified addresses. Relocating loaders allow programs to be relocated to different addresses using modification records or relocation bits. Bootstrap loaders load the initial operating system programs.

Uploaded by

Faizal Basheer
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)
118 views22 pages

System Software Unit 3

The document discusses loaders and linkers. It describes the functions of loading, relocation and linking. Loading brings the object program into memory for execution. Relocation modifies the program so it can be loaded at different addresses. Linking combines multiple object programs and allows references between them. Absolute loaders load programs without relocation or linking at specified addresses. Relocating loaders allow programs to be relocated to different addresses using modification records or relocation bits. Bootstrap loaders load the initial operating system programs.

Uploaded by

Faizal Basheer
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/ 22

www.csetube.

in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 1 Page 2 of 2

Loaders and Linkers

Loading, Relocation, and Linking

Loading which brings the object program into memory for execution.
Relocation which modifies the object program so that it can be loaded at an address different
from the location originally specified.

.in
Linking which combines two or more separate object programs and supplies the information
needed to allow references between them.

Loaders and Linkers

be
A loader is a system program that performs the loading function. Many loaders also support
relocation and linking.
Some systems have a linker (or linkage editor) to perform the linking operations and a separate
loader to handle relocation and loading.
tu
Basic loader functions

The fundamental functions of a loader is bringing an object program into memory and starting its
se

execution.

Absolute loader


.c

It is very simple.
All operations are accomplished in a single pass.
An object program is loaded at the address specified on the START directive.

w

No relocation or linking is needed.


The loader jumps to the address specified on the END directive to begin execution of the loaded
program.
w

Algorithm for an absolute loader


w

begin

read Header record

verify program name and length read first Text record

while record type E do begin

{ if object code is in character form, convert into internal representation }


move object code to specified location in memory read next object program record

end

jump to address specified in End record


end

1
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 1 Page 2 of 2

.in
be
tu
se
.c

Disadvantages of the scheme of absolute loaders

This scheme needs the programmer to specify the actual address at which it will be loaded into
w

memory.
This scheme make it difficult to use subroutine libraries efficiently.
The subroutines must be pre-assigned absolute addresses.
w
w

2
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 2 Page 2 of 2

Bootstrap loader
When a computer is turned on or restarted, a special type of absolute loader, called bootstrap loader,
is executed.
The bootstrap loader loads the first program to be run by the computer usually an operating system,
from the boot disk (e.g., a hard disk or a floppy disk)

.in
Bootstrap loader for SIC/XE

be
tu
se
.c
w
w
w

3
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 2 Page 2 of 2

Example: Bootstrap loader for x86 systems

On x86 systems, when the CPU is powered on or reset, it sets its registers to a known state and
jumps to the starting address of boot ROM.
The boot ROM code reads the first block of the floppy or the hard disk into memory location zero
and then jumps to location zero.
The program in the first block in turn loads a slightly larger operating system boot program from a

.in
known place on the disk into memory, and it jumps to that program.
The just loaded program loads in the operating system and starts it.
There can be even more steps such as a boot manager that decides from which disk partition to read
the operating system boot program.

be
tu
se
.c
w
w
w

4
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 3 Page 2 of 2

Machine-dependent loader features

Relocation

Loaders that allow for program relocation are called relocating loaders or relative loaders.
The need for program relocation is a consequence of the change to larger and more powerful computers.
o We would like to run several programs on a more advanced machine.

.in
Schemes for relocation

Modification record

be
A modification record is used to describe each part of the object code that must be changed when
the program is relocated.
Relocation bit
A relocation bit associated with each word of object code is used to indicate whether or not this
tu
word should be changed when the program is relocated.
Hardware relocation
Some computers provide a hardware relocation capability that eliminates some of the need for the
loader to perform program relocation.
se

Modification record

In the object program, there is one Modification record for each value that must be changed during
.c

relocation.
Each modification record specifies the starting address and length of the field
whose value is to be altered.
w

Col. 1 M
Starting location of the address field to be modified,
w

Col. 2-7 relative to the


beginning of the program
Col. 8-9 Length of the address field to be modified, in half-bytes
w

If the field contains an odd number of half-bytes, the


starting location begins in
the middle of the first byte.

5
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 3 Page 2 of 2

Object program with relocation by Modification record

.in
be
tu
Example of a SIC/XE program
se
.c
w
w
w

6
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 3 Page 2 of 2

.in
Relocation bit

be
tu
On a machine that
o primarily uses direct addressing and has a fixed instruction format,
it is often more efficient to specify relocation using the technique of the relocation
se
bit.
A relocation bit associated with each word of object code is used to indicate
whether or not this word should be changed when the program is relocated.
o If the relocation bit corresponding to a word of object code is set to 1, the
programs starting address is to be added to this word when the program is
.c

relocated.
o A bit value of 0 indicates that no modification is necessary.
o The bits corresponding to unused words are set to 0.
w

Object program with relocation by bit mask


w
w

7
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 4 Page 2of2

Program linking

To link control sections together.


o External definition
o External reference

The assembler evaluate as mush of an expression as it can. The remaining terms are
passed on to the linking loader via Modification records.

.in
be
tu
se
.c
w
w

Relocation and linking operations performed on REF4 from PROGA


w

8
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 5 Page 2 of 2

Algorithm for a linking loader

Two passes
o Pass 1 assigns address to all external symbols.
o Pass 2 performs the actual loading, relocating, and linking.
Main data structure for a linking loader

An external symbol table ESTAB,

.in
o It is used to store the name and address of each external symbol in the set of
control section being loaded.
o A hash organization is often used for ESTAB.
A variable, program load address, PROGADDR

be
o It is the beginning address in memory where the linked program is to be
loaded.
o Its value is supplied by the operating system.
A variable, control section address, CSADDR
o It contains the starting address assigned to the control section currently being
tu
scanned by the loader.
o This value is added to all relative addresses within the control section to
convert them to actual addresses.
se
.c
w
w
w

9
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 6 Page 2 of 2

Algorithm for Pass 1 of a linking loader


get PROGADDR from operating while record type != E {
system
read next input record
set CSADDR to PROGADDR

.in
if record type == D {
// for first control section
for each symbol in record {
while not end of input do { search ESTAB for symbol
name

be
read next input
if found {
// head record for control section
set CSLTH to control section set error flag
tu
length
search ESTAB for control // duplicate external symbol
section name
} else {
se

if found { enter symbol into ESTAB


with
set error flag
value (CSADDR+indicated
.c

// duplicate external symbol


address)
} else {
}
w

enter control section name into


ESTAB with }
value CSADDR
}
w

}
}
add CSLTH to CSADDR
w

// starting address for next control


section
20
}

10
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 6 Page 2 of 2

Algorithm for Pass 2 of a linking loader

set CSADDR to PROGADDR set EXECADDR to PROGADDR while not end of input {

read next input record // head record set CSLTH to control section length while record type
!= E {

read next input record if record tyoe == T {

.in
if object code is in character form, convert into internal representation move object code
from record to location (CSADDR + specified address)

} else if record type ==M {

be
search ESTAB for modifying symbol name if found {

add or subtract symbol value at location (CSADDR + specified address)


}}}
tu
if an address is specified { A transfer address would be
placed in the
set EXECADDR to (CSADDR + specified End record for a main program
se
address) but not for
add CSLTH to CSADDR
a subroutine.
}}
jump to location given by EXECADDR // to start execution of loaded
.c

program
w
w
w

11
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 7 Page 2 of 2

Machine-Independent Loader Features

Automatic library search

Many linking loaders can automatically incorporate routines from a


subprogram library into the program being loaded.

.in
The subroutines called by the program are automatically fetched from the
library, linked with the main program, and loaded.
The programmer does not need to take any action beyond mentioning the
subroutine names as external references in the source program

be
Linking loaders that support automatic library search must keep track of
external symbols that are referred to, but not defined, in the primary input to
the loader.
At the end of pass 1, the symbols in ESTAB that remain undefined represent
tu
unresolved external references.
The loader searches the library for routines that contain the definitions of
these symbols, and processes the subroutines found by this search process
exactly as if they had been part of the primary input stream.
se

The subroutines fetched from a library in this way may themselves contain
external references. It is necessary to repeat the library search process until
all references are resolved.
If unresolved references remain after the library search is completed, they
.c

are treated as errors.


If a symbol (or a subroutine name) is defined both in the source program
and in the library, the one in the source program is used first.
w

Search in library
w

The library to be searched by the loader contain assembled or compiled


version of the subroutines (object programs).
w

Search libraries

By scanning the Define records for all of the object programs on


the library.

an inefficient method

by scanning a special file structure for the libraries.

This structure contains a directory that gives the name of each


routine and a pointer to its address within the file.

Some operating systems keep the directory for commonly used


libraries permanently in memory.
12
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period:8 Page 2 of 2

Loader options

Many loaders allow the user to specify options that modify the standard
processing.
Many loaders have a special command language that is used to specify
options.

.in
For example:
Include program-name (library name)
Direct the loader to read the designated object program from a

be
library
Delete csect-name
Instruct the loader to delete the named control sections from the
set of programs being loaded
Change name1, name2
tu
Cause the external symbol name1 to be changed to name2
wherever it appears in the program
se

Application examples for loader options

In the COPY program, two subroutines RDREC and WRREC are designed
to perform read records and write records.
.c

Suppose that a set of utility subroutines provides READ and WRITE


subroutines which have similar functions.
We can use the following loader commands to make the COPY object
w

program use READ instead of RDREC and WRITE rather than WRREC
without modifying the source
w

Program and reassembling it,


w

Include READ (UTLIB)

Include WRITE (UTLIB)

Delete RDREC, WRREC

Change RDREC, READ

Change WRREC, WRITE

Another common loader option involves the automatic inclusion of library


routines to satisfy external references.

13
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period:8 Page 2 of 2

For example,

LIBRARY MYLIB

Such user- specified libraries are normally searched before the standard
system libraries.

.in
Loaders that perform automatic library search to satisfy external references
often allow the user to specify that some references not be resolved in this
way.

be
For example,

NOCALL STDDEV, PLOT, COREL


tu
Instruct the loader that these external references are to remain
unresolved.
se

This avoids the overhead of loading and linking the unneeded routines,
and saves the memory space that would otherwise be required.

Print out a load map.


.c

Specify the location at which execution is to begin.


Control whether or not the loader should attempt to execute the program if
errors are detected during the load.
w
w
w

14
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 9 Page 2 of 2

Linkage editors

Difference between a linkage editor and a linking loader:

Linking loader

Performs all linking and relocation operations, including automatic library search,

.in
and loads the linked program into memory for execution.

Linkage editor

be
Produces a linked version of the program, which is normally written to a file or
library for later execution.
o A simple relocating loader (one pass) can be used to load the program into
memory for execution.
tu
o The linkage editor performs relocation of all control sections relative to the
start of the linked program.
o The only object code modification necessary is the addition of an actual load
address to relative values within the program
se

Compare between linking loader and linkage editor

Linking loader
.c

Suitable when a program is reassembled for nearly every execution


o In a program development and testing environment
w

o When a program is used so infrequently that it is not worthwhile to store the


assembled and linked version.
w

Linkage editor
w

Suitable when a program is to be executed many times without being


reassembled because resolution of external references and library searching are
only performed once.

15
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 9 Page 2 of 2

Linking loader vs. linkage editor

Linking loader Linkage editor

.in
be
tu
se
.c
w
w

Additional Functions of Linkage Editors


w

Replacement of subroutines in the linked program

For example:
INCLUDE PLANNER(PROGLIB)
DELETE PROJECT {DELETE from existing PLANNER}
INCLUDE PROJECT(NEWLIB) {INCLUDE new version}
REPLACE PLANNER(PROGLIB)

Construction of a package for subroutines generally used together

There are a large number of cross-references between these subroutines due to their closely related
functions.
16
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 9 Page 2 of 2

For example (P.155):

INCLUDE READR(FTNLIB)

INCLUDE WRITER(FTNLIB)

SAVE FTNIO(SUBLIB)

.in
Specification of external references not to be resolved by automatic library search
o Can avoid multiple storage of common libraries in programs.
o Need a linking loader to combine the common libraries at execution time

be
tu
se
.c
w
w
w

17
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 10 Page 2 of 2

Dynamic Linking

Comparison
o Linkage editors perform linking operations before the program is loaded for
execution
o Linking loaders perform linking operations at load time
o Dynamic linking (dynamic loading, load on call) perform linking at execution

.in
time

Delayed Binding
o Avoid the necessity of loading the entire library for each execution, i.e. load

be
the routines only when they are needed
o Allow several executing programs to share one copy of a subroutine or library
(Dynamic Link Library, DLL)
tu
O.S. services request of dynamic linking
o Dynamic loader is one part of the OS
o Instead of executing a JSUB instruction that refers to an external symbol,
se
the program makes a load-and-call service request to the OS

Example (Figure 3.14)


o When call a routine, pass routine name as parameter to O.S. (a)
o If routine is not loaded, O.S. loads it from library and pass the control to the routine
.c

(b and c)
o When the called routine completes it processing, it returns to the caller (O.S.) (d)
o When call a routine and the routine is still in memory, O.S. simply passes the control
w

to the routine (e)


w
w

18
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 10 Page 2 of 2

Example of Dynamic Linking

.in
be
tu
se
.c
w
w
w

19
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period:11 Page 2 of 2

Bootstrap Loaders

How is the loader itself loaded into memory?

o An absolute loader program is permanently resident in a read-only memory ROM

.in
o Copy absolute loader in ROM into RAM for execution (optional)

o Read a fixed-length record from some device into memory at a fixed location. After

be
the read operation, control is automatically transferred to the address in memory
tu
se
.c
w
w
w

20
www.csetube.in
www.csetube.in

Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 12 Page 2 of 2

Implementation examples

Basic kinds of information of an object file

Header information
o Header record

.in
o Object code
o Text record
Relocation information
o Modification record
Symbols

be
o Define record
o Refer record
Debugging information
End record
tu
MS-DOS Linker
se

MS-DOS LINK is a two-pass linkage editor.

Record
types Descripti
on SIC/XE
.c

THEADR Translator header Head record


TYPDE
w

F External symbols and references Define Record


PUBDE Refer Record
LNAME
F
w

S Segment definition and grouping


EXTDEF
SEGDE
LEDATA Text record
F Translated instructions and data
w

LIDATA
GRPDEF
FIXUPP Relocation and linking information Modification record
MODEND End of object module End record
MS-DOS object module

DOS COM files: the null object format

A COM file consists of nothing other than binary codes.


o For example, printe.com prints a single
character E. printe.com is an executable
program on DOS. The length of this program is only 8
bytes.
.
21
www.csetube.in
Lecture plan
Code & Name of subject: CS2304 System Software R/TP/01
Unit number & Name: UNIT III LOADERS AND LINKERS ISSUE: C REV: 00
Period: 12 Page 2 of 2

Loading a COM file


Loading a COM file requires the following steps.

.in
o Find a suitable area of memory.
o Create a PSP.
o Read in the program code immediately after the PSP.

be
o Set the stack point and segment registers.
o Jump to CS:100 to start the program.
DOS EXE Files: relocation



fixups.
tu
The DOS EXE files contain relocation entries, often called
Loading an EXE file is only slightly more complicated than
se
loading a COM file, requiring the following steps.
Read in the header and check the magic number for validity.
Find a suitable area of memory.
Create a PSP.
.c

Read in the program code immediately after the PSP.


Start reading fixups.
Set the stack point and jump to the starting address to start the program.
w
w
w

22
www.csetube.in

You might also like