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 Symbols Resolving 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-timeDynamic Linking at Run-timeCase Study: Library InterpositioningSome Interpositioning ApplicationsExample: malloc() StatisticsLinkingOctober 10, 2007LinkingOctober 10, 200715-213TopicsTopics Static linking Dynamic linking Case study: Library interpositioningclass13.ppt15-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.c unix> ./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: Modularity 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)z e.g., Math library, standard C library–5–15-213, F’07Why Linkers? (cont)Why Linkers? (cont)Reason 2: EfficiencyReason 2: Efficiency Time: Separate Compilationz Change one source file, compile, and then relink.z No need to recompile other source files. Space: Libraries z Common functions can be aggregated into a single file...z 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 resolution Programs define and reference symbols (variables and functions):z void swap() {…} /* define symbol swap */z swap(); /* reference symbol a */z int *xp = &x; /* define symbol xp, reference x */ Symbol definitions are stored (by compiler) in symbol table.z Symbol table is an array of structsz Each 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. Relocation Merges separate code and data sections into single sections Relocates 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. 1. RelocatableRelocatableobject file (object file (.o.ofile)file) Contains code and data in a form that can be combined with other relocatable object files to form executable object file.z 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.sofile)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 Unix Later adopted by BSD Unix variants and LinuxOne unified format for One unified format for Relocatable object files (.o), Executable object files Shared object files (.so)Generic name: ELF binariesGeneric name: ELF binaries–10–15-213, F’07ELF Object File FormatELF Object File FormatElf headerElf header Magic number, type (.o, exec, .so), machine, byte ordering, etc.Segment header tableSegment header table Page size, virtual addresses memory segments (sections), segment sizes..text.textsectionsection Code.data.datasectionsection Initialized global variables..bssbsssectionsection Uninitialized 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)..symtabsymtabsectionsection Symbol table Procedure and static variable names Section names and locations..rel.textrel.textsectionsection Relocation info for .text section Addresses of instructions that will need to be modified in the executable Instructions for modifying...rel.datarel.datasectionsection Relocation info for .data section Addresses of pointer data that will need to be modified in the merged executable.debug.debugsectionsection Info for symbolic debugging (gcc -g)Section header tableSection header table Offsets 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 symbols Symbols defined by module m that can be referenced by other modules. Ex: non-static C functions and non-static global variables.External symbolsExternal symbols Global symbols that are referenced by module
View Full Document