Linkers and Loaders
Linkers and Loaders
Linkers and Loaders
foo.c bar.c
run preprocessor (cpp) & compiler proper (cc1)
gcc –S <filename> gcc –S <filename>
foo.s bar.s
run assembler (as)
as <filename> as <filename>
foo.o bar.o
linker
ld <object files> ld <object files>
a.out
How?
Symbol resolution – associating one symbol definition with
files
Linkers and loaders perform various related but conceptually different tasks:
Program Loading: This refers to copying a program image from hard disk
to the main memory in order to put the program in a ready-to-run state. In
some cases, program loading also might involve allocating storage space or
mapping virtual addresses to disk pages.
Relocation: Compilers and assemblers generate the object code for each
input module with a starting address of zero. Relocation is the process of
assigning load addresses to different parts of the program by merging all
sections of the same type into one section. The code and data section also
are adjusted so they point to the correct runtime addresses.
080483a0 <main>:
file1.c: 80483a0: 55 push %ebp
80483a1: 89 e5 mov %esp,%ebp
80483a3: 83 ec 08 sub $0x8,%esp
#include <stdio.h> 80483a6: 83 e4 f0 and $0xfffffff0,%esp
char a[] = "Hello"; 80483a9: b8 00 00 00 00 mov $0x0,%eax
80483ae: 83 c0 0f add $0xf,%eax
extern void bar(); 80483b1: 83 c0 0f add $0xf,%eax
80483b4: c1 e8 04 shr $0x4,%eax
80483b7: c1 e0 04 shl $0x4,%eax
int main() 80483ba: 29 c4 sub %eax,%esp
{ 80483bc: e8 1f 00 00 00 call 80483e0 <bar>
bar(); 80483c1: c9 leave
80483c2: c3 ret
} 080483c3 <baz>:
80483c3: 55 push %ebp
80483c4: 89 e5 mov %esp,%ebp
80483c6: 83 ec 08 sub $0x8,%esp
void baz(char *s) 80483c9: 83 ec 08 sub $0x8,%esp
{ 80483cc: ff 75 08 pushl 0x8(%ebp)
80483cf: 68 f0 84 04 08 push $0x80484f0
printf("%s", s); 80483d4: e8 ff fe ff ff call 80482d8 <printf@plt>
} 80483d9: 83 c4 10 add $0x10,%esp
80483dc: c9 leave
80483dd: c3 ret
80483de: 90 nop
80483df: 90 nop
ELF Header
#include<stdio.h>
#include<math.h>
int abc;
int main()
{
int a = 25;
int b;
printf("Hello World: %f\n",sqrt(a));
}
Section Header
Problems
Change in library requires re-linking
Copying library contents to target program wastes disk
space and memory especially for commonly used libraries
such as the C library.
With large number of active programs considerable amount
of memory goes to storing these copies of library functions.
Example