Linking‖l
1 Linking Ⅱ
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 <similar process for swap ld -o p [system ob] files] /tmp/main. o/ tmp/swap.o unix>
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
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