Example C Program 15 213 The course that gives CMU its Zip Linking March 2 2004 Topics n Static linking n Dynamic linking n Case study Library interpositioning main c swap c int buf 2 1 2 extern int buf int main swap return 0 int bufp0 buf 0 int bufp1 void swap int temp bufp1 buf 1 temp bufp0 bufp0 bufp1 bufp1 temp Static Linking Why Linkers Programs are translated and linked using a compiler driver driver n n Modularity unix gcc O2 g o p main c swap c unix p main c swap c Translators cpp cc1 as swap o Program can be written as a collection of smaller source files rather than one monolithic mass n Can build libraries of common functions more on this later l e g Math library standard C library Efficiency n Time l Change one source file compile and then relink l No need to recompile other source files Separately compiled relocatable object files n Space l Libraries of common functions can be aggregated into a single Linker ld p n Source files Translators cpp cc1 as main o 3 15 213 S 04 2 class15 ppt file l Yet executable files and running memory images contain only Fully linked executable object file contains code and data for all functions defined in main c and swap c code for the functions they actually use 15 213 S 04 4 Page 15 213 S 04 What Does a Linker Do Object Files Modules Symbol resolution Relocatable object file o o file n Programs define and reference symbols variables and functions l code a l data int xp x n n reference to symbol a reference to symbol x Executable object file Symbol definitions are stored by compiler in symbol table n l Each entry includes name size and location of object n Linker associates each symbol reference with exactly one symbol definition n n 5 Merges separate code and data sections into single sections 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 15 213 S 04 n 15 213 S 04 n Page size virtual addresses memory segments sections segment sizes text section n Code data section n Initialized static data bss section Generic name ELF binaries n n n n 7 Magic number type o exec so machine byte ordering etc Program header table One unified format for Executable object files Shared object files so Called Dynamic Link Libraries DLLs by Windows n Later adopted by BSD Unix variants and Linux n n Elf header Originally proposed by AT T System V Unix Relocatable object files o Special type of relocatable object file that can be loaded into memory and linked dynamically at either load time or runtime ELF Object File Format Standard binary format for object files n n 6 Executable and Linkable Format ELF n Contains code and data in a form that can be copied directly into memory and then executed Shared object file so so file Relocation n Contains code and data in a form that can be combined with other relocatable object files at compile time to form an executable 15 213 S 04 8 Page Uninitialized static data Block Started by Symbol Better Save Space Has section header but occupies no space 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 15 213 S 04 0 ELF Object File Format cont symtab section n n n ELF header Symbol table Procedure and static variable names Section names and locations n n n n Relocation info for data section Addresses of pointer data that will need to be modified in the merged executable n bss section symtab n rel data debug n Section header table required for relocatables Symbols that are defined and referenced exclusively by module m Ex C functions and variables that are defined with the static attribute Key Point Local linker symbols are not local program variables 15 213 S 04 9 15 213 S 04 10 Relocating Symbol Definitions and References Resolving Symbols Ref to external symbol buf Def of global symbol buf Relocatable Object Files Executable Object File swap c main c int buf 2 1 2 extern int buf static int bufp0 buf 0 static int bufp1 int main swap return 0 void swap int temp Linker knows nothing of temp m o bufp1 buf 1 temp bufp0 bufp0 bufp1 bufp1 temp Def of local symbol bufp0 system code text system data data 0 headers system code main a o 11 Global symbols that are referenced by module m but defined by some other module Local symbols rel text Info for symbolic debugging gcc g Ref to external symbol swap Symbols defined by module m that can be referenced by other modules Ex non static C functions and non static global variables External symbols data section Relocation info for text section Addresses of instructions that will need to be modified in the executable Instructions for modifying debug section n n text section rel data rel data section n Global symbols 0 Program header table required for executables rel text rel text section n Linker Symbols 15 213 S 04 12 Page main text int e 7 data a text int ep e int x 15 int y data bss text a more system code system data int e 7 int ep e int x 15 uninitialized data symtab debug data bss 15 213 S 04 main o Relocation Info int buf 2 1 2 int main swap return 0 0000000 main 0 55 1 89 e5 3 83 ec 08 6 e8 fc ff ff ff b d f 10 31 c0 89 ec 5d c3 swap o Relocation Info text push ebp mov esp ebp sub 0x8 esp call 7 main 0x7 7 R 386 PC32 swap xor eax eax mov ebp esp pop ebp ret extern int buf Disassembly of section text static int bufp0 buf 0 static int bufp1 00000000 swap 0 55 1 8b 15 00 00 00 00 7 a1 void swap int temp c 89 e c7 15 00 bufp1 buf 1 temp bufp0 bufp0 bufp1 bufp1 temp Disassembly of section data 00000000 buf 0 01 00 00 00 02 00 00 00 15 213 S 04 Disassembly of section data static int bufp0 buf 0 static int bufp1 00000000 bufp0 0 00 00 00 00 Executable After Relocation text 080483b4 main 80483b4 55 80483b5 89 80483b7 83 80483ba e8 80483bf 31 80483c1 89 80483c3 5d 80483c4 c3 080483c8 swap 80483c8 55 80483c9 8b 80483cf a1 80483d4 89 80483d6 c7 80483dd 94 80483e0 89 80483e2 8b 80483e4 89 80483e6 a1 80483eb 89 80483ed 5d 80483ee c3 0 R 386 32 buf void swap int temp bufp1 buf 1 temp bufp0 bufp0 bufp1 bufp1 temp 15 15 213 S 04 14 a o Relocation Info data extern int buf 89 8b 89 a1 23 89 25 5d 26 c3 Source objdump 13 18 1a 1c 1e push ebp …
View Full Document