Chapter 3
Chapter 3
1
Introduction
2
Overview of Chapter 3
•Type of loaders
•assemble-and-go loader
•absolute loader (bootstrap loader)
•relocating loader (relative loader)
•direct linking loader
•Design options
•linkage editors
•dynamic linking
•bootstrap loaders
3
Assemble-and-go Loader
•Characteristic
•the object code is stored in memory after assembly
•single JUMP instruction
•Advantage
•simple, developing environment
•Disadvantage
•whenever the assembly program is to be executed, it has to be
assembled again
•programs have to be coded in the same language
4
3.1 Basic Loader Functions
•Absolute Loader
•Advantage
• Simple and efficient
•Disadvantage
• The need for programmer to specify the actual address
• difficult to use subroutine libraries
•Program Logic
•Next slice
6
(a) Object program
Figure 3.1 Loading an absolute program
Memory
address Contents
9
Object Code Representation
10
3.1.2 A Simple Bootstrap Loader
•Bootstrap Loader
•When a computer is first tuned on or restarted, a special type of
absolute loader, called bootstrap loader is executed
•This bootstrap loads the first program to be run by the computer -
- usually an operating system
•Example (SIC bootstrap loader)
•The bootstrap itself begins at address 0
•It loads the OS starting address 0x80
•No header record or control information, the object code is
consecutive bytes of memory
11
Figure 3.3 Bootstrap loader for SIC/XE
Fig. 3.3 SIC Bootstrap Loader Logic
Begin
X=0x80 (the address of the next memory location to be loaded
Loop
A←GETC (and convert it from the ASCII character code to the value
of the hexadecimal digit)
save the value in the high-order 4 bits of S
A←GETC
combine the value to form one byte A← (A+S)
store the value (in A) to the address in register X
X←X+1
End GETC A ← read one character
if A = 0x04 then jump to 0x80
if A < 48 then GETC
A ← A-48 (0x30)
if A < 10 then return
0~9: 4810 ~ 5710(0x30 ~ 0x39)
A ← A-7 (58+7=65)
A~F: 6510 ~ 7010(0x41 ~ 0x46)
return
13
3.2 Machine-Dependent Loader Feature
•3.2.1 Relocation
•3.2.2 Program Linking
•3.2.3 Algorithm and Data Structures for a Linking Loader
3.2.1 Relocating Loaders
•Motivation
•efficient sharing of the machine with larger memory and when
several independent programs are to be run together
•support the use of subroutine libraries efficiently
•Two methods for specifying relocation
•modification record (Fig. 3.4, 3.5)
•relocation bit (Fig. 3.6, 3.7)
• each instruction is associated with one relocation bit
• these relocation bits in a Text record is gathered into bit masks
15
Line Loc Source statement Object code
18
Line Loc Source statement Object code
21
Program Linking
•Goal
•Resolve the problems with EXTREF and EXTDEF from different
control sections
•Linking
•1. User, 2. Assembler, 3. Linking loader
•Example
•Program in Fig. 3.8 and object code in Fig. 3.9
•Use modification records for both relocation and linking
• address constant
• external reference
22
Loc Source statement Object code
26
Figure 3.9 Object programs corresponding to Fig. 3.8.
Figure 3.9 Object programs corresponding to Fig. 3.8 (cont’d).
Figure 3.9 Object programs corresponding to Fig. 3.8 (cont’d).
Program Linking Example
•Fig. 3.10
•Load address for control sections
» PROGA 004000 63
» PROGB 004063 7F
» PROGC 0040E2 51
•Load address for symbols
•LISTA: PROGA+0040=4040
•LISTB: PROGB+0060=40C3
•LISTC: PROGC+0030=4112
•REF4 in PROGA
•ENDA-LISTA+LISTC=14+4112=4126
•T0000540F000014FFFFF600003F000014FFFFC0
•M00005406+LISTC
30
Memory
Contents
address
Figure 3.10(a) Programs for Fig. 3.9 after linking and loading.
Object programs Memory contents
(Actual address of
LISTC)
Load addresses
Figure 3.10(b) Relocation and linking operations performed on REF4 from PROGA.
Program Logic and Data Structure
33
Pass 1 Program Logic
•Pass 1:
•assign addresses to all external symbols
•Variables
•PROGADDR (program load address) from OS
•CSADDR (control section address)
•CSLTH (control section length)
•ESTAB
•Fig. 3.11(a)
•Process Define Record
34
Control section Symbol name Address Length
PROGA 4000 0063
LISTA 4040
ENDA 4054
PROGB 4063 007F
LISTB 40C3
ENDB 40D3
PROGC 40E2 0051
LISTC 4112
ENDC 4124
Figure 3.11(a) Algorithm for Pass 1 of a linking loader.
Pass 1:
•Pass 2:
•perform the actual loading, relocation, and linking
•Modification record
•lookup the symbol in ESTAB
•End record for a main program
•transfer address
•Fig. 3.11(b)
•Process Text record and Modification record
37
Pass 2:
39
Figure 3.12 Object programs corresponding to Fig 3.8 using reference
numbers for code modification. (Reference numbers are underlined for
easier reading.)
Figure 3.12 Object programs corresponding to Fig 3.8 using reference
numbers for code modification (cont’d). (Reference numbers are underlined
for easier reading.)
Figure 3.12 Object programs corresponding to Fig 3.8 using reference numbers for
code modification (cont’d). (Reference numbers are underlined for easier reading.)
Figure 3.12
43
3.3 Machine-Independent Loader Features
45
3.3.1 Automatic Library Search (cont’d)
•Implementation
•Linking loaders that support automatic library search must keep
track of external symbols that are referred to, but not defined, in
the primary input to the loader
•At the end of Pass 1, the symbols in ESTAB that remain
undefined represented unresolved external references
•Then, the loader searches the library or libraries specified for
routines that contain the definitions of these symbols
•Note that the subroutines fetched from a library in this way may
themselves contain external references.
• It is therefore necessary to repeat the library search process until all
references are resolved.
46
3.3.1 Automatic Library Search (cont’d)
•Implementation
•The process allows the programmer to override the standard
subroutines in the library by supplying his or her own routines
•The libraries to be searched by the loader ordinarily
contain assembled or compiled versions of the
subroutines (i.e., object programs)
•For efficient searching
• Directory
•Some operating systems can keep the directory for commonly
used libraries permanently in memory
•The same technique applies equally well to the resolution
of external references to data items
47
3.3.2 Loader Options
48
3.3.2 Loader Options (cont’d)
49
Line Source statement
INCLUDE READ(UTLIB)
INCLUDE WRITE(UTLIB)
DELETE RDREC, WRREC
CHANGE RDREC, READ
CHANGE WRREC, WRITE
3.3.2 Loader Options (cont’d)
52
3.4 Loader Design Options
53
3.4.1 Linkage Editors
55
3.4.1 Linkage Editors (cont’d)
•A linkage editor
•Resolution of external references and library searching are only
performed once
•In the linked version of programs
• All external references are resolved, and relocation is indicated by some
mechanism such as modification records or a bit mask
•External references is often retained in the linked program
• To allow subsequent relinking of the program to replace control sections,
modify external references, etc.
56
3.4.1 Linkage Editors (cont’d)
INCLUDE PLANNER(PROGLIB)
DELETE PROJECT DELETE from existing PLANNER
INCLUDE PROJECT(NEWLIB) INCLUDE new version
REPLACE PLANNER(PROGLIB)
57
3.4.1 Linkage Editors (cont’d)
2. Linkage editors can also be used to build packages of
subroutines or other control sections that are generally used
together
It could be used to combine the appropriate subroutines into a package with a
command sequence
INCLUDE READR(FTNLIB)
INCLUDE WRITER(FTNLIB)
INCLUDE BLOCK(FTNLIB)
INCLUDE DEBLOCK(FTNLIB)
INCLUDE ENCODE(FTNLIB)
INCLUDE DECODE(FTNLIB)
.
.
.
SAVE FTNIO(SUBLIB)
58
3.4.1 Linkage Editors (cont’d)
3. Linkage editors often allow the user to specify that external
references are not to be resolved by automatic library search
Only the external references between user-written routines would be resolved
59
3.4.2 Dynamic Linking
(a) (b)
Figure 3.14 Loading and calling of a subroutine using dynamic linking. (cont’d)
3.4.3 Bootstrap Loaders
63
3.5 Implement Examples
•MS-DOS Linker
•SunOS Linkers
•Cary MPP Linker
3.5.1 MS-DOS Linker
•THEADR
•the name of the object module
•MODEND
•Marks the end of the module, and can contain a reference to the
entry point of the program
•TYPDEF
•The data type designed by an external name
•PUBDEF
•A list of the external symbols (public names)
•EXTDEF
•A list of the external symbols that referred to this object module
3.5.1 MS-DOS Linker (cont’d)
•LNAMES
•A list of all the segment and class names used in the program
•SEGDEF
•Describe the segment in the object module (include name, length,
and alignment)
•GRPDEF
•Specify how these segments are combined into groups
3.5.1 MS-DOS Linker (cont’d)
•LEDATA
•Contain translated instructions and data from the source program
•LIDATA
•Specify translated instructions and data that occur in a repeating
pattern
•FIXUPP
•Used to resolve external references, and address modifications
that are associated with relocation and grouping of segments
within the program.
•FIXUPP record must immediately follow the LEDATA and
LIDATA
3.5.1 MS-DOS Linker (cont’d)