Linking Oct. 9, 2003Example C ProgramStatic LinkingWhy Linkers?What Does a Linker Do?Object Files (Modules)Executable and Linkable Format (ELF)ELF Object File FormatELF Object File Format (cont)Linker SymbolsResolving SymbolsRelocating Symbol Definitions and Referencesmain.o Relocation Infoswap.o Relocation Info (.text)a.o Relocation Info (.data)Executable After Relocation (.text)Executable After Relocation (.data)Strong and Weak SymbolsLinker’s Symbol RulesLinker PuzzlesPackaging Commonly FunctionsCreating Static LibrariesCommonly Used LibrariesLinking with Static LibrariesUsing Static LibrariesLoading Executable Object FilesShared LibrariesShared Libraries (cont)Dynamic Linking at Load-timeDynamic Linking at Run-timeSlide 31Some Interpositioning ApplicationsCase Study: Library InterpositioningLinkingOct. 9, 2003LinkingOct. 9, 2003TopicsTopicsStatic linkingDynamic linkingCase study: Library interpositioningclass14.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’03Example C ProgramExample C Programint buf[2] = (1,2) int main() { swap(); return 0;} main.c swap.cextern int buf[]; int *bufp0 = &buf[0];int *bufp1;void swap(){ int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp;}– 3 –15-213, F’03Static 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’03Why Linkers?Why Linkers?ModularityModularityProgram 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 libraryEfficiencyEfficiencyTime: 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.– 5 –15-213, F’03What Does a Linker Do?What Does a Linker Do?Symbol resolutionSymbol resolutionPrograms define and reference symbols (variables and functions):code: a(); /* reference to symbol a */data: int *xp=&x; /* reference to symbol x */Symbol definitions are stored (by compiler) in symbol table.Each entry includes name, size, and location of object.Linker associates each symbol reference with exactly one symbol definition.RelocationRelocationMerges separate code and data sections into single sectionsRelocates 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.– 6 –15-213, F’03Object Files (Modules)Object Files (Modules)Relocatable object file (Relocatable object file (.o.o file) file)Contains code and data in a form that can be combined with other relocatable object files at compile time to form an executable.Executable object file Executable object file Contains code and data in a form that can be copied directly into memory and then executed.Shared object file (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– 7 –15-213, F’03Executable and Linkable Format (ELF)Executable and Linkable Format (ELF)Standard binary format for object filesStandard binary format for object filesDerives from AT&T System V UnixDerives from 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– 8 –15-213, F’03ELF Object File FormatELF Object File FormatElf headerElf headerMagic number, type (.o, exec, .so), machine, byte ordering, etc.Program header tableProgram header tablePage size, virtual addresses memory segments (sections), segment sizes..text.text section sectionCode.data.data section sectionInitialized (static) data.bss.bss section sectionUninitialized (static) data“Block Started by Symbol”“Better Save Space”Has section header but occupies no spaceELF headerProgram header table(required for executables).text section.data section.bss section.symtab.rel.txt.rel.data.debugSection header table(required for relocatables)0– 9 –15-213, F’03ELF 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)ELF headerProgram header table(required for executables).text section.data section.bss section.symtab.rel.text.rel.data.debugSection header table(required for relocatables)0– 10 –15-213, F’03Linker 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 module m but defined by some other module.Local symbolsLocal symbolsSymbols 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 Key Point: Local linker symbols are notnot local program local program variablesvariables– 11 –15-213, F’03Resolving SymbolsResolving Symbolsint buf[2] = (1,2) int main() { swap(); return 0;} main.cswap.cextern int buf[]; static int *bufp0 = &buf[0];static int *bufp1;void swap(){ int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp;}Def of
View Full Document