90 likes | 279 Views
Memory Layout for Process. ∞. Stack. Data. Code. 0. ∞. Stack. Data. Code. 0. x.c. y.c. cc. cc. y.s. x.s. as. as. y.o. x.o. 101010101010101010101010101010101010101010101010. z.c. cc. z.s. as. z.o. 101010101010101010101010101010101010101010101010.
E N D
Memory Layout for Process ∞ Stack Data Code 0 CS 140 Lecture Notes: Linkers
∞ Stack Data Code 0 x.c y.c cc cc y.s x.s as as y.o x.o 101010101010101010101010101010101010101010101010 z.c cc z.s as z.o 101010101010101010101010101010101010101010101010 101010101010101010101010101010101010101010101010 101010101010101010101010101010101010101010101010 Creating a Process SourceCode Assembly Code Object Code Executable OS ld a.out Compiler Assembler Linker Loader CS 140 Lecture Notes: Linkers
A Simple Example main.c stdio.c 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); } int printf(char *fmt, ...) { ... } int scanf(char *fmt, ...) { ... } math.c double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x)… } return res; } CS 140 Lecture Notes: Linkers
Object File main.c main.o 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); } ... call printf ... call scanf ... call sin ... call printf def: main@T:0 ref: printf@T:20, T:56 ref: scanf@T:32 ref: sin@T:40 text segment 20 32 40 56 symbols relocation CS 140 Lecture Notes: Linkers
Object File math.c math.o double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x) … } return res; } ... load lastx ... store lastx ... load res res: lastx: def: sin@T:0 def: lastx@D:0 def: res@D:8 ref: lastx@T:8, T:20 ref: res@T:204 text segment 8 20 204 0 8 data segment symbols relocation CS 140 Lecture Notes: Linkers
After Pass 1 Memory map: Symbol table: 836 main: 0 sin: 64 lastx: 700 result: 708 printf: 314 scanf: 508 stdio.o data 708 math.o data 700 stdio.o text 276 math.o text 64 main.o text 0 CS 140 Lecture Notes: Linkers
Relocation ... call 0 ... 40 text segment in main.o ref: sin@T:40 relocation record in main.o sin: 64 symbol table ... call 64 ... 40 text segment in a.out CS 140 Lecture Notes: Linkers