6p-pdf - 國立政治大學資訊科學系

國立政治大學資訊科學系
2014/4/30
Assembling Programs for Execution
Assembly Programs
Macro Processors
系統程式
Expanded Assembly Programs
Core Assemblers
Object
Programs
Chapter 3:
Loaders and Linkers
Linkers
Libraries
Loaders
Executables
國立政治大學資訊科學系
Fundamental System Software
Assembly
Programs
Assembler directives
One-pass
Symbolic operands
Multi-pass
Mnemonic op codes
Fundamental System Software (2)
Object
Programs
Assemblers
SP.Chap3.2
Assembly
Program 1
Object
Program 1
Header records
Text records
Assembly
Program 2
End records
Literals
Assemblers
Object
Program 2
Modification records
Symbol-defining stmts
Expressions
Define records
Program blocks
Refer records
Object
Program 3
Assembly
Program 3
Control sections
國立政治大學資訊科學系
SP.Chap3.3
Fundamental System Software (3)
Object
Prog 1
Object
Prog 1
Assemblers
Assembly
Program 3
國立政治大學資訊科學系
Object
Prog 2
Object
Prog 3
Assembly
Program 2
Linkers
SP.Chap3.4
Fundamental System Software (4)
Assembly
Program 1
Assembly
Program 1
Assembly
Program 2
國立政治大學資訊科學系
Assemblers
Object
Prog 2
Linkers
Loaders
Loaders
Linkers may combine
multiple object codes
into a single executable
file.
SP.Chap3.5
C/C++ ...
Programs
Compilers
國立政治大學資訊科學系
Object
Progs
A standardized format for the
object codes allows linkers to
combine object codes created
by assemblers and compilers
implemented by different
providers.
SP.Chap3.6
1
國立政治大學資訊科學系
2014/4/30
Fundamental System Software (5)
Assembly
Programs
C/C++ ...
Programs
Assemblers
Compilers
Object
Progs
Object
Progs
Linkers
Loaders
Linkers and Loaders
Loaders bring object programs into
memory and start their execution.
Linkers

Libraries
國立政治大學資訊科學系
Since the format of object
codes is standardized,
linkers do not need to know
the original computer
languages that were used to
implement the libraries.
SP.Chap3.7
Simple Loaders

In most cases all program translators on a
particular system produce object programs
in the same format.
Discuss why
國立政治大學資訊科學系
SP.Chap3.8
Absolute Loader
An absolute loader



An absolute loader brings the object code to the
memory location chosen by its programmer
Use the algorithm shown in Figure 3.2 to review
the operation of a simple loader with Figure 3.1.
Explain the object code and actual object code.
A bootstrap loader



The bootstrap loader loads the first program to be
run by the computer--usually an operating system.
Algorithm shown on Figure 3.3
Bootstrap loaders in ROM (Section 3.4.3)
國立政治大學資訊科學系
SP.Chap3.9
Fetch-Decode-Execute Cycles
國立政治大學資訊科學系
SP.Chap3.10
A Simplified System
ALU (Data Path)
R
A
M
address
MEMORY
data
Registers
control
CPU
I/O 1
Source: Charles S. Parker, Understanding Computers Today & Tomorrow, Dryden, 1998
國立政治大學資訊科學系
SP.Chap3.11
國立政治大學資訊科學系
Control Unit
I/O 2
The CPU loads the very
first program , including
the bootstrap loader, to
be executed from the
ROM.
R
O
M
SP.Chap3.12
2
國立政治大學資訊科學系
2014/4/30
Bootstrap Loader
BOOT START 0
Relocation and Linking
GETC TD
INPUT
JEQ GETC
RD
INPUT
COMP #4
EOF
JEQ 80
COMP #48
‘0’
JLT
GETC
SUB #48
<10
COMP #10
JLT
RETURN
SUB #7
A~F
RETURN RSUB
INPUT BYTE X’F1’
END LOOP
CLEAR A
LDX #128
LOOP JSUB GETC
RMO A,S
SHIFTL S,4
JSUB GETC
ADDR S,A
STCH 0,X
TIXR X,X
J
LOOP
國立政治大學資訊科學系


