CS 140 Lecture Notes: LinkersSlide 1 Memory Layout for Process Code 0 ∞ Data Stack
CS 140 Lecture Notes: LinkersSlide 2 Creating a Process Code 0 ∞ Data Stack ccx.cx.sasx.o ccy.cy.sasy.o ccz.cz.sasz.o Source Code Assembly Code Object Code Executable a.out CompilerAssemblerLinkerLoader ld OS
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 int printf(char *fmt,...) {... } int scanf(char *fmt,...) {... } int printf(char *fmt,...) {... } int scanf(char *fmt,...) {... } stdio.c double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x) … } return res; } double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x) … } return res; } math.c
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 call printf... call scanf... call sin... call printf def: ref: T:56 ref: ref: call printf... call scanf... call sin... call printf def: ref: T:56 ref: ref: text segment symbols relocation
CS 140 Lecture Notes: LinkersSlide 5 Object File double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x) … } return res; } double sin(double x) { static double res, lastx; if (x != lastx) { lastx = x; … compute sin(x) … } return res; } math.cmath.o load lastx... store lastx... load res res: lastx: def: def: def: ref: T:20 ref: load lastx... store lastx... load res res: lastx: def: def: def: ref: T:20 ref: text segment data segment relocation symbols
CS 140 Lecture Notes: LinkersSlide 6 After Pass 1 main.o text math.o text stdio.o text math.o data 708 stdio.o data 836 main:0 sin:64 lastx:700 result:708 printf:314 scanf:508 Memory map:Symbol table:
CS 140 Lecture Notes: LinkersSlide 7 Relocation text segment in main.o call 0... ref: relocation record in main.o sin: 64 symbol table text segment in a.out call 64...
CS 140 Lecture Notes: LinkersSlide 8