0% found this document useful (0 votes)
107 views53 pages

Linker and Loader

The document outlines the processes of linking and loading programs, detailing the steps involved in program execution, including translation, linking, relocation, and loading. It explains key terminologies such as translated, linked, and load time addresses, as well as the concepts of program relocation and linking, emphasizing the roles of linkers and loaders. Additionally, it describes different types of loaders and their functionalities, highlighting the importance of relocation and linking in program execution.

Uploaded by

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

Linker and Loader

The document outlines the processes of linking and loading programs, detailing the steps involved in program execution, including translation, linking, relocation, and loading. It explains key terminologies such as translated, linked, and load time addresses, as well as the concepts of program relocation and linking, emphasizing the roles of linkers and loaders. Additionally, it describes different types of loaders and their functionalities, highlighting the importance of relocation and linking in program execution.

Uploaded by

kkp002003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Linker and Loader

Execution steps of Program


• Translation of program
• Linking of program with other programs needed for its execution
• Relocation of the program to execute from the specific memory area
allocated to it
• Loading of the program in the memory for the purpose of execution

• 1st step is performed by translator


• 2nd and 3rd step is performed by linker
• 4th step is performed by Loader
Execution steps of Program
Terminologies Used for Program Entity at
Different Times
• Translation Time Address:-
– Address assigned by translator
• Linked Address:-
– Address Assigned by the linker
• Load Time Address:-
– Address Assigned by the Loader
Terminologies Used for Program Entity at
Different Times
• Translated origin:-
– Address of the origin assumed by the translator
– This is the address specified by the programmer in an ORIGIN
statement
• Linked origin:-
– Address of origin assigned by linker while producing binary
program
• Load origin:-
– Address of origin assigned by loader while loading the program
for execution
Why The linked and load origins may differ from
translated origin?
• Same set of translated addresses may have been used by different
object modules of the program. This results conflict in memory
allocation.
• OS may require that a program should execute from specific area
of memory. This may require change in its origin, thus changing
execution start address and symbol addresses.
• Thus, require changes in origin by linker and loader.
Relocation and Linking Concepts
Program Relocation:-
• AA - Set of Absolute Addresses, may be instruction address or data
address.
• AA ǂ ɸ means instruction or data occupy memory words with specific
address.
• Address Sensitive Program Contains:
1. Address Sensitive Instruction: an instruction which contains
address ai Є AA.
2. Address Constant: a data word which contains an address ai Є AA
Relocation and Linking Concepts
Program Relocation:-
• Address Sensitive Program P can execute correctly only if the start
address of the memory area allocated to it is the same as its
translated origin.
Start Address == Translation Address
• Thus, to execute correctly from any memory area, address used in
each address sensitive instruction of P must be 'corrected'.
Relocation and Linking Concepts
Program Relocation definition:-
The process of modifying the addresses used in the address
sensitive instructions of a program such that the program can execute
correctly from the designated area of memory
• If Linked origin ǂ Translated origin then relocation is performed by
Linker
• If Load origin ǂ Linked origin then relocation is performed by Loader
• In general Linker always performs relocation
Relocation and Linking Concepts
Relocation and Linking Concepts
Performing Relocation :-
• Let translated and linked origin of program p be t_origin and l_origin
respectively.
• Consider a symbol symb in p.
• Let it’s translation time address and link time address be tsymb and
lsymb respectively
• Then relocation factor of p is defined as
relocation_factor = l_origin - t_origin --------------(1)

• Relocation factor may be positive or negative


