Linking October 10, 2007Example C ProgramStatic LinkingWhy Linkers?Why Linkers? (cont)What Do Linkers Do?What Do Linkers Do? (cont)Three Kinds of Object Files (Modules)Executable and Linkable Format (ELF)ELF Object File FormatELF Object File Format (cont)Linker SymbolsResolving SymbolsRelocating Code and Datamain.o Relocation Infoswap.o Relocation Info (.text)swap.o Relocation Info (.data)Executable After Relocation (.text)Executable After Relocation (.data)Strong and Weak SymbolsLinker’s Symbol RulesLinker PuzzlesPackaging Commonly Used FunctionsStatic LibrariesCreating Static LibrariesCommonly Used LibrariesLinking with Static LibrariesUsing Static LibrariesLoading Executable Object FilesShared LibrariesShared Libraries (cont)Dynamic Linking at Load-timeDynamic Linking at Run-timeSlide 34Case Study: Library InterpositioningSome Interpositioning ApplicationsExample: malloc() StatisticsLinkingOctober 10, 2007LinkingOctober 10, 2007TopicsTopicsStatic linkingDynamic linkingCase study: Library interpositioningclass13.ppt15-21315-213, F’07– 2 –15-213, F’07Example C ProgramExample C Programint buf[2] = {1, 2}; int main() { swap(); return 0;} main.c swap.cextern int buf[]; static int *bufp0 = &buf[0];static int *bufp1;void swap(){ int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp;}– 3 –15-213, F’07Static LinkingStatic LinkingPrograms are translated and linked using a Programs are translated and linked using a compiler drivercompiler driver::unix> gcc -O2 -g -o p main.c swap.cunix> ./pLinker (ld)Translators(cpp, cc1, as)main.cmain.oTranslators(cpp, cc1, as)swap.cswap.opSource filesSeparately compiledrelocatable object filesFully linked executable object file(contains code and data for all functionsdefined in main.c and swap.c– 4 –15-213, F’07Why Linkers?Why Linkers?Reason 1: ModularityReason 1: ModularityProgram 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– 5 –15-213, F’07Why Linkers? (cont)Why Linkers? (cont)Reason 2: EfficiencyReason 2: EfficiencyTime: Separate CompilationChange one source file, compile, and then relink.No need to recompile other source files.Space: Libraries 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.– 6 –15-213, F’07What Do Linkers Do?What Do Linkers Do?Step 1. Symbol resolutionStep 1. Symbol resolutionPrograms define and reference symbols (variables and functions):void swap() {…} /* define symbol swap */swap(); /* reference symbol a */int *xp = &x; /* define symbol xp, reference x */Symbol definitions are stored (by compiler) in symbol table.Symbol table is an array of structsEach entry includes name, size, and location of symbol.Linker associates each symbol reference with exactly one symbol definition.– 7 –15-213, F’07What Do Linkers Do? (cont)What Do Linkers Do? (cont)Step 2. RelocationStep 2. RelocationMerges separate code and data sections into single sectionsRelocates symbols from their relative locations in the .o files to their final absolute memory locations in the executable.Updates all references to these symbols to reflect their new positions.– 8 –15-213, F’07Three Kinds of Object Files (Modules)Three Kinds of Object Files (Modules)1. Relocatable object file (1. Relocatable object file (.o.o file) file)Contains code and data in a form that can be combined with other relocatable object files to form executable object file.Each .o file is produced from exactly one source (.c) file2. Executable object file 2. Executable object file Contains code and data in a form that can be copied directly into memory and then executed.3. Shared object file (3. Shared object file (.so.so file) file)Special type of relocatable object file that can be loaded into memory and linked dynamically, at either load time or run-time.Called Dynamic Link Libraries (DLLs) by Windows– 9 –15-213, F’07Executable and Linkable Format (ELF)Executable and Linkable Format (ELF)Standard binary format for object filesStandard binary format for object filesOriginally proposed by AT&T System V UnixOriginally proposed by AT&T System V UnixLater adopted by BSD Unix variants and LinuxOne unified format for One unified format for Relocatable object files (.o), Executable object filesShared object files (.so)Generic name: ELF binariesGeneric name: ELF binaries– 10 –15-213, F’07ELF Object File FormatELF Object File FormatElf headerElf headerMagic number, type (.o, exec, .so), machine, byte ordering, etc.Segment header tableSegment header tablePage size, virtual addresses memory segments (sections), segment sizes..text.text section sectionCode.data.data section sectionInitialized global variables.bss.bss section sectionUninitialized global variables“Block Started by Symbol”“Better Save Space”Has section header but occupies no spaceELF headerSegment header table(required for executables).text section.data section.bss section.symtab section.rel.txt section.rel.data section.debug sectionSection header table0– 11 –15-213, F’07ELF Object File Format (cont)ELF Object File Format (cont).symtab.symtab section sectionSymbol tableProcedure and static variable namesSection names and locations.rel.text.rel.text section sectionRelocation info for .text sectionAddresses of instructions that will need to be modified in the executableInstructions for modifying..rel.data.rel.data section sectionRelocation info for .data sectionAddresses of pointer data that will need to be modified in the merged executable.debug.debug section sectionInfo for symbolic debugging (gcc -g)Section header tableSection header tableOffsets and sizes of each sectionELF headerSegment header table(required for executables).text section.data section.bss section.symtab section.rel.text section.rel.data section.debug sectionSection header table0– 12 –15-213, F’07Linker SymbolsLinker SymbolsGlobal symbolsGlobal symbolsSymbols defined by module m that can be referenced by other modules.Ex: non-static C functions and non-static global variables.External symbolsExternal symbolsGlobal symbols that are referenced by
View Full Document