Top Banner
1 Linking
59

Linking Ⅱ

Dec 31, 2015

Download

Documents

sierra-roth

Linking Ⅱ. Outline. Static linking Symbols & Symbol Table Relocation Executable Object Files Loading Suggested reading: 7.3~7.5, 7.7~7.9. Figure 7.1 P541. Example P542. unix> gcc -O2 -g -o p main.c swap.c cpp [args] main.c /tmp/main.i - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Linking  Ⅱ

1

Linking Ⅱ

Page 2: Linking  Ⅱ

2

Outline

• Static linking• Symbols & Symbol Table• Relocation• Executable Object Files• Loading• Suggested reading: 7.3~7.5, 7.7~7.9

Page 3: Linking  Ⅱ

3

main.c swap.c1. /*main.c */2. void swap() ;3. 4. int buf[2] = {1, 2};5. 6. Int main()7. {8. swap() ;9. return 0 ;10. }

1. /*swap.c */2. extern int buf[];3. 4. int *bufp0 = &buf[0]5. int *bufp1 ;6. 7. void swap()8. {9. int temp ;10. 11. bufp1 = &buf[1];12. temp = *bufp0 ;13. *bufp0 = *bufp1 ;14. *bufp1 = temp ;15. }

Figure 7.1 P541

Page 4: Linking  Ⅱ

4

unix> gcc -O2 -g -o p main.c swap.c

cpp [args] main.c /tmp/main.i

cc1 /tmp/main.i main.c -O2 [args] -o /tmp/main.s

as [args] -o /tmp/main.o /tmp/main.s

<similar process for swap.c>

ld -o p [system obj files] /tmp/main.o /tmp/swap.o

unix>

Example P542

Page 5: Linking  Ⅱ

5

Object file

• Object file

– Various code and data sections

– Instructions are in one section

– Initialized global variables are in one section

– Uninitialized global variables are in one section

Page 6: Linking  Ⅱ

6

ELF header

Program header table(required for executables)

.text section

.data section

.bss section

.symtab

.rel.txt

.rel.data

.debug

Section header table(required for relocatables)

.line

.strtab

ELF object file format

Figure 7.3 P544

Page 7: Linking  Ⅱ

7

7.3 Object Files

Page 8: Linking  Ⅱ

8

Object files

• Relocatable object file

– Contain binary code and data in a form that can

be combined with other relocatable object files

to create an executable file

• Executable object file

– Contains binary code and data in a form that

can be copied directly into memory and

executed

Page 9: Linking  Ⅱ

9

Object files

• Shared object file

– A special type of relocatable object file that can

be loaded into memory and linked dynamically,

at either load time or run time

Page 10: Linking  Ⅱ

10

7.2 Static Linking

Page 11: Linking  Ⅱ

11

Static linking

• Input

– A relocatable object files and command line

arguments

• Output

– Fully linked executable object file that can be

loaded and run

Page 12: Linking  Ⅱ

12

Static linking

• Symbol resolution

– resolves external references.

• external reference: reference to a symbol defined in

another object file

Page 13: Linking  Ⅱ

13

Static linking

• Relocation

– relocates symbols from their relative locations in the .o files to new absolute positions in the executable.

– updates all references to these symbols to reflect their new positions.

• references can be in either code or data

– code: a(); /* ref to symbol a */

– data: int *xp=&x; /* ref to symbol x */

Page 14: Linking  Ⅱ

14

7.4 Relocatable Object Files

Page 15: Linking  Ⅱ

15

Executable and Linkable Format (ELF)

• Standard binary format for object files• Derives from AT&T System V Unix

– later adopted by BSD Unix variants and Linux

• One unified format for relocatable object files (.o), executable object files, and shared object files (.so)– generic name: ELF binaries

• Better support for shared libraries than old a.out formats.

Page 16: Linking  Ⅱ

16

ELF header

Program header table(required for executables)

.text section

.data section

.bss section

.symtab

.rel.txt

.rel.data

.debug

Section header table(required for relocatables)

.line

.strtab

EFI object file format

Figure 7.3 P544

Page 17: Linking  Ⅱ

17

EFI object file format

