Linker puzzles 15 213 int x p1 p1 int x p1 int x p2 both instances of x refer to the same uninitialized int int x int y p1 double x p2 writes to x in p2 might overwrite y Evil link time error two strong symbols p1 The course that gives CMU its Zip Linking Oct 16 2001 int x 7 int y 5 p1 Topics static linking object files static libraries loading dynamic linking of shared libraries int x 7 p1 class15 ppt writes to x in p2 will overwrite something Nasty double x p2 references to x refer to the same initialized variable int x p2 class15 ppt 2 A simplistic program translation scheme Linkers m c ASCII source file m c Translator binary executable object file memory image on disk p CS 213 F 01 a c Translators Translators m o a o separately compiled relocatable object files Linker ld p Problems efficiency small change requires complete recompilation modularity hard to share common functions e g printf Solution static linker or linker class15 ppt 3 CS 213 F 01 class15 ppt executable object file contains code and data for all functions defined in m c and a c 4 CS 213 F 01 Translating the example program What does a linker do Merges object files Compiler driver 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 and linker ld Passes command line args to appropriate phases Example create executable p from m c and a c bass gcc O2 v o p m c a c cpp args m c tmp cca07630 i cc1 tmp cca07630 i m c O2 args o tmp cca07630 s as args o tmp cca076301 o tmp cca07630 s similar process for a c ld o p system obj files tmp cca076301 o tmp cca076302 o bass class15 ppt 5 CS 213 F 01 Why linkers Program can be written as a collection of smaller source files rather than one monolithic mass Can build libraries of common functions more on this later e g math library standard C library Efficiency Time change one source file compile and then relink no need to recompile other source files Space libraries of common functions can be aggregated into a single file yet executable files and running memory images contain only code for the functions they actually use 7 Resolves external references as part of the merging process resolves external references external reference reference to a symbol defined in another object file Relocates symbols 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 because of this modifying linking is sometimes called link editing class15 ppt 6 CS 213 F 01 Executable and linkable format ELF Modularity class15 ppt merges multiple relocatable o object files into a single executable object file that can loaded and executed by the loader CS 213 F 01 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 class15 ppt 8 CS 213 F 01 ELF object file format ELF object file format symtab section Elf header ELF header magic number type o exec so machine byte ordering etc Program header table required for executables Program header table page size virtual addresses for memory segments sections segment sizes text section data section bss section text section symtab code rel txt data section rel data initialized static data debug bss section uninitialized static data Block Started by Symbol Better Save Space has section header but occupies no space class15 ppt Section header table required for relocatables 9 0 symbol table procedure and static variable names section names and locations symtab rel text rel data relocation info for data section addresses of pointer data that will need to be modified in the merged executable class15 ppt CS 213 F 01 debug Section header table required for relocatables 10 CS 213 F 01 Merging o files into an executable Relocatable object files system code text system data data bss Executable object file 0 headers system code main m o a o 11 bss section info for symbolic debugging gcc g int a return ep x y class15 ppt data section rel data section int ep e int x 15 int y int main int r a exit 0 text section relocation info for text section addresses of instructions that will need to be modified in the executable instructions for modifying debug section a c extern int e int e 7 Program header table required for executables rel text section Example C program m c 0 ELF header CS 213 F 01 main text int e 7 data a text int ep e int x 15 int y data class15 ppt text a more system code system data int e 7 int ep e int x 15 uninitialized data symtab debug bss 12 CS 213 F 01 data bss Relocating symbols and resolving external references m o relocation info m c Symbols are lexical entities that name functions and variables Each symbol has a value typically a memory address Code consists of symbol definitions and references References can be either local or external m c a c extern int e int e 7 Def of local symbol e int ep e int main int x 15 int r a int y exit 0 Def of int a local return ep x y symbol Ref to external ep symbol exit Ref to external defined in symbol a Def of Refs of local libc so local symbols e x y symbol a class15 ppt 13 int e 7 Disassembly of section text int main int r a exit 0 00000000 main 00000000 main 0 55 pushl ebp 1 89 e5 movl esp ebp 3 e8 fc ff ff ff call 4 main 0x4 4 R 386 PC32 a 8 6a 00 pushl 0x0 a e8 fc ff ff ff call b main 0xb b R 386 PC32 exit f 90 nop Ref to external symbol e Disassembly of section data Defs of local symbols x and y 00000000 e 0 07 00 00 00 source objdump class15 ppt CS 213 F 01 a o relocation info text a c extern int e Disassembly of section text int ep e int x 15 int y 00000000 a 0 55 1 8b 15 00 00 00 6 00 int a return ep x y class15 ppt 7 a1 00 00 00 00 c e 10 12 17 89 03 89 03 00 18 19 5d c3 e5 02 ec 05 00 00 00 15 pushl movl int ep e int x 15 int y ebp 0x0 edx 3 R 386 32 …
View Full Document