Linking Ⅱ
Outline Static linking Symbols & Symbol Table Relocation Executable Object Files Loading Suggested reading: 7.3~7.5, 7.7~7.9
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
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>
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
(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.3 Object Files
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
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
7.2 Static Linking
Static linking Input Output A relocatable object files and command line arguments Output Fully linked executable object file that can be loaded and run
Static linking Symbol resolution resolves external references. external reference: reference to a symbol defined in another object file
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 */
7.4 Relocatable Object Files
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.
(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
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.
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
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.
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)
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.
7.5 Symbols and Symbol Tables
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
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
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
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
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
(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
Examples int f() { static int x=1 ; return x; } int g() x.1 and x.2 are allocated in .data
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
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 ;
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
ELF Symbol Tables P547 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 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
7.6 Symbol Resolution
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
7.6.1 How Linkers Resolve Multiply Defined Global Symbols
7.6.2 Linking with Static Libraries
7.6.3 How Linkers Use Static Libraries to Resolve References
7.7 Relocation
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
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
Relocation Relocation Entry Figure 7.8 P558 typedef struct { int offset ; int symbol:24, type:8 ; } Elf32_Rel ; Figure 7.8 P558
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
Relocation 2) Relocating Absolute References int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 There is a relocation entry in rel.data offset symbol type 0 buf R_386_32
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
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
Relocation int *bufp0 = &buf[0] ; 00000000 <bufp0>: 0: 00 00 00 00 int *bufp0 = &buf[0]; 0: R_386_32 buf relocation entry ADDR(r.symbol) = ADDR(buf) = 0x8049454 *refptr = (unsigned) (ADDR(r.symbol)+ *refptr) = (unsigned) (0x8049454) 0804945c <bufp0>: 0804945c: 54 94 04 08
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
Relocation 080483b4<main>: 08483b4: 55 push %ebp 08483b5: 89 e5 mov %esp, %ebp 08483b7: 83 ec 08 sub $0x8, %esp 08483ba: e8 09 00 00 00 call 80483c8 <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
Relocation Figure 7.10 (a) P562 080483c8<swap>: 80483c8: 55 push %ebp 80483c9: 8b 15 5c 94 04 08 mov 0x804945c, %edx get *bufp0 80483cf: a1 58 94 04 08 mov 0x8049458, %edx get buf[1] 80483d4: 89 e5 mov %esp, %ebp 80483d6: c7 05 48 85 04 08 58 movl $0x8049458, 0x8049548 80483dd: 94 04 08 bufp1 = &buf[1] 80483e0: 89 ec mov %ebp, %esp 80483e2: 8b 0a mov (%edx), %ecx 80483e4: 80 02 mov %eax, (%edx) 80483e6: a1 48 95 04 08 mov 0x8049548, %eax 80483eb: 89 08 mov %ecx, (%eax) 80483ed: 5d pop %ebp 80483ee: c3 ret Figure 7.10 (a) P562
Relocation Figure 7.10 (b) P562 08049454 <buf>: 8049454: 01 00 00 00 02 00 00 00 084945c<bufp0>: 84945c: 54 94 04 08 Figure 7.10 (b) P562
7.8 Executable Object Files
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 7.11 P563
Executable Object Files ELF header Overal information Entry point .init section A small function _init Initialization Segment header table
7.9 Loading Executable Object Files
Figure 7.13 P565
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
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
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