LinkingOct 16, 2001Topics• static linking• object files• static libraries• loading• dynamic linking of shared librariesclass15.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class15.pptLinker 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() {}link time error: two strong symbols (p1)both instances of x refer to the same uninitialized int.writes to x in p2 might overwrite y!Evil!writes to x in p2 will overwrite something!Nasty! references to x refer to the same initializedvariable.CS 213 F’01– 3 –class15.pptA simplistic program translation schemeProblems:• 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)CS 213 F’01– 4 –class15.pptLinkersLinker (ld)Translatorsm.cm.oTranslatorsa.ca.opseparately compiledrelocatable object filesexecutable object file(contains code and data for allfunctions defined in m.c and a.c)CS 213 F’01– 5 –class15.pptTranslating the example program Compiler driver coordinates all steps in the translationand linking process.• Typically included with each compilation system (e.g., gcc)• Invokes preprocessor (cpp), compiler (cc1), assembler (as), andlinker (ld).• Passes command line args to appropriate phasesExample: create executable p from m.c and a.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>CS 213 F’01– 6 –class15.pptWhat does a linker do?Merges object files• merges multiple relocatable (.o) object files into a single executableobject file that can loaded and executed by the loader.Resolves external references• as part of the merging process, resolves external references.– external reference: reference to a symbol defined in another object file.Relocates symbols• relocates symbols from their relative locations in the .o files to newabsolute positions in the executable.• updates all references to these symbols to reflect their newpositions.–references can be in either code or data»code: a(); /* ref to symbol a */»data: int *xp=&x; /* ref to symbol x */–because of this modifying, linking is sometimes called link editing.CS 213 F’01– 7 –class15.pptWhy linkers?Modularity• Program can be written as a collection of smaller source files, ratherthan one monolithic mass.• Can build libraries of common functions (more on this later)–e.g., math library, standard C library• Efficiency• Time:–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 forthe functions they actually use.CS 213 F’01– 8 –class15.pptExecutable and linkable format (ELF)Standard binary format for object filesDerives from AT&T System V Unix• later adopted by BSD Unix variants and LinuxOne unified format for relocatable object files (.o),executable object files, and shared object files (.so)• generic name: ELF binariesBetter support for shared libraries than old a.outformats.CS 213 F’01– 9 –class15.pptELF object file formatElf header• magic number, type (.o, exec, .so),machine, byte ordering, etc.Program header table• page size, virtual addresses for memorysegments (sections), segment sizes..text section• code.data section• initialized (static) data.bss section• uninitialized (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)0CS 213 F’01– 10 –class15.pptELF object file format.symtab section• symbol table• procedure and static variable names• section names and locations.rel.text section• relocation info for .text section• addresses of instructions that will needto be modified in the executable• instructions for modifying..rel.data section• relocation info for .data section• addresses of pointer data that will needto be modified in the mergedexecutable.debug section• 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)0CS 213 F’01– 11 –class15.pptExample 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; } CS 213 F’01– 12 –class15.pptMerging .o files into an executablemain()m.oint *ep = &ea()a.oint e = 7headerssystem codemain()a()0system codeint *ep = &eint e = 7system datamore system codeint x = 15int ysystem dataint x = 15Relocatable object filesExecutable object file.text.text.data & .bss.text.data.text.data.bss .symtab.debug.datauninitialized data.bssCS 213 F’01– 13 –class15.pptRelocating symbols and resolving external referencesSymbols are lexical entities that name functions and variables.Each symbol has a value (typically a memory address).Code consists of symbol definitions and references.References can be either local or external.Ref to external symbol aDef oflocal symbol ep Ref toexternalsymbol eDefs oflocalsymbols xand yDef of localsymbol eint 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; } Ref to external symbol exit(defined in libc.so)Refs of localsymbols e,x,yDef oflocal symbol a CS 213 F’01– 14 –class15.pptm.o relocation infoDisassembly of section .text: 00000000 <main>: 00000000 <main>: 0: 55 pushl %ebp 1: 89 e5 movl %esp,%ebp 3: e8 fc ff ff ff call 4 <main+0x4> 4: R_386_PC32 a 8: 6a 00 pushl $0x0 a: e8 fc ff ff ff call b <main+0xb> b: R_386_PC32 exit f: 90 nop Disassembly of section .data: 00000000 <e>: 0: 07 00 00 00 source: objdumpint
View Full Document