Professional Documents
Culture Documents
• Loaders
(adapted from CS-167)
– bind relocatable addresses to absolute addresses
1 2
3 4
Linkers and Loaders Linkers and Loaders
A Simple Relocating Loader A Direct-Linking Relocating Loader
5 6
Offset Assembler Code Value ESD Offset Assembler Code Value ESD
Symbol Type Relative Location Symbol Type Relative Location
0 main: 0 .globl main,result
0 .globl main,result main entry 0 0 .extrn global,sub main entry 0
0 .extrn sum result entry 24 0 main: rlw $s0,asub. .+24 global external 0
0 lw $s1,aparm sum external 0 4 rsw $s0,data .+24 sub external 0
4 lw $s2,asum 8 lw $s1,global 0 result entry 36
8 jr $s2 12 rlw $s2,adata .+20
10 done RLD 16 jr $s0
12 aparm: Symbol Relative Location 18 rsw $r0,result .+18 RLD
12 .word parm 20 22 done Symbol Relative Location
16 asum: main 0 24 asub: .word sub 0
16 .word sum 0 main 4 28 data: .word 10 10 main 32
20 parm: main 12 32 adata: .word data 28 global 8
20 .word 10 10 sum 16 36 result: .word sub 24
24 result: 40 end
24 .word
28 end
CS-031 Lecture 25 CS-031 Lecture 25
7 8
Shared Libraries Shared Libraries
Position-Independent Code
Process A Process B
lw $2,r1(printf)
jr $2 0 printf( ) { lw $2, r1(printf)
... jr $2
lw $2, r1(doprint)
printf(...) printf(...) jr $2
...
}
r1→ printf 18000
stdio doprint 19000
r1 → printf 10000 1000 doprint( ) {
doprint 11000 ...
}
printf( )
9 10
11 12
Linking and Loading in Solaris 2 Linking and Loading in Solaris 2
Shared Libraries (2) Position-Independent Code
13 14
plt0: .plt0:
call runtime_linker call runtime_linker
Global Offset Table → nop nop
... ...
plt101: .plt101:
errno → errno address li $a0,plt101-plt0 lui $a0,hi(printf)
Before j plt0 ori $a0,low(printf) After
first call nop jr $a0
first call
to printf plt102: .plt102:
to printf
li $a0,plt102-plt0 li $a0,plt102-plt0
j plt0 j plt0
myglob → myglob address nop nop
15 16
Linking and Loading in Solaris 2
Dynamic Linking
proc(...) {
void ∗handle;
int (∗fptr)(int);
int i;
...
...
}
CS-031 Lecture 25
17