Docsity

Linking in Program Translation and Execution: Object Files, Static and Shared Libraries, Slides of Computer System Design and Architecture

Partial preview of the text

Download Linking in Program Translation and Execution: Object Files, Static and Shared Libraries and more Slides Computer System Design and Architecture in PDF only on Docsity!

Program Translation and Execution I:

Linking

Sept. 29, 1998

Topics

object files

linkers

class11.ppt

Introduction to Computer Systems

  • 2 –

class11.ppt

A simplistic program

translation scheme

Problems:

  • modularity: hard to share common functionality (e.g. printf)• efficiency: small change requires complete recompilation

Translators

p.c p

Loader

p

(memory image)executing program(memory image on disk)executable object file source file

  • 4 –

class11.ppt

Static libraries

Translator

p1.o^ p1.c

Translator

p2.o p2.c

libc.a

object filesof relocatable static library

from p1.c and p2.c)libc functions that are called(only contains code and data forexecutable object file

common functions in different processesFurther improves modularity and efficiency, but duplicates

Static Linker

p

Loader

  • 5 –

class11.ppt

Shared libraries

processes.time and shared amongand p2.c are loaded at run- libc functions called by p1.c

relocatable object filesshared library of dynamically

Translator

p1.o p1.c

Translator

p2.o p2.c

libc.so

Static Linker

p

Loader/Dynamic Linker

CS 213 F’

  • 7 –

class11.ppt

Object files

main() {int n=20;double d; char c[100];

c[i] = ‘z’;i=5; int i;

}

Headers

Relocation info Symbol table^ text

sections:

read-only code and data

data

sections

initialized static variables

0

bss

sections

uninitialized static variables Block Started by Symbol

Better Save Space

C Program

Object file

runtime on the stack.object files. They are created atNote: local variables don’t go in

addressesIncreasing

  • 8 –

class11.ppt

Example C program

main() {

zip();

code(int *p) {int x=0xfe;

srand(*p);

int *pextern int x;

&x;

zip()

code(p);

main.c:

zip.c:

code.c:

CS 213 F’

  • 10 –

class11.ppt

Linker functions

main()

Resolving external references

Relocating symbol definitions and references

main.o

*p

zip()

zip.o

x

code()

code.o

x

crt

main() zip()

crt

crt0.o

code()

*p

main

Data Text

  • 11 –

class11.ppt

Symbols

Lexical entities that correspond to functions and global variables

Each symbol has a

value (address)

Code consists of symbol

definitions and

references

References can be either

local or

external

main() {

zip();

code(int *p) {int x=0xfe;

srand(*p);

int *pextern int x;

&x;

zip()

code(p);

main.c:

zip.c:

code.c:

symbol zipexternal Ref to

symbol zipDef of local

symbol plocalDef of

symbol xexternalRef to

symbol xlocalDef of

(defined in libc.so)symbol srandRef to external

Ref to local symbol p

  • 13 –

class11.ppt

Section data (relocatable object files)

.bss

.sbss

.sdata

.lita.lit8.lit

.data

.init/.fini

.text

.pdata/.xdata

.rdata

.rconst

bss segment

(better save space!)(block started by symbol) uninitialized variables

data segment

initialized variables

text segment

machine code

machine instructionsprocess init/deinit codelarge datasmall dataread only dataread only constants8-byte literal address pool8-byte literal pool4-byte literal poolsupport for exceptionssmall (4-byte) bssuninitialized data

(Runtime memory image)

(File format)

  • 14 –

class11.ppt

.text section (main.o)

main:

0x0:

lda

sp,

-16(sp)

0x4:

ldah

gp,

1(t12)

0x8:

lda

gp,

-32704(gp)

0xc:

stq

ra,

0(sp)

0x10: ldq

t12,

-32752(gp)

0x14: jsr

ra,

(t12),

zip

0x18: ldah

gp,

1(ra)

0x1c: ldq

ra,

0(sp)

0x20: bis

zero,

zero,

v

0x24: lda

gp,

-32728(gp)

0x28: lda

sp,

16(sp)

0x2c: ret

zero,

(ra),

main() {

zip();

.text .lita

0x00 (0)

0x50 (80) start

0x10 (16)0x30 (48) size

(Note: ra = pc)resets gp to 32,832t12 = address stored at .lita+0(Note that t12 = pc-4)that gp=32,832=(64K+128)/2Assembler assumes

referencing of data and functions?Question: Why use gp-relative

CS 213 F’

  • 16 –

class11.ppt

Relocation entries (main.o)

Vaddr Symndx

Type

Extern Name

0x04 .text:

GPDISP

local

0x

LITERAL

local

.lita

0x

LITUSE

local

R_LU_JSR

0x

HINT

extern zip

0x

GPDISP

local

0x50.lita:

REFQUAD extern zip

main() {

zip();

.text .lita

0x00 (0)

0x50 (80) start

0x10 (16)0x30 (48) size

GPDISP :

instruction pair that

LITERAL: sets gp value

gp-relative

LITUSE: reference to literal in .lita

identifies instance of

HINT: loaded into a registera literal address previously

14 bit jsr hint reference

REFQUAD:

64-bit reference to

the symbol’s virtual address

CS 213 F’

  • 17 –

class11.ppt

Relocations (main.o)

main:

0x0:

lda

sp, -16(sp)

0x4:

ldah

gp, 1(t12)

0x8:

lda

gp, -32704(gp)

0xc:

stq

ra, 0(sp)

0x10:

ldq

t12, -32752(gp)

0x14:

jsr

ra, (t12), zip

0x18:

ldah

gp, 1(ra)

0x1c:

ldq

ra, 0(sp)

0x20:

bis

zero, zero, v

0x24:

lda

gp, -32728(gp)

0x28:

lda

sp, 16(sp)

0x2c: ret

zero, (ra), 1

main() {

zip();

.lita^ .text

0x00 (0)

0x50 (80) start

0x10 (16)0x30 (48) Vaddr Symndx Type size

Extern Name

0x04 .text:

4

GPDISP

local

0x

13

LITERAL

local

.lita

0x

3

LITUSE

local

R_LU_JSR

0x

1

HINT

extern zip

0x

12

GPDISP

local

0x50.lita:

1

REFQUAD extern zip

  1. LITUSE at 0x14 allows jsr to be replaced by bsr Notes:
  • 19 –

class11.ppt

Symbol table (zip.o)

[Index]

Name

Value

Sclass

Symtype

[0]

p

0x

SData

Global

[1]

x

0x

SUndefined Global

[2]

zip

0x

Text

Proc

[3]

code

0x

Undefined

Proc

zip() {int *p = &x; extern int x;

code(p);

.text .lita

0x00 (0)

0x60 (96) start

0x10 (16)0x40 (64) size

.sdata

0x70 (112)

0x10 (16)

CS 213 F’

  • 20 –

class11.ppt

Relocation entries (zip.o)

zip() {int *p = &x; extern int x;

code(p);

.text .lita

0x00 (0)

0x60 (96) start

0x10 (16)0x40 (64) size

.sdata

0x70 (112)

0x10 (16)

Vaddr Symndx

Type

Extern

Name

.text:

0x

4

GPDISP

local

0x

13

LITERAL

local

.lita

0x

1

LITUSE

local

R_LU_BASE

0x

13

LITERAL

local

.lita

0x1c

3

LITUSE

local

R_LU_JSR

0x1c

3

HINT

extern code

0x

8

GPDISP

local

.lita:

0x

0

REFQUAD

extern p

0x

3

REFQUAD

extern code

.sdata:

0x

1

REFQUAD

extern x

0x0:

lda

sp, -16(sp)

0x4:

ldah

gp, 1(t12)

0x8:

lda

gp, -32688(gp)

0xc:

stq

ra, 0(sp)

0x10: ldq

t12, -32744(gp)

0x14: ldq

a0, -32752(gp)

0x18: ldq

a0, 0(a0)

0x1c: jsr

ra, (t12), code

0x20: ldah

gp, 1(ra)

0x24: ldq

ra, 0(sp)

0x28: lda

gp, -32720(gp)

0x2c: lda

sp, 16(sp)

0x30: ret

zero, (ra), 1