80 likes | 162 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); } intprintf(char *fmt, ...) { ... } intscanf(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 ref: printf@T:56 ref: scanf@T:32 ref: sin@T:40 text section 20 32 40 56 symbols relocation “Store the final location of printfat offset 20 in the text section” 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:8 def: res@D:0 ref: lastx@T:8 ref: lastx@T:20 ref: res@T:204 text section 8 20 204 0 8 data section symbols relocation CS 140 Lecture Notes: Linkers
After Pass 1 Memory map: Symbol table: main: 0 sin: 64 lastx: 700 result: 708 printf: 314 scanf: 508 836 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 section in main.o ref: sin@T:40 relocation record in main.o sin: 64 symbol table ... call 64 ... 40 text section in a.out CS 140 Lecture Notes: Linkers