Unit 3 SP
Unit 3 SP
Linking- which combines two or more separate object programs and supplies the information needed to
allow references between them - (Linker)
Relocation - which modifies the object program so that it can be loaded at an address different from the
location originally specified - (Linking Loader)
A loader is a system program that performs the loading function. It brings object program into
memory and starts its execution. The role of loader is as shown in the figure 3.1. In figure 3.1 translator
may be assembler/complier, which generates the object Program and later loaded to the memory by the
loader for execution. In figure 3.2 the translator is specifically an assembler, which generates the object
loaded, which becomes input to the loader. The figure 3.3 shows the role of both loader and linker.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 1
SYSTEM PROGRAMMING UNIT 3
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 2
SYSTEM PROGRAMMING UNIT 3
The algorithm for this type of loader is given here. The object program and, the object program loaded
into memory by the absolute loader are also shown. Each byte of assembled code is given using its
hexadecimal representation in character form. Easy to read by human beings. Each byte of object code is
stored as a single byte. Most machine store object programs in a binary form, and we must be sure that
our file and device conventions do not cause some of the program bytes to be interpreted as control
characters.
Begin
read Header record
verify program name and length
read first Text record
while record type is <> ‘E’ do
begin
{if object code is in character form, convert into internal representation}
move object code to specified location in memory
read next object program record
end
jump to address specified in End record
end
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 3
SYSTEM PROGRAMMING UNIT 3
When a computer is first turned on or restarted, a special type of absolute loader, called bootstrap
loader is executed. This bootstrap loads the first program to be run by the computer – usually an operating
system. The bootstrap itself begins at address 0. It loads the OS starting address 0x80. No header record
or control information, the object code is consecutive bytes of memory.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 4
SYSTEM PROGRAMMING UNIT 3
This needs the design and implementation of a more complex loader. The loader must provide program
relocation and linking, as well as simple loading functions.
Relocation
The concept of program relocation is, the execution of the object program using any part of the
available and sufficient memory. The object program is loaded into memory wherever there is room for
it. The actual starting address of the object program is not known until load time. Relocation provides the
efficient sharing of the machine with larger memory and when several independent programs are to be run
together. It also supports the use of subroutine libraries efficiently. Loaders that allow for program
relocation are called relocating loaders or relative loaders.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 5
SYSTEM PROGRAMMING UNIT 3
The relocation bit method is used for simple machines. Relocation bit is 0: no modification is
necessary, and is 1: modification is needed. This is specified in the columns 10-12 of text record (T), the
format of text record, along with relocation bits is as follows.
Text record
col 1: T
col 2-7: starting address
col 8-9: length (byte)
col 10-12: relocation bits
col 13-72: object code
Twelve-bit mask is used in each Text record (col:10-12 – relocation bits), since each text record
contains less than 12 words, unused words are set to 0, and, any value that is to be modified during
relocation must coincide with one of these 3-byte segments. For absolute loader, there are no relocation
bits column 10-69 contains object code. The object program with relocation by bit mask is as shown
below. Observe FFC - means all ten words are to be modified and, E00 - means first three records are to
be modified.
Program Linking
The Goal of program linking is to resolve the problems with external references (EXTREF) and
external definitions (EXTDEF) from different control sections.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 6
SYSTEM PROGRAMMING UNIT 3
EXTDEF (external definition) - The EXTDEF statement in a control section names symbols, called
external symbols, that are defined in this (present) control section and may be used by other sections.
ex: EXTDEF BUFFER, BUFFEND, LENGTH
EXTDEF LISTA, ENDA
EXTREF (external reference) - The EXTREF statement names symbols used in this (present) control
section and are defined elsewhere.
ex: EXTREF RDREC, WRREC
EXTREF LISTB, ENDB, LISTC, ENDC
Define record
The format of the Define record (D) along with examples is as shown here.
Col. 1 D
Col. 2-7 Name of external symbol defined in this control section
Col. 8-13 Relative address within this control section (hexadecimal)
Col.14-73 Repeat information in Col. 2-13 for other external symbols
Example records
D LISTA 000040 ENDA 000054
D LISTB 000060 ENDB 000070
Refer record
The format of the Refer record (R) along with examples is as shown here.
Col. 1 R
Col. 2-7 Name of external symbol referred to in this control section
Col. 8-73 Name of other external reference symbols
Example records
R LISTB ENDB LISTC ENDC
R LISTA ENDA LISTC ENDC
R LISTA ENDA LISTB ENDB
Here are the three programs named as PROGA, PROGB and PROGC, which are separately Chapter: LOADERS & LINKERS
assembled and each of which consists of a single control section. LISTA, ENDA in PROGA, LISTB,
ENDB in PROGB and LISTC, ENDC in PROGC are external definitions in each of the control sections.
Similarly, LISTB, ENDB, LISTC, ENDC in PROGA, LISTA, ENDA, LISTC, ENDC in PROGB, and
LISTA, ENDA, LISTB, ENDB in PROGC, are external references. These sample programs given here
are used to illustrate linking and relocation. The following figures give the sample programs and their
corresponding object programs. Observe the object programs, which contain D and R records along with
other records.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 7
SYSTEM PROGRAMMING UNIT 3
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 8
SYSTEM PROGRAMMING UNIT 3
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 9
SYSTEM PROGRAMMING UNIT 3
The above figure shows these three programs as they might appear in memory after loading and
linking. PROGA has been loaded starting at address 4000, with PROGB and PROGC immediately
following.
For example, the value for REF4 in PROGA is located at address 4054 (the beginning address of
PROGA plus 0054, the relative address of REF4 within PROGA). The following figure shows the details
of how this value is computed.
Linking Loader uses two-passes logic. ESTAB (external symbol table) is the main
data structure for a linking loader.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 10
SYSTEM PROGRAMMING UNIT 3
ESTAB - ESTAB for the example (refer three programs PROGA PROGB and PROGC) given as shown
below. The ESTAB has four entries in it; they are name of the control section, the symbol appearing in
the control section, its address and length of the control section.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 11
SYSTEM PROGRAMMING UNIT 3
the program being loaded are automatically fetched from the library, linked with the main program and
loaded. The loader searches the library or libraries specified for routines that contain the definitions of
these symbols in the main program.
Loader Options
Loader options allow the user to specify options that modify the standard processing. The options may be
specified in three different ways. They are, specified using a command language, specified as a part of job
control language that is processed by the operating system, and an be specified using loader control
statements in the source program.
INCLUDE program-name (library-name) - read the designated object program from a library
DELETE csect-name – delete the named control section from the set pf programs being loaded
CHANGE name1, name2 - external symbol name1 to be changed to name2 wherever it appears in the
object programs
LIBRARY MYLIB – search MYLIB library before standard libraries
NOCALL STDDEV, PLOT, CORREL – no loading and linking of unneeded routines
Here is one more example giving, how commands can be specified as a part of object file, and the
respective changes are carried out by the loader.
LIBRARY UTLIB
INCLUDE READ (UTLIB)
INCLUDE WRITE (UTLIB)
DELETE RDREC, WRREC
CHANGE RDREC, READ
CHANGE WRREC, WRITE
NOCALL SQRT, PLOT
The commands are, use UTLIB ( say utility library), include READ and WRITE control sections from the
library, delete the control sections RDREC and WRREC from the load, the change command causes all
external references to the symbol RDREC to be changed to the symbol READ, similarly references to
WRREC is changed to WRITE, finally, no call to the functions SQRT, PLOT, if they are used in the
program.
Linking Loaders
The above diagram shows the processing of an object program using Linking Loader. The source program
is first assembled or compiled, producing an object program. A linking loader performs all linking and
loading operations, and loads the program into memory for execution.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 13
SYSTEM PROGRAMMING UNIT 3
Linkage Editors
The figure below shows the processing of an object program using Linkage editor. A linkage editor
produces a linked version of the program – often called a load module or an executable image – which is
written to a file or library for later execution. The linked program produced is generally in a form that is
suitable for processing by a relocating loader.
Some useful functions of Linkage editor are, an absolute object program can be created, if starting address
is already known. New versions of the library can be included without changing the source program.
Linkage editors can also be used to build packages of subroutines or other control sections that are
generally used together. Linkage editors often allow the user to specify that external references are not to
be resolved by automatic library search – linking will be done later by linking loader – linkage editor +
linking loader – savings in space
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 14
SYSTEM PROGRAMMING UNIT 3
Dynamic Linking
The scheme that postpones the linking functions until execution. A subroutine is loaded and linked to the
rest of the program when it is first called – usually called dynamic linking, dynamic loading or load on
call. The advantages of dynamic linking are, it allows several executing programs to share one copy of a
subroutine or library. In an object-oriented system, dynamic linking makes it possible for one object to be
shared by several programs. Dynamic linking provides the ability to load the routines only when (and if)
they are needed. The actual loading and linking can be accomplished using operating system service
request.
THEADR specifies the name of the object module. MODEND specifies the end of the module. PUBDEF
contains list of the external symbols (called public names). EXTDEF contains list of external symbols
referred in this module, but defined elsewhere. TYPDEF the data types are defined here. SEGDEF
describes segments in the object module (includes name, length, and alignment). GRPDEF includes how
segments are combined into groups. LNAMES contains all segment and class names. LEDATA contains
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 15
SYSTEM PROGRAMMING UNIT 3
translated instructions and data. LIDATA has above in repeating pattern. Finally, FIXUPP is used to
resolve external references.
_____________________________________________________________________________________
From the desk of Mr. Chaitanya Reddy, SMDC Ballari. 16