SP Unit3 RNP
SP Unit3 RNP
Reshma Pise 1
Course Outcomes :
On completion of the course, the students will be able to –
1. Discriminate among different System software and their functionalities.
3. Develop approaches and methods for implementing compiler, linker and loader.
4. Use LEX tool for lexical analysis.
5. Interpret the techniques of implementing utility software.
Reshma Pise 2
Outline
Linkers: Relocation and linking concepts, static and dynamic linker, Subroutine
linkages.
Self-relocating programs.
Loaders: Loader schemes: Compile and Go, General loader scheme, Absolute loaders,
Relocating loaders, Direct linking loaders, Overlay Structure.
Dynamic linking, API compatibility, Dynamically Linked libraries.
Reshma Pise 3
Schematic of Program Execution:
Performed By:
Translator of L:
1. Translation of program
2. Linking of program with other programs and libraries,
external symbols needed for its execution.
Linker:
3. Relocation of the program to execute from the
specific memory area allocated to it.
Loader: 4. Loading of the program in the memory for the
purpose of execution.
Reshma Pise 4
Schematic of Program Execution:
Data
Binary
Translator Linker Loader
Program
Results
Source
Program
Object Binary
Module Programs (.exe)
Reshma Pise 5
Terminologies:
Reshma Pise 6
Change of origin
• The linked and load origins may differ from translated origin.
• Reason?
– Same set of translated addresses may have been used by different object
modules of the program. This results in conflict during 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, origin may be changed by linker and loader.
Reshma Pise 7
Example: P
Statement Address Code
START 500
(b) Linking
– EXTERN & ENTRY statement
– Resolving external references
– Binary Programs
Reshma Pise 10
Program Relocation : Definition
• The process of modifying the address used in the address sensitive instructions
such that the program can execute correctly from the designated area of memory.
• Linker performs relocation if
– Linked Origin ≠ Translated Origin
• Loader performs relocation if
– Load Origin ≠ Linked Origin
• In general: Linker always performs relocation, whereas some loaders do not.
• Absolute loaders do not perform relocation, then
– Load Origin = Linked Origin
– Thus, Load Origin and Linked Origin are used interchangeably.
Reshma Pise 11
Performing Relocation:
= t symb + relocation_factorp
Reshma Pise 12
IRR: Instruction Requiring Relocation
Reshma Pise 13
Linking:
Reshma Pise 14
EXTRN & ENTRY statements:
Reshma Pise 15
Resolving External Reference:
• Every external reference should be bound to correct link time address , before
application program executes.
• Who will do this binding?
– Here comes Linker into the picture.
• Linking:
Linking is the process of binding an external reference to correct link time address.
• External reference is said to be unresolved until linking is performed and resolved
once linking is completed.
Reshma Pise 16
Example: Linking
Consider Program Unit Q linked with P (Slide No. 8)
ALPHA is external reference in example ‘P’ which is linked with above given unit.
Reshma Pise 17
Binary Programs:
Machine language program comprising of set of program units SP such that for all Pi Є SP,
– Pi relocated at linked origin.
– 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.
Reshma Pise 18
Object Module:
• Object Module(OM) contains all the information necessary to relocate and link the program with other
programs.
• OM consist of four components:
1. Header: Contains translated origin, size and execution start address of P.
2. Program: Contains machine language program corresponding P.
3. Relocation Table: (RELOCTAB) describing IRRp (Instruction Requiring Relocation). Eachentry
contains field: ‘Translated Address’ - of address sensitive instruction.
4. Linking Table: (LINKTAB) contains information concerning public definition and external reference.
Has three fields:
• Symbol: symbolic name
• Type: PD/EXT i.e public definition or external reference
• Translated Address: If PD, address of first memory word allocated. If EXT, address of memory
word containing reference of symbol.
Reshma Pise 19
Example:
• For previously given assembly program, object module contains following information:
Consider first statement START 500
1. Header :Translated Origin: 500, Size:42, Execution Start Add= 500
2. Machine Language Instructions
3. Relocation table: 500
538
Reshma Pise 20
DESIGN OF LINKER:
Reshma Pise 21
Linking in Segmented Addressing
Sr. No Statement Offset
0001 DATA_HERE SEGMENT
0002 ABC DW 25 0000
0003 B DW ? 0002
. .
. .
0012 SAMPLE SEGMENT
0013 SEGMENT CS:SAMPLE,
DS:DATA_HERE
0014 MOV AX, DATA_HERE 0000
0015 MOV DS, AX 0003
0016 JMP A 0005
0017 MOV AL,B 0008
.
0027 A MOV AX,BX 0196
.
0043 SAMPLE ENDS
0044 END
Reshma Pise 22
Linking in Segmented Addressing
• Code written in assembly language for Intel 8088.
• ASSUME statements declares segment register CS and DS for memory addressing.
So, all memory addressing is performed using suitable displacement of their contents.
• Translation time address of A is 0196.
• In statement 16, reference of A due to JMP statement makes displacement of 196 from the content of CS register.
Hence avoids usage of absolute addressing. Thus, instruction is not address sensitive.
Displacement ->avoids usage of absolute address->not address sensitive
instruction.
• As DS is loaded with execution time address of DATA_HERE, reference to B would be automatically relocated to correct
address.
• Thus, Use of segment register reduces relocation requirement but doesn’t eliminate it.
• Inter Segment Calls & Jumps are handled in similar way.
• Relocation is more involved in case of intra segment jumps assembled in FAR format.
• Linker computes both:
– Segment Base Address
– Offset of External Symbol
• No reduction in linking requirements.
Reshma Pise 23
Quiz
1.) _______ is the process binding a program with other programs and
libraries, external symbols.
3) Relocation corrects the address used in _______ instructions such that the
program can execute correctly from the designated area of memory.
Reshma Pise 24
Quiz : Solution
1.) _____is the process binding a program with other programs and libraries,
external symbols. Linking
3) Relocation corrects the address used in _______ instructions such that the
program can execute correctly from the designated area of memory.
address sensitive instructions
4) Relocation may be performed by a) Linker b.) Loader c.) Both c) Both
Reshma Pise 25
Quiz
6.) ________ indicates a symbol defined in a program unit which may be
referenced in other program.
7.) The ______ statement lists the public definitions of program unit whereas
______ statement lists the symbols to which external references are made
in the program unit.
8.) The four components of OM are : ________
Reshma Pise 26
Quiz : Solution
6.) ________ indicates a symbol defined in a program unit which may be
referenced in other program. Public Definition
7.) The ______ statement lists the public definitions of program unit whereas
______ statement lists the symbols to which external references are made
in the program unit. ENTRY, EXTRN
8.) The four components of OM are : ________
Header, Program, RELOCTAB and LINKTAB
9.) The three fields of LINKTAB are Symbol, type and Translated address
Reshma Pise 27
Assignments
1) Briefly explain the concept of relocation with an example.
2) Briefly explain the concept of Linking with an example.
3) Describe how the relocation is performed.
4) Elaborate the four components in Object Module.
5) Explain the purpose and structure of LINKTAB and RELOCTAB.
6) Justify :
Segmented addressing reduces the relocation requirements with
valid explanation and example.
Reshma Pise 28
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;
Reshma Pise 29
Example:
Reshma Pise 30
Linking Requirements:
Linker processes all object modules being linked & builds a table of all public
definition & load time address.
Name Table (NTAB)
Reshma Pise 31
Algorithm: Program Linking
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.
(f) Enter (object module name, program_linked_origin) in NTAB.
(g) program_linked_origin := program_linked_origin + OM_size;
Reshma Pise 32
Algorithm: Program Linking
3. For each object module mentioned in Linker Command,
(a) t_origin := translated origin of the object module.
program_linked_origin = linked_address from NTAB
(a) 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.
Reshma Pise 33
Example: Program Linking
• Linker Command be :
> Linker 900 , P , Q
NTAB : Name Table Global
linked_origin=900, NTAB contains information shown in fig:
e.g: work_area = 300. Symbol Linked
Address
• When LINKTAB entry of alpha is processed during linking,
P 900
address_in_work_area := 300 + 900 - 900 + 518 – 500.
TOTAL 941
= 318.
Q 942
Linked address of ALPHA is 973, is copied from NTAB entry of
ALPHA 973
ALPHA and added to word in address 318.
300+900-900+519-500
= 319
Reshma Pise 34
SELF RELOCATING PROGRAMS
• Types of programs:
– Non Re-locatable Programs
– Re-locatable Programs
– Self Re-locating 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.
Reshma Pise 35
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.
Reshma Pise 36
A LINKER FOR MS DOS
A linker for Intel 8088/80/x86 processors which resembles LINK of MS DOS.
– Each object record contains variable length information and may refer to contents of previous
object records.
Reshma Pise 37
Types of Object Records (order is important)
Reshma Pise 38
Object Record Formats
Reshma Pise 39
Object Record Formats:
LNAMES: record lists the names for use by SEGDEF records.
96H Length Name List Check Sum
Reshma Pise 40
Object Record Formats:
EXTDEF record : The EXTDEF record contains a list of external references used by module
External
8CH Length Check Sum
Reference List
Reshma Pise 41
Object Record Formats:
FIXUPP record : Contains information necessary for performing Relocation and Linking
Frame Target Target Check
9CH Length Locat (1) Fix dat (2) …
datam (1) datum(1) offset(2) Sum
Reshma Pise 42
Object Record Formats:
MODEND record : Indicates the end of module
Reshma Pise 43
Example MS DOS Assembly Program and Object Module
In first pass, linker only processes the object records relevant for building NTAB.
Second pass performs relocation and linking.
Reshma Pise 46
Algorithm for Second Pass of LINKER
1. list_of_object_module := object modules named in LINKER command;
2. Repeat step 3 until list_of_object_modules is empty.
3. Select an object module and process its object records.
(a) if an LNAMES record
Enter the names in NAMELIST.
(b) if a SEGDEF record
i := name index; segment_name := NAMELIST[i];
(c) if an EXTDEF record
(i) external_name := name from EXTDEF record;
(ii) if external_name is not found in NTAB, then
- Locate object module in library which contains external_name as a segment or public definition.
- Add name of object module to list_of_object_module.
- Perform first pass of LINKER, for new object module.
(iii) Enter (external_name, load address from NTAB) in EXTTAB.
Reshma Pise 47
Algorithm for Second Pass of LINKER (Contd.)
(d) if an LEDATA record
(i) i:= segment index; d := data offset;
(ii) program_linked_origin := SEGTAB[i].linked address;
(iii) address_in_work_area := address of work_area + program_linked_origin - <load origin> + d ;
(iv) move data from LEDATA into the memory area starting at the address address_in_work_area.
Reshma Pise 48
Linking for OVERLAYS:
Definition: (Overlay) An overlay is a part of a program ( or software package) which has the same
load origin as some other parts of the program.
Overlays are used to reduce the main memory requirement of a program.
Reshma Pise 49
Linking for OVERLAYS:
Reshma Pise 50
Linking for OVERLAYS:
Reshma Pise 51
Quiz
1.) Linker processes all object modules being linked & builds ______ table.
2.) Information in NTAB is derived from LINKTAB entries with type _____ .
5.) _______ program can perform relocation of its own address sensitive
instruction.
5.) _______ program can perform relocation of its own address sensitive
instruction. Self Relocating Program
Reshma Pise 54
Loaders
A loader is a system
program that performs
the loading of object
program into memory.
Loaders also support
relocation & linking.
Others have a separate
linker and loader,
Reshma Pise 55
Loader : Basic Functions
Reshma Pise 56
Loaders
Two Types:
1. Absolute Loader:
– Can load only programs with load origin = linked origin.
– This is inconvenient when load address differ from the one specified for execution.
2. Relocating loader:
– Performs relocation while loading a program for execution.
– Permits program to be executed in different parts of memory.
MS DOS support 2 object program forms:
1. .COM:
– Contains non re-locatable object program.
– Absolute loaders are invoked.
2. .EXE:
– Contains re-locatable object program.
– Relocating loaders are invoked.
At the end of loading, execution starts.
Reshma Pise 57
Loader Schemes
Compile and Go
Reshma Pise 58
Absolute Loader
Reshma Pise 59
Disadvantages
Reshma Pise 60
Disadvantages
If changes were made to MAIN that increased its length to more than 300 bytes
◦ the end of MAIN (at 100 + 300 = 400) would overlap the start of SQRT (at 400)
◦ It would then be necessary to assign SQRT to a new location
◦ changing its START and re-assembling it?!
Furthermore, it would also be necessarily to modify all other subroutines that referred
to the address of SQRT.
Reshma Pise 61
Disadvantages of Absolute Loaders
Reshma Pise 62
Disadvantages of Absolute Loaders
Allocation - by programmer
Linking - by programmer
Relocation - None required-loaded where assembler assigned
Loading - by loader
Reshma Pise 63
A Simple Bootstrap Loader
Reshma Pise 64
General Loader Scheme
Linking
Relocation
Loading
Reshma Pise 65
Dynamic Loading
If the total amount of memory required by all subroutines exceeds the amount
available
The module loader loads the only the procedures as they are needed.
◦ Allocating an overlay structure
The Flipper or overlay supervisor is the portion of the loader that actually
intercepts the "calls" and loads the necessary procedure.
Reshma Pise 66
Example
Suppose a program consisting of five subprograms (A{20k},B{20k}, C{30k},
D{10k}, and E{20k}) that require 100K bytes of core.
◦ Subprogram A only calls B, D and E;
◦ subprogram B only calls C and E;
◦ subprogram D only calls E
◦ subprogram C and E do not call any other routines
Note that procedures B and D are never in used the same time; neither are C
and E.
Reshma Pise 67
Longest Path Overlay Structure
100k Vs. 70k needed
A B C
20K 20K 30K
D E
10K 20K
Reshma Pise 68
Dynamic Linking
The loading and linking of external references are postponed until execution
time.
The loader loads only the main program
If the main program should
◦ execute a branch to an external address,
◦ reference an external variable
The loader is called
◦ Only then has the segment containing the external reference loaded.
Reshma Pise 69
Quiz (Practice)
1) A linker is given object module for a set of programs that were compiled
separately. What information need not be included in an object
module?______________.
2) The process of assigning load addresses to the various parts of the program
and adjusting the code and data in the program to reflect the assigned
addresses is called _______.
3) Object modules generated by assembler that contains unresolved external
references are resolved for two or more object module by a/an__________.
4) Object code is the output of ______.
5) In an absolute loading scheme, which loader function is accomplished by a
loader ________.
6) A system program that combines the separately compiled modules of a
program into a form suitable for execution ?_________.
Reshma Pise 70
Quiz (Practice)
7) Load address for the first word of the program is called?________.
8) Consider a program with a linked origin of 5000. Let the memory area
allocated to it have the start address of 70000. Which among the following will
be the value to be loaded in relocation register?__________
9) Resolution of externally defined symbols is performed by___________.
10)Static memory allocation is typically performed during _____________.
11)Dynamic memory allocation is typically performed during ________ .
12)Linking is process of binding________.
13)If load origin is not equal to linked origin then relocation is performed
by___________.
14)If linked origin is not equal to translated address then relocation is
performed by____________.
15)Binder performs the functions of______________.
Reshma Pise 71
Assignments
Reshma Pise 72
Assignments
Reshma Pise 73
Reference
Reshma Pise 74
THANK YOU
Reshma Pise 75