Linker and Loader Functions Explained
Linker and Loader Functions Explained
MODULE IV
• Linker and Loader
o Basic Loader functions
Design of absolute loader
Simple bootstrap Loader
o Machine dependent loader features
Relocation
Program Linking
• Algorithm and data structures of two pass Linking Loader
o Machine independent loader features
Automatic Library Search
Loader Options
o Loader Design Options.
1 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
o Type of loaders
Absolute loader
Bootstrap loader
Assemble-and-go loader
Relocating loader (Relative loader)
Direct linking loader
o Absolute loader
The object code is loaded to the specified location in the memory.
All functions are accomplished in a single pass as follows:
• The Header record of object programs is checked to verify that the
correct program has been presented for loading.
• As each Text record is read, the object code it contains is moved to
the indicated address in memory.
• When the End record is encountered, the loader jumps to the
specified address to begin execution of the loaded program.
No linking and relocation needed.
2 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
• Each pair of bytes from the object program record must be packed
together into one byte during loading.
• Eg: Opcode for STL is 14. It is saved in object program as 2 bytes(2
characters). While loading it is converted to single byte(00010100).
• The content of the memory location for which there is no Text
record are shown as xxxx.
Advantage:
• Simple
• Efficient(less space and loading time)
Disadvantage:
• Programmer should specify the actual address
o If the system having small memory, only one program can run at
a time. So it does not create much difficulty to specify the
address.
3 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
o Bootstrap Loader
It is a special type of absolute loader.
When a computer is first turned on or restarted, bootstrap loader is
executed.
This bootstrap loads the operating system into memory.
The bootstrap itself begins at address 0
It loads the OS(from device F1) starting at address 0x80.
The object code having no header record, end record or control
information.
After loading the OS, the control is transferred to the instruction at
address 0x80.
Algorithm
X = 0x80 //initial location of the OS to be loaded
Loop
{ A = GETC //read and convert from ASCII to hexadecimal digit
Save the value in the higher order 4 bits of S
A = GETC //read and convert from ASCII to hexadecimal digit
Combine the value to form one byte A = (A + S)
Store the value of A to the address in register
X=X+1
}
4 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Program
5 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Eg: = C “D8”
Program Relocation
• Program Relocation
o The object program is loaded into memory wherever there is
room for it.
o The actual starting address of the object program is not known
until load time.
• Relocating Loader / Relative Loader: Loaders that allow program
relocation.
• Two methods for specifying relocation as part of the object
program
o Modification records
o Relocation bits
6 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
• Modification records
o A Modification record is used to describe each part of the object
code that must be changed when the program is relocated.
o Used when a small number of relocations is required.
o Format
o Example
7 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
8 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
o Algorithm
Get PROGADDR from Operating System
Read a record from input file
While record_type != ‘E’ do
{
If record_type = ‘T’ do
{
Move object code from record to location PROGADDR + Specified address
}
If record_type = ‘M’ do
{
Add PROGADDR at the location (PROGADDR + Specified address)
}
Read next input record
}
o Disadvantage
It is not well suited for use with all machine architectures. It
is not suited for standard SIC programs.
• Relocation bits
o Each instruction is associated with one relocation bit
o These relocation bits are gathered into bit masks in a Text
record.
Relocation bit is 0: no modification is needed
Relocation bit is 1: modification is needed.
o Used when a large number of relocations is required.
o Format
9 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
10 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
11 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Program Linking
• The goal of program linking is to resolve the problems with external
references (EXTREF) and external definitions (EXTDEF) from
different control sections.
o EXTDEF (external definition) - The EXTDEF statement in a
control section names symbols that are defined in present control
section and may be used by other sections.
Eg: EXTDEF LISTA, ENDA
12 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
13 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
14 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
15 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
16 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
PROGB
• Here LISTA, ENDA and LISTC are the references to
external symbols
• Put 000000 as object code.
• Place modification records for LISTA, ENDA and
LISTC
PROGC
• Here LISTA and ENDA are the references to external
symbols
• LISTC is the labels in the current control section.
• The object code is the value of LISTC (000030). While
loading PROGC the address of LISTC may change. So
place a modification record for PROGC.
• Place two more modification records for LISTA and
ENDA
17 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
18 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Pass 1
• The loader is concerned only with Header and Define records in the
control sections
• 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.
• 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.
• When the End record is read, the control section length CSLTH 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.
• Pass 1 optionally generate load map
19 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Pass 2
• As each Text record is read, the object code is moved to the
specified address + 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.
o 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.
o If more than one control section specifies a transfer address, the
loader arbitrarily uses the last one encountered.
o If no control section contains a transfer address, the loader uses
the beginning of the linked program (i.e., PROGADDR) as the
transfer point.
o Normally, a transfer address would be placed in the End record
for a main program, but not for a subroutine.
20 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
21 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
22 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
23 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
24 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Loader Options
• Many loaders allow the user to specify options that modify the
standard processing.
• Many loaders have a special command language that is used to
specify loader options.
• Different ways to provide these control statements
o A separate input file to the loader that contains such control
statements.
o These statements are embedded in the primary input stream
between object programs
25 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
26 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
27 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
• Linkage Editor
o Which perform linking prior to load time
o It produces a linked version of the program (load module or
executable image), which is written to a file or library for later
execution.
o A simple relocating loader can be used to load the linked version
of program into memory for execution.
28 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
29 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
30 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
31 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
Bootstrap Loaders
• Given an idle computer with no program in memory, how do we get
things started?
• With the machine empty and idle there is no need for program
relocation.
• We can specify the absolute address for which the OS is first loaded.
• An absolute loader is used to accomplish this task.
32 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
• Different options:
o Required an operator to enter the object code for an absolute
loader into memory using switches on the computer console.
Disadvantage: Inconvenient and error prone
• The absolute loader program may permanently resident in a ROM.
o When some hardware signal occurs (the operator pressing a
system start switch), the machine begins to execute this ROM
program.
o There are 2 options
The program is executed directly in the ROM
The program is copied from ROM to main memory and
executed there.
o Disadvantage:
Some systems do not have such read only storage
It is inconvenient to change the ROM program, if the
modification in the absolute loader is required.
• Have a built in hardware function (or a short ROM program) that
reads a fixed length record from some device into memory at a fixed
location.
o This device may be selected via console switches.
o After the read operation is complete, control is automatically
transferred to the address in memory where the record was
stored.
o This record contains machine instructions that load the absolute
program that follows.
o The first record is generally referred to as bootstrap loader
o This first record causes the reading of others, and these in turn
can cause the reading of still more records – hence the term
boots trap.
o Such a loader is added to the beginning of all object programs.
o This includes the OS itself and all stand-alone programs that are
to be run without an OS.
33 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck
Module IV System Software(CST 305)
34 CS KTU LECTURES
Reference Book: System Software: An Introduction to System Programming, Leland L Beck