



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The role of linkers in compiling programs and the difference between static and dynamic linking. It covers the concept of shared libraries, their advantages, and how to create and use them. The document also discusses the linker's algorithm for resolving external references and the disadvantages of static libraries.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




2
3
4
Problems:
Translator
m.c
p
ASCII source file
Binary executable object file (memory image on disk)
5
Linker (ld)
Translators
m.c
m.o
Translators
a.c
a.o
p
Separately compiled relocatable object files
Executable object file (contains code and data for all functions defined in m.c and a.c)
6
bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s
7
What Does a Linker Do?
Merges multiple relocatable (. o ) object files into a single executable object file that can be loaded and executed by the loader.
As part of the merging process, resolves external references. External reference : reference to a symbol defined in another object file.
Relocates symbols from their relative locations in the .o files to new absolute positions in the executable. Updates all references to these symbols to reflect their new positions. References can be in either code or data » code: a(); /* reference to symbol a */ » data: int xp=&x; / reference to symbol x */
8
Why Linkers?
Modularity
e.g., Math library, standard C library
Efficiency
Change one source file, compile, and then relink. No need to recompile other source files.
Libraries of common functions can be aggregated into a single file... Yet executable files and running memory images contain only code for the functions they actually use.
9
Executable and Linkable Format
(ELF)
Later adopted by BSD Unix variants and Linux
Relocatable object files ( .o ), Executable object files Shared object files (. so )
10
ELF Object File Format
Magic number, type (.o, exec, .so), machine, byte ordering, etc.
Page size, virtual addresses memory segments (sections), segment sizes.
Code
Initialized (static) data
Uninitialized (static) data “Block Started by Symbol” “Better Save Space” Has section header but occupies no space
ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.txt .rel.data .debug Section header table (required for relocatables)
11
ELF Object File Format (cont)
Symbol table Procedure and static variable names Section names and locations
Relocation info for .text section Addresses of instructions that will need to be modified in the executable Instructions for modifying.
Relocation info for .data section Addresses of pointer data that will need to be modified in the merged executable
Info for symbolic debugging ( gcc -g )
ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.text .rel.data .debug Section header table (required for relocatables)
12
Example C Program
int e=7;
int main() { int r = a(); exit(0); }
m.c a.c extern int e;
int *ep=&e; int x=15; int y;
int a() { return *ep+x+y; }
19
Executable After Relocation and
External Reference Resolution(. data )
Disassembly of section .data:
0804a018
0804a01c
0804a020
int e=7;
int main() { int r = a(); exit(0); }
m.c
a.c extern int e;
int *ep=&e; int x=15; int y;
int a() { return *ep+x+y; }
20
Strong and Weak Symbols
Program symbols are either strong or weak
int foo=5;
p1() { }
int foo;
p2() { }
p1.c p2.c
strong
weak
strong
strong
21
Linker’s Symbol Rules
Rule 1. A strong symbol can only appear once.
Rule 2. A weak symbol can be overridden by a strong symbol of the same name.
Rule 3. If there are multiple weak symbols, the linker can pick an arbitrary one.
22
Linker Puzzles
int x; p1() {}
int x; p2() {}
int x; int y; p1() {}
double x; p2() {}
int x=7; int y=5; p1() {}
double x; p2() {}
int x=7; p1() {}
int x; p2() {}
int x; p1() {} p1() {} Link time error: two strong symbols ( p1 )
References to x will refer to the same uninitialized int. Is this what you really want?
Writes to x in p2 might overwrite y! Evil!
Writes to x in p2 will overwrite y! Nasty!
Nightmare scenario: two identical weak structs, compiled by different compilers with different alignment rules.
References to x will refer to the same initialized variable.
23
Packaging Commonly Used
Functions
Math, I/O, memory management, string manipulation, etc.
Option 1: Put all functions in a single source file Programmers link big object file into their programs Space and time inefficient Option 2: Put each function in a separate source file Programmers explicitly link appropriate binaries into their programs More efficient, but burdensome on the programmer
Concatenate related relocatable object files into a single file with an index (called an archive). Enhance linker so that it tries to resolve unresolved external references by looking for the symbols in one or more archives. If an archive member file resolves reference, link into executable.
24
Static Libraries (archives)
Translator
p1.c
p1.o
Translator
p2.c
p2.o libc.a
static library (archive) of relocatable object files concatenated into one file.
executable object file (only contains code and data for libc functions that are called from p1.c and p2.c)
Further improves modularity and efficiency by packaging commonly used functions [e.g., C standard library ( libc ), math library ( libm )]
Linker selectively only the .o files in the archive that are actually needed by the program.
Linker (ld)
p
25
Creating Static Libraries
Translator
atoi.c
atoi.o
Translator
printf.c
printf.o
libc.a
Archiver (ar)
... (^) Translator
random.c
random.o
ar rs libc.a
atoi.o printf.o … random.o
Archiver allows incremental updates:
C standard library
26
Commonly Used Libraries
8 MB archive of 900 object files. I/O, memory allocation, signal handling, string handling, data and time, random numbers, integer math
1 MB archive of 226 object files. floating point math (sin, cos, tan, log, exp, sqrt, …) % ar -t /usr/lib/libc.a | sort … fork.o … fprintf.o fpu_control.o fputc.o freopen.o fscanf.o fseek.o fstab.o …
% ar -t /usr/lib/libm.a | sort … e_acos.o e_acosf.o e_acosh.o e_acoshf.o e_acoshl.o e_acosl.o e_asin.o e_asinf.o e_asinl.o …
27
Using Static Libraries
Linker’s algorithm for resolving external references:
Problem:
bass> gcc -L. libtest.o -lmine bass> gcc -L. -lmine libtest.o libtest.o: In function main': libtest.o(.text+0x4): undefined reference tolibfun'
28
Loading Executable Binaries
ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.text .rel.data .debug Section header table (required for relocatables)
.text segment (r/o)
.data segment (initialized r/w)
.bss segment (uninitialized r/w)
Executable object file for example program p
Process image
0x
init and shared lib segments
0x080483e
Virtual addr
0x0804a
0x0804a3b
29
Shared Libraries
Potential for duplicating lots of common code in the executable files on a filesystem. e.g., every C program needs the standard C library Potential for duplicating lots of code in the virtual memory space of many processes. Minor bug fixes of system libraries require each application to explicitly relink
Shared libraries (dynamic link libraries, DLLs) whose members are dynamically loaded into memory and linked into an application at run-time. Dynamic linking can occur when executable is first loaded and run. » Common case for Linux, handled automatically by ld-linux.so. Dynamic linking can also occur after program has begun. » In Linux, this is done explicitly by user with dlopen(). » Basis for High-Performance Web Servers. Shared library routines can be shared by multiple processes.
30
Dynamically Linked Shared Libraries
libc.so functions called by m.c and a.c are loaded, linked, and (potentially) shared among processes.
Shared library of dynamically relocatable object files
Translators (cc1, as)
m.c
m.o
Translators (cc1,as)
a.c
a.o
libc.so
Linker (ld)
p
Loader/Dynamic Linker (ld-linux.so)
Fully linked executable p’ (in memory)
Partially linked executable p (on disk)