• Elf header

– magic number, type (.o, exec, .so), machine,

byte ordering, etc.

• Program header table

– page size, virtual addresses for memory

segments (sections), segment sizes.

Page 18: Linking  Ⅱ

18

EFI object file format

• .text section– code

• .data section– initialized (static) data

• .bss section– uninitialized (static) data– “Block Started by Symbol”– “Better Save Space”– has section header but occupies no space

Page 19: Linking  Ⅱ

19

EFI object file format

• .symtab section– symbol table

– procedure and static variable names

– section names and locations

• .rel.text section– relocation info for .text section

– addresses of instructions that will need to be modified in the executable

– instructions for modifying.

Page 20: Linking  Ⅱ

20

EFI object file format

• .rel.data section– relocation info for .data section

– addresses of pointer data that will need to be modified in the merged executable

• .debug section– debugging symbol table, local variables and

typedefs, global variables, original C source file (gcc -g)

Page 21: Linking  Ⅱ

21

EFI object file format

• .line:

– Mapping between line numbers in the original C

source program and machine code instructions

in the .text section.

• .strtab:

– A string table for the symbol tables and for the

section names.

Page 22: Linking  Ⅱ

22

7.5 Symbols and Symbol Tables

Page 23: Linking  Ⅱ

23

Symbols

• Three kinds of symbols– 1) Defined global symbols

• Defined by module m and can be referenced by other modules– Nonstatic C functions– Global variables that are defined without the C static

attribute

– 2) Referenced global symbols• Referenced by module m but defined by some other module

– C functions and variables that are defined in other modules

– 3) Local symbols• Defined and referenced exclusively by module m.

– C functions and global variables with static attribute

Page 24: Linking  Ⅱ

24

Symbols

• Three kinds of symbols– 1) Defined global symbols

• Defined by module m and can be referenced by other modules

– Nonstatic C functions

– Global variables that are defined without the C static attribute

Page 25: Linking  Ⅱ

25

Symbols

• Three kinds of symbols– 2) Referenced global symbols

• Referenced by module m but defined by some other module

– C functions and variables that are defined in other modules

– 3) Local symbols• Defined and referenced exclusively by module m.

– C functions and global variables with static attribute

Page 26: Linking  Ⅱ

26

Symbol Tables

• Each relocatable object module has a symbol table

• A symbol table contains information about the symbols that are defined and referenced by the module

Page 27: Linking  Ⅱ

27

Symbol Tables

• Local nonstatic program variables – does not contain in the symbol table in .symbol

• Local static procedure variables– Are not managed on the stack

– Be allocated in .data or .bss

Page 28: Linking  Ⅱ

28

ELF header

Program header table(required for executables)

.text section

.data section

.bss section

.symtab

.rel.txt

.rel.data

.debug

Section header table(required for relocatables)

.line

.strtab

ELF object file format

Figure 7.3 P544

Page 29: Linking  Ⅱ

29

Examples

1. int f()2. {3. static int x=1 ;4. return x;5. }6. 7. int g()8. {9. static int x = 1;10. return x ;11. }• x.1 and x.2 are allocated in .data

Page 30: Linking  Ⅱ

30

Symbol Tables

• Compiler exports symbols in .s file

• Assembler builds symbol tables using exported symbols

• An ELF symbol table is contained in .symtab section

• Symbol table contains an array of entries

Page 31: Linking  Ⅱ

31

main.c swap.c1. /*main.c */2. void swap() ;3. 4. int buf[2] = {1, 2};5. 6. Int main()7. {8. swap() ;9. return 0 ;10. }

1. /*swap.c */2. extern int buf[];3. 4. int *bufp0 = &buf[0]5. int *bufp1 ;6. 7. void swap()8. {9. int temp ;10. 11. bufp1 = &buf[1];12. temp = *bufp0 ;13. *bufp0 = *bufp1 ;14. *bufp1 = temp ;15. }

Page 32: Linking  Ⅱ

32

ELF Symbol Tables P547

