15-213 Introduction to Computer Systems Program Translation and Execution I: Linking Sept. 29, 1998 Topics • object files • linkers class11.ppt.

Download Report

Transcript 15-213 Introduction to Computer Systems Program Translation and Execution I: Linking Sept. 29, 1998 Topics • object files • linkers class11.ppt.

15-213
Introduction to Computer Systems
Program Translation and Execution I:
Linking
Sept. 29, 1998
Topics
• object files
• linkers
class11.ppt
A simplistic program
translation scheme
p.c
source file
Translators
executable object file
(memory image on disk)
p
Loader
executing program
(memory image)
p
Problems:
• efficiency: small change requires complete recompilation
• modularity: hard to share common functionality (e.g. printf)
class11.ppt
–2–
CS 213 F’98
Separate compilation
p1.c
p2.c
libc.c
Translator
Translator
Translator
p1.o
p2.o
libc.o
relocatable
object files
Static Linker
p
Loader
executable object file
(contains code and
data for all functions
defined in libc.c)
Improves modularity and efficiency, but still hard to package common functions
class11.ppt
–3–
CS 213 F’98
Static libraries
p1.c
p2.c
Translator
Translator
p1.o
p2.o
libc.a
static library
of relocatable
object files
Static Linker
executable object file
(only contains code and data for
libc functions that are called
from p1.c and p2.c)
p
Loader
Further improves modularity and efficiency, but duplicates
common functions in different processes
class11.ppt
–4–
CS 213 F’98
Shared libraries
p1.c
p2.c
Translator
Translator
p1.o
p2.o
Static Linker
p
libc.so
Loader/Dynamic Linker
class11.ppt
–5–
shared library of dynamically
relocatable object files
libc functions called by p1.c
and p2.c are loaded at runtime and shared among
processes.
CS 213 F’98
The complete picture
p1.c
p2.c
Translator
Translator
p1.o
p2.o
libwhatever.a
Static Linker
p
libc.so
libm.so
Loader/Dynamic Linker
running program
class11.ppt
–6–
CS 213 F’98
Object files
C Program
char c[100];
double d;
int n=20;
main() {
int i;
Object file
bss sections
uninitialized static variables
Block Started by Symbol
Better Save Space
data sections
initialized static variables
Increasing
addresses
i=5;
c[i] = ‘z’;
}
text sections:
read-only code and data
Note: local variables don’t go in
object files. They are created at
runtime on the stack.
Headers
Relocation info
Symbol table
0
class11.ppt
–7–
CS 213 F’98
Example C program
main.c:
zip.c:
code.c:
main() {
zip();
}
extern int x;
int x=0xfe;
int *p = &x;
code(int *p) {
srand(*p);
}
zip() {
code(p);
}
class11.ppt
–8–
CS 213 F’98
Compiling and linking the
example program
% cc -v -c main.c
cfe main.c > /tmp/ctmfaacgja
ugen -O1 /tmp/ctmfaacgja -o /tmp/ctmcaacgja
as1 -O1 /tmp/ctmcaacgja -o main.o
% cc -c zip.c
% cc -c code.c
% cc -v -Wl,-O0 -o main main.o zip.o code.o
ld -o main -O0 -call_shared /usr/lib/cmplrs/cc/crt0.o main.o zip.o code.o -lc
Unix linker
class11.ppt
C runtime system
–9–
CS 213 F’98
Linker functions
1. Resolving external references
2. Relocating symbol definitions and references
crt0.o
main.o
0
0
zip.o
main
crt
x
*p
Data
main()
code()
*p
zip()
zip()
0
main()
x
code.o
Text
crt
code()
0
0
class11.ppt
– 10 –
CS 213 F’98
Symbols
•
•
•
•
Lexical entities that correspond to functions and global variables
Each symbol has a value (address)
Code consists of symbol definitions and references
References can be either local or external
main.c:
zip.c:
code.c:
main() {
zip();
}
extern int x;
int x=0xfe;
int *p = &x;
code(int *p) {
srand(*p);
}
Ref to
external
symbol zip
Def of
local
symbol x
zip() {
code(p);
}
Def of
local
symbol p
Def of local
symbol zip
Ref to
external
symbol x
Ref to external
symbol srand
(defined in libc.so)
Ref to local symbol p
class11.ppt
– 11 –
CS 213 F’98
Alpha ECOFF object files
File and optional headers
0
Symbol table
Section relocation info
Section data
Section headers
Optional header
File header
• magic number, size and number of
sections, flags
– byte ordering, executable or relocatable?,
shared?, calls shared library functions?
• entry point, gp value, start and sizes of
text, data, and bss segments
Section headers
• for each section in “Section data”:
address, size, number of relocation entries
Section data
• program code and data
Section relocation info
• which instructions and data items will
need to be relocated?
Symbol table
class11.ppt
• type and value of relevant symbols
– 12 –
CS 213 F’98
Section data (relocatable object files)
(Runtime memory image)
bss segment
uninitialized variables
(block started by symbol)
(better save space!)
data segment
initialized variables
text segment
machine code
class11.ppt
(File format)
.bss
.sbss
.pdata/.xdata
.lit4
.lit8
.lita
.rconst
.rdata
.sdata
.data
.init/.fini
.text
– 13 –
uninitialized data
small (4-byte) bss
support for exceptions
4-byte literal pool
8-byte literal pool
8-byte literal address pool
read only constants
read only data
small data
large data
process init/deinit code
machine instructions
CS 213 F’98
.text section (main.o)
main() {
zip();
}
main:
0x0: lda
0x4: ldah
0x8: lda
0xc: stq
0x10: ldq
0x14: jsr
0x18: ldah
0x1c: ldq
0x20: bis
0x24: lda
0x28: lda
0x2c: ret
class11.ppt
.text
.lita
start
0x00 (0)
0x50 (80)
sp, -16(sp)
gp, 1(t12)
gp, -32704(gp)
ra, 0(sp)
t12, -32752(gp)
ra, (t12), zip
gp, 1(ra)
ra, 0(sp)
zero, zero, v0
gp, -32728(gp)
sp, 16(sp)
zero, (ra), 1
– 14 –
size
0x30 (48)
0x10 (16)
Assembler assumes
that gp=32,832=(64K+128)/2
(Note that t12 = pc-4)
t12 = address stored at .lita+0
resets gp to 32,832
(Note: ra = pc)
Question: Why use gp-relative
referencing of data and functions?
CS 213 F’98
Symbol table (main.o)
main() {
zip();
}
[Index]
[0]
[1]
class11.ppt
Name
main
zip
.text
.lita
start
0x00 (0)
0x50 (80)
Value
0x0
0x0
– 15 –
size
0x30 (48)
0x10 (16)
Sclass Symtype
Text
Proc
Undefined Proc
CS 213 F’98
Relocation entries (main.o)
main() {
zip();
}
.text
.lita
start
0x00 (0)
0x50 (80)
size
0x30 (48)
0x10 (16)
Vaddr Symndx Type
Extern Name
.text:
0x04
0x10
0x14
0x14
0x18
GPDISP : instruction pair that
sets gp value
local
local .lita
local R_LU_JSR
extern zip
local
LITERAL: gp-relative
reference to literal in .lita
.lita:
0x50
4
13
3
1
12
GPDISP
LITERAL
LITUSE
HINT
GPDISP
LITUSE: identifies instance of
a literal address previously
loaded into a register
HINT: 14 bit jsr hint reference
1
class11.ppt
REFQUAD extern zip
– 16 –
REFQUAD: 64-bit reference to
the symbol’s virtual address
CS 213 F’98
Relocations (main.o)
main() {
zip();
}
0x0:
0x4:
0x8:
0xc:
0x10:
0x14:
0x18:
0x1c:
0x20:
0x24:
0x28:
0x2c:
main:
lda
ldah
lda
stq
ldq
jsr
ldah
ldq
bis
lda
lda
ret
.text
.lita
start
0x00 (0)
0x50 (80)
sp, -16(sp)
gp, 1(t12)
gp, -32704(gp)
ra, 0(sp)
t12, -32752(gp)
ra, (t12), zip
gp, 1(ra)
ra, 0(sp)
zero, zero, v0
gp, -32728(gp)
sp, 16(sp)
zero, (ra), 1
class11.ppt
size
0x30 (48)
0x10 (16)
Vaddr Symndx Type
Extern Name
.text:
0x04
4
GPDISP
local
0x10
13
LITERAL local .lita
0x14
3
LITUSE
local R_LU_JSR
0x14
1
HINT
extern zip
0x18
12
GPDISP
local
.lita:
0x50
1
REFQUAD extern zip
Notes:
1. LITUSE at 0x14 allows jsr to be replaced by bsr
– 17 –
CS 213 F’98
extern int x;
int *p = &x;
zip() {
code(p);
}
0x0:
0x4:
0x8:
0xc:
0x10:
0x14:
0x18:
0x1c:
0x20:
0x24:
0x28:
0x2c:
0x30:
lda
ldah
lda
stq
ldq
ldq
ldq
jsr
ldah
ldq
lda
lda
ret
class11.ppt
.text section (zip.o)
.text
.lita
.sdata
start
0x00 (0)
0x60 (96)
0x70 (112)
sp, -16(sp)
gp, 1(t12)
gp, -32688(gp)
ra, 0(sp)
t12, -32744(gp)
a0, -32752(gp)
a0, 0(a0)
ra, (t12), code
gp, 1(ra)
ra, 0(sp)
gp, -32720(gp)
sp, 16(sp)
zero, (ra), 1
size
0x40 (64)
0x10 (16)
0x10 (16)
Assembler assumes
that gp=32,848
t12 = addr(code) = cont(.lita+8)
a0 = addr(p) = cont(.lita+0)
a0 = p
– 18 –
CS 213 F’98
Symbol table (zip.o)
extern int x;
int *p = &x;
.text
.lita
.sdata
zip() {
code(p);
}
class11.ppt
start
0x00 (0)
0x60 (96)
0x70 (112)
[Index]
Name Value
[0]
[1]
[2]
[3]
p
x
zip
code
0x70
0x04
0x00
0x00
size
0x40 (64)
0x10 (16)
0x10 (16)
Sclass
SData
SUndefined
Text
Undefined
– 19 –
Symtype
Global
Global
Proc
Proc
CS 213 F’98
Relocation entries (zip.o)
extern int x;
int *p = &x;
zip() {
code(p);
}
0x0:
0x4:
0x8:
0xc:
0x10:
0x14:
0x18:
0x1c:
0x20:
0x24:
0x28:
0x2c:
0x30:
lda
ldah
lda
stq
ldq
ldq
ldq
jsr
ldah
ldq
lda
lda
ret
.text
.lita
.sdata
sp, -16(sp)
gp, 1(t12)
gp, -32688(gp)
ra, 0(sp)
t12, -32744(gp)
a0, -32752(gp)
a0, 0(a0)
ra, (t12), code
gp, 1(ra)
ra, 0(sp)
gp, -32720(gp)
sp, 16(sp)
zero, (ra), 1
class11.ppt
start
0x00 (0)
0x60 (96)
0x70 (112)
size
0x40 (64)
0x10 (16)
0x10 (16)
Vaddr Symndx Type
.text:
0x04
4
GPDISP
0x14
13
LITERAL
0x18
1
LITUSE
0x10
13
LITERAL
0x1c
3
LITUSE
0x1c
3
HINT
0x20
8
GPDISP
.lita:
0x60
0
REFQUAD
0x68
3
REFQUAD
.sdata:
0x70
1
REFQUAD
– 20 –
Extern Name
local
local
local
local
local
extern
local
.lita
R_LU_BASE
.lita
R_LU_JSR
code
extern p
extern code
extern x
CS 213 F’98
.text section (code.o)
int x=0xfe;
code(int *p) {
srand(*p);
}
0x0:
0x4:
0x8:
0xc:
0x10:
0x14:
0x18:
0x1c:
0x20:
0x24:
0x28:
0x2c:
0x30:
0x34:
lda
ldah
lda
stq
stq
ldq
bis
ldl
jsr
ldah
ldq
lda
lda
ret
class11.ppt
.text
.lita
.sdata
start
0x00 (0)
0x60 (96)
0x70 (112)
sp, -64(sp)
gp, 1(t12)
gp, -32688(gp)
ra, 0(sp)
a0, 16(sp)
t12, -32752(gp)
a0, a0, t0
a0, 0(t0)
ra, (t12), srand
gp, 1(ra)
ra, 0(sp)
gp, -32724(gp)
sp, 64(sp)
zero, (ra), 1
size
0x40 (64)
0x10 (16)
0x10 (16)
Assembler assumes
that gp=32,848
t12 = addr(srand) = cont(.lita+0)
srand is unresolved
– 21 –
CS 213 F’98
Symbol table (code.o)
int x=0xfe;
code(int *p) {
srand(*p);
}
[Index]
[0]
[1]
[2]
Name
x
code
srand
class11.ppt
.text
.lita
.sdata
Value
0x70
0x00
0x00
start
0x00 (0)
0x60 (96)
0x70 (112)
Sclass
SData
Text
Undefined
– 22 –
size
0x40 (64)
0x10 (16)
0x10 (16)
Symtype
Global
Proc
Proc
CS 213 F’98
Relocation entries (code.o)
int x=0xfe;
code(int *p) {
srand(*p);
}
0x0:
0x4:
0x8:
0xc:
0x10:
0x14:
0x18:
0x1c:
0x20:
0x24:
0x28:
0x2c:
0x30:
0x34:
lda
ldah
lda
stq
stq
ldq
bis
ldl
jsr
ldah
ldq
lda
lda
ret
.text
.lita
.sdata
sp, -64(sp)
gp, 1(t12)
gp, -32688(gp)
ra, 0(sp)
a0, 16(sp)
t12, -32752(gp)
a0, a0, t0
a0, 0(t0)
ra, (t12), srand
gp, 1(ra)
ra, 0(sp)
gp, -32724(gp)
sp, 64(sp)
zero, (ra), 1
class11.ppt
start
0x00 (0)
0x60 (96)
0x70 (112)
size
0x40 (64)
0x10 (16)
0x10 (16)
Vaddr Symndx
Type
Extern Name
.text:
0x04
0x14
0x20
0x20
0x24
4
13
3
2
8
GPDISP
LITERAL
LITUSE
HINT
GPDISP
.lita:
0x60
2
REFQUAD extern srand
local
local .lita
local R_LU_JSR
extern srand
local
Why no relocation entry for x?
– 23 –
CS 213 F’98
Executable object file (main)
.rconst
.text
.data
.lit8
.sdata
.got
.sbss
0x120001080 (4224)
0x120001140 (4416)
0x140000000 (0)
0x1400000a0 (160)
0x140000b0 (176)
0x140000d0 (208)
0x1400001f0 (496)
main:
0x120001320:
0x120001324:
0x120001328:
0x12000132c:
0x120001330:
0x120001334:
0x120001338:
0x12000133c:
0x120001340:
0x120001344:
0x120001348:
0x12000134c:
0x120001350:
class11.ppt
lda
ldah
lda
stq
ldq
ldah
ldq
bsr
ldah
ldq
lda
lda
ret
0x60 (96)
0x450 (1104)
0x10 (16)
0x10 (16)
0x20 (32)
0x120 (288)
0x40 (64)
sp, -16(sp)
gp, 8192(t12)
gp, 28064(gp)
ra, 0(sp)
t12, -32672(gp)
a0, -1(gp)
a0, 32752(a0)
ra, code
gp, 8192(ra)
ra, 0(sp)
gp, 28032(gp)
sp, 16(sp)
zero, (ra), 1
– 24 –
Linker sets gp at
.data+32960
addr(code) at .data+288
changed by linker!
CS 213 F’98
Symbol table (main)
[0] .rconst
[1] .pdata
[2] .text
[3] .init
[4] .fini
[5] .data
[6] .xdata
[7] .rdata
[8] .lit8
[9] .lit4
[10] .sdata
[11 .sbss
[12] .bss
class11.ppt
0x120001080
0x1200010e0
0x120001140
0x120001590
0x1200015d0
0x140000000
0x140000010
0x140000010
0x1400000b0
0x1400000b0
0x1400000b0
0x1400001f0
0x140000230
– 25 –
scRConst
PData
Text
Init
Fini
Data
XData
RData
SData
SData
SData
SBss
Bss
Local
Local
Local
Local
Local
Local
Local
Local
Local
Local
Local
Local
Local
CS 213 F’98
Symbol table (main)
[13]
[24]
[25]
[26]
[29]
[31]
[32]
[33]
[42]
[43]
[44]
[56]
class11.ppt
__start
x
__environ
errno
exit
main
zip
code
p
__Argc
__Argv
srand
0x1200011a0
0x1400000c0
0x000000000
0x000000004
0x120001140
0x1200012f0
0x120001320
0x120001360
0x1400000b0
0x1400001f0
0x1400001f8
0x000000000
– 26 –
Text
SData
Common
Common
Undefined
Text
Text
Text
SData
SBss
SBss
Undefined
Proc
Global
Global
Global
Proc
Proc
Proc
Proc
Global
Global
Global
Proc
CS 213 F’98
Strong and weak symbols
Program symbols are either strong or weak
• strong: procedures and ininitialized globals
• weak: uninitialized globals
p1.c:
p2.c:
strong
int foo=5;
int foo;
strong
p1() {
}
p2() {
}
class11.ppt
– 27 –
weak
strong
CS 213 F’98
Linker’s symbol rules
1. A strong symbol can only appear once.
2. A weak symbol can be overridden by a strong
symbol of the same name.
3. If multiple weak symbols, the linker can pick either
one.
class11.ppt
– 28 –
CS 213 F’98
Linker puzzles
int foo;
p1() {}
p1() {}
int foo;
p1() {}
int foo;
p2() {}
int foo;
p1() {}
double foo;
p2() {}
int foo=7;
p1() {}
double foo;
p2() {}
int foo=7;
p1() {}
int foo;
p2() {}
class11.ppt
– 29 –
CS 213 F’98