Outline Static linking Symbols Symbol Table Relocation Executable Object Files Loading Suggested reading: 7.3-7.5, 7.7-7.9
2 Outline • Static linking • Symbols & Symbol Table • Relocation • Executable Object Files • Loading • Suggested reading: 7.3~7.5, 7.7~7.9
main. c swap c 1. /main. c * swap c void swapO 123 extern int bufa: 23456789 nt buf[2]={1,2} 4. int bufpo &buf[o] 5. int "bufp1 Int maino 6 7. void swapO swapo eturn o If temp 10.} 10. 11 bufp1 =&buf[1] 12 remp =*bufo 13 bufo = bufp1 14 bufpl temp Figure 7.1 P541
3 main.c swap.c 1. /*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
Example P542 unix> gCc -O2 -g -o p main. c swapc cpp [args] main. c/tmp/main.i ccl /tmp/main. i main. c -o2 [args] -o /tmp/main. s as [args] -o /tmp/main. o /tmp/main.s
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 ld -o p [system obj files] /tmp/main.o /tmp/swap.o unix> Example P542
Obiect 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 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
ELF object file format ElF header Program header table (required for executables) text section data section bss section symtab rel. txt rel.data debue g line strap Section header table (required for relocatables) Figure 7.3 P544
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
Obiect files Relocatable object file Contain binary code and data in a form that can be combined with other relocatable ob ject 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 • 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
Obiect files Shared ob Ject file e A special type of relocatable object file that can be loaded into memory and linked dynamically,at either load time or run time
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