Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views19 pages

Module III

Module III discusses loaders and linkers, detailing the processes of loading, linking, and relocation of object programs into memory for execution. It explains different types of loaders, such as absolute loaders and bootstrap loaders, along with their functions and algorithms. The document also covers program linking, external references, and the design options for loaders, including linkage editors and dynamic linking.

Uploaded by

arathysp18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Module III

Module III discusses loaders and linkers, detailing the processes of loading, linking, and relocation of object programs into memory for execution. It explains different types of loaders, such as absolute loaders and bootstrap loaders, along with their functions and algorithms. The document also covers program linking, external references, and the design options for loaders, including linkage editors and dynamic linking.

Uploaded by

arathysp18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

MODULE III

LOADERS AND LINKERS:


Introduction:

The Source Program written in assembly language or high level language will be converted to object
program, which is in the machine language form for execution. This conversion either from assembler or
from compiler, contains translated instructions and data values from the source program, or specifies
addresses in primary memory where these items are to be loaded for execution.

HIGH LEVEL PROGRAM

COMPILER

ASSEMBLY LANGUAGE PROGRAM

ASSEMBLER

OBJECT: MACHINE LANGUAGE MODULE OBJECT: LIBRARY ROUTINE (Machine language) LINKER

EXECUTABLE: MACHINE LANGUAGE PROGRAM

LOADER

MEMORY

This contains the following three processes, and they are,


∙ Loading - which allocates memory location and brings the object program into memory for
execution - (Loader)

∙ 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)

Basic Loader Functions:

∙ The most fundamental functions of a loader – bringing an object program into memory and starting
its execution.
∙ The different types of loaders are, absolute loader, bootstrap loader, relocating loader (relative
loader), and, direct linking loader.

Design of Absolute Loader:

∙ The operation of absolute loader (performs loading only) is very simple.


∙ The object code is loaded to specified locations in the memory. At the end the loader jumps to the
specified address to begin execution of the loaded program.
∙ The advantage of absolute loader is simple and efficient. But the disadvantages are, the need for
programmer to specify the actual address, and, difficult to use subroutine libraries. ∙ Each byte of
assembled code is given using its hexadecimal representation in character form. Easy to read by
human beings.
∙ 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.

ALGORITHM:

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

Eg:
BOOTSTRAP LOADER:

When a computer is first turned on or restarted, a special type of absolute loader, called a 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.

ALGORITHM:
MACHINE DEPENDENT LOADER FEATURES:
∙ Absolute loader is simple and efficient, but the scheme has potential disadvantages. ∙ One of the most
disadvantage is the programmer has to specify the actual starting address, from where the program to
be loaded.
∙ This does not create difficulty, if one program to run, but not for several programs. Further it is
difficult to use subroutine libraries efficiently.
∙ 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 (performs
relocation and loading).
Methods for specifying relocation
∙ Use of modification record and, use of relocation bit, are the methods available for
specifying relocation.
∙ In the case of modification record (SIC/XE), a modification record M is used in the object
program to specify any relocation.
∙ In the case of use of relocation bit (SIC), each instruction is associated with one relocation
bit and, these relocation bits in a Text record is gathered into bit masks.
∙ Modification records are used in complex machines and is also called Relocation and
Linkage Directory (RLD) specification.
∙ The format of the modification record (M) is as follows. Modification record col 1: M
col 2-7: relocation address
col 8-9: length (halfbyte)
col 10: flag (+/-)
col 11-17: segment name
eg:
∙ The relocation bit method is used for simple SIC 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.
Eg:
Program Linking
The Goal of program linking is to resolve the problems with external references (EXTREF) and external
definitions (EXTDEF) from different control sections.

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. Eg:
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.
Eg: EXTREF RDREC, WRREC
EXTREF LISTB, ENDB, LISTC, ENDC

How to implement EXTDEF and EXTREF


The assembler must include information in the object program that will cause the loader to insert
proper values where they are required – in the form of Define record (D) and, Refer record(R).

Define record
The format of the Define record (D):
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
Eg: D LISTA 000040 ENDA 000054
D LISTB 000060 ENDB 000070

Refer record
The format of the Refer record (R):
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
Eg: 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 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.
ALGORITHM AND DATA STRUCTURES FOR LINKING LOADER:
Linking Loader uses two-pass logic. ESTAB (external symbol table) is the main data structure for a
linking loader. [The input for the linking loader is set of object programs or control sections.] Pass 1:
Assign addresses to all external symbols
Pass 2: Perform the actual loading, relocation, and linking


The main data structure needed for our linking loader is an external symbol table ESTAB. This table,
which is analogous to SYMTAB in our assembler algorithm, is used to store the name and address of
each external symbol in the set of control sections being loaded.
∙ Three other important variables are PROGADDR (program load address) and CSADDR (control
section address) and CSLTH (control section length).
∙ 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.
∙ 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.

