20 - Linking 1 Outline SymbolTable SymbolResolution...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Linking
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Symbol Table Symbol Resolution Relocation Executable Object Files Loading Dynamic Linking Suggested reading: 7.1~7.11
Background image of page 2
3 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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
Background image of page 4
5 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. 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. }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 ELF Symbol Tables 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
Background image of page 6
7 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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Symbol Resolution 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
Background image of page 8
9 Multiply Defined Global Symbols Strong: Functions and initialized global variables Weak: Uninitialized global variables Rules: Multiple strong symbols are not allowed Given a strong symbol and multiple weak symbols, choose the strong  symbol Given multiple weak symbols, choose any of the weak symbol
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Multiply Defined Global Symbols 1. /*bar1.c*/ 2. int main() 3. { 4. return 0; 5. } 1. /*foo2.c*/ 2. int x=15213; 3. 4. int main() 5. { 6. return 0; 7. } 1. /*bar2.c*/ 2. int x=15213; 3. 4. void f() 5. { 6. } 1. /*foo1.c*/ 2. int main() 3. { 4. return 0; 5. }
Background image of page 10
1. /*foo3.c*/ 2. #include <stdio.h> 3. void f(); 4. 5. int x=15213; 6. 7. int main() 8. { 9. f(); 10. printf(“x=%d\n”,x) 11. return 0; 12. } 1. /*bar3.c*/ 2. int x ; 3. 4.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

20 - Linking 1 Outline SymbolTable SymbolResolution...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online