You are on page 1of 12

Machine-dependent loader

features

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

1
Schemes for relocation
z Modification record
• A modification record is used to describe each part of
the object code that must be changed when the
program is relocated.
z Relocation 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.
z Hardware relocation
• Some computers provide a hardware relocation
capability that eliminates some of the need for the
loader to perform program relocation.

Modification record
z In the object program, there is one
Modification record for each value that
must be changed during relocation.
z Each modification record specifies the
starting address and length of the field
whose value is to be altered.
Col.
Col.11 MM
Col.
Col.2-7
2-7 Starting
Startinglocation
locationof
ofthe
theaddress
addressfield
fieldtotobe
bemodified,
modified,relative
relativetotothe
the
beginning
beginningofofthe
theprogram
program
Col.
Col.8-9
8-9 Length
Lengthofofthe
theaddress
addressfield
fieldtotobe
bemodified,
modified,ininhalf-bytes
half-bytes
IfIfthe
thefield
fieldcontains
containsananodd
oddnumber
numberofofhalf-bytes,
half-bytes,the
thestarting
startinglocation
locationbegins
beginsinin
the
themiddle
middleofofthe
thefirst
firstbyte.
byte.
4

2
Object program with relocation
by Modification record

Relocatable

the same as the absolute program with


starting address 0

Modification records

Example of a SIC/XE program (1/3)

actual address
6

3
Example of a SIC/XE program (2/3)

Example of a SIC/XE program (3/3)

4
Relocatable program for a
standard SIC machine

The standard SIC machine does not


use relative addressing. Therefore, in
this program the addresses in all the
instructions except RSUB must be
modified when the program is
relocated. This would enlarge the size
of the object program.
9

Relocation bit
z On a machine that
• primarily uses direct addressing and
• has a fixed instruction format,
it is often more efficient to specify relocation using the
technique of the relocation bit.
z 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.
• If the relocation bit corresponding to a word of object code is
set to 1, the program’s starting address is to be added to this
word when the program is relocated.
• A bit value of 0 indicates that no modification is necessary.
• The bits corresponding to unused words are set to 0.

10

5
Object program with relocation
by bit mask
bit mask

does not coincide with a 3-byte segment

1111111111002 1110000000002
1
1 1
1 1
1 0
1 0
1 0
1 0
1 0
1 0
1 0
1 0
0 0
0

each relocation bit is associated with a 3-byte


segment of object code in the Text record.
11

Program linking
z To link control sections together.
• External definition
• External reference
z The assembler evaluate as mush of an
expression as it can. The remaining
terms are passed on to the linking loader
via Modification records.

12

6
PROGA

evaluate by assembler

13

PROGB

14

7
PROGC

15

After linking and loading

8
Relocation and linking operations
performed on REF4 from PROGA

Algorithm for a linking loader

z Two passes
• Pass 1 assigns address to all external
symbols.
• Pass 2 performs the actual loading, relocating,
and linking.

18

9
Main data structure for a linking
loader (cont’d)

z An external symbol table ESTAB,


• It is used to store the name and address of each external
symbol in the set of control section being loaded.
• A hash organization is often used for ESTAB.
z A variable, program load address, PROGADDR
• It is the beginning address in memory where the linked
program is to be loaded.
• Its value is supplied by the operating system.
z A variable, control section address, CSADDR
• It contains the starting address assigned to the control
section currently being scanned by the loader.
• This value is added to all relative addresses within the
control section to convert them to actual addresses.

19

Algorithm for Pass 1 of a linking loader


get PROGADDR from operating system while record type != ‘E’ {
set CSADDR to PROGADDR read next input record
// for first control section if record type == ‘D’ {
while not end of input do { for each symbol in record {
read next input search ESTAB for symbol name
// head record for control section if found {
set CSLTH to control section length set error flag
search ESTAB for control section name // duplicate external symbol
if found { } else {
set error flag enter symbol into ESTAB with
// duplicate external symbol value (CSADDR+indicated
address)
} else {
}
enter control section name into ESTAB with
value CSADDR }
} }
}
add CSLTH to CSADDR
// starting address for next control section
20
}

10
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’ {
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’ {
search ESTAB for modifying symbol name
if found {
add or subtract symbol value at location (CSADDR + specified address)
}}}
if an address is specified { A transfer address would be placed in the
set EXECADDR to (CSADDR + specified address)End record for a main program but not for
add CSLTH to CSADDR a subroutine.
}}
jump to location given by EXECADDR // to start execution of loaded program
21

ESTAB
Control Symbol
Address Length
section name

PROGA 4000 0063


LISTA 4040
ENDA 4054
PROGB 4063 007F
LISTB 40C3
ENDB 40D3
PROGC 40E2 0051
LISTC 4112
ENDC 4124

22

11
Reference number

The reference number 01 is


assigned to control section name

••Each
Eachexternal
externalreference
referenceininaacontrol
controlsection
sectionisis
assigned
assignedaareference
referencenumber.
number.
••The
Themain
mainadvantage
advantageofofthis
thisreference-number
reference-number
mechanism
mechanism is that it avoidsmultiple
is that it avoids multiplesearches
searchesofof
ESTAB
ESTABfor forthe
thesame
samesymbol
symbolduring
duringthe
theloading
loadingofof
control
controlsection.
section.
• •An
Anexternal
externalreference
referencesymbol
symbolcancanbebelooked
lookedupuponceonceinin
ESTAB
ESTABonce
oncefor
foreach
eachcontrol
controlsection
sectionthat
thatuses
usesit.it.
• •The
Thevalues
valuesfor
forcode
codemodification
modificationcancanthen
thenbe
beobtained
obtainedby by
simply
simplyindexing
indexinginto
intoan
anarray
arrayofofthese
thesevalues.
values.

12

You might also like