Module 3 Loaders and Linkers Design of An Absolute Loader
Module 3 Loaders and Linkers Design of An Absolute Loader
UNIT III
INTRODUCTION
For a simple absolute loader, all functions are accomplished in a single pass as follows:
1) The Header record of object programs is checked to verify that the correct
program has been presented for loading.
2) As each Text record is read, the object code it contains is moved to the
indicated address in memory.
3) When the End record is encountered, the loader jumps to the specified
address to begin execution of the loaded program.
43
Fig (b) shows a representation of the program from Fig (a) after loading.
44
The object code from device F1 is always loaded into consecutive bytes
of memory, starting at address 80. The main loop of the bootstrap keeps
the address of the next memory location to be loaded in register X.
After all of the object code from device F1 has been loaded, the bootstrap jumps to
address 80, which begins the execution of the program that was loaded.
Much of the work of the bootstrap loader is performed by the subroutine GETC.
GETC is used to read and convert a pair of characters from device F1
representing 1 byte of object code to be loaded. For example, two bytes =
C “D8” ‘4438’H converting to one byte ‘D8’H.
The resulting byte is stored at the address currently in register X, using
STCH instruction that refers to location 0 using indexed addressing.
The TIXR instruction is then used to add 1 to the value in X.
The absolute loader has several potential disadvantages. One of the most
obvious is the need for the programmer to specify the actual address at
which it will be loaded into memory.
On a simple computer with a small memory the actual address at which
the program will be loaded can be specified easily.
On a larger and more advanced machine, we often like to run several
independent programs together, sharing memory between them. We do
not know in advance where a program will be loaded. Hence we write
relocatable programs instead of absolute ones.
Writing absolute programs also makes it difficult to use subroutine libraries
efficiently. This could not be done effectively if all of the subroutines had
pre-assigned absolute addresses.
The need for program relocation is an indirect consequence of the change
to larger and more powerful computers. The way relocation is
implemented in a loader is also dependent upon machine characteristics.
Loaders that allow for program relocation are called relocating loaders or
relative loaders.
3.2.1 Relocation
Object program
Each Modification record specifies the starting address and length of the
field whose value is to be altered.
It then describes the modification to be performed.
In this example, all modifications add the value of the symbol COPY,
which represents the starting address of the program.
10
.
.
.
The Modification record is not well suited for use with all machine
architectures.Consider, for example, the program in Fig (2) .This is a
relocatable program written for standard version for SIC.
The important difference between this example and the one in Fig (1) is
that the standard SIC machine does not use relative addressing.
In this program the addresses in all the instructions except RSUB must modified
when the program is relocated. This would require 31 Modification records, which
results in an object program more than twice as large as the one in Fig (1).
The relocation bits are gathered together into a bit mask following the
length indicator in each Text record. In Fig (3) this mask is represented (in
character form) as three hexadecimal digits.
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.
If a Text record contains fewer than 12 words of object code, the bits
corresponding to unused words are set to 0.
For example, the bit mask FFC (representing the bit string 111111111100)
in the first Text record specifies that all 10 words of object code are to be
modified during relocation.
Example: Note that the LDX instruction on line 210 (Fig (2)) begins a new
Text record. If it were placed in the preceding Text record, it would not be
properly aligned to correspond to a relocation bit because of the 1-byte
data value generated from line 185.
Consider the three (separately assembled) programs in the figure, each of which
consists of a single control section.
Program 1 (PROGA):
12
Program 2 (PROGB):
Program 3 (PROGC):
13
PROGA:
14
PROGB:
PROGC:
15
Fig (4): The three programs as they might appear in memory after loading
and linking.
16
PROGA has been loaded starting at address 4000, with PROGB and PROGC
immediately following.
For example, the value for reference REF4 in PROGA is located at address 4054
(the beginning address of PROGA plus 0054).
17
The initial value (from the Text record) is 000014. To this is added the address
assigned to LISTC, which 4112 (the beginning address of PROGC plus 30).
Two other important variables are PROGADDR (program load address) and
CSADDR (control section address).
(1) PROGADDR is the beginning address in memory where the linked program
is to be loaded. Its value is supplied to the loader by the OS.
(2) CSADDR 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.
3.2.3.1 PASS 1
During Pass 1, the loader is concerned only with Header and Define
record types in the control sections.
19
1) The beginning load address for the linked program (PROGADDR) is obtained
from the OS. This becomes the starting address (CSADDR) for the first control
section in the input sequence.
2) The control section name from Header record is entered into ESTAB, with value given
by CSADDR. All external symbols appearing in the Define record for the control
section are also entered into ESTAB. Their addresses are obtained by adding the
value specified in the Define record to CSADDR.
3) When the End record is read, the control section length CSLTH (which was
saved from the End record) is added to CSADDR. This calculation gives the
starting address for the next control section in sequence.
At the end of Pass 1, ESTAB contains all external symbols defined in the
set of control sections together with the address assigned to each.
Many loaders include as an option the ability to print a load map that
shows these symbols and their addresses.
3.2.3.2 PASS 2
Pass 2 performs the actual loading, relocation, and linking of the program.
1) As each Text record is read, the object code is moved to the specified address
(plus the current value of CSADDR).
2) When a Modification record is encountered, the symbol whose value is to be
used for modification is looked up in ESTAB.
3) This value is then added to or subtracted from the indicated location in memory.
4) The last step performed by the loader is usually the transferring of control to
the loaded program to begin execution.
The End record for each control section may contain the address of the first
instruction in that control section to be executed. Our loader takes this as the
transfer point to begin execution. If more than one control section specifies a
transfer address, the loader arbitrarily uses the last one encountered.
If no control section contains a transfer address, the loader uses the
beginning of the linked program (i.e., PROGADDR) as the transfer point.
Normally, a transfer address would be placed in the End record for a main
program, but not for a subroutine.
20
This algorithm can be made more efficient. Assign a reference number, which is
used (instead of the symbol name) in Modification records, to each external
symbol referred to in a control section. Suppose we always assign the reference
number 01 to the control section name.
Fig (6): Object programs using reference numbers for code modification
21
22
Many loaders allow the user to specify options that modify the standard
processing
Typical loader option 1: Allows the selection of alternative sources of
input.
Ex : INCLUDE program-name (library-name) might direct the loader to
read the designated object program from a library and treat it as if it
were part of the primary loader input.
Loader option 2: Allows the user to delete external symbols or entire
control sections.
23
at load time have values that are relative to the start of the linked
program.
This means that the loading can be accomplished in one pass with no
external symbol table required.
If a program is to be executed many times without being reassembled,
the use of a linkage editor substantially reduces the overhead required.
Linkage editors can perform many useful functions besides simply
preparing an object program for execution. Ex., a typical sequence of
linkage editor commands used:
INCLUDE PLANNER (PROGLIB)
Fig (7): Processing of an object program using (a) Linking loader and
(b) Linkage editor
25
26
Fig (c): Control is then passed from OS to the routine being called
Fig (d): When the called subroutine completes it processing, it returns to its
caller (i.e., OS). OS then returns control to the program that issued the request.
Fig (e): If a subroutine is still in memory, a second call to it may not require another load
operation. Control may simply be passed from the dynamic loader to the called routine.
With the machine empty and idle there is no need for program relocation.
We can specify the absolute address for whatever program is first loaded
and this will be the OS, which occupies a predefined location in memory.
27
When some hardware signal occurs, the machine begins to execute this
ROM program.
On some computers, the program is executed directly in the ROM: on others,
the program is copied from ROM to main memory and executed there.
The particular device to be used can often be selected via console switches.
After the read operation is complete, control is automatically transferred to
the address in memory where the record was stored, which contains
machine where the record was stored, which contains machine
instructions that load the absolute program that follow.
If the loading process requires more instructions that can be read in a
single record, this first record causes the reading of others, and these in
turn can cause the reading of still more records – boots trap.
The first record is generally referred to as bootstrap loader:
Such a loader is added to the beginning of all object programs that are to
be loaded into an empty and idle system.
This includes the OS itself and all stand-alone programs that are to be run
without an OS.
28