1. typedef struct {2. int name ; /* string table offset */3. int value ; /* section offset, or VM address */4. int size ; /* object size in bytes */5. char type:4 , /* data, func, section, or src file name

*/6. binding:4 ; /* local or global */7. char reserved ;/* unused */8. char section ; /* section header index, ABS, UNDEF,

*/9. /* or COMMON */10. }• ABS, UNDEF, COMMON

Page 33: Linking  Ⅱ

33

Num: Value Size Type Bind Ot Ndx Name

8: 0 8 OBJECT GLOBAL 0 3 buf

9: 0 17 FUNC GLOBAL 0 1 main

10: 0 0 NOTYPE GLOBAL 0 UND swap

Num: Value Size Type Bind Ot Ndx Name

8: 0 4 OBJECT GLOBAL 0 3 bufp0

9: 0 0 NOTYPE GLOBAL 0 UND buf

10: 0 39 FUNC GLOBAL 0 1 swap

11: 4 4 OBJECT GLOBAL 0 COM bufp1

alignment

ELF Symbol Tables P547

Page 34: Linking  Ⅱ

34

7.6 Symbol Resolution

Page 35: Linking  Ⅱ

35

Symbol Resolution P549

1. void foo(void)2. 3. int main()4. {5. foo() ;6. return 0 ;7. }

Unix> gcc –Wall –O2 –o linkerror linkerror.c

/tmp/ccSz5uti.o: In function ‘main’:

/tmp/ccSz5uti.o (.text+0x7): undefined reference to ‘foo’

collect2: ld return 1 exit status

Page 36: Linking  Ⅱ

36

7.6.1 How Linkers Resolve Multiply Defined Global Symbols

Page 37: Linking  Ⅱ

37

7.6.2 Linking with Static Libraries

Page 38: Linking  Ⅱ

38

7.6.3 How Linkers Use Static Libraries to Resolve References

Page 39: Linking  Ⅱ

39

7.7 Relocation

Page 40: Linking  Ⅱ

40

Relocation

• Relocation

– Merge the input modules

– Assign runtime address to each symbol

• Two steps

– Relocating sections and symbol definitions

– Relocating symbol references within sections

Page 41: Linking  Ⅱ

41

Relocation

• For each reference to an object with

unknown location

– Assembler generates a relocation entry

– Relocation entries for code are placed

in .rel.text

– Relocation entries for data are placed

in .rel.data

Page 42: Linking  Ⅱ

42

Relocation

• Relocation Entry

typedef struct {

int offset ;

int symbol:24,

type:8 ;

} Elf32_Rel ;

Figure 7.8 P558

Page 43: Linking  Ⅱ

43

Relocation P559 1) Recolating PC-relative References

6 e8 fc ff ff ff call 7<main+0x7> swap();

There is a relocation entry in rel.txt

offset symbol type

7 swap R_386_PC32

Page 44: Linking  Ⅱ

44

Relocation 2) Relocating Absolute References

int *bufp0 = &buf[0] ;

00000000 <bufp0>:

0: 00 00 00 00

There is a relocation entry in rel.dataoffset symbol type

0 buf R_386_32

Page 45: Linking  Ⅱ

45

Relocation

• For each reference to an object with

unknown location

– Assembler generates a relocation entry

– Relocation entries for code are placed

in .rel.text

– Relocation entries for data are placed

in .rel.data

Page 46: Linking  Ⅱ

46

6 e8 fc ff ff ff call 7<main+0x7> swap();7: R_386_PC32 swap relocation entry

r.offest = 0x7r.symbol = swapr.type = R_386_PC32

ADDR(main)=ADDR(.text) = 0x80483b4ADDR(swap)=0x80483c8refaddr = ADDR(main)+r.offset = 0x80483bbADDR(r.symbol)=ADDR(swap)=0x80483c8*refptr = (unsigned) (ADDR(r.symbol) + *refptr – refaddr

= (unsigned) (0x80483c8 + (-4) – 0x80483bb) = (unsigned) 0x9

Relocation P559 1) Relocating PC-relative References

Page 47: Linking  Ⅱ

47

Relocation

int *bufp0 = &buf[0] ;

00000000 <bufp0>:0: 00 00 00 00 int *bufp0 = &buf[0];

