LinkingOct. 15, 2002LinkingOct. 15, 2002TopicsTopicsn Static linkingn Object filesn Static librariesn Loadingn Dynamic linking of shared librariesclass15.ppt15-213“The course that gives CMU its Zip!”– 2 –15-213, F’02Linker PuzzlesLinker Puzzlesint x;p1() {}int x;p2() {}int x;int y;p1() {}double x;p2() {}int x=7;int y=5;p1() {}double x;p2() {}int x=7;p1() {}int x;p2() {}int x;p1() {}p1() {}– 3 –15-213, F’02A Simplistic Program TranslationSchemeA Simplistic Program TranslationSchemeProblems:• Efficiency: small change requires complete recompilation• Modularity: hard to share common functions (e.g. printf)Solution:• Static linker (or linker)Translatorm.cpASCII source fileBinary executable object file(memory image on disk)– 4 –15-213, F’02A Better Scheme Using a LinkerA Better Scheme Using a LinkerLinker (ld)Translatorsm.cm.oTranslatorsa.ca.opSeparately compiledrelocatable object filesExecutable object file (contains codeand data for all functions defined in m.cand a.c)– 5 –15-213, F’02Translating the Example ProgramTranslating the Example ProgramCompiler driverCompiler driver coordinates all steps in the translation coordinates all steps in the translationand linking process.and linking process.n Typically included with each compilation system (e.g., gcc)n Invokes preprocessor (cpp), compiler (cc1), assembler (as),and linker (ld).n Passes command line arguments to appropriate phasesExample: create executable Example: create executable pp from from m.cm.c and and a.ca.c::bass> gcc -O2 -v -o p m.c a.c cpp [args] m.c /tmp/cca07630.i cc1 /tmp/cca07630.i m.c -O2 [args] -o /tmp/cca07630.s as [args] -o /tmp/cca076301.o /tmp/cca07630.s <similar process for a.c>ld -o p [system obj files] /tmp/cca076301.o /tmp/cca076302.o bass>– 6 –15-213, F’02What Does a Linker Do?What Does a Linker Do?Merges object filesMerges object filesn Merges multiple relocatable (.o) object files into a single executableobject file that can loaded and executed by the loader.Resolves external referencesResolves external referencesn As part of the merging process, resolves external references.l External reference: reference to a symbol defined in another object file.Relocates symbolsRelocates symbolsn Relocates symbols from their relative locations in the .o files tonew absolute positions in the executable.n Updates all references to these symbols to reflect their newpositions.l References can be in either code or data» code: a(); /* reference to symbol a */» data: int *xp=&x; /* reference to symbol x */– 7 –15-213, F’02Why 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.– 8 –15-213, F’02Executable 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 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 binariesBetter support for shared libraries than old Better support for shared libraries than old a.outa.out formats. formats.– 9 –15-213, F’02ELF 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)0– 10 –15-213, F’02ELF 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– 11 –15-213, F’02Example C ProgramExample C Programint e=7; int main() { int r = a(); exit(0); } m.c a.cextern int e; int *ep=&e;int x=15; int y; int a() { return *ep+x+y; } – 12 –15-213, F’02Merging Relocatable Object Filesinto an Executable Object FileMerging Relocatable Object Filesinto an Executable Object Filemain()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 code– 13 –15-213, F’02Relocating Symbols and ResolvingExternal ReferencesRelocating Symbols and ResolvingExternal ReferencesnSymbols are lexical entities that name functions and variables.n Each symbol has a value (typically a memory address).n Code consists of symbol definitions and references.n References can be either local or external.int e=7; int main() { int r = a(); exit(0); } m.c a.cextern int e; int *ep=&e;int x=15; int y; int a() { return *ep+x+y; } Def of localsymbol eRef to external symbol exit(defined in libc.so)Ref toexternalsymbol eDef oflocal symbol ep Defs oflocalsymbolsx and yRefs of localsymbols ep,x,yDef oflocal symbol a Ref to external symbol a– 14 –15-213, F’02m.o Relocation Infom.o Relocation
View Full Document