PROGRAM LOGIC FOR PASS 1:

∙ Pass 1 assigns addresses to all external symbols.


▪ The loader is concerned only with Header and Define record types in the control sections. ∙
To build up ESTAB
▪ Add control section name into ESTAB.
▪ Add all external symbols in the define record into ESTAB.
ALGORITHM:

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.

4) 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.
PROGRAM LOGIC FOR PASS 2:

∙ Performs the actual loading, relocation, and linking of the program.


∙ When Text record is encountered
▪ Read into the specified address (+CSADDR)
∙ When modification record is encountered
▪ Lookup the symbol in ESTAB
▪ This value is then added to or subtracted from the indicated location in memory. ∙
When the End record is encountered
▪ Transfer control to the loaded program to begin execution.

PASS 2 ALGORITHM:
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. MACHINE INDEPENDENT LOADER FEATURES:

Automatic Library Search and Loader Options are Machine-independent Loader Features.

1) Automatic Library Search


∙ This feature allows a programmer to use standard subroutines without explicitly including them in
the program to be loaded.
∙ The routines are automatically retrieved from a library as they are needed during linking. This
allows programmer to use subroutines from one or more libraries.
∙ The subroutines called by 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.
∙ Linking loaders that support automatic library search must keep track of external symbols that are
referred to, but not defined, in the primary input to the loader.
∙ 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.
NOTE:
Linking loaders that support automatic library search:

Enter the symbols from each Refer record into ESTAB


(Define record), the address is assigned
At the end of Pass 1, the symbols in ESTAB that remain undefined represent unresolved
external references.
loader searches the 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.

external references
,the library search process may be repeated.

programmers can override the standard subroutines in the library by supplying their own
routines

2) 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 can be specified using loader control statements in the
source program.
Eg: INCLUDE program-name (library-name) - read the designated object program from a library.
DELETE csect-name – delete the named control section from the set of 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. The above
example shows 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.

LOADER DESIGN OPTIONS:

Linking loaders perform all linking and relocation at load time. There are two alternatives:

Linkage editors, which perform linking prior to load time, and dynamic linking, in which

the linking function is performed at execution time.

� Precondition: The source program is first assembled or compiled, producing an object

program. 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.


1. Linkage Editors

The linkage editor 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.
If a program is to be executed many times without being reassembled, the use of a linkage
editor substantially reduces the overhead required.

Additional Functions of Linkage Editors


∙ Replacement of subroutines in the linked program
∙ Construction of a package for subroutines generally used together
∙ Specification of external references not to be resolved by automatic library search ∙
Replacement of subroutines in the linked program

Eg: Replacement of subroutines in the linked program. A typical sequence of linkage editor
commands used:
INCLUDE PLANNER (PROGLIB)
DELETE PROJECT {delete from existing PLANNER}
INCLUDE PROJECT (NEWLIB) {include new version}
REPLACE PLANNER (PROGLIB)

� Linkage editors can also be used to build packages of subroutines or other control sections
that are generally used together. This can be useful when dealing with subroutine libraries
that support high level programming languages.

� Compared to linking loaders, linkage editors in general tend to offer more flexibility and control.
NOTE:

Linking Time

o Linkage editors : before load time

o Linking loaders : at load time

o Dynamic linking : after load time

2. DYNAMIC LINKING

∙ A scheme that postpones the linking function until execution time.


∙ A subroutine is loaded and linked to the rest of the program when it is first called
∙ Other names: dynamic loading, load on call
∙ Dynamic linking is often used to allow several executing programs to share one copy of a subroutine
or library, ex. 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.

Fig 3.14(a): Instead of executing a JSUB instruction referring to an external symbol, the program makes a
load-and-call service request to OS. The parameter of this request is the symbolic name of the routine to
be called.

Fig 3.14(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 3.14(c): Control is then passed from OS to the routine being called

Fig 3.14(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 3.14(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.

Bootstrap loader
• A bootstrap loader is a program that resides in the computer's EPROM, ROM, or another non-volatile
memory.

• It is automatically executed by the processor when turning on the computer.

• The bootstrap loader reads the hard drives boot sector to continue to load the computer's operating
system.
• When the computer is turned on or restarted, the bootstrap loader first performs the power-on self-test,
also known as POST. If the POST is successful and no issues are found, the bootstrap loader loads the
operating system for the computer into memory.

The computer can then access, load, and run the operating system.

• The term bootstrap comes from the old phrase "Pull yourself up by your bootstraps." •
Alternatively referred to as bootstrapping, bootloader, or boot program,

You might also like