0: R_386_32 buf relocation entryADDR(r.symbol) = ADDR(buf) = 0x8049454*refptr = (unsigned) (ADDR(r.symbol)+ *refptr)

= (unsigned) (0x8049454)

0804945c <bufp0>: 0804945c: 54 94 04 08

Page 48: Linking  Ⅱ

48

1 foreach section s {2 foreach relocation entry r {3 refptr = s + r.offset ; /* ptr to reference to be relocated */4 5 /* relocate a PC-relative reference */6 if (r.type == R_386_PC32) {7 refaddr = ADDR(s) + r.offset ; /* ref’s runtime address */8 *refptr = (unsigned) (ADDR(r.symbol) + *refptr –refaddr) ;9 }10 11 /* relocate an absolute reference */12 if ( r.type == R_386_32 )13 *refptr = (unsigned) (ADDR(r.symbol) + *refptr) ;14 }15 }

Relocation

Figure 7.9 P559

Page 49: Linking  Ⅱ

49

1 080483b4<main>:

2 08483b4:55 push %ebp

3 08483b5:89 e5 mov %esp, %ebp

4 08483b7:83 ec 08 sub $0x8, %esp

5 08483ba: e8 09 00 00 00 call 80483c8 <swap>

6 08483bf: 31 c0 xor %eax, %eax

7 08483c1: 89 ec mov %ebp, %esp

8 08483c3: 5d pop %ebp

9 08483c4: c3 ret

10 08483c5: 90 nop

11 08483c6: 90 nop

12 08483c7: 90 nop

Relocation

Page 50: Linking  Ⅱ

50

13 080483c8<swap>:

14 80483c8: 55 push %ebp

15 80483c9: 8b 15 5c 94 04 08 mov 0x804945c, %edx get *bufp0

16 80483cf: a1 58 94 04 08 mov 0x8049458, %edx get buf[1]

17 80483d4: 89 e5 mov %esp, %ebp

18 80483d6: c7 05 48 85 04 08 58 movl $0x8049458, 0x8049548

19 80483dd: 94 04 08 bufp1 = &buf[1]

20 80483e0: 89 ec mov %ebp, %esp

21 80483e2: 8b 0a mov (%edx), %ecx

22 80483e4: 80 02 mov %eax, (%edx)

23 80483e6: a1 48 95 04 08 mov 0x8049548, %eax

24 80483eb: 89 08 mov %ecx, (%eax)

25 80483ed: 5d pop %ebp

26 80483ee: c3 ret

Relocation

Figure 7.10 (a) P562

Page 51: Linking  Ⅱ

51

1 08049454 <buf>:

2 8049454: 01 00 00 00 02 00 00 00

3 084945c<bufp0>:

4 84945c: 54 94 04 08

Relocation

Figure 7.10 (b) P562

Page 52: Linking  Ⅱ

52

7.8 Executable Object Files

Page 53: Linking  Ⅱ

53

ELF header

Segment header table

.init section

.data section

.bss section

.symtab

.debug

Section header table

.line

.strtab

.rodata section

.text section

EFI object file format

Figure 7.11 P563

Page 54: Linking  Ⅱ

54

Executable Object Files

• ELF header– Overal information

– Entry point

• .init section– A small function _init

– Initialization

• Segment header table

Page 55: Linking  Ⅱ

55

7.9 Loading Executable Object Files

Page 56: Linking  Ⅱ

56

Figure 7.13 P565

Page 57: Linking  Ⅱ

57

Loading

Unix> ./p

• Loader– Memory-resident operating system code

– Invoked by call the execve function

– Copy the code and data in the executable object file from disk into memory

– Jump to the entry point

– Run the program

Page 58: Linking  Ⅱ

58

Loading P565

• Startup code– At the _start address defined in the crt1.o– Same for all C program

1. 0x080480c0<_start>:

2. call _libc_init_first

3. call _init

4. call atexit

5. call main

6. call _exit

Page 59: Linking  Ⅱ

59

7.10 Dynamic Linking with Shared Libraries

7.11 Loading and Linking Shared Libraries from Applications

7.12 *Position-Independent Code (PIC)7.13 Tools for Manipulating Object Files