Linking 1 Outline Static linking Symbols Symbol Table
Linking Ⅱ 1
Outline • • • Static linking Symbols & Symbol Table Relocation Executable Object Files Loading Suggested reading: 7. 3~7. 5, 7. 7~7. 9 2
main. c swap. c 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. /*main. c */ void swap() ; int buf[2] = {1, 2}; Int main() { swap() ; return 0 ; } Figure 7. 1 P 541 /*swap. c */ extern int buf[]; int *bufp 0 = &buf[0] int *bufp 1 ; void swap() { int temp ; bufp 1 = &buf[1]; temp = *bufp 0 ; *bufp 0 = *bufp 1 ; *bufp 1 = temp ; } 3
Example P 542 unix> gcc -O 2 -g -o p main. c swap. c cpp [args] main. c /tmp/main. i cc 1 /tmp/main. i main. c -O 2 [args] -o /tmp/main. s as [args] -o /tmp/main. s <similar process for swap. c> ld -o p [system obj files] /tmp/main. o /tmp/swap. o unix> 4
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 5
ELF object file format 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) Figure 7. 3 P 544 6
7. 3 Object Files 7
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 8
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 9
7. 2 Static Linking 10
Static linking • Input – A relocatable object files and command line arguments • Output – Fully linked executable object file that can be loaded and run 11
Static linking • Symbol resolution – resolves external references. • external reference: reference to a symbol defined in another object file 12
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 */ 13
7. 4 Relocatable Object Files 14
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. 15
EFI object file format 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) Figure 7. 3 P 544 16
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. 17
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 18
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. 19
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) 20
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. 21
7. 5 Symbols and Symbol Tables 22
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 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 24
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 25
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 26
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 27
ELF object file format 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) Figure 7. 3 P 544 28
Examples 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. • 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 29
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 30
main. c swap. c 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. /*main. c */ void swap() ; int buf[2] = {1, 2}; Int main() { swap() ; return 0 ; } /*swap. c */ extern int buf[]; int *bufp 0 = &buf[0] int *bufp 1 ; void swap() { int temp ; bufp 1 = &buf[1]; temp = *bufp 0 ; *bufp 0 = *bufp 1 ; *bufp 1 = temp ; } 31
ELF Symbol Tables P 547 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 32
ELF Symbol Tables P 547 Num: 8: 9: 10: Value 0 0 0 Size 8 17 0 Type Bind OBJECT GLOBAL FUNC GLOBAL NOTYPE GLOBAL Ot 0 0 0 Ndx 3 1 UND Name buf main swap Num: 8: 9: 10: 11: Value 0 0 0 4 Size 4 0 39 4 Type Bind OBJECT GLOBAL NOTYPE GLOBAL FUNC GLOBAL OBJECT GLOBAL Ot 0 0 Ndx 3 UND 1 COM Name bufp 0 buf swap bufp 1 alignment 33
7. 6 Symbol Resolution 34
Symbol Resolution P 549 1. 2. 3. 4. 5. 6. 7. void foo(void) int main() { foo() ; return 0 ; } Unix> gcc –Wall –O 2 –o linkerror. c /tmp/cc. Sz 5 uti. o: In function ‘main’: /tmp/cc. Sz 5 uti. o (. text+0 x 7): undefined reference to ‘foo’ collect 2: 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 ; } Elf 32_Rel ; Figure 7. 8 P 558 42
Relocation P 559 1) 6 e 8 fc ff ff ff call Recolating PC-relative References 7<main+0 x 7> swap(); There is a relocation entry in rel. txt offset symbol type 7 swap R_386_PC 32 43
Relocation 2) Relocating Absolute References int *bufp 0 = &buf[0] ; 0000 <bufp 0>: 0: 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 P 559 1) 6 e 8 fc ff ff ff Relocating PC-relative References call 7<main+0 x 7> 7: R_386_PC 32 swap(); relocation entry r. offest = 0 x 7 r. symbol = swap r. type = R_386_PC 32 ADDR(main)=ADDR(. text) = 0 x 80483 b 4 ADDR(swap)=0 x 80483 c 8 refaddr = ADDR(main)+r. offset = 0 x 80483 bb ADDR(r. symbol)=ADDR(swap)=0 x 80483 c 8 *refptr = (unsigned) (ADDR(r. symbol) + *refptr – refaddr = (unsigned) (0 x 80483 c 8 + (-4) – 0 x 80483 bb) = (unsigned) 0 x 9 46
Relocation int *bufp 0 = &buf[0] ; 0000 <bufp 0>: 0: 00 00 int *bufp 0 = &buf[0]; relocation entry 0: R_386_32 buf ADDR(r. symbol) = ADDR(buf) = 0 x 8049454 *refptr = (unsigned) (ADDR(r. symbol)+ *refptr) = (unsigned) (0 x 8049454) 0804945 c <bufp 0>: 0804945 c: 54 94 04 08 47
Relocation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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_PC 32) { 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 P 559 48
Relocation 1 2 3 4 5 6 7 8 9 10 11 12 080483 b 4<main>: 08483 b 4: 55 08483 b 5: 89 e 5 08483 b 7: 83 ec 08 08483 ba: e 8 09 00 00 00 08483 bf: 31 c 0 08483 c 1: 89 ec 08483 c 3: 5 d 08483 c 4: c 3 08483 c 5: 90 08483 c 6: 90 08483 c 7: 90 push %ebp mov %esp, %ebp sub $0 x 8, %esp call 80483 c 8 <swap> xor %eax, %eax mov %ebp, %esp pop %ebp ret nop nop 49
Relocation 13 14 15 16 17 18 19 20 21 22 23 24 25 26 080483 c 8<swap>: 80483 c 8: 55 80483 c 9: 8 b 15 5 c 94 04 08 mov 80483 cf: a 1 58 94 04 08 80483 d 4: 89 e 5 80483 d 6: c 7 05 48 85 04 08 58 80483 dd: 94 04 08 80483 e 0: 89 ec 80483 e 2: 8 b 0 a 80483 e 4: 80 02 80483 e 6: a 1 48 95 04 08 80483 eb: 89 08 80483 ed: 5 d 80483 ee: c 3 Figure 7. 10 (a) P 562 push %ebp 0 x 804945 c, %edx get *bufp 0 mov 0 x 8049458, %edx get buf[1] mov %esp, %ebp movl $0 x 8049458, 0 x 8049548 bufp 1 = &buf[1] mov %ebp, %esp mov (%edx), %ecx mov %eax, (%edx) mov 0 x 8049548, %eax mov %ecx, (%eax) pop %ebp ret 50
Relocation 1 2 08049454 <buf>: 8049454: 01 00 00 00 02 00 00 00 3 4 084945 c<bufp 0>: 84945 c: 54 94 04 08 Figure 7. 10 (b) P 562 51
7. 8 Executable Object Files 52
EFI object file format ELF header Segment header table. init section. text section. rodata section. bss section. symtab. debug. line. strtab Section header table Figure 7. 11 P 563 53
Executable Object Files • ELF header – Overal information – Entry point • . init section – A small function _init – Initialization • Segment header table 54
7. 9 Loading Executable Object Files 55
56 Figure 7. 13 P 565
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 57
Loading P 565 • Startup code – At the _start address defined in the crt 1. o – Same for all C program 1. 0 x 080480 c 0<_start>: 2. 3. 4. 5. 6. call _libc_init_first call _init call atexit call main call _exit 58
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 59
- Slides: 59