Page ‹#›LinkingMarch 2, 2004LinkingMarch 2, 2004TopicsTopicsn Static linkingn Dynamic linkingn Case study: Library interpositioningclass15.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, S’04Example 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, S’04Static LinkingStatic LinkingPrograms are translated and linked using a Programs are translated and linked using a compiler drivercompiler driver::n unix> gcc -O2 -g -o p main.c swap.cn 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, S’04Why Linkers?Why Linkers?ModularityModularityn Program can be written as a collection of smaller sourcefiles, rather than one monolithic mass.n Can build libraries of common functions (more on this later)l e.g., Math library, standard C libraryEfficiencyEfficiencyn Time:l Change one source file, compile, and then relink.l No need to recompile other source files.n Space:l Libraries of common functions can be aggregated into a singlefile...l Yet executable files and running memory images contain onlycode for the functions they actually use.Page ‹#›– 5 –15-213, S’04What Does a Linker Do?What Does a Linker Do?Symbol resolutionSymbol resolutionn Programs define and reference symbols (variables andfunctions):l code: a(); /* reference to symbol a */l data: int *xp=&x; /* reference to symbol x */n Symbol definitions are stored (by compiler) in symbol table.l Each entry includes name, size, and location of object.n Linker associates each symbol reference with exactly onesymbol definition.RelocationRelocationn Merges separate code and data sections into single sectionsn Relocates symbols from their relative locations in the .o filesto new absolute positions in the executable.n Updates all references to these symbols to reflect their newpositions.– 6 –15-213, S’04Object Files (Modules)Object Files (Modules)Relocatable Relocatable object file (object file (.o.o file) file)n Contains code and data in a form that can be combined withother relocatable object files at compile time to form anexecutable.Executable object fileExecutable object filen Contains code and data in a form that can be copied directlyinto memory and then executed.Shared object file (Shared object file (.so.so file) file)n Special type of relocatable object file that can be loaded intomemory and linked dynamically, at either load time or run-time.n Called Dynamic Link Libraries (DLLs) by Windows– 7 –15-213, S’04Executable 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 Unixn Later adopted by BSD Unix variants and LinuxOne unified format forOne unified format forn Relocatable object files (.o),n Executable object filesn Shared object files (.so)Generic name: ELF binariesGeneric name: ELF binaries– 8 –15-213, S’04ELF Object File FormatELF Object File FormatElf headerElf headern Magic number, type (.o, exec, .so),machine, byte ordering, etc.Program header tableProgram header tablen Page size, virtual addresses memorysegments (sections), segment sizes..text.text section sectionn Code.data.data section sectionn Initialized (static) data..bssbss sectionsectionn Uninitialized (static) datan “Block Started by Symbol”n “Better Save Space”n 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)0Page ‹#›– 9 –15-213, S’04ELF Object File Format (cont)ELF Object File Format (cont)..symtabsymtab sectionsectionn Symbol tablen Procedure and static variable namesn Section names and locations..relrel.text.text section sectionn Relocation info for .text sectionn Addresses of instructions that will need tobe modified in the executablen Instructions for modifying...relrel.data.data section sectionn Relocation info for .data sectionn Addresses of pointer data that will need tobe modified in the merged executable.debug.debug section sectionn Info 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, S’04Linker SymbolsLinker SymbolsGlobal symbolsGlobal symbolsn Symbols defined by module m that can be referenced by othermodules.n Ex: non-static C functions and non-static global variables.External symbolsExternal symbolsn Global symbols that are referenced by module m but definedby some other module.Local symbolsLocal symbolsn Symbols that are defined and referenced exclusively bymodule m.n Ex: C functions and variables that are defined with the staticattribute.Key Point: Local linker symbols are Key Point: Local linker symbols are notnot local program local programvariablesvariables– 11 –15-213, S’04Resolving 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 globalsymbol bufRef to externalsymbol bufRef to externalsymbol swapDef of localsymbol bufp0Linker knowsnothing of temp– 12 –15-213, S’04Relocating Symbol Definitionsand ReferencesRelocating Symbol Definitionsand Referencesmain()m.oint *ep = &ea()a.oint e = 7headersmain()a()0system codeint *ep = &eint e = 7system datamore system codeint x = 15int ysystem dataint x = 15Relocatable Object FilesExecutable Object File.text.text.data.text.data.text.data.bss .symtab.debug.datauninitialized data.bsssystem codePage ‹#›– 13 –15-213, S’04main.o Relocation Infomain.o Relocation Info0000000 <main>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 08 sub $0x8,%esp 6: e8 fc ff ff ff call 7 <main+0x7> 7: R_386_PC32 swap b: 31 c0 xor %eax,%eax d:
View Full Document