120 likes | 401 Views
Memory Layout for Process. 0. Code. Data. Stack. ∞. x.c. y.c. cc. cc. x.s. y.s. as. as. x.o. y.o. 101010101010101010101010101010101010101010101010. z.c. cc. z.s. as. z.o. 101010101010101010101010101010101010101010101010. 101010101010101010101010101010101010101010101010.
E N D
Memory Layout for Process 0 Code Data Stack ∞ CS 140 Lecture Notes: Linkers
x.c y.c cc cc x.s y.s as as x.o y.o 101010101010101010101010101010101010101010101010 z.c cc z.s as z.o 101010101010101010101010101010101010101010101010 101010101010101010101010101010101010101010101010 101010101010101010101010101010101010101010101010 Creating a Process 0 Code SourceCode Assembly Code Object Code Executable Data Stack ∞ 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); } FILE* stdin, stdout; intprintf(const char* format, ...) { ... fputc(c, stdout); ... } intscanf(const char* format, ...) { ... c = fgetc(stdin); ... } math.c double sin(double x) { ... } 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); } 0 30 52 60 86 0 14 17 main: ... call printf ... call scanf ... call sin ... call printf _s1: "Type number: " _s2: "%f" _s3: "Sine is %f\n" main T[0] _s1 D[0] _s2 D[14] _s3 D[17] printf T[30] printf T[86] scanf T[52] sin T[60] _s1 T[24] _s2 T[54] _s3 T[80] text section data section symbols “Store the final location of sinat offset 60 in the text section” relocation CS 140 Lecture Notes: Linkers
Object File printf.c printf.o FILE* stdin, stdout; intprintf(const char* format, ...) { ... fputc(c, stdout); ... } intscanf(const char* format, ...) { ... c = fgetc(stdin); ... } ... 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 section 44 118 232 306 0 8 data section symbols relocation CS 140 Lecture Notes: Linkers
During Pass 2 Memory map: Symbol table: 0 Name File Sec Offset Addr main: main T 0 0 _s1: main D 0 720 _s2: main D 14 734 _s3: main D 17 737 printf: stdio T 38 134 scanf: stdio T 232 328 stdin: stdio D 0 760 stdout: stdio D 8 768 sin: math T 0 508 main.o text 96 stdio.o text 508 math.o text 720 main.odata 760 stdio.o data 836 CS 140 Lecture Notes: Linkers
Relocation ... call 0 ... 30 text section in main.o printf T[30] relocation record in main.o printf 134 symbol table ... call 134 ... 30 text section in a.out CS 140 Lecture Notes: Linkers