Slide 1 Memory Layout for Process Code 0 Data Stack CS 140 Lecture Notes Linkers Slide 2 Creating a Process Code 0 Data Stack 101010101010101010101010101010101010101010101010 ID: 477346
Download Presentation The PPT/PDF document "CS 140 Lecture Notes: Linkers" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
CS 140 Lecture Notes: Linkers
Slide 1
Memory Layout for Process
Code
0
∞
Data
StackSlide2
CS 140 Lecture Notes: Linkers
Slide 2
Creating a Process
Code
0
∞
Data
Stack
101010101010101010101010101010101010101010101010
cc
x.c
x.s
as
x.o
101010101010101010101010101010101010101010101010
cc
y.c
y.s
as
y.o
101010101010101010101010101010101010101010101010
cc
z.c
z.s
as
z.o
101010101010101010101010101010101010101010101010
Source
Code
Assembly
Code
Object
Code
Executable
a.out
Compiler
Assembler
Linker
Loader
ld
OSSlide3
CS 140 Lecture Notes: Linkers
Slide 3
A Simple Example
extern float sin();
extern printf(), scanf
();main() { double x, result; printf("Type number: ");
scanf("%f", &x); result = sin(x); printf("Sine is %f\n", result);}
main.c
FILE*
stdin, stdout;
int
printf
(
const
char* format,
...) {
...
fputc
(c,
stdout
);
...
}
int scanf
(const char* format, ...) { ... c = fgetc(stdin
); ...
}
stdio.c
double sin(double x) { ...}
math.cSlide4
CS 140 Lecture Notes: Linkers
Slide 4
Object File
extern float sin();
extern printf(), scanf();main() { double x, result;
printf("Type number: "); scanf("%f", &x); result = sin(x); printf("Sine is %f\n", result);}
main.c
main.o
0
20
32
40
56
main:
...
call
printf
...
call
scanf
...
call sin
...
call
printf
main T[0]
printf
T[20]
printf
T[56]
scanf T[32]sin T[40]
text
section
symbols
relocation
“Store the final location of
printf
at offset 20 in the text section”Slide5
CS 140 Lecture Notes: Linkers
Slide 5
Object File
FILE*
stdin, stdout;
int printf(const char* format, ...) {
... fputc(c, stdout);
...}int
scanf(const char* format,
...) { ... c = fgetc
(
stdin
);
...
}
printf.c
printf.o
44
118
232
306
0
8
...
printf
:
...
load
stdout
...
scanf
:
...
load stdin...
stdin:stdout:
printf T[44]scanf
T[232]stdin D[0]stdout
D[8]stdout T[118]stdin
T[306]
text
sectiondata sectionrelocationsymbolsSlide6
CS 140 Lecture Notes: Linkers
Slide
6
During Pass
1
main.o text
0
64
276
700
math.o text
stdio.o text
math.o data
728
stdio.o data
836
Name File Sec Offset
Addr
main
:
main T 0 ??
sin:
math T 0 ??
printf
:
stdio
T 38 ??
scanf
:
stdio
T 232 ??
stdin
:
stdio
D 0 ??
stdout
:
stdio
D 8 ??
Memory map:
Symbol table:Slide7
CS 140 Lecture Notes: Linkers
Slide
7
After Pass 1
main.o text
0
64
276
700
math.o text
stdio.o text
math.o data
728
stdio.o data
836
Name File Sec Offset
Addr
main
:
main T 0 0
sin:
math T 0 64
printf
:
stdio
T 38 314
scanf
:
stdio
T 232 508
stdin
:
stdio
D 0 728
stdout
:
stdio
D 8 736
Memory map:
Symbol table:Slide8
CS 140 Lecture Notes: Linkers
Slide 8
Relocation
text
section in
main.o
20
...call 0
...
printf T[20]
relocation record in main.o
printf
314
symbol table
text
section in
a.out
20
...
call
314
...Slide9
CS 140 Lecture Notes: Linkers
Slide 9