Module 2
• LOADERS AND LINKERS: Basic loader
functions - Design of an Absolute Loader – A
Simple Bootstrap Loader -Machine dependent
loader features - Relocation – Program Linking
– Algorithm and Data Structures for Linking
Loader - Machine-independent loader
features – Automatic Library Search – Loader
Options - Loader design options - Linkage
Editors – Dynamic Linking –
Chapter 1
Loaders
Loaders
• It is a utility program which takes object code
as input, prepares it for execution and loads
the executable code into the memory.
• Thus loader is actually responsible for
initiating the execution process.
Basic Functions of Loader
• The loader is responsible for the activities such as
allocation,linking,relocation,loading.
1.ALLOCATION: it allocates the space for program in the memory, by
calculating the size of the program.
2.LINKING: it resolves the symbolic references (code/data) between the
object modules by assigning all the user subroutine and library
subroutine addresses.
3.RELOCATION: There are some address dependent locations in the
program such as address constants must be adjusted according to
allocated space such activity done by loader.
4.LOADING:finally it places the machine instructions and data of
corresponding programs and subroutines into the memory. Thus
program now becomes ready for execution.
Absolute Loader
• Absolute Loader is a kind of loader in which relocated
object files are created, loader accepts these files
and places them at specified locations in the
memory. This type of loader is called absolute
because no relocation information is needed rather it
is obtained from the programmer or assembler.
• The starting address of every module is known to the
programmer this corresponding address is stored in
the object file, then task of loader becomes very
simple and that is to simply place the executable
form of the machine instructions at the locations
mentioned in the object file.
Absolute Loader
The programmer should take care of two things :
• First, Specification of starting address of each module to
be used. If some modification is done in some module
then the length of that module may vary. This causes a
change in the starting address of immediate next
modules, its then programmer duty to make necessary
changes in the starting addresses of respective modules.
• Second, while branching from one segment to another
the absolute starting address of respective module is to
be known by the programmer so that such address can
be specified at respective JMP instruction.
Absolute Loader
Absolute Loader
Bootstrap Loader
• 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.
Bootstrap Loader
Various characteristics of bootstrap loader
1. The bootstrap loader is a small program and it should be fitted in the
ROM.
2. The bootstrap loader must load the necessary portions of OS in the main
memory
3. The initial address at which the bootstrap loader is to be loaded is
generally the lowest for example at location 0000 or at the highest location
and not a intermediate location.
Bootstrap Loader for SIC/XE
Bootstrap Loader for SIC/XE
MACHINE DEPENDENT LOADER FEATURES:
1.RELOCATION
2. PROGRAM LINKING
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.
Relocation
• There are two methods for specifying
relocation as a part of object program and
those are
• 1. Modification record (SIC/XE program)
• 2. Relocation bits(SIC program)
Relocation-Modification Record
Relocation-Modification Record
Disadvantage: This method is not well suited for SIC program . because these
programs will require lot of modified records and then the size of object program will
get increased
Relocation Bits
• The relocation bit method is used for simple machines.
Relocation bit is 0: no modification is necessary,
1: modification is needed.
This is specified in the columns 10-12 of text record (T).
Relocation Bits
Relocation Bits
EXTDEF and EXTREF directives
Define and Refer Record
Relocation and Linking
Relocation and Linking
Relocation and Linking
Define and Refer record- Example
Relocation and Linking
• PROGA🡪4000(Starting address )+0063(length of PROGA)->4063
• PROGB🡪4063(staring address )+007F(length of PROGB)🡪40E2
• PROGC🡪40E2(starting address)+0051(length of PROGC)🡪4133
Data Structures for Linking Loader
• Data Structures:
1.ESTAB:External Symbol Table
• This table is used to store the name and address
of each external symbol in the set of control
sections being loaded.
• The table also often indicates in which control
sections the symbol is defined.
• A hashed organization is typically used for this
table.
Data Structures for Linking Loader
2. PROGADDR: Program load address
• It is the beginning address in memory where the linked
program is to be loaded.
• Its value is supplied by OS to the loader.
3. CSADDR : Control Section address
• Contains the starting address assigned to control
sections currently being scanned by the loader.
• This value is added to all relative addresses within the
control section to convert them to actual addresses.
Pass 1 of Linking loader
Pass 1 of a Linking Loader
Pass 1 of a Linking Loader
Pass 1 of a Linking Loader
Pass 2 of a Linking Loader
Pass 2 of a Linking Loader
Pass 2 of a Linking Loader
Pass 2 of a Linking Loader
MACHINE INDEPENDENT LOADER
FEATURES
1. AUTOMATIC LIBRARY SEARCH
2. USE OF LOADER OPTIONS
Automatic Library Search
Automatic Library Search
Loader Option Commands
Loader Option Commands
LOADER DESIGN OPTIONS:
• LINKAGE EDITOR
• DYNAMIC LINKAGE.
Linking Loader
• 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.
• There is no need of relocating loader.
• The linking loader searches the libraries and resolves the
external references every time the program is executed.
• When program is in development stage then at that time the
linking loader can be used.
• The loading requires two passes
Linking Loader
Linkage Editor
• The linkage editor produces a linked version of the program. Such
a linked version is also called as load module. This load module is
generally written in a file or library for later execution.
• The relocating loader loads the load module into memory.
• If the program is executed many times without being
reassembled then linkage editor is the best choice.
• When program development is finished or when the library is
built, then linkage editor is the best choice.
• The loading can be done in one pass.
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 not to be resolved by automatic library search –
linking will be done later by linking loader – linkage editor +
linking loader – savings in space.
Dynamic Linkage
• In overlay structure certain selective subroutines can
be resident in the memory.
• That means it is not necessary to resident all the
subroutines in the memory for all time.
• only necessary routines can be present in the main
memory and during execution the required
subroutines can be loaded in the memory.
• The process of postponing linking and loading of
external references until execution is called dynamic
linking dynamic loading or load on call.
Dynamic Linking
• Dynamic linking is often used to allow several executing
programs to share one copy of a subroutine or library.
• for ex run time support routines for a high level
language like C could be stored in a dynamic link library.
• A single copy of the routines in this library could be
loaded into the memory of the computer. All c programs
currently in execution could be linked to this one copy ,
instead of linking a separate copy into each object
program.
Dynamic Linking
• Suppose that in any execution, a program uses
only a few out of large number of possible
routines, but the exact routine needed cannot
be predicted until program receives its input.
dynamic linking avoids the necessity of loading
the entire library for each execution.
Dynamic Linking
Dynamic Linking
Dynamic Linking
Dynamic Linking
Dynamic Linking
• Advantages:
1. The overhead on the loader is reduced. The required
subroutine will be loaded in the main memory only at
the time of execution.
2. The system can be dynamically configured.
• Disadvantages The linking and loading has to be
suspended until execution. During the execution if at all
any subroutine is needed then the process of execution
needs to be suspended until the require subroutine gets
loaded in the main memory