/
CS 140 Lecture Notes: Linkers CS 140 Lecture Notes: Linkers

CS 140 Lecture Notes: Linkers - PowerPoint Presentation

stefany-barnette
stefany-barnette . @stefany-barnette
Follow
420 views
Uploaded On 2016-04-11

CS 140 Lecture Notes: Linkers - PPT Presentation

Slide 1 Memory Layout for Process Code 0 Data Stack CS 140 Lecture Notes Linkers Slide 2 Creating a Process 101010101010101010101010101010101010101010101010 cc xc xs as xo ID: 278576

main printf sin scanf printf main scanf sin lecture notes linkers slide text stdin stdout 140 stdio call data

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS 140 Lecture Notes: Linkers" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CS 140 Lecture Notes: Linkers

Slide 1

Memory Layout for Process

Code

0

Data

StackSlide2

CS 140 Lecture Notes: Linkers

Slide 2

Creating a Process

101010101010101010101010101010101010101010101010

cc

x.c

x.s

as

x.o

101010101010101010101010101010101010101010101010

cc

y.c

y.s

as

y.o

101010101010101010101010101010101010101010101010

cc

z.c

z.s

as

z.o

101010101010101010101010101010101010101010101010

Source

Code

Assembly

Code

Object

Code

Executable

a.out

Compiler

Assembler

Linker

Loader

ld

OS

Code

0

Data

StackSlide3

CS 140 Lecture Notes: Linkers

Slide 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);}

main.c

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) {

...}

math.cSlide4

CS 140 Lecture Notes: Linkers

Slide 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);}

main.c

main.o

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 sectiondata section

relocation

“Store the final location of sinat offset 60 in the text section”symbolsSlide5

CS 140 Lecture Notes: Linkers

Slide 5

Object File

FILE*

stdin, stdout;

int printf(const char* format, ...) {

... fputc(c, stdout);

...}int

scanf(const char* format,

...) { ... c = fgetc

(

stdin

);

...

}

printf.c

printf.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]

text

section

data sectionrelocationsymbolsSlide6

CS 140 Lecture Notes: Linkers

Slide

6

During Pass 2

Name File Sec Offset

Addr

main

: main T 0 0_s1: main D 0 720_s2: main D 14 734

_s3: main D 17 737printf: stdio T 38 134

scanf: stdio T 232 328stdin: stdio D 0 760

stdout

:

stdio

D 8 768

sin: math T 0

508

Memory map:

Symbol table:

main.o

text

0

508

760

math.o

text

stdio.o

text

main.o

data

stdio.o

data

836

96

720Slide7

CS 140 Lecture Notes: Linkers

Slide 7

Relocation

text

section in

main.o

30

...call 0

...

printf T[30]

relocation record in main.o

printf

134

symbol table

text

section in

a.out

30

...

call

134

...Slide8

CS 140 Lecture Notes: Linkers

Slide 8