Unit-III-Loader & Linker - Sri Eshwar
Unit-III-Loader & Linker - Sri Eshwar
Unit-III-Loader & Linker - Sri Eshwar
Veningston .K
Department of CSE Government College of Technology, Coimbatore [email protected]
Outline
Basic loader functions Design of an Absolute Loader A Simple Bootstrap Loader Machine dependent loader features
Program Relocation Program Linking Algorithm and Data Structures for Linking Loader
Recap [1/3]
Machine architectures
Simplified Instructional Computer (SIC) SIC/XE [eXtra Equipment]
Assembler
Translate mnemonic operation codes to their machine language equivalents Assign machine addresses to symbolic labels used by the programmer Write the object program
24/August/2013
Recap [2/3]
Format of Object program
24/August/2013
Recap [3/3]
Object program contains translated instructions and data values from the source program, and specifies addresses in memory where these items are to be loaded
24/August/2013
Relocation
Modifies the object program so that it can be loaded at an address different from the location originally specified
Linking
Combines two or more separate object programs and supplies information needed to allow references between them
Linking Loader
6
Absolute loader
No linking and relocation needed All functions are accomplished in a single pass Records in object program perform
Header record
Check the Header record for program name, starting address, and length (available memory)
Text record
Bring the object program contained in the Text record to the indicated address
End record
Transfer control to the address specified in the End record to begin execution of the loaded program
24/August/2013
System Software - Sri Eshwar College of Engineering
24/August/2013
No text record indicates that the previous contents of these locations remain unchanged System Software - Sri Eshwar College of
24/August/2013 Engineering
24/August/2013
10
Binary form
Each byte of object code is stored as a single byte in the object program Most machine store object programs in a binary form
Less space and loading time Not good for reading
24/August/2013
System Software - Sri Eshwar College of Engineering
11
12
24/August/2013
13
24/August/2013
14
Solution
A more complex loader that provides
Program relocation Program linking
System Software - Sri Eshwar College of Engineering
24/August/2013
15
16
24/August/2013
17
Example
24/August/2013
18
From the object program, it is not possible to distinguish the address and constant
The assembler must keep some information to tell the loader The object program that contains the modification record is called a relocatable program
24/August/2013
System Software - Sri Eshwar College of Engineering
19
24/August/2013
20
21
24/August/2013
22
24/August/2013
23
Relocation
Loaders that allow for program relocation are called relocating loaders or relative loaders. Efficient sharing of the machine requires that we write relocatable programs instead of absolute ones. The way relocation is implemented in a loader is also dependent upon machine characteristics. Linking is not a machine-dependent function
24/August/2013
System Software - Sri Eshwar College of Engineering
24
Relocation
Two methods for specifying relocation as part of the object program Modification records
For a small number of relocations required when relative or immediate addressing modes are extensively used
Relocation bits
For a large number of relocations required when only direct addressing mode can be used in a machine with fixed instruction format (e.g., the standard SIC machine)
24/August/2013
System Software - Sri Eshwar College of Engineering
25
24/August/2013
26
24/August/2013
27
24/August/2013
28
24/August/2013
29
30
Relocation Bits
If there are many addresses needed to be modified, it is more efficient to use a relocation bit, instead of a Modification record, to specify every relocation.
When the instruction format is fixed There is a relocation bit for each word of the object program Relocation bits are put together into a bit mask If the relocation bit corresponding to a word of object code is set to 1, the programs starting address will be added to this word when the program is relocated
24/August/2013
System Software - Sri Eshwar College of Engineering
31
Relocation Bits
24/August/2013
32
Relocation Bits
A bit value of 0 indicates that no modification is necessary [e.g. data constants and JSUB] If a text record contains fewer than 12 words of object code, the bits corresponding to unused words are set to 0 A new Text record is created for proper alignment Relocation bits are generated by the Assembler and used by the Loader.
24/August/2013
System Software - Sri Eshwar College of Engineering
33
Program linking
Goal
Resolve the problems with EXTREF and EXTDEF from different control sections
A program is a logical entity that combines all of the related control sections. Control sections could be assembled together, or they could be assembled independently of one another. Control sections are to be linked, relocated, and loaded by loaders.
24/August/2013
34
(Recap)Expression
The assemblers allow the use of expressions as operand The assembler evaluates the expressions and produces a single operand address or value Expressions consist of
Operator
+,-,*,/ (division is usually defined to produce an integer result)
Individual terms
Constants User-defined symbols Special terms, e.g. *, the current value of LOCCTR
Examples
MAXLEN STAB EQU RESB BUFEND-BUFFER (6+3+2)*MAXENTRIES
24/August/2013
35
Expressions can be
Absolute
Only absolute terms
MAXLEN EQU 1000
Relative terms in pairs with opposite signs for each Pair [independent on program starting address]
MAXLEN EQU BUFEND-BUFFER
Relative
All the relative terms except one can be paired as described in absolute. The remaining unpaired relative term must have a positive sign.
STAB
24/August/2013
EQU
24/August/2013
37
38
External reference
EXTREF name [,name]
EXTREF names symbols that are used in this control section and are defined elsewhere E.g. EXTREF RDREC, WRREC
39
24/August/2013
40
24/August/2013
41
Program linking
Example
Program for Linking and Relocation Use modification records for both relocation and linking
address constant external reference
24/August/2013
42
24/August/2013
43
24/August/2013
44
24/August/2013
45
These programs are given to emphasize the relationship between the relocation and linking process.
24/August/2013
System Software - Sri Eshwar College of Engineering
46
47
24/August/2013
48
24/August/2013
49
24/August/2013
50
24/August/2013
51
Note
The general approach taken is for the assembler to evaluate as much of the expression as it can The remaining terms are passed on to the loader via modification records
24/August/2013
52
Example - 1
Consider REF4, the assembler for PROGA can evaluate all of the expression in REF4 except for the value of LISTC. This results in an initial value of 000014 and one Modification record
24/August/2013
53
Example - 2
Consider REF4, in PROGB contains no terms that can be evaluated by the assembler The object code therefore contains an initial value of 000000 and three Modification record
24/August/2013
54
Example - 3
Consider REF4, for PROGC assembler can supply the value of LISTC relative to the beginning or the program (but not the actual address which is not known until the program is loaded) The initial value of this data word contains the relative address of LISTC 000030 Modification records instruct the loader to add the beginning address of the program (i.e. the value of PROGC), to add the value of ENDA, and to subtract the value of LISTA
24/August/2013
System Software - Sri Eshwar College of Engineering
55
24/August/2013
56
Exercise
To work through references REF5 through REF8 by yourself to be sure that you have understood how the object code and modification records are generated
24/August/2013
57
24/August/2013
58
24/August/2013
59
Example
The value for reference REF4 in PROGA is located at address 4054 (the beginning address of PROGA + 0054, the relative address of REF4 within PROGA) Next slide shows how this value is computed?
24/August/2013
60
24/August/2013
61
Note
For the reference that are instruction operands, the calculated values after loading do not always appear to be equal This is because there is an additional address calculation step involved for PC-relative or Base-relative instructions.
24/August/2013
62
24/August/2013
63
64
24/August/2013
65
66
67
24/August/2013
68
24/August/2013
69
24/August/2013
70
24/August/2013
71
This reference number is used (instead of the symbol name) in Modification records Avoids multiple searches of ESTAB for the same symbol during the loading of a control section.
Search of ESTAB for each external symbol can be performed once and the result is stored in a table indexed by the reference number. The values for code modification can then be obtained by simply indexing into the table.
24/August/2013
System Software - Sri Eshwar College of Engineering
72
24/August/2013
73
The reference numbers are underlined in the Refer and modification records
24/August/2013
74
24/August/2013
75
Solution
24/August/2013
A flag for each symbol in ESTAB (defined/undefined) When add a symbol on R record to ESTAB, set it as undefined If a symbol on D record is found, set is as defined At the end of Pass 1, search the library for undefined symbol If all libraries are searched and some undefined symbol (unresolved external references) still remains, output error a message
System Software - Sri Eshwar College of Engineering
76
The same techniques applies equally well to the resolution of external references to data items
24/August/2013
System Software - Sri Eshwar College of Engineering
77
Loader Options
Users can specify options that modify the standard processing of the loader. Many loaders have a special command language that is used to specify options
Written in a separate input file Embedded in the primary input stream between object programs Included in source program Specified in job control language
24/August/2013
System Software - Sri Eshwar College of Engineering
78
79
NOCALL LIB_name
Specify the external reference name are to remain unresolved. If it is known that some external reference is not to be performed in an execution
80
Dynamic linking
Linking at the execution time
Bootstrap loader
24/August/2013
81
Linkage Editors
Difference between a linkage editor and a linking loader
Linking loader
Performs all linking and relocation operations, including automatic library search, and loads the linked program into memory for execution.
Linkage editor
Produces a linked version of the program, which is normally written to a file or library for later execution.
A simple relocating loader (one pass) can be used to load the program into memory for execution. The linkage editor performs relocation of all control sections relative to the start of the linked program. The only object code modification necessary is the addition of an actual load address to relative values within the program
24/August/2013
System Software - Sri Eshwar College of Engineering
82
Linkage editor
Suitable when a program is to be executed many times without being reassembled because resolution of external references and library searching are only performed once.
24/August/2013
System Software - Sri Eshwar College of Engineering
83
24/August/2013
84
85
Dynamic Linking
Comparison
Linkage editors perform linking operations before the program is loaded for execution Linking loaders perform linking operations at load time Dynamic linking (dynamic loading, load on call) perform linking at execution time
Delayed Binding
Avoid the necessity of loading the entire library for each execution, i.e. load the routines only when they are needed Allow several executing programs to share one copy of a subroutine or library (Dynamic Link Library (DLL))
24/August/2013
System Software - Sri Eshwar College of Engineering
86
Dynamic Linking
O.S. services request of dynamic linking
Dynamic loader is one part of the OS Instead of executing a JSUB instruction that refers to an external symbol, the program makes a load-and-call service request to the OS
Example Figures
When call a routine, pass routine name as parameter to O.S. (a) If routine is not loaded, O.S. loads it from library and pass the control to the routine (b and c) When the called routine completes it processing, it returns to the caller (O.S.) (d) When call a routine and the routine is still in memory, O.S. simply passes the control to the routine (e)
24/August/2013
System Software - Sri Eshwar College of Engineering
87
24/August/2013
88
24/August/2013
89
Bootstrap Loaders
How is the loader itself loaded into memory?
An absolute loader program is permanently resident in a read-only memory (ROM) Copy absolute loader in ROM into RAM for execution (optional) Read a fixed-length record from some device into memory at a fixed location. After the read operation, control is automatically transferred to the address in memory
24/August/2013
System Software - Sri Eshwar College of Engineering
90
24/August/2013
91