Relocation and Linking Concepts
Performing Relocation :-
• Consider a statement which uses symb as an operand. The translator
puts the address tsymb in the instruction generated for it
tsymb = t_origin + dsymb ……(2)
Where,
dsymb is the offset of symb in p
lsymb = l_origin + dsymb
Using 1,
lsymb = t_origin + relocation_factor + dsymb
= tsymb + relocation_factor , using 2
Relocation and Linking Concepts
Performing Relocation :-
Relocation and Linking Concepts
Linking:-
• Consider an application program AP consists of program units
SP = {Pi}.
• A program unit Pi interacts with another unit Pj by using addresses of
Pj’s instruction and data in it’s own instructions.
• To realize such interaction Pj and Pi must contain public definitions
and External References:
Relocation and Linking Concepts
Linking:-
• Public Definition:-
– A symbol pub_symb defined in a program unit which may be
referenced in other program units.
• External Reference:-
– A reference to a symbol ext_symb which is not defined in the
program unit containing the reference.
Relocation and Linking Concepts
Linking:-
• EXTRN & ENTRY statements:-
– The ENTRY statement lists the public definitions of a program unit
i.e. it lists those symbols defined in a program unit which may be
referenced in other program units.
– The EXTRN statement lists the symbols to which external
references are made in the program.
Relocation and Linking Concepts

Figure. Program Unit P


Relocation and Linking Concepts
Linking:-
• Before Application Program AP can be executed, it is necessary that
for each Pi in SP, every external reference in Pi should be bound to
correct link time address.
• “Linking is the process of binding an external reference to the correct
link time address”.
• An external reference is said to be unresolved until linking is
performed for it.
• It is said to be resolved when linking is complete.
Relocation and Linking Concepts
Linking:-
Statement Address
START 200
ENTRY ALPHA
-------
-------
ALPHA DS 20 231)

Figure. Program Unit Q


Relocation and Linking Concepts
Linking:-
• Let the Program Unit P be linked with the Program unit Q.
• P contain the external reference to symbol ALPHA which is public
definition in Q with the translated time address 231.
• If the link origin of P is 500, and size is 42 words then the link origin of
Q is 542 and the link time address of ALPHA is 573.
• Linking is performed by putting the link time address of ALPHA in the
instruction of P.
Relocation and Linking Concepts
Object Module:-
• The Object Module of a Program contains the information necessary
to relocate and link the program with other programs.
• It consists of 4 components:-
1) Header:-
– It contains translated origin , size and execution start address of P.
2) Program:-
– It contains machine language program corresponding to P.
3) Relocation Table (RELOCTAB):-
– It describes IRRp(Instruction Requies Relocation) of program P.
– Each RELOCTAB contains a single field i.e. Translation Address of an Address
Sensitive Instruction
Relocation and Linking Concepts
Object Module:-
• The Object Module of a Program contains the information necessary
to relocate and link the program with other programs.
• It consists of 4 components:-
4) Linking Table (LINKTAB):-
– It contains information related to public definition and external references
in P
– Each LINKTAB entry contains 3 fields:
» Symbol:- Symbolic Name
» Type:- PD/EXT
» Translation Address:- For PD this is the address of first memory word
allocated to symbol. For EXT it is address of memory word
which is required to contain address of symbol
Relocation and Linking Concepts
Object Module:-
• For previously given assembly program, object module
contains following information:
1. Header:-
– Translated Origin: 500, Size:42,Exe. Strt. Add=500.
2. Program:-
– Machine Language Instructions..
Relocation and Linking Concepts
Object Module:-
• For previously given assembly program, object module
contains following information:
3. Relocation table:
500
538

Symbol Type Translated Address


