1 / 59

Linking Ⅱ

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

sierra-roth
Download Presentation

Linking Ⅱ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linking Ⅱ

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

  3. Figure 7.1 P541

  4. Example P542 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>

  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

  6. ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.txt .rel.data .debug .line .strtab Section header table (required for relocatables) ELF object file format Figure 7.3 P544

  7. 7.3 Object Files

  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

  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

  10. 7.2 Static 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

  12. Static linking • Symbol resolution • resolves external references. • external reference: reference to a symbol defined in another object file

  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 */

  14. 7.4 Relocatable Object Files

  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.

  16. ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.txt .rel.data .debug .line .strtab Section header table (required for relocatables) EFI object file format Figure 7.3 P544

  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.

  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

  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.

  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)

  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.

  22. 7.5 Symbols and Symbol Tables

  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

  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

  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

  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

  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

  28. ELF header Program header table (required for executables) .text section .data section .bss section .symtab .rel.txt .rel.data .debug .line .strtab Section header table (required for relocatables) ELF object file format Figure 7.3 P544

  29. Examples • int f() • { • static int x=1 ; • return x; • } • int g() • { • static int x = 1; • return x ; • } • x.1 and x.2 are allocated in .data

  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

  31. ELF Symbol Tables P547 • typedef struct { • int name ; /* string table offset */ • int value ; /* section offset, or VM address */ • int size ; /* object size in bytes */ • char type:4 , /* data, func, section, or src file name */ • binding:4 ; /* local or global */ • char reserved ;/* unused */ • char section ; /* section header index, ABS, UNDEF, */ • /* or COMMON */ • } • ABS, UNDEF, COMMON

  32. ELF Symbol Tables P547 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

  33. 7.6 Symbol Resolution

  34. Symbol Resolution P549 • void foo(void) • int main() • { • foo() ; • return 0 ; • } 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

  35. 7.6.1 How Linkers Resolve Multiply Defined Global Symbols

  36. 7.6.2 Linking with Static Libraries

  37. 7.6.3 How Linkers Use Static Libraries to Resolve References

  38. 7.7 Relocation

  39. Relocation • Relocation • Merge the input modules • Assign runtime address to each symbol • Two steps • Relocating sections and symbol definitions • Relocating symbol references within sections

  40. 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

  41. Relocation • Relocation Entry typedef struct { int offset ; int symbol:24, type:8 ; } Elf32_Rel ; Figure 7.8 P558

  42. Relocation P5591) Recolating PC-relative References • 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

  43. Relocation 2) Relocating Absolute References int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 There is a relocation entry in rel.data offset symbol type 0 buf R_386_32

  44. 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

  45. Relocation P5591) Relocating PC-relative References • e8 fc ff ff ff call 7<main+0x7> swap(); 7: R_386_PC32 swap relocation entry r.offest = 0x7 r.symbol = swap r.type = R_386_PC32 ADDR(main)=ADDR(.text) = 0x80483b4 ADDR(swap)=0x80483c8 refaddr = ADDR(main)+r.offset = 0x80483bb ADDR(r.symbol)=ADDR(swap)=0x80483c8 *refptr = (unsigned) (ADDR(r.symbol) + *refptr – refaddr = (unsigned) (0x80483c8 + (-4) – 0x80483bb) = (unsigned) 0x9

  46. Relocation int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 int *bufp0 = &buf[0]; 0: R_386_32 buf relocation entry ADDR(r.symbol) = ADDR(buf) = 0x8049454 *refptr = (unsigned) (ADDR(r.symbol)+ *refptr) = (unsigned) (0x8049454) 0804945c <bufp0>: 0804945c: 54 94 04 08

  47. Relocation • foreach section s { • foreach relocation entry r { • refptr = s + r.offset ; /* ptr to reference to be relocated */ • /* relocate a PC-relative reference */ • if (r.type == R_386_PC32) { • refaddr = ADDR(s) + r.offset ; /* ref’s runtime address */ • *refptr = (unsigned) (ADDR(r.symbol) + *refptr –refaddr) ; • } • /* relocate an absolute reference */ • if ( r.type == R_386_32 ) • *refptr = (unsigned) (ADDR(r.symbol) + *refptr) ; • } • } Figure 7.9 P559

  48. Relocation • 080483b4<main>: • 08483b4: 55 push %ebp • 08483b5: 89 e5 mov %esp, %ebp • 08483b7: 83 ec 08 sub $0x8, %esp • 08483ba: e8 09 00 00 00 call 80483c8 <swap> • 08483bf: 31 c0 xor %eax, %eax • 08483c1: 89 ec mov %ebp, %esp • 08483c3: 5d pop %ebp • 08483c4: c3 ret • 08483c5: 90 nop • 08483c6: 90 nop • 08483c7: 90 nop

  49. Relocation • 080483c8<swap>: • 80483c8: 55 push %ebp • 80483c9: 8b 15 5c 94 04 08 mov 0x804945c, %edx get *bufp0 • 80483cf: a1 58 94 04 08 mov 0x8049458, %edx get buf[1] • 80483d4: 89 e5 mov %esp, %ebp • 80483d6: c7 05 48 85 04 08 58 movl $0x8049458, 0x8049548 • 80483dd: 94 04 08 bufp1 = &buf[1] • 80483e0: 89 ec mov %ebp, %esp • 80483e2: 8b 0a mov (%edx), %ecx • 80483e4: 80 02 mov %eax, (%edx) • 80483e6: a1 48 95 04 08 mov 0x8049548, %eax • 80483eb: 89 08 mov %ecx, (%eax) • 80483ed: 5d pop %ebp • 80483ee: c3 ret Figure 7.10 (a) P562

More Related