Professional Documents
Culture Documents
● What is a Loader?
o Basic Functions of a Loader
● What is a Linker?
o Functions of a Linker
● General schematic of Linking / Loading function
● Loading Schemes:
o Translate-and-go Loader
o Absolute Loader
o Relocation and Relocating Loaders
▪ Relocation Concept
▪ Relocation Bits
▪ Types of Programs w.r.t Relocation
▪ Relocating Loader
o Direct Linking Loader
● Loader Design Options
● Chapter Summary
● Expected Viva Questions
Exam Questions:
Q) What is a Loader? Explain the four basic functions of any loader. (Dec ‘04 [IT]
– 5M).
Q) What is a Loader? Explain its functionality and components in detail. (Dec ‘05
[Comps] – 10M).
Source
Program
4.1.1 Basic Functions of a Loader
Assemble
Object
r Code
A Loader performs four basic functions:
Final Machine
Code
2. Linking: A single program may include references between its different
Object Code
segments and may refer to external program data and subroutines as well.
3. Relocation: As per its need, the OS may move (i.e. relocate) one or more
segments of the program from one are of the memory to another.
When the program gets relocated, instructions referring to code or data in these
relocated segments must also be changed. Such instructions which must be
changed when relocation occurs are called as “address sensitive” instructions.
(Note: Relocation function must be carried out every time the OS relocates the
program segments; Relocation is often performed by a Linker for the Loader).
A Linker is a system program that resolves all address references within and among
several object modules of a program and combines them to produce a single
“monolithic” .exe file.
1. Linking Object Files: A linker links multiple relocatable object files used by a
program and generates a single .exe file that can be loaded and executed by the
Loader.
2. Resolving External References : While linking those object files, the linker
resolves inter-segment and inter-program references to generate a single
continuous executable file.
Obj: Search
Linker
Searc
h Assemble
r
Parent
Program Obj:
SQRT
SQRT
Obj:
COUNT
External Program 1
COUNT .exe
External Program 2
Final Executable
Program goes into Loader
Memory
The Loader function here is simply an extension of Assembler. (Sometimes it’s also
called as Translate-and-Go Loader).
Source
Program Assemble
Loader
Final Machine
Code
Memory
The Loader simply takes each line of machine code generated by Assembler and places
it into the main memory at a fixed location. This process continues until all of machine
code has been placed into the memory.
Finally, the Loader initiates the execution by transferring control of execution to the
starting location of the program.
As seen in the figure, the Assemble-and-Go Loader does not generate an object file.
Advantage of such loading scheme is that is a very simple scheme to implement as the
only function of Loader is to place the object code in the memory and initiate the
execution.
Disadvantages are:
1. Since no object file is generated in this scheme, the program needs to be
assembled for every run, even if no change is made to the source code. Due to
this the execution time is high.
2. Relocation would require the programmer to change the start address of the
program (by using START pseudo-opcode) and re-assemble it in order to relocate
it. So, relocation is a tedious and time-consuming job.
3. Also, this loading scheme cannot handle multiple program segments and
subroutines in the program; the entire program must be written as a single piece
of code.
In this scheme, the assembler generates the object file which can be stored on
secondary storage instead of being directly loaded into the memory.
Here, the programmer does the allocation and linking functions explicitly for the Loader
i.e. he must know where the program must be placed in the memory and must link the
subroutines explicitly and manually (by using the available instructions appropriately).
The loader simply does the task of loading the object file into the memory and initiating
the execution.
Segment
One major
1 Object
advantage of this scheme is that since the 1
assembler does not always reside
in the main memory, it leaves more main memory (core)
Start space
Addr:available to the user.
100
Assembler 100
Segment
2 Length: 40 Object 1
But its disadvantages are:
140
1. The programmer must do memory management since he explicitly does
Segment
Object 2
the allocation and linking for the Loader. 200
3
Start Addr:
200 Object 2
2. Relocation is a tedious and time-consuming 240 time for
job as every
Length: 40
relocation, the program must be re-assembled.
300
Object 3 Object 3
4.4.3 Relocation
Start Addr: 340
Exam Question: 300
Length:
Q) Explain the Relocation and Linking Concepts (May 40
‘04 [Comps] – 10M).
(For Linking, refer sections 4.1.1 and 4.2)
Main
Memory
Fig. 4.4: Absolute
Loader
When relocation is to be performed, the ‘new’ starting location of the program is added
to relative address of all relocatable bytes.
To identify which byte of object file is relocatable and which is not, relocation bits are
used.
Typically, for each byte of machine code, we have a single bit that is set to 1 if it is
relocatable (i.e. if it’s an address-sensitive entity) or 0 if it is not relocatable.
There are two ways in which these relocation bits can be added to the object file in 2
ways:
As we can see in above code, opcodes and data definitions are not relocatable since
they do not include any address-sensitive entities; only symbolic references, to variable
names or subroutine / procedure names (not in the example), are relocatable.
One drawback with this way to supplying relocation information is that storage and
retrieval becomes complex.
A relocation bit is associated with each byte of object code and all such bits form a
relocation bit mask. This mask is appended at the end of machine code.
So, to retrieve this mask, length of machine code should be supplied in the header.
For example: Machine code of same source code as in the previous example can
now be added with relocation bit as follows:
Machine
Code 08 07 01 08 09 09 12 09 18 00 00
Relocation 0 1 0 1 0 1 0 0 0 0 0
Bit Mask
The advantage here is that storage and retrieval of relocation bits is simplified.
With the help of relocation information in the .exe file, the linker (at compile
time) or relocating loader (at run time) will perform the functions needed to
relocate the program.
When OS relocates some (or all) part of code, control is transferred to the
“relocating subprogram” which adjusts addresses of its address sensitive
portions of the code.
Exam Question:
Q) Differentiate Absolute Loader vs. Relocation Loader stating who performs
which function in the particular loading scheme. (Dec ‘04 [IT] – 5M).
To overcome this problem, a general class of relocating loaders was introduced which
allows multiple procedure segments and one data segments shared by all.
For each program, the assembler produces following information to be used by the
Relocating Loader:
Transfer Vector (TV) is a global table for entire program; it contains list of external
subroutines referenced by the program.
. .
. .
. .
For each subroutine, the transfer vector contains a transfer instruction that branches
the execution flow to the start address of subroutine in the memory.
The Assembler replaces call to each subroutine in the program with a branch to its
corresponding entry in the Transfer Vector.
2. If there are several subroutine linkages in the program, size of TV (and therefore the
object code) increases. So, it occupies more space, as TV is always needed in the main
memory.
Exam Questions:
Q) Explain the design of Direct-Linking Loader (DLL) (Dec ‘04 [Comps] – 10M,
May ‘06 [Comps] – 10M, Dec ‘06 [Comps] – 10M, May ‘07 [Comps] – 10M, Dec ‘07
[Comps] – 10M, Dec ‘07 [IT] – 10M, June ‘08 [Comps] – 10M, Dec ‘08 [Comps] –
10M).
Direct-Linking Loader is the most widely used loading scheme; it belongs to the class of
relocating loaders i.e. it performs all four main functions of a loader (allocation, linking,
relocation, loading)
It has two significant advantages over the general Relocating Loader scheme:
1. It allows use of multiple program segments as well as multiple data
segments.
The Assembler generates following information for every program segment and passes
on to the DLL:
. .
. .
. .
5. EXTERN Table – includes name, type and (relative) usage addresses of symbols
used by a current program that has been defined externally in some other
program (i.e. symbols specified by EXTERN keyword).
. .
. .
. .
(Note that all above mentioned entities are generated by Assembler separately
for each program segment)
The addresses in these PUBLIC and EXTERN keywords are all relative to the starting
location from where their respective program is loaded in the main memory.
1. Replacing the relative addresses in PUBLIC and EXTERN tables with their actual
(offset) locations within the concatenated load module.
2. While object modules are being concatenated, DLL also replaces all external
references in machine code of the program with their actual (offset) locations
within the load module.
Purpose of Pass 2 is to use GEST and generate a single concatenated load module.
1. PUBLIC and EXTERN Tables for every program segment (supplied by Assembler)
2. Global External Symbol Table (GEST)
GEST is simply a global collection of external references that gathers all symbols
from all PUBLIC an EXTERN tables into one table.
3. Intermediate Load Module (ILM) – Pass 1 of DLL dumps machine code of all
input object modules into ILM and reads only PUBLIC and EXTERN tables of each
program segment to construct GEST. These ILM and GEST are then passed onto
pass 2 which actually performs relocation.
1. GEST_PTR – To keep track of location being read from GEST (in pass 1) or
being written into GEST (in pass 2).
2. Location Counter (LC) – To keep a track of location being written into the
Intermediate Load Module i.e. ILM (in pass 1) or into main memory (in pass
2).
Initializations in Pass 1:
1. Open the Source Object Files in Read mode and Intermediate Load Module (ILM)
in Write mode.
2. Setup GEST.
3. Initialize GEST_PTR to first entry of GEST.
X
4.4.4 START
Pass 2 :ofPass 1
DLL Algorithm
Add Current LC
Read next record to Usage
Working
from of DLL Pass
PUBLIC 2:
table Address
Step 1: Copy all machine code statements from ILM to FLM.
Step 2: Read GEST Update Usage
Address in GEST
🡪 Read a record of GEST entry
NO
Symbol
🡪 Copy definition address of the record to all usage addresses of current
present in X
GEST entry.
GEST?
End of
🡪 Is END of GEST reached?
Copy entry EXTERN
If NO, then increment
from GEST_PTR
PUBLIC and repeat from Step 2.1/ Table?
YES NO
table
If YES, then initiate to GEST
execution R to start of final load
by transferring the control
module in the memory. YES
Add Current LC
to Definition
(Note that if control is transferred to some otherRead Next in the memory
program
Address NO Object File
from the loader, it means that the loader itself has stopped executing. So, no
need of explicitly mentioning STOP in the algorithm).
End of
Update Definition PUBLIC
Address in GEST End of
4.4.5 Flowchart for Pass 2 of DLL Table? all
entry
object
NO files?
YES Fig. 4.7: DLL Pass
START : Pass YES 2
4.5 Loader Flowchart
2 Design Options
X
Fig. 4.6: DLL Pass 1 Flowchart GOTO : Pass 2
Copy all machine code
Loading schemes describe
statements from ILMhow to the functions of a loader can be accomplished; Loader
FLM
Design Options refers to the timing of using a loader.
Increment
Read record of GEST_PTR
Loader can be used as per following design options:
GEST
NO
Copy definition address
of the record to all of its YES
END
usage addresses of
Dr.Sharvari Govilkar TE COMP A Page 19
GEST
? Initiate
Execution
1. Linking Loader – It performs linking and relocation at run time and loads the
linked program directly into memory for execution; no “executable image” is
generated.
It is more suitable in a program development and testing environment (where
size of programs being executed is comparatively small), as it avoids steps of
reading and writing the linked program into an executable image.
2. Linkage Editor – It performs all linking and (some) relocation prior to load time
and writes the linked program into an “executable image”.
This approach is suitable when the program has to be re-executed many times
without being re-assembled.
(The basic difference between Linking Loader and Linkage Editor is analogous to
the difference between Assemble-and-Go Loader and Absolute Loader)
3. Dynamic Linking – Linking function is performed at run time and facilities of the
OS are used to load and link subprograms at the time when they are first called.
Dynamic Linking is often used to allow several executing programs to share one
copy of a subroutine through a library; all supporting run-time routines common
to a set of executing programs (e.g. programs of same language) can be packed
into a single “library” of subroutines, which is shared by all programs.
Dynamic Linking avoids the necessity of loading the entire library for each
execution; subroutines in the library can be loaded only when required.
Q) Differentiate between Linking Loader and Linkage Editor (May ‘06 [Comps] –
Following
8M, Dec figure describes
‘07 [Comps] the difference between a Linking Loader and a Linkage Editor:
– 10M).
Object
Object Program(s)
Program(s)
Linkage
Linking Editor
Loader Library
Memory
The differences between a Linking Loader and a Linkage Editor can be described in detail
as follows: Executabl
e Image
Relocating
Loader
Memory
A Bootstrap (or Boot) Loader is a program that resides in the computers EPROM, ROM,
or other non-volatile memory that is automatically executed by the processor when the
computer is turned on.
The bootstrap loader reads the Master Boot Record (MBR) from the boot sector (which
is the first sector) of hard drive.
The MBR provides information about the drive's partitions and also tells the computer
where to find and how to load the operating system.
So, in short, a Bootstrap Loader loads the MBR from first sector of the hard drive and
the MBR causes the OS to get loaded into main memory from the secondary memory.
● Linker is a system program that resolves all address references within and
among several object modules of a program and combines them to produce a
single .exe file.
● Functions of a Linker include:
o Linking Object Files.
o Resolve inter-segment and inter-program references.
o Relocate symbols
● Loading Schemes:
● Loader Design Options – It is concerned with timing of using the loader function.
o Linking Loader generates a linked program and loads it directly into
memory for execution; no “executable image” is generated here.
o Linkage Editor writes the linked program into an .exe file which can then
be loaded into the memory and executed repeatedly.
o Dynamic Linking – The linking function is performed at run time and
facilities of the OS are used to load and link subprograms at the time
when they are first called.
● Bootstrap Loader loads the MBR from first sector of the hard drive and the MBR
causes the OS to get loaded into main memory from the secondary memory.