4. Linking table:
ALPHA EXT 518
MAX EXT 519
A PD 540
Relocation and Linking Concepts
Binary Programs:-
• Is a machine language program comprising set of program units SP such that
for all Pi Є SP,
1. Pi relocated at link origin.
2. Linking is performed for each external reference in Pi.
• Linker Command:
linker <link origin> <object module names> [, <execution start address> ]
• Linker converts object module into-> the set of program units (SP) into-> a
binary program.
• If link address = load address, loader simply loads binary program into
appropriate memory area for execution.
Design of Linker
Relocation Algorithm:-
1. program_linked_origin:=<link origin> from Linker command;
2. For each object module
(a) t_origin:= translated origin of object module;
OM_size:= size of object module;
(b) relocation_factor:= program_linked_origin–t_origin;
(c) Read the machine language program in work_area;
(d) Read RELOCTAB of object module.
Design of Linker
Relocation Algorithm:-
e) For each entry in RELOCTAB
(i) Translated_addr:= address in RELOCTAB entry;
(ii) address_in_work_area:= address of work_area + translated_address –
t_origin;
(iii) Add relocation_factor to the operand address in the word with the address
‘address_in_work_area’.
(f) Program_linked_origin:= program_linked_origin+ OM_Size.
Design of Linker
Relocation Algorithm:- Example
• Let address of work_area= 300.
• While relocating OM, relocation factor = 400.
• For the first RELOCTAB entry, address_in_work_area = 300+500-500=300.
• This word contains the instruction for READ A.
• It is reallocated by adding 400 to operand address in it.
• For the second RELOCTAB entry, address_in_work_area=300+538-
500=338.
• The instruction in this word is similarly relocated by adding 400 to the
operand address in it.
Design of Linker
Linking Requirements:-
• In Fortran all program units are translated separately.
• Hence, all sub program calls and common variable reference require linking.
• Programs are nested in main program.
• Procedure reference do not require linking, they can be handled using relocation.
• Build in functions require linking.
• Linker processes all object modules being linked & builds a table of all public definition &
load time address.
• Name Table (NTAB)
–(a) Symbol: Symbol Name for external reference or object module.
–(b) Linked Address: For public definition contains link_address. For object module
contains link_origin.
• Most information in NTAB is derived from LINKTAB entries with type=PD.
Design of Linker
Program linking Algorithm:-
1. program_linked_origin:= <link origin> from linker command.
2. For each object module
(a)t_origin:= translated origin of object module. OM_size:= size of the object
module;
(b)Relocation_factor:= program_linked_origin–t_origin.
(c)Read the machine language program in work_area.
(d)Read LINKTAB of the object module.
(e)For each LINKTAB entry with type = PD
• name:= symbol;
• linked_address:= translated_address+ relocation_factor;
• enter(name,linked_address) in NTAB.
Design of Linker
Program linking Algorithm:-
(f) Enter (object module name, program_linked_origin) in NTAB.
(g) program_linked_origin:= program_linked_origin+ OM_size;
• 3. for each object module
(a) t_origin:= translated origin of the object module.
(b) For each LINKTAB entry with type=EXT
(i) address_in_work_area:= address of work_area +
program_linked_origin– <link_origin> + translated address –t_origin;
(ii) Search symbol in NTAB and copy its linked address. Add the linked
address to the operand address in the word with the address
“address_in_work_area”.
Design of Linker
Program linking Algorithm:- Example
• While linking program P and program Q with linked_origin=900, NTAB
contains following information:

• Work_area= 300.
• When LINKTAB of alpha is processed during linking,
address_in_work_area:= 300 + 900 -900 + 518 –500. i.e. 318.
• Hence linked address of ALPHA is 973, is copied from NTAB entry of
ALPHA and added to word in address 318.
Self Relocating Programs
• Types:
–Non Re-locatable Programs
–Re-locatable Programs
–Self Re-locating Programs
Self Relocating Programs
1. Non-Re-locatable Programs:
–It cannot be executed in any memory area other than area
starting on its translated origin.
–Has address sensitive programs.
–Lack information concerning address sensitive instruction
–Eg. Hand coded machine language program.
Self Relocating Programs
2. Re-locatable Programs:
–It can be processed to relocate it into desired data area of memory.
–Eg: Object Module.

3. Self Relocating Programs:


