








































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
An in-depth look into the process of linking and loading executables, focusing on object files, the executable and linkable format (elf), and static and shared libraries. It covers topics such as relocatable object files, relocation information, and the role of the linker in resolving external references.
Typology: Study notes
1 / 48
This page cannot be seen from the preview
Don't miss anything!









































Problems:
Solution:
Translator
m.c
p
Included with each compilation system ( cc or gcc )
Invokes preprocessor ( cpp ), compiler ( cc1 ), assembler ( as ),
and linker ( ld ).
Passes command line arguments to appropriate phases
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
ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o
bass>
Linker (ld)
C compiler
m.c
m.
s
C compiler
a.c
a.o
p
assembler
m.
o
Libraries
libc.a
This is the executable program
assembler
a.
s
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
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.
Elf header Elf header
Magic number, type (.o, exec, .so),
machine, byte ordering, etc.
Program header table Program header table
Page size, virtual addresses of memory
segments (sections), segment sizes.
.text .text (^) sectionsection
Code
.data .data (^) sectionsection
Initialized (static) data
. .bssbss sectionsection
Uninitialized (static) data
“Block Started by Symbol”
Has section header but occupies no
space in the disk file
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)
0
. .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)
0
main()
m.o
int *ep = &e
a()
a.o
int e = 7
headers
main()
a()
system code
int *ep = &e
int e = 7
system data
more system code
int x = 15
int y
system data
int x = 15
Relocatable Object Files (^) Executable Object File
.text
.text
.data
.text
.data
.text
.data
.bss
.symtab
.debug
.data
uninitialized data .bss
system code
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
The compiler doesn’t know anything about Unix system calls
The compiler knows about names and data types
Disassembly of section .text:
00000000
0: 55 pushl %ebp
1: 89 e5 movl %esp,%ebp
3: e8 fc ff ff ff call 4 <main+0x4>
4: R_386_PC32 a
8: 6a 00 pushl $0x
a: e8 fc ff ff ff call b <main+0xb>
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.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