Unit-2 Ch-4
Unit-2 Ch-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
Linking - which combines two or more separate object programs and supplies the information
needed to allow references between them
Page 1
Chapter 04 Unit – 02
Loading and Allocation - which allocates memory location and brings the object program
into memory for execution
• 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.
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.
Page 2
Chapter 04 Unit – 02
end
2. On some computers, there’s a built-in hardware which read a fixed-length record from
some device into memory at a fixed location. After the read operation, control is
automatically transferred to the address in memory.
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
Chapter 04 Unit – 02
• Each byte of object code to be loaded is represented on device F1 as two hexadecimal digits just
as it is in a Text record of a SIC object program.
• 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.
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 HIGH�ORDER 4 BITS OF BYTE
JSUB GETC GET NEXT HEX DIGIT
ADDR S ,A COMBINE DIGITS TO FORM ONE BYTE
Page 4
Chapter 04 Unit – 02
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)
1. Program Relocation
2. Program Linking
Page 5
Chapter 04 Unit – 02
• 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 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.
• 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.
Page 6
Chapter 04 Unit – 02
• 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 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.
Page 7
Chapter 04 Unit – 02
• 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.
• 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.
• Linking is the process in which references to "externally" defined symbols are processed so as to
make them operational.
• A linker or link editor is a program that combines object modules to form an executable program.
Page 8
Chapter 04 Unit – 02
• A Linking Loader is a program that has the capability to perform relocation, linking and loading.
Linking and relocation is performed at load time.
• A linking loader usually makes two passes over its input, just as an assembler does.
• In terms of general function, the two passes of a linking loader are quite similar to the two passes
of an assembler:
• 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.
Page 9
Chapter 04 Unit – 02
Page 10
Chapter 04 Unit – 02
• 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.
Page 11
Chapter 04 Unit – 02
• 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 12
Chapter 04 Unit – 02
• As each Text record is read, the object code is moved to the specified address (plus the current
value of CSADDR).
• 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.
At the end of Pass 1, the symbols in ESTAB that remain undefined represent unresolved
external references. The loader searches the library or libraries specified for routines that contain
the definitions of these symbols, and processes the subroutines found by this search exactly as if
they had been part of the primary input stream. Note that the subroutines fetched from a library in
this way may themselves contain external references. It is therefore necessary to repeat the library
search process until all references are resolved. If unresolved external references remain after the
library search is completed, these must be treated as errors.
Page 13
Chapter 04 Unit – 02
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 14
Chapter 04 Unit – 02
Option 1:
• allows the selection of alternative sources of input.
• Ex. INCLUDE program-name (library-name)
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)
Page 15
Chapter 04 Unit – 02
• 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 16
Chapter 04 Unit – 02
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.
Page 17
Chapter 04 Unit – 02
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 18
Chapter 04 Unit – 02
Page 19
Chapter 04 Unit – 02
• 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.
2. On some computers, there’s a built-in hardware which read a fixed-length record from
some device into memory at a fixed location. After the read operation, control is
automatically transferred to the address in memory. If the loading process requires
more instructions than can be read in a single record, this first record causes the reading
of others, and these in turn can cause the reading of more records
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 20
Chapter 04 Unit – 02
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
Page 21