Lectures-Linkload CSS2
Lectures-Linkload CSS2
Lecture 8:
Linking and Loading
● Loading:
● Process of getting an executable running on the machine
● Requires resolving references to external symbols in the program (dynamic linking)
main.c swap.c
main.c swap.c
Definition of swap()
Definition of main()
Declaration of buf[]
main.c swap.c
Reference to swap()
References
to buf[]
Separately compiled
main.o swap.o relocatable object files
int main()
{
swap();
return 0;
}
00000000 <main>:
...
a: 55 push %ebp
b: 89 e5 mov %esp,%ebp
d: 51 push %ecx
e: 83 ec 04 sub $0x4,%esp
11: e8 fc ff ff ff call 12 <main+0x12>
16: b8 00 00 00 00 mov $0x0,%eax
...
int main()
{
swap(); .data section
return 0; buf[]
}
symbol table
name section off
main .text 0
buf .data 0
swap undefined
relocation info
for .text
Relocation info tells the linker name offset
where references to external swap 12
symbols are in the code
void swap()
{
int temp; Again, these are placeholders.
temp = buf[1]; 0x0 refers to buf[0]
buf[1] = buf[0]; 0x4 refers to buf[1]
buf[0] = temp;
00000000 <swap>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 15 04 00 00 00 mov 0x4,%edx
9: a1 00 00 00 00 mov 0x0,%eax
e: a3 04 00 00 00 mov %eax,0x4
13: 89 15 00 00 00 00 mov %edx,0x0
19: 5d pop %ebp
1a: c3 ret
void swap()
{
int temp; symbol table
Where's
.data section code for swap() buf?
buf[] symbol table
name section off
symbol table swap .text 0
buf undefined
name section off
main .text 0
buf .data 0 relocation info
swap undefined for .text .data section
name offset buf[]
relocation info Right here
buf 0x5
for .text buf 0xa
name offset buf 0xf
swap 12 buf 0x15
08048368 <swap>:
8048368: 55 push %ebp
8048369: 89 e5 mov %esp,%ebp
804836b: 8b 15 5c 95 04 08 mov 0x804955c,%edx
8048371: a1 58 95 04 08 mov 0x8049558,%eax
8048376: a3 5c 95 04 08 mov %eax,0x804955c
804837b: 89 15 58 95 04 08 mov %edx,0x8049558
8048381: 5d pop %ebp
8048382: c3 ret
$ objdump -t myprog
...
08049558 g O .data 00000008 buf
...
Address Section Size Symbol name
Weak symbols:
● Uninitialized global variables
foo1.c foo2.c
double x; Weak
void f(void);
int x = 38;
int y = 39; void f() {
Strong x = 42.0;
int main() { }
f();
printf(“x = %d\n”, x);
printf(“y = %d\n”, y);
return 0;
}
“double x” is 8 bytes in size!
But resolves to address
$ gcc -o myprog foo1.c foo2.c of “int x” in foo1.c.
$ ./myprog
x = 0
y = 1078263808
foo1.c foo2.c
Weak
void f(void); int x;
int x; Weak
void f() {
int main() { x = 42;
x = 38; }
f();
printf(“x = %d\n”, x);
return 0;
}
Sections:
Idx Name Size VMA LMA File off Algn
0 .interp 00000013 08048134 08048134 00000134 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
1 .note.ABI-tag 00000020 08048148 08048148 00000148 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
2 .hash 00000028 08048168 08048168 00000168 2**2
CONTENTS, ALLOC, LOAD, READONLY, DATA
...
SYMBOL TABLE:
08048134 l d .interp 00000000 .interp
08048148 l d .note.ABI-tag 00000000 .note.ABI-tag
08048168 l d .hash 00000000 .hash
08048190 l d .gnu.hash 00000000 .gnu.hash
...
ar
Problem:
● Command line order matters!
● Moral: put libraries at the end of the command line.
.text section
Shared libraries
.data section 0x40000000
.bss section
.symtab
.rel.text Heap
(used by malloc)
.rel.data
Loaded
.debug Data from
the
Section header table executable
(required for relocatables) file
Code
0x08048000
0 Unused
© 2009 Matt Welsh – Harvard University 37
Shared Libraries
Static libraries have the following disadvantages:
● Lots of code duplication in the resulting executable files
● Every C program needs the standard C library.
● e.g., Every program calling printf() would have a copy of the printf() code in