Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.

Similar presentations


Presentation on theme: "1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5."— Presentation transcript:

1 1 Linking

2 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5

3 3 Monolithic source file Problems: efficiency: small change requires complete recompilation modularity: hard to share common functions (e.g. printf) Translator main.c p ASCII source file binary executable object file (memory image on disk)

4 4 main.cswap.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. int bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

5 5 Linker (ld) Translators main.c main.o Translators swap.c swap.o p separately compiled relocatable object files executable object file (contains code and data for all functions defined in main.c and swap.c) Separate Compilation

6 6 What is linker Linking is the process of –collecting and combining various pieces of code and data into a single executable file Executable file –Can be loaded (copied) into memory and executed.

7 7 What is linker Linking can be performed –at compile time, when the source code is translated into machine code by the linker –at load time, when the program is loaded into memory and executed by the loader –at run time, by application programs.

8 8 main.cswap.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. int bufp1 = &buf[1]; 12. temp = *bufp0 ; 13. *bufp0 = *bufp1 ; 14. *bufp1 = temp ; 15.}

9 9 Example Two functions –main() and swap() Three global variables –buf, bufp0 which are initialized explicitly – bufp1 implicitly initialized to 0

10 10 Compiler Drivers Coordinates all steps in the translation and linking process –Typically included with each compilation system (e.g., gcc) –Invokes preprocessor (cpp) compiler (cc1) assembler (as), linker (ld). –Passes command line args to appropriate phases

11 11 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

12 12 Static linking Input –A relocatable object files and command line arguments Output –Fully linked executable object file that can be loaded and run

13 13 Static linking 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

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

15 15 Code Examples C code int accum = 0; int sum(int x, int y) { int t = x+y; accum += t; return t; } _sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax addl %eax, accum movl %ebp,%esp popl %ebp ret Obtain with command gcc –O2 -S code.c Assembly file code.s

16 16 Code Examples 55 89 e5 8b 45 0c 03 45 08 01 05 00 00 00 00 89 ec 5d c3 Obtain with command gcc –O2 -c code.c Relocatable object file code.o

17 17 Code Examples Obtain with command objdump -d code.o Disassembly output 0x80483b4 : 0x80483b4 55 0x80483b5 89 e5 0x80483b7 8b 45 0c 0x80483ba 03 45 08 0x80483bd 01 05 00 00 00 00 0x80483c3 89 ec 0x80483c5 5d 0x80483c6 c3 push %ebp mov %esp,%ebp mov 0xc(%ebp),%eax add 0x8(%ebp),%eax add %eax, 0x0 mov %ebp,%esp pop %ebp ret

18 18 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: swap(); /* ref to symbol swap */ –data: int *bufp0=&buf[0]; /* ref to symbol buf */

19 19 1080483b4 : 2080483b4: 55push %ebp 3080483b5: 89 e5mov %esp, %ebp 4080483b7: 83 ec 08sub $0x8, %esp 5080483ba: e8 09 00 00 00call 80483c8 6080483bf: 31 c0xor %eax, %eax 7080483c1: 89 ecmov %ebp, %esp 8080483c3: 5dpop %ebp 9080483c4: c3ret 10080483c5: 90nop 11080483c6: 90nop 12080483c7: 90nop Relocation

20 20 13080483c8 : 14 80483c8:55 push%ebp 15 80483c9: 8b 15 5c 94 04 08 mov0x804945c, %edx get *bufp0 16 80483cf: a1 58 94 04 08 mov0x8049458, %edx get buf[1] 17 80483d4:89 e5 mov%esp, %ebp 18 80483d6:c7 05 48 95 04 08 movl $0x8049458, 0x8049548 19 80483de: 58 94 04 08 bufp1 = &buf[1] 20 80483e0:89 ec mov%ebp, %esp 21 80483e2:8b 0a mov (%edx), %ecx 22 80483e4:80 02 mov%eax, (%edx) 23 80483e6:a1 48 95 04 08 mov0x8049548, %eax 24 80483eb:89 08 mov%ecx, (%eax) 25 80483ed:5d pop%ebp 26 80483ee:c3 ret Relocation

21 21 1 08049454 : 2 8049454:01 00 00 00 02 00 00 00 3 0804945c : 4 804945c:54 94 04 08 Relocation

22 22 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

23 23 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

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

25 25 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

26 26 EFI object file format Elf header –magic number, type (.o, exec,.so), machine, byte ordering, etc. Section header table

27 27 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

28 28 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

29 29 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)

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

31 31 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 EFI object file format

32 32 Symbols Three kinds of symbols –Defined global symbols –Referenced global symbols –Local symbols

33 33 Symbols 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

34 34 Symbols Referenced global symbols –Referenced by module m but defined by some other module C functions and variables that are defined in other modules Local symbols –Defined and referenced exclusively by module m. C functions and global variables with static attribute

35 35 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

36 36 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

37 37 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

38 38 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

39 39 extern int buf[]; int *bufp0 = &buf[0]; static int *bufp1; static void incr() { static int count=0; count++; } Example (Symbol Table)

40 40.file"symbol.c".globl _bufp0.data.align 4 _bufp0:.long_buf.lcomm _count.1181,16 Example (Symbol Table)

41 41.text.def_incr;.scl3;.type32;.endef _incr: pushl%ebp movl%esp, %ebp movl_count.1181, %eax addl$1, %eax movl%eax, _count.1181 popl%ebp ret.lcomm _bufp1,16 Example (Symbol Table)

42 42 void swap() { int temp; incr(); bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } Example (Symbol Table)

43 43 the module that defines the symbol –swap.o –main.o the symbol type –local –global –extern the section –.text –.data –.bss Example (Symbol Table)

44 44 main.cswap.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.}

45 45 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

46 46 Num:ValueSizeType BindOtNdxName 8: 0 8 OBJECT GLOBAL03buf 9: 0 17 FUNC GLOBAL01main 10: 0 0 NOTYPE GLOBAL0UNDswap Num:ValueSizeType BindOtNdxName 8: 0 4 OBJECT GLOBAL03bufp0 9: 0 0 NOTYPE GLOBAL0UNDbuf 10: 0 39 FUNC GLOBAL01swap 11: 4 4 OBJECT GLOBAL0COMbufp1 alignment ELF Symbol Tables


Download ppt "1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5."

Similar presentations


Ads by Google