Module 4
Module 4
MODULE 4
SYLLABUS
Basic Loader functions - Design of absolute loader, Simple bootstrap Loader, Machine
dependent loader features- Relocation, Program Linking, Algorithm and data structures of two
pass Linking Loader, Machine independent loader features , Loader Design Options
Page 1
CS303 System Software Module 4
The system software which performs linking operation is called linker. The system software
which loads the object program into memory and starts its execution is called loader.
Linkers and loaders perform several related but conceptually separate actions.
Page 2
CS303 System Software Module 4
When a computer is first turned on or restarted, a special type of absolute loader, called
a bootstrap loader, is executed. This bootstrap loader loads the first program to be run by the
computer – usually an operating system.
Page 3
CS303 System Software Module 4
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.
This bootstrap main function reads object code from device F1 and enters it into memory starting at address 80
(hexadecimal) . After all of the code from dev F1 has been seen entered into memory, the bootstrap executes a
jump to address 80 to begin execution of the program just loaded. Register X contains the next address to be
loaded.
BOOT START 0
CLEAR A CLEAR REGISTER A TO ZERO
LDX #128 INITIALIZE REGISTER X TO HEX 80
LOOP JSUB GETC READ HEX DIGIT FROM PROGRAM BEING LOADED
RMO A, S SAVE IN REGISTER S
SHIFTL S , 4 MOVE TO HIGHORDER 4 BITS OF BYTE
JSUB GETC GET NEXT HEX DIGIT
ADDR S ,A COMBINE DIGITS TO FORM ONE BYTE
STCH 0 ,X STORE AT ADDRESS IN REGISTER X
TIXR X ADD 1 TO MEMORY ADDRESS BEING LOADED
JUMP LOOP LOOP UNTIL END OF INPUT IS REACHED
Page 4
CS303 System Software Module 4
GETC subroutine read one character from input device and convert it from ASCII code to
hexadecimal digit value. The converted digit value is returned in register A. When an end of file
is read, control is transferred to the starting address (hex 80)
Page 5
CS303 System Software Module 4
Writing absolute programs also makes it difficult to use subroutine libraries efficiently.
This could not be done effectively if all of the subroutines had preassigned 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.
Program relocation is explained in Module 2
Loaders that has the capability to perform relocation are called relocating loaders or
relative loaders.
Modification Record
A Modification record is used to describe each part of the object code that must be
changed when the program is relocated.
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.
Consider the following object program, here the records starting with M represents the
modification record. In this example, the record M 000007 05 + COPY is the
Page 6
CS303 System Software Module 4
modification suggested for the statement at location 000007 and requires modification
of 5-half bytes and the modification to be performed is add the value of the symbol
COPY, which represents the starting address of the program.(means add the starting
address of program to the statement at 000007). Similarly for other records.
The Modification record is not well suited for certain cases. In some programs the
addresses in majority of instructions need to be modified when the program is relocated. This
would require large number of Modification records, which results in an object program more
than twice as large as the normal. In such cases, the second method called relocation bit is used.
Relocation Bit
To overcome the disadvantage of modification record, relocation bit is used.
The Text records are the same as before except that there is a relocation bit associated
with each word of object code.
Since all SIC instructions occupy one word, this means that there is one relocation bit
for each possible instruction.
The relocation bits are gathered together into a bit mask following the length indicator
in each Text record.
Text record format
Page 7
CS303 System Software Module 4
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.
If a Text record contains fewer than 12 words of object code, the bits corresponding to
unused words are set to 0.
In the following object code, 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.
Page 8
CS303 System Software Module 4
Page 9
CS303 System Software Module 4
Page 10
CS303 System Software Module 4
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.
Page 11
CS303 System Software Module 4
When a Modification record is encountered, the symbol whose value is to be used for
modification is looked up in ESTAB.
This value is then added to or subtracted from the indicated location in memory.
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. Loader takes this as
the transfer point to begin execution.
Page 12
CS303 System Software Module 4
3. At the end of Pass 1, the symbols in ESTAB that remain undefined represent unresolved
external references
4. The loader searches the libraries specified (or standard) for undefined symbols or
subroutines
The library search process may be repeated since the subroutines fetched from a library
may themselves contain external references. Programmer defined subroutines have higher
priority. So the programmer can override the standard subroutines in the library by supplying
their own routines. Searching on the libraries is done by scanning through the define records of
all the object programs in the library. This method is quiet inefficient. So we go for a directory
structure. Assembled or compiled versions of the subroutines in a library is structured using a
directory that gives the name of each routine and a pointer to its address within the library. Thus
the library search involves only a search on the directory, followed by reading the object
programs indicated by this search.
The library contains an internal directory where each files along with their address are
stored. This facilitates the linking of library functions more easy, because whenever a library
function is needed its address can be directly obtained from internal directory.
Page 13
CS303 System Software Module 4
This 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.
Option 2:
allows the user to delete external symbols or entire control sections.
Ex. DE LETE csect-name
This instruct the loader to delete the named control section(s) from the set of programs
being loaded.
Option 3:
allows the user to change the name of external symbol
Ex: CHANGE name1, name2
this cause the external symbol name1 to be changed to name2 wherever it appears in
the object programs.
Option 4:
This involves the automatic inclusion of library routines to satisfy external references.
Ex: LIBRARY MYLIB
Such user-specified libraries are normally searched before the standard system libraries.
This allows the user to use special versions of the standard routines.
Option 5:
NOCALL STDDEV, PLOT, CORREL
To instruct the loader that these external references are to remain unresolved. This
avoids the overhead of loading and linking the unneeded routines, and saves the
memory space that would otherwise be required.
Example:
If we would like to use the utility routines READ and WRITE instead of RDREC and
WRREC in our programs, for a temporary measure, we use the following loader
commands
INCLUDE READ(UTLIB)
INCLUDE WRITE(UTILB)
DELETE RDREC, WRREC
CHANGE RDREC, READ
CHANGE WRREC, WRITE
Page 14
CS303 System Software Module 4
These commands would ask the loader to include control sections READ and
WRITE from the library UTLIB and to delete the control sections WRREC and
RDREC. The first CHANGE command would change all the external references to the
symbol RDREC to be changed to refer to READ and second CHANGE will cause
references to WRREC to be changed to WRITE.
• A linking loader performs all linking and relocation operations, including automatic
library search if specified, and loads the linked program directly into memory for execution.
• A linkage editor produces a linked version of the program (load module or executable
image), which is written to a file or library for later execution.
A linkage editor produces a linked version of the program (load module or executable
image), which is written to a file or library for later execution. When the user is ready to run
the linked program, a simple relocating loader can be used to load the program into memory.
The only object code modification necessary is the addition of an actual load address to
relative values within the program.
Page 15
CS303 System Software Module 4
Figure: Processing of an object program using a) linking loader and b)linkage editor
The Linkage Editor(LE) performs relocation of all control sections relative to the start of
the linked program. Thus, all items that need to be modified 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. Resolution of external
reference and library searching are only performed once for linkage editor.
If a program is under development or is used infrequently, the use of a linking loader
outperforms a linkage editor.
Consider a program PLANNER with a number of subroutines. You want to improve a
subroutine (PROJECT) of the program (PLANNER) without going back to the original
versions of all of the other subroutines. For that you can use linkage editor commands as
follows:
Page 16
CS303 System Software Module 4
Linkage editors perform linking operations before the program is loaded for
execution.
Linking loaders perform these same operations at load time.
Dynamic linking, dynamic loading, or load on call postpones the linking
function until execution time. That is a subroutine is loaded and linked to the
rest of the program when it is first called.
Dynamic linking, dynamic loading, or load on call postpones the linking function
until execution time. That is a subroutine is loaded and linked to the rest of the program
when it is first called.
Dynamic linking is often used to allow several executing programs to share one copy of
a subroutine or library (eg. run-time support routines for a high-level language like C.)
With a program that allows its user to interactively call any of the subroutines of a large
mathematical and statistical library, all of the library subroutines could potentially be
needed, but only a few will actually be used in any one execution. Dynamic
linking can avoid the necessity of loading the entire library for each execution except
those necessary subroutines.
For example, that a program contains subroutines that correct or clearly diagnose error
in the input data during execution. If such error are rare, the correction and diagnostic
routines may not be used at all during most execution of the program. However, if the
program were completely linked before execution, these subroutines need to be loaded
and linked every time.
Fig 3.14 illustrates a method in which routines that are to be dynamically loaded must
be called via an OS service request.
Page 17
CS303 System Software Module 4
Page 18
CS303 System Software Module 4
Fig (a): Whenever the user program needs a subroutine for its execution, the program
makes a load-and-call service request to OS(instead of executing a JSUB instruction
referreing to an external symbol) . The parameter of this request is the symbolic
name(ERRHANDL) of the routine to be called.
Fig (b): OS examines its internal tables to determine whether or not the routine is
already loaded. If necessary, the routine is loaded from the specified user or system
libraries.
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.
When a computer is first turned on or restarted, a special type of absolute loader, called
a bootstrap loader, is executed. This bootstrap loader loads the first program to be run by the
computer – usually an operating system.
Page 19
CS303 System Software Module 4
Module 4 Summary:
Basic Loader functions - Design of absolute loader, Simple bootstrap Loader
Machine dependent loader features- Relocation, Program Linking,
Algorithm and data structures of two pass Linking Loader
Machine independent loader features – Automatic Library Search, Loader
options
Loader Design Options – Linkage Editors, Dynamic Linking, Bootstrap
Loaders
Page 20