Ss ch7
Ss ch7
By:
Bhargavi Goswami
Assistant Professor
Sunshine Group of Institutions
Rajkot, Gujarat, India.
Email: bhargavigoswami@gmail.com
Mob: +918140099018
Steps for Execution:
Performed By:
Translator of L: 1. Translation of program
2. Linking of program with other
programs needed for its
execution.
Linker:
3. Relocation of the program to
execute from the specific
memory area allocated to it.
4. Loading of the program in the
Loader: memory for the purpose of
execution.
Schematic of Program Execution:
Data
Binary
Translator Linker Loader
Program
Control
Flow
Results
Source
Program
Object Binary
Module Programs Arrows indicate Data flows
Few Terminologies:
• Translation Time (or Translated) Address: Address
assigned by the translator.
• Linked Address: Address assigned by the linker.
• Load Time (or Load) Address: Address assigned
by the loader.
• 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 the origin assigned by
the linker while producing a binary program.
• Load Origin: Address of the origin assigned by the
loader while loading the program for execution.
Origin may need to be changed:
• 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 to 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.
Example:
Statement Address Code
START 500
ENTRY TOTAL The translated
EXTRN MAX, ALPHA origin
READ A 500) + 09 0 540
LOOP 501)
. The Translated Time
Address of LOOP
.
MOVER AREG, ALPHA 518) + 04 1 000
BC ANY, MAX 519) + 06 6 000
.
.
BC LT, LOOP 538) + 06 1 501
STOP 539) + 00 0 000
A DS 1 540)
TOTAL DS 1 541)
END
RELOCATION & LINKING CONCEPTS
• TOPIC LIST:
(a) Program Relocation
– Performing Relocation
(b) Linking
– EXTERN & ENTRY statement
– Resolving external references
– Binary Programs
= t symb + relocation_factorp
IRR: Instruction Requiring Relocation
• It’s a set of instructions that perform
relocation in program p.
• Steps:
– Calculate Relocation Factor(RF)
– Add it to Translation Time Address(es) for every
instruction which is member of IIR.
• Eg: relocation factor = 900 -500 = 400
IRR contains translation addresses 540 & 538
(instruction : read A)
Address is changed to 540 + 400 = 940 and
538 + 400 = 938.
Linking:
• AP is an Application Program consisting of a Set
of Program units SP = { Pi }.
• A program unit Pi interacts with another program
unit Pj using instructions & addresses of Pj.
• For this it must have:
– Public Definition: a symbol pub_symb defined in
program unit which may be referenced in other
program.
– External Reference: a reference to a symbol ext_symb
which is not defined in program unit containing the
reference.
• Who will handle the these two things?
EXTRN & ENTRY statements:
• The ENTRY statement lists the public definition of
program unit.
– i.e it list those symbols defined in program unit which may
be referenced in other program units.
• The EXTRN statement list the symbols to which
external references are made in the program unit.
• Eg: see the pg 223.
– TOTAL is ENTRY statement.(public defination)
– MAX and ALPHA are EXTRN statements (external
reference)
– Assembler don’t know address of EXTRN symb and so it
puts 0’s address field of instruction wherever these symb
are found.
– What if we don’t refer these var with EXTRN stmts?
– Assembler gives errors.
– Thus, requirement arises for resolving external references.
Resolving External Reference:
• Before AP (application program) executes, every
external reference should be bound to correct
link time address.
• Who will do this binding?
– Here comes Linker into the picture.
• Def: 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.
• Linking is performed for the eg defined in pg. no
223 and the Q program unit defined as follows:
Example: Linked with Program Unit Q
it is not PD.
DESIGN OF LINKER:
• What influence relocation requirement of a program?
• Ans: Addressing Structure of Computer System.
• How to reduce relocation requirement of a program?
• Ans: By using segmented addressing structure.
• JUMP avoids use of absolute address, hence instruction
is no more address sensitive.
• Thus, no relocation is needed.
• Effective Operand Address would be calculated
considering starting address as 2000 would be
<CS> + 0196 = 2196 (which is corrected address)
• Lets take example to understand it more clearly.
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
Example:
• 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
• Thus, no reduction in linking requirements.
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;
• LNAMES:
– record lists the names for use by SEGDEF records.
• SEGDEF:
– Designates a segment name
– Uses index into in to the list
– Attribute field indicates whether segment is re-locatable or
absolute.
– Also indicates the manner in which it is combined with other
segments.
– Also indicates alignment requirement of its base address.
– Attribute field also contains origin specification for absolute
segment.
– Stack segments with same names are concatenated and
common segments with same names are overlapped.
Attributes Segment
98H Length Name Index (1) Check Sum
(1-4) Length (2)
EXTDEF record
External
8CH Length Check Sum
Reference List
PUBDEF record
90H Length base (2-4) Name offset (2) … Check Sum
• EXTDEF:
– The EXTDEF record contains a list of external
references used by this module.
• PUBDEF:
– Contains list of public names declared.
– Base specification identifies the segment.
– (name, offset) pair defines one public name.
FIXUPP record
Frame Target Target Check
9CH Length Locat (1) Fix dat (2) …
datam (1) datum(1) offset(2) Sum
FIXUPP codes
• FIXUPP: Loc Meaning:
– Designates external symbol name Code
– Uses index to the list 0 Low order byte to be fixed
• Locate contains offset of fix up 1 Offset is to be fixed
location in previous LEDATA.
2 Segment is to be fixed
• Frame datum refers to SEGDEF
record. 3 Pointer (segment offset) to be
fixed.
• Target datum and target offset
specify relocation and linking Fixdat field codes
information.
Loc Meaning:
• Target Datum contains segment index
Code
or an external index.
• Target offset contains offset from 0 Segment index & displacement
name indicated in target datum. 2 External index &target displcmt
• Fix data field indicates manner in 4 Segment index (without offset)
which target datum and fix datum 6 External index (without offset)
fields are to be interpreted.
MODEND record
Start addr Check
8AH Length Type (1)
(5) sum
• MODEND record:
– Signifies the end of module.
– Type field indicates whether it is the main program.
– Has 2 components: (a) segment, (b) offset
• LEDATA records:
– Contains binary image of code generated by language translator.
– Segment index identifies the segment to which the code
belongs.
– Offset specifies the location of the code within the segment.
LEDATA record
Segment Data Check
AOH Length data
Index (1-2) Offset (2) sum
Algorithm 7.3 (First Pass of LINKER)
1. program_linked_origin := <load origin>;
2. Repeat step 3 for each object module to be linked.
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) i := name index; segment_name := NAMELIST[i];
segment_addr := start address in attributes;
(ii) If an absolute segment, enter (segment_name, segment_addr) in
NTAB.
(iii) If the segment is re-locatable and cannot be combined with
other segments
- Align the address containing in program_linked_origin on the
next word or paragraph as indicated in attribute list.
- Enter (segment_name,program_linked_origin) in NTAB.
- program_linked_origin := program load origin + segment
length;
(c) For each PUBDEF record
(i) i := base; segment_name := NAMELIST[i];
(ii) segment_addr := load address of
segment_name in NTAB;
(iii) sym_addr := segment_addr + offset;
(iv) Enter (symbol,sym_addr) in NTAB.
-----------------------------------------------------------------
• In first pass, linker only processes the object
records relevant for building NTAB.
• Second pass performs relocation and linking.
Algorithm 7.4 (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.
(d) if an LEDATA record
(i) i:= segment index; d:=data offset;
(ii) program_load_origin:= SEGTAB[i].load address;
(iii) address_in_work_area := address of work_area +
program_load_origin - <load origin> + d;
(iv) move data from LEDATA into the memory area
starting at the address address_in_work_area.
THANK YOU