Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 140 Lecture Notes: LinkersSlide 1 Memory Layout for Process Code 0 ∞ Data Stack.

Similar presentations


Presentation on theme: "CS 140 Lecture Notes: LinkersSlide 1 Memory Layout for Process Code 0 ∞ Data Stack."— Presentation transcript:

1 CS 140 Lecture Notes: LinkersSlide 1 Memory Layout for Process Code 0 ∞ Data Stack

2 CS 140 Lecture Notes: LinkersSlide 2 Creating a Process Code 0 ∞ Data Stack 10101010 10101010 10101010 ccx.cx.sasx.o 10101010 10101010 10101010 ccy.cy.sasy.o 10101010 10101010 10101010 ccz.cz.sasz.o 10101010 10101010 10101010 Source Code Assembly Code Object Code Executable a.out CompilerAssemblerLinkerLoader ld OS

3 CS 140 Lecture Notes: LinkersSlide 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); } 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);... } 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) {... } double sin(double x) {... } math.c

4 CS 140 Lecture Notes: LinkersSlide 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); } 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.cmain.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] 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”

5 CS 140 Lecture Notes: LinkersSlide 5 Object File FILE* stdin, stdout; int printf(const char* format,...) {... fputc(c, stdout);... } int scanf(const char* format,...) {... c = fgetc(stdin);... } FILE* stdin, stdout; int printf(const char* format,...) {... fputc(c, stdout);... } int scanf(const char* format,...) {... c = fgetc(stdin);... } printf.cprintf.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]... 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 data section relocation symbols

6 CS 140 Lecture Notes: LinkersSlide 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 NameFileSecOffsetAddr main:mainT0?? sin:mathT0?? printf:stdioT38?? scanf:stdioT232?? stdin:stdioD0?? stdout:stdioD8?? Memory map:Symbol table:

7 CS 140 Lecture Notes: LinkersSlide 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 NameFileSecOffsetAddr main:mainT00 sin:mathT064 printf:stdioT38314 scanf:stdioT232508 stdin:stdioD0728 stdout:stdioD8736 Memory map:Symbol table:

8 CS 140 Lecture Notes: LinkersSlide 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...

9 CS 140 Lecture Notes: LinkersSlide 9


Download ppt "CS 140 Lecture Notes: LinkersSlide 1 Memory Layout for Process Code 0 ∞ Data Stack."

Similar presentations


Ads by Google