Slide 1TodayExample C ProgramStatic LinkingWhy Linkers? Modularity!Why Linkers? Efficiency!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 DataRelocation Info (main)Relocation Info (swap, .text)Relocation Info (swap, .data)Executable After Relocation (.text)Executable After Relocation (.data)Strong and Weak SymbolsLinker’s Symbol RulesLinker PuzzlesGlobal VariablesPackaging Commonly Used FunctionsSolution: Static LibrariesCreating Static LibrariesCommonly Used LibrariesLinking with Static LibrariesUsing Static LibrariesLoading Executable Object FilesShared LibrariesShared Libraries (cont.)Dynamic Linking at Load-timeDynamic Linking at RuntimeDynamic Linking at Run-timeCarnegie MellonIntroduction to Computer Systems15-213, fall 2009xxth Lecture, Oct. xth Instructors: Majd Sakr and Khaled HarrasCarnegie MellonTodayLinkingCarnegie MellonExample 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;}Carnegie MellonStatic LinkingPrograms are translated and linked using a compiler 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.cCarnegie MellonWhy Linkers? 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)e.g., Math library, standard C libraryCarnegie MellonWhy Linkers? Efficiency!Time: 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.Carnegie MellonWhat Do Linkers Do?Step 1: Symbol resolutionPrograms define and reference symbols (variables and functions):void swap() {…} /* define symbol swap */swap(); /* reference symbol swap */int *xp = &x; /* define xp, reference x */Symbol definitions are stored (by compiler) in symbol table.Symbol table is an array of structsEach entry includes name, type, size, and location of symbol.Linker associates each symbol reference with exactly one symbol definition.Carnegie MellonWhat Do Linkers Do? (cont.)Step 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.Carnegie MellonThree Kinds of Object Files (Modules)Relocatable object file (.o 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) fileExecutable object file Contains code and data in a form that can be copied directly into memory and then executed.Shared object file (.so 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 WindowsCarnegie MellonExecutable and Linkable Format (ELF)Standard binary format for object filesOriginally proposed by AT&T System V UnixLater adopted by BSD Unix variants and LinuxOne unified format for Relocatable object files (.o), Executable object filesShared object files (.so)Generic name: ELF binariesCarnegie MellonELF Object File FormatElf headerWord size, byte ordering, file type (.o, exec, .so), machine type, etc.Segment header tablePage size, virtual addresses memory segments (sections), segment sizes..text sectionCode.rodata sectionRead only data: jump tables, ....data sectionInitialized global variables.bss 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.rodata section.bss section.symtab section.rel.txt section.rel.data section.debug sectionSection header table0.data sectionCarnegie MellonELF Object File Format (cont.).symtab sectionSymbol tableProcedure and static variable namesSection names and locations.rel.text sectionRelocation info for .text sectionAddresses of instructions that will need to be modified in the executableInstructions for modifying..rel.data sectionRelocation info for .data sectionAddresses of pointer data that will need to be modified in the merged executable.debug sectionInfo for symbolic debugging (gcc -g)Section header tableOffsets and sizes of each sectionELF headerSegment header table(required for executables).text section.rodata section.bss section.symtab section.rel.txt section.rel.data section.debug sectionSection header table0.data sectionCarnegie MellonLinker SymbolsGlobal symbolsSymbols defined by module m that can be referenced by other modules.E.g.: non-static C functions and non-static global variables.External symbolsGlobal symbols that are referenced by module m but defined by some other module.Local symbolsSymbols that are defined and referenced exclusively by module m.E.g.: C functions and variables defined with the static attribute.Local linker symbols are not local program variablesCarnegie MellonResolving Symbolsint buf[2] = {1, 2}; int main() { swap(); return 0;} main.cextern int buf[]; static int *bufp0 = &buf[0];static int *bufp1;void swap(){ int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp;}swap.cGlobalExternalExternal LocalGlobalLinker knowsnothing of tempCarnegie MellonRelocating Code and Datamain()main.oint *bufp0=&buf[0]swap()swap.oint buf[2]={1,2}Headersmain()swap()0System codeint *bufp0=&buf[0]int buf[2]={1,2}System dataMore system codeint *bufp1System dataRelocatable Object Files Executable
View Full Document