Memory Layout for Process ∞ Stack Data Code 0 CS 140 Lecture Notes: Linkers Slide 1
Creating a Process Source Assembly Object Executable Code Code File 10101010 10101010 10101010 10101010 10101010 10101010 x.c cc x.s as x.o ∞ Stack 10101010 10101010 10101010 10101010 OS 10101010 10101010 10101010 10101010 10101010 10101010 10101010 10101010 Data y.c cc y.s as y.o ld a.out Code 0 10101010 10101010 10101010 10101010 10101010 10101010 z.c cc z.s as z.o Compiler Assembler Linker Loader CS 140 Lecture Notes: Linkers Slide 2
A Simple Example main.c stdio.c extern float sin(); FILE* stdin, stdout; extern printf(), scanf(); int printf(const char* format,...) { main() { ... double x, result; fputc(c, stdout); printf("Type number: "); ... scanf("%f", &x); } result = sin(x); printf("Sine is %f\n", int scanf(const char* format,...) { result); ... } c = fgetc(stdin); ... } math.c double sin(double x) { ... } CS 140 Lecture Notes: Linkers Slide 3
main.o Object File main.c main.o extern float sin(); 0 main: text section extern printf(), scanf(); ... 30 call printf main() { ... double x, result; 52 call scanf printf("Type number: "); ... scanf("%f", &x); 60 call sin result = sin(x); ... printf("Sine is %f\n", 86 call printf result); } 0 _s1: "Type number: " data section 14 _s2: "%f" _s3: "Sine is %f\n" 17 symbols main T[0] _s1 D[0] “Store the final location of sin _s2 D[14] at offset 60 in the text section” _s3 D[17] relocation printf T[30] printf T[86] scanf T[52] sin T[60] _s1 T[24] _s2 T[54] _s3 T[80] CS 140 Lecture Notes: Linkers Slide 4
stdio.o Object File stdio.c stdio.o FILE* stdin, stdout; ... text section 44 printf: int printf(const char* format, ... ...) { load stdout 118 ... ... fputc(c, stdout); 232 scanf: ... ... } 306 load stdin ... int scanf(const char* format, ...) { 0 stdin: data section ... 8 stdout: c = fgetc(stdin); ... printf T[44] symbols } scanf T[232] stdin D[0] stdout D[8] stdout T[118] relocation fputc T[122] stdin T[306] fgetc T[310] CS 140 Lecture Notes: Linkers Slide 5
After Pass 1 Memory map: 836 stdio.o data 760 main.o data 720 math.o text 508 stdio.o text 96 main.o text 0 CS 140 Lecture Notes: Linkers Slide 6
After Pass 2 Memory map: Symbol table: 836 Name File Sec Offset Addr main main.o T 0 0 stdio.o data _s1 main.o D 0 720 760 _s2 main.o D 14 734 main.o data 720 _s3 main.o D 17 737 printf stdio.o T 38 134 math.o text scanf stdio.o T 232 328 508 stdin stdio.o D 0 760 stdout stdio.o D 8 768 stdio.o text sin math.o T 0 508 96 main.o text 0 CS 140 Lecture Notes: Linkers Slide 7
Relocation ... call 0 text section in main.o 30 ... 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 Slide 8
Recommend
More recommend