You are on page 1of 5

Linkers and Loaders

• Compilers and assemblers produce output with:


– relocatable addresses
– external references
Linkers and Loaders • Linkers
– resolve references to separately compiled or assembled subroutines

• Loaders
(adapted from CS-167)
– bind relocatable addresses to absolute addresses

Cs-031 Lecture 25 CS-031 Lecture 25

1 2

Linkers and Loaders Linkers and Loaders


Absolute Loaders Relocating Loaders

.org #100 .org #400


main: sub: Offset Assembler Code

... ... 0 main:

lw $s0, asub jr $ra ...


jr $s0
... 40 lw $s0,aX
...
done ...
asub: .word 400
done 100 aX: .word X
104 X: .long 10

CS-031 Lecture 25 CS-031 Lecture 25

3 4
Linkers and Loaders Linkers and Loaders
A Simple Relocating Loader A Direct-Linking Relocating Loader

• For each separately compiled or assembled module:


Offset Assembler Code Value Reloc Flag – a header describing the sizes of the module’s components

0 .extrn subr1 0 – an external symbol directory (ESD)


8 .extrn subr2 0 – a relocation and linkage directory (RLD)
16 main:
– object code
16 lw $s1,aparm 32 1
20 jal subr1 0 1
24 jal subr2 8 1
28 done 0
32 aparm:
32 .word parm 36 1
36 parm:
36 .word 0:40
76 end

CS-031 Lecture 25 CS-031 Lecture 25

5 6

Linkers and Loaders Linkers and Loaders


A Direct-Linking Relocating Loader (Example 1) A Direct-Linking Relocating Loader (Example 2)

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( )

CS-031 Lecture 25 CS-031 Lecture 25

9 10

Linking and Loading in Solaris 2 Linking and Loading in Solaris 2


Shared Libraries (1)

• Shared libraries 1) Compile program


2) Track down linkages with ld
• Dynamic linking
– archives in “.a” files are statically linked
• Interposition – shared objects in “.so” files are dynamically linked
3) Run program
– ld.so is invoked to complete the linking and relocation steps, if necessary

CS-031 Lecture 25 CS-031 Lecture 25

11 12
Linking and Loading in Solaris 2 Linking and Loading in Solaris 2
Shared Libraries (2) Position-Independent Code

karla% ls • Data references


libpriv1.a libpriv2.so libpriv3.so prog.c
– global offset table (got)
libpriv2.a
– completely filled in by ld.so when program starts up
karla% cc -o prog prog.c -L. -lpriv1 -lpriv2 -lpriv3
• Procedure references
karla% prog – procedure linkage table (plt)
ld.so.1: prog: fatal: libpriv2.so: can’t open file: errno=2
– partially filled in by ld.so when program starts up
killed
– each entry is “lazily evaluated”—completed only when used for the first
karla% cc -o prog prog.c -L. -lpriv1 -lpriv2 -lpriv3 -R. time

karla% ldd prog


libpriv2.so => ./libpriv2.so
libpriv3.so => ./libpriv3.so

CS-031 Lecture 25 CS-031 Lecture 25

13 14

Linking and Loading in Solaris 2 Linking and Loading in Solaris 2


Global Offset Table Procedure Linkage Table

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

CS-031 Lecture 25 CS-031 Lecture 25

15 16
Linking and Loading in Solaris 2
Dynamic Linking

proc(...) {
void ∗handle;
int (∗fptr)(int);
int i;

...

handle = dlopen("mylib.so", RTLD_LAZY)


fptr = dlsym(handle, "my_func");
i = (∗fptr)(17);

...
}

CS-031 Lecture 25

17

You might also like