– It can perform relocation of its own address sensitive instruction.
– Consist of:
• A table of Information (for address sensitive prg)
• Relocating logic
– Self re-locating program can execute in any area of memory.
– As load address is different for each execution, good for time sharing
systems.
Loaders
• A Loader is a system software program that performs the loading
function.
• Loading is the process of placing the program from secondary
memory into the main memory for execution.
• Loader is responsible for initiating the execution of the program.
Functions of Loader
1. Allocation: Allocate memory space for program.
2. Linking: Link object program code with necessary library routines &
generate executable program code.
3. Relocation: It change memory address of program when it loaded
into the memory.
4. Loading: Finally executable file contents (machine instructions &
Data) will be placed physically into memory for execution.
Types of Loader
1. Compile-Go Loader
2. General Loader
3. Absolute Loader
4. Relocating Loader
Compile- Go Loader
• The instructions are read line by line, its machine code is obtained and it
is directly put in the main memory
• Here assembler is work as loader and execute in one part of memory and
data directly put into assign part of memory location
• After completion of assembly process, it assigns the starting address of
the program to the location counter.
• Example:- WATFOR-77 used in fortan
Compile- Go Loader
Compile- Go Loader
• Advantages:
1. Easy to design & implementation.
2. Relocation performed by translator.
3. No object file created.
4. Suitable for basic program.
• Disadvantages:
1. Wastage in memory space due to the presence of the assembler.
2. No production of object file, the source code is directly converted to
executable form. Hence even though there is no modification in the source
program it needs to be assembled and executed each time.
3. Difficult to handle multiple linking modules.
General Loader
• This scheme avoids the disadvantage of “Compile-Go Loader”
• In this loader scheme, the source program is converted to object
program by some translator (assembler).
• The loader accepts these object modules and puts the machine
instruction and data in an executable form at their assigned memory.
• The loader occupies the same portion of main memory.
• Size of Loader is smaller than assembler
General Loader
Absolute Loader
• It is a kind of loader in which relocated object files are created.
• Loaders accepts .obj files and places them at a specified locations in
the memory
• This type of loader is called absolute because no relocation
information is needed; it is obtained from the programmer or
assembler.
• The starting address of every module is known to the programmer,
this corresponding starting address is stored in the object file.
Absolute Loader

Object Code
Object Code
Assembler for
Segment 1 Starting Add
Segment1

Object Code Absolute


Loader
Object Code
Segment 2 Assembler for
Starting Add
Segment2

Object Code Object Code


Segment 3 Assembler for
Starting Add Segment3
Absolute Loader
• In this scheme the programmer or assembler should have knowledge
of memory management.
• Absolute loader is simple to implement in this scheme because
• Allocation is done by programmer
• Linking is done by the programmer or assembler
• Relocation is done by assembler
• Simply loading is done by loader
Absolute Loader
1. Example of Absolute Loader
Bootstrap Loader:-
• When computer is first turned on or restarted, a special type of absolute
loader called as Bootstrap Loader is executed.
• This bootstrap loads the first program to be run by the compiler usually
operating system.
• The bootstrap itself is begins at address 0
• It loads the OS starting address 0x80
Relocating Loader
• Loaders that allows for program relocation are called relocating loaders.
• It loads program in specific area of memory so that it can execute
properly
• The term "relocating" refers to the ability of the loader to adjust, or
"relocate", the absolute addresses in a program so that it can be
loaded at an address different from the one originally specified for
it.
• Example:- BSS(Binary Symbolic Subroutines) loader used in IBM 7094,
IBM 1130 etc.
• BSS scheme stored object programs and information about all programs
Relocating Loader
Important Questions
• Explain the execution step of program with schematic representation
• Explain the different Terminologies Used for Program Entity during execution
• What is program Relocation? Why it is needed?
• What is program Relocation? Explain how it is performed.
• Explain the use of EXTRN and ENTRY statement in linking with an example
• Explain the information contained in Object Module with an example
• Explain the Relocation algorithm with an example
• Explain the Program Linking algorithm with an example
• Explain the types of programs.
• What is Loader? Explain the types of Loader with diagram.

You might also like