Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linking Ⅱ.

Similar presentations


Presentation on theme: "Linking Ⅱ."— Presentation transcript:

1 Linking Ⅱ

2 Outline Static linking Symbols & Symbol Table Relocation Executable Object Files Loading Suggested reading: 7.3~7.5, 7.7~7.9

3 Figure 7.1 P541 main.c swap.c /*main.c */ void swap() ;
int buf[2] = {1, 2}; Int main() { swap() ; return 0 ; } /*swap.c */ extern int buf[]; int *bufp0 = &buf[0] int *bufp1 ; void swap() int temp ; bufp1 = &buf[1]; temp = *bufp0 ; *bufp0 = *bufp1 ; *bufp1 = temp ; Figure 7.1 P541

4 Example P542 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>

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

6 (required for executables) (required for relocatables)
ELF object file format 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 Figure 7.3 P544

7 7.3 Object Files

8 Relocatable object file
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

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

10 7.2 Static Linking

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

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

13 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 */

14 7.4 Relocatable Object Files

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

16 (required for executables) (required for relocatables)
EFI object file format 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 Figure 7.3 P544

17 EFI object file format Elf header Program header table
magic number, type (.o, exec, .so), machine, byte ordering, etc. Program header table page size, virtual addresses for memory segments (sections), segment sizes.

18 EFI object file format .text section .data section .bss section code
initialized (static) data .bss section uninitialized (static) data “Block Started by Symbol” “Better Save Space” has section header but occupies no space

19 EFI object file format .symtab section .rel.text 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.

20 EFI object file format .rel.data section .debug 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)

21 EFI object file format .line: .strtab:
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.

22 7.5 Symbols and Symbol Tables

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

24 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

25 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

26 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

27 Local nonstatic program variables Local static procedure variables
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

28 (required for executables) (required for relocatables)
ELF object file format 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 Figure 7.3 P544

29 Examples int f() { static int x=1 ; return x; } int g()
x.1 and x.2 are allocated in .data

30 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

31 main.c swap.c /*main.c */ void swap() ; int buf[2] = {1, 2};
Int main() { swap() ; return 0 ; } /*swap.c */ extern int buf[]; int *bufp0 = &buf[0] int *bufp1 ; void swap() int temp ; bufp1 = &buf[1]; temp = *bufp0 ; *bufp0 = *bufp1 ; *bufp1 = temp ;

32 ELF Symbol Tables P547 ABS, UNDEF, COMMON typedef struct {
int name ; /* string table offset */ int value ; /* section offset, or VM address */ int size ; /* object size in bytes */ char type:4 , /* data, func, section, or src file name */ binding:4 ; /* local or global */ char reserved ;/* unused */ char section ; /* section header index, ABS, UNDEF, */ /* or COMMON */ } ABS, UNDEF, COMMON

33 ELF Symbol Tables P547 Num: Value Size Type Bind Ot Ndx Name
8: OBJECT GLOBAL 0 3 buf 9: FUNC GLOBAL 0 1 main 10: NOTYPE GLOBAL 0 UND swap 8: OBJECT GLOBAL 0 3 bufp0 9: NOTYPE GLOBAL 0 UND buf 10: FUNC GLOBAL 0 1 swap 11: OBJECT GLOBAL 0 COM bufp1 alignment

34 7.6 Symbol Resolution

35 Symbol Resolution P549 void foo(void) int main() { foo() ; return 0 ;
} 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

36 7.6.1 How Linkers Resolve Multiply Defined Global Symbols

37 7.6.2 Linking with Static Libraries

38 7.6.3 How Linkers Use Static Libraries to Resolve References

39 7.7 Relocation

40 Relocation Relocation Two steps Merge the input modules
Assign runtime address to each symbol Two steps Relocating sections and symbol definitions Relocating symbol references within sections

41 For each reference to an object with unknown location
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

42 Relocation Relocation Entry Figure 7.8 P558 typedef struct {
int offset ; int symbol:24, type:8 ; } Elf32_Rel ; Figure 7.8 P558

43 Relocation P559 1) Recolating PC-relative References
e8 fc ff ff ff call 7<main+0x7> swap(); There is a relocation entry in rel.txt offset symbol type 7 swap R_386_PC32

44 Relocation 2) Relocating Absolute References
int *bufp0 = &buf[0] ; <bufp0>: 0: There is a relocation entry in rel.data offset symbol type 0 buf R_386_32

45 For each reference to an object with unknown location
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

46 Relocation P559 1) Relocating PC-relative References
e8 fc ff ff ff call 7<main+0x7> swap(); 7: R_386_PC32 swap relocation entry r.offest = 0x7 r.symbol = swap r.type = R_386_PC32 ADDR(main)=ADDR(.text) = 0x80483b4 ADDR(swap)=0x80483c8 refaddr = ADDR(main)+r.offset = 0x80483bb ADDR(r.symbol)=ADDR(swap)=0x80483c8 *refptr = (unsigned) (ADDR(r.symbol) + *refptr – refaddr = (unsigned) (0x80483c8 + (-4) – 0x80483bb) = (unsigned) 0x9

47 Relocation int *bufp0 = &buf[0] ; <bufp0>: 0: int *bufp0 = &buf[0]; 0: R_386_32 buf relocation entry ADDR(r.symbol) = ADDR(buf) = 0x *refptr = (unsigned) (ADDR(r.symbol)+ *refptr) = (unsigned) (0x ) c <bufp0>: c:

48 Relocation Figure 7.9 P559 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_PC32) { 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 P559

49 Relocation 080483b4<main>: 08483b4: 55 push %ebp
08483b5: 89 e5 mov %esp, %ebp 08483b7: 83 ec 08 sub $0x8, %esp 08483ba: e call c8 <swap> 08483bf: 31 c0 xor %eax, %eax 08483c1: 89 ec mov %ebp, %esp 08483c3: 5d pop %ebp 08483c4: c3 ret 08483c5: 90 nop 08483c6: 90 nop 08483c7: 90 nop

50 Relocation Figure 7.10 (a) P562 080483c8<swap>:
80483c8: 55 push %ebp 80483c9: 8b 15 5c mov 0x804945c, %edx get *bufp0 80483cf: a mov 0x , %edx get buf[1] 80483d4: 89 e5 mov %esp, %ebp 80483d6: c movl $0x , 0x 80483dd: bufp1 = &buf[1] 80483e0: 89 ec mov %ebp, %esp 80483e2: 8b 0a mov (%edx), %ecx 80483e4: mov %eax, (%edx) 80483e6: a mov 0x , %eax 80483eb: mov %ecx, (%eax) 80483ed: 5d pop %ebp 80483ee: c3 ret Figure 7.10 (a) P562

51 Relocation Figure 7.10 (b) P562 08049454 <buf>:
: 084945c<bufp0>: 84945c: Figure 7.10 (b) P562

52 7.8 Executable Object Files

53 EFI object file format Figure 7.11 P563 ELF header
Segment header table .init section .data section .bss section .symtab .debug Section header table .line .strtab .rodata section .text section Figure P563

54 Executable Object Files
ELF header Overal information Entry point .init section A small function _init Initialization Segment header table

55 7.9 Loading Executable Object Files

56 Figure P565

57 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

58 Loading P565 Startup code At the _start address defined in the crt1.o
Same for all C program 0x080480c0<_start>: call _libc_init_first call _init call atexit call main call _exit

59 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


Download ppt "Linking Ⅱ."

Similar presentations


Ads by Google