A linkage loader, pp. 134-141

1110 0000 0000
0000
PROGA
0020
0023
0027
0040
REF1
REF2
REF3
LISTA
.
ENDA
REF4
REF5
REF6
REF7
REF8
0054
0020
0023
0027
0040
REF1
REF2
REF3
LISTA
.
ENDA
REF4
REF5
REF6
REF7
REF8
0054
0054
0057
005A
005D
0060
國立政治大學資訊科學系
START
EXTDEF
EXTREF
.
LDA
+LDT
LDX
EQU
EQU
WORD
WORD
WORD
WORD
WORD
SP.Chap3.14
START
EXTDEF
EXTREF
.
LDA
+LDT
LDX
EQU
EQU
WORD
WORD
WORD
WORD
WORD
0
LISTA,ENDA
LISTB, ENDB, LISTC, ENDC
LISTA
LISTB+4
#ENDA-LISTA
*
*
ENDA-LISTA+LISTC
ENDC-LISTC-10
ENDC-LISTC+LISTA-1
ENDA-LISTA-(ENDB-LISTB)
LISTB-LISTA
國立政治大學資訊科學系
SP.Chap3.15
Program Linking
PROGA
The assembler does whatever it can do for
address calculation. The loader will do the rest.
國立政治大學資訊科學系
0054
0057
005A
005D
0060
0000
required line change (Figure 3.7)
Review Section 2.3.5
Program Linking
?
國立政治大學資訊科學系
multiprogramming, libraries,...
Using modification record may not be good
for all machine architectures. (Figure 3.6)
Relocation bits and bit masks
SP.Chap3.13
Relocation Masks
1111 1111 1100
Why relocation?
SP.Chap3.16
Program Linking - PROGB
0
LISTA,ENDA
LISTB, ENDB, LISTC, ENDC
0000
LISTA
LISTB+4
#ENDA-LISTA
*
0036 REF1
003A REF2
003D REF3
0060
0070
*
ENDA-LISTA+LISTC
ENDC-LISTC-10
ENDC-LISTC+LISTA-1
ENDA-LISTA-(ENDB-LISTB)
LISTB-LISTA
0070
0073
0076
0079
007C
SP.Chap3.17
PROGB
LISTB
.
ENDB
REF4
REF5
REF6
REF7
REF8
國立政治大學資訊科學系
START
EXTDEF
EXTREF
.
LDA
+LDT
LDX
EQU
EQU
WORD
WORD
WORD
WORD
WORD
0
LISTB,ENDB
LISTA, ENDA, LISTC, ENDC
LISTA
LISTB+4
#ENDA-LISTA
*
*
ENDA-LISTA+LISTC
ENDC-LISTC-10
ENDC-LISTC+LISTA-1
ENDA-LISTA-(ENDB-LISTB)
LISTB-LISTA
SP.Chap3.18
3
國立政治大學資訊科學系
2014/4/30
Program Linking - PROGB
0000
PROGB
START
EXTDEF
EXTREF
.
LDA
+LDT
LDX
EQU
0036 REF1
003A REF2
003D REF3
0060
0070
0070
0073
0076
0079
007C
LISTB
.
ENDB
REF4
REF5
REF6
REF7
REF8
EQU
WORD
WORD
WORD
WORD
WORD
Program Linking - PROGC
0
LISTB,ENDB
LISTA, ENDA, LISTC, ENDC
0000
LISTA
LISTB+4
#ENDA-LISTA
*
0018 REF1
001C REF2
0020 REF3
0030
0042
*
ENDA-LISTA+LISTC
ENDC-LISTC-10
ENDC-LISTC+LISTA-1
ENDA-LISTA-(ENDB-LISTB)
LISTB-LISTA
國立政治大學資訊科學系
0042
0045
0048
004B
004E
SP.Chap3.19








PROGADDR
CSADDR
CSLTH
ESTAB

EQU
WORD
WORD
WORD
WORD
WORD
0
LISTC,ENDC
LISTA, ENDA, LISTB, ENDB
LISTA
LISTB+4
#ENDA-LISTA
*
*
ENDA-LISTA+LISTC
ENDC-LISTC-10
ENDC-LISTC+LISTA-1
ENDA-LISTA-(ENDB-LISTB)
LISTB-LISTA
國立政治大學資訊科學系
Control
section
Pass 2
H and D
records
Data structure
LISTC
.
ENDC
REF4
REF5
REF6
REF7
REF8
START
EXTDEF
EXTREF
.
LDA
+LDT
LDX
EQU
SP.Chap3.20
ESTAB
A Two-Pass Linking Loader
Pass 1
PROGC
Load maps
E and M
records
Data structure
Symbol
name
Address
Length
4000
0063
LISTA
4040
ENDA
4056
LISTB
40C3
ENDB
40D3
LISTC
4112
ENDC
4124
PROGA
PROGB
 PROGADDR
 CSADDR
4063
PROGC
 EXECADDR
 CSLTH
What are the R records for?
40E2
007F
0051
reducing the number of table search operations...
國立政治大學資訊科學系
SP.Chap3.21
Reference number
H^PROGA ^000000^..
D^LISTA ^000040….
R^LISTB ^ENDB
^LISTC ^ENDC
……
M^000024^05^+LISTB
M^000054^06^+LISTC
M^000057^06^+ENDC
M^000057^06^-LISTC
國立政治大學資訊科學系
國立政治大學資訊科學系
SP.Chap3.22
Optional Loader Features
H^PROGA ^000000^..
D^LISTA ^000040….
R^02LISTB ^03ENDB
^04LISTC ^05ENDC
……
M^000024^05^+02
M^000054^06^+04
M^000057^06^+05
M^000057^06^-04
SP.Chap3.23
Automatic library search


What?
How?
 Enter referred symbols into ESTAB
 Search libraries for undefined symbols at the
end of pass 1 (and continue the search if
necessary)
 Issue error messages if undefined symbols still
exist
國立政治大學資訊科學系
SP.Chap3.24
4
國立政治大學資訊科學系
2014/4/30
Optional Loader Features
Linkage Editor
Loader options

INCLUDE program-name(library-name)

DELETE csect-name

CHANGE name1, name2

LIBRARY MYLIB

NOCALL STDDEV, PLOT, CORREL
Linking loaders vs. linkage editors

國立政治大學資訊科學系
SP.Chap3.25
Frequency of reassembling matters.
國立政治大學資訊科學系
SP.Chap3.26
Dynamic Linking
The linked program reserves the
information for resolving external
references.
Edition of linked programs (page 154)
Creation of packages (pp. 154-155)
Timing for linking library functions (page
155)
國立政治大學資訊科學系
SP.Chap3.27
aka. dynamic loading and load on call
Link the subroutines when they are called
Advantages



Processes share one copy of a subroutine or
library.
Processes always use the up-to-dated objects.
Subroutines are loaded only when necessary.
How? Figure 3.14 on page 157
Binding: the association of an actual
addresses with symbolic names
國立政治大學資訊科學系
SP.Chap3.28
MS-DOS and Windows
MS-DOS



Most MS-DOS compilers and assemblers
produce modules.
MS-DOS LINK is a linkage editor.
MS-DOS object module (Figure 3.15)
Windows

國立政治大學資訊科學系
SP.Chap3.29
*.DLL files
國立政治大學資訊科學系
SP.Chap3.30
5
國立政治大學資訊科學系
2014/4/30
SunOS Linkers
Link-editor and run-time linker
Link-editor linker


Relocation and linking operations are
specified using a set of processor-specific
codes.
Symbolic references
 Library search: archives
 Shared objects: dynamic linking
Run-time linker: dynamic linking
國立政治大學資訊科學系
SP.Chap3.31
6