You are on page 1of 12

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.

We would like to run several programs on a


more advanced machine.
2

Schemes for relocation


Modification record

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

Relocation bit

Hardware relocation

Modification record
In the object program, there is one Modification record for each value that must be changed during relocation. Each modification record specifies the starting address and length of the field whose value is to be altered.
Col. 11 Col. Col. 2-7 Col. 2-7 M M Starting location of the address field to be modified, relative to the Starting location of the address field to be modified, relative to the beginning of the program beginning of the program Col. 8-9 Length of the address field to be modified, in half-bytes Col. 8-9 Length of the address field to be modified, in half-bytes
If the field contains an odd number of half-bytes, the starting location begins in If the field contains an odd number of half-bytes, the starting location begins in the middle of the first byte. the middle of the first byte.
4

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

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

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

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.

Relocation bit
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. 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 programs 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

Object program with relocation by bit mask


bit mask

does not coincide with a 3-byte segment

1111111111002
1 1 1 1 1 1 1 1 1 1 0 0

1110000000002
1 1 1 0 0 0 0 0 0 0 0 0

each relocation bit is associated with a 3-byte segment of object code in the Text record.
11

Program linking
To link control sections together.

External definition 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.

12

PROGA

evaluate by assembler

13

PROGB

14

PROGC

15

After linking and loading

Relocation and linking operations performed on REF4 from PROGA

Algorithm for a linking loader


Two passes

Pass 1 assigns address to all external


symbols. Pass 2 performs the actual loading, relocating, and linking.

18

Main data structure for a linking loader (contd)


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. It is the beginning address in memory where the linked program is to be loaded. Its value is supplied by the operating system. 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

A variable, program load address, PROGADDR

A variable, control section address, CSADDR

Algorithm for Pass 1 of a linking loader


while record type != E { get PROGADDR from operating system read next input record set CSADDR to PROGADDR if record type == D { // for first control section for each symbol in record { while not end of input do { search ESTAB for symbol name read next input if found { // head record for control section set error flag set CSLTH to control section length // duplicate external symbol search ESTAB for control section name } else { if found { enter symbol into ESTAB with set error flag value (CSADDR+indicated // duplicate external symbol 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 section Symbol name Address Length

PROGA LISTA ENDA PROGB LISTB ENDB PROGC LISTC ENDC

4000 4040 4054 4063 40C3 40D3 40E2 4112 4124

0063

007F

0051

22

11

Reference number

The reference number 01 is assigned to control section name

Eachexternal reference in aacontrol section is Each external reference in control section is assigned aareference number. assigned reference number. Themain advantage of this reference-number The main advantage of this reference-number mechanism is that ititavoids multiple searches of mechanism is that avoids multiple searches of ESTAB for the same symbol during the loading of ESTAB for the same symbol during the loading of control section. control section.

Anexternal reference symbol can be looked up once in An external reference symbol can be looked up once in ESTAB once for each control section that uses it. ESTAB once for each control section that uses it. Thevalues for code modification can then be obtained by The values for code modification can then be obtained by simply indexing into an array of these values. simply indexing into an array of these values.

12

You might also like