Download Dynamic Linking of Shared Libraries: A Better Scheme Using a Linker - Prof. Richard Carver and more Study notes Computer Science in PDF only on Docsity!
Linking
TopicsTopics
Static linking
Object files
Static libraries
Loading
Dynamic linking of shared libraries
lecture13.ppt
CS 367
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() {}
A Simplistic Program Translation
Scheme
Problems:
- Efficiency: small change requires complete recompilation
- Modularity: hard to share common functions (e.g. printf )
Solution:
- Static linker (or linker)
Translator
m.c
p
ASCII source file
Binary executable object file
(memory image on disk)
A Better Scheme Using a Linker
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 )
Why Linkers?
Modularity Modularity
Program can be written as a collection of smaller source
files, rather than one monolithic mass.
Can build libraries of common functions (more on this later)
e.g., Math library, standard C library
Efficiency Efficiency
Time:
Change one source file, compile, and then relink.
No need to recompile other source files.
Space:
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.
Executable and Linkable Format
(ELF)
Standard binary format for object filesStandard binary format for object files
Derives from AT&T System V UnixDerives from AT&T System V Unix
Later adopted by BSD Unix variants and Linux
One unified format forOne unified format for
Relocatable object files ( .o ),
Executable object files
Shared object files (. so )
Generic name: ELF binariesGeneric name: ELF binaries
Better support for shared libraries than oldBetter support for shared libraries than old a.outa.out formats.formats.
ELF Object File Format
Elf headerElf header
Magic number, type (.o, exec, .so),
machine, byte ordering, etc.
Program header tableProgram header table
Page size, virtual addresses memory
segments (sections), segment sizes.
.text.text sectionsection
Code
.data.data sectionsection
Initialized (static) data
..bssbss^ sectionsection
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)
ELF Object File Format (cont)
..symtabsymtab^ sectionsection
Symbol table
Procedure and static variable names
Section names and locations
..relrel.text.text sectionsection
Relocation info for .text section
Addresses of instructions that will need to
be modified in the executable
Instructions for modifying.
..relrel.data.data sectionsection
Relocation info for .data section
Addresses of pointer data that will need to
be modified in the merged executable
.debug.debug sectionsection
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)
Relocating Symbols and Resolving
External References
Symbols are lexical entities that name functions and variables.
Each symbol has a value (typically a memory address).
Code consists of symbol definitions and references.
References can be either local or external.
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;
Def of local
symbol e
Ref to external
symbol exit
(defined in
libc.so )
Ref to
external
symbol e
Def of
local
symbol
ep
Defs of
local
symbols
x and y
Refs of local
symbols ep,x,y
Def of
local
symbol a
Ref to external
symbol a
m.o Relocation Info
Disassembly of section .text: 00000000 : 00000000 : 0: 55 pushl %ebp 1: 89 e5 movl %esp,%ebp 3: e8 fc ff ff ff call 4 4: R_386_PC32 a 8: 6a 00 pushl $0x a: e8 fc ff ff ff call b b: R_386_PC32 exit f: 90 nop Disassembly of section .data: 00000000 : 0: 07 00 00 00 source: objdump
int e=7;
int main() {
int r = a();
exit(0);
m.c
a.o Relocation Info ( .text )
a.c
extern int e;
int *ep=&e;
int x=15;
int y;
int a() {
return *ep+x+y;
Disassembly of section .text: 00000000 : 0: 55 pushl %ebp 1: 8b 15 00 00 00 movl 0x0,%edx 6: 00 3: R_386_32 ep 7: a1 00 00 00 00 movl 0x0,%eax 8: R_386_32 x c: 89 e5 movl %esp,%ebp e: 03 02 addl (%edx),%eax 10: 89 ec movl %ebp,%esp 12: 03 05 00 00 00 addl 0x0,%eax 17: 00 14: R_386_32 y 18: 5d popl %ebp 19: c3 ret
a.o Relocation Info (. data )
a.c
extern int e;
int *ep=&e;
int x=15;
int y;
int a() {
return *ep+x+y;
Disassembly of section .data: 00000000 : 0: 00 00 00 00 0: R_386_32 e 00000004 : 4: 0f 00 00 00
Strong and Weak Symbols
Program symbols are either strong or weak Program symbols are either strong or weak
strong: procedures and initialized globals
weak: uninitialized globals
int foo=5;
p1() {
int foo;
p2() {
p1.c p2.c
strong
weak
strong
strong
Linker’s Symbol Rules
Rule 1. A strong symbol can only appear once. Rule 1. A strong symbol can only appear once.
Rule 2. A weak symbol can be overridden by a strong Rule 2. A weak symbol can be overridden by a strong
symbol of the same name. symbol of the same name.
references to the weak symbol resolve to the strong symbol.
Rule 3. If there are multiple weak symbols, the linker Rule 3. If there are multiple weak symbols, the linker
can pick an arbitrary one. can pick an arbitrary one.
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.
Packaging Commonly Used
Functions
How to package functions commonly used by programmers?How to package functions commonly used by programmers?
Math, I/O, memory management, string manipulation, etc.
Awkward, given the linker framework so far:Awkward, given the linker framework so far:
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
Solution:Solution: static libraries^ static libraries^ (.(. aa^ archive filesarchive files))
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.
Commonly Used Libraries
libclibc.a.a^ (the C standard library)(the C standard library)
8 MB archive of 900 object files.
I/O, memory allocation, signal handling, string handling, data and
time, random numbers, integer math
libmlibm.a.a (the C math library)(the C math library)
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 …
Using Static Libraries
Linker Linker’’s algorithm for resolving external references:s algorithm for resolving external references:
Scan .o files and .a files in the command line order.
During the scan, keep a list of the current unresolved
references.
As each new .o or .a file obj is encountered, try to resolve
each unresolved reference in the list against the symbols in
obj.
If any entries in the unresolved list at end of scan, then error.
Problem: Problem:
Command line order matters!
Moral: put libraries at the end of the command line.
bass> gcc -L. libtest.o - lmine bass> gcc -L. - lmine libtest.o libtest.o: In function main': libtest.o(.text+0x4): undefined reference tolibfun'
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
Shared Libraries
Static libraries have the following disadvantages:Static libraries have the following disadvantages:
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
Solution:Solution:
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.