System-level Programming I: Building and running programs Feb. 22, 2000A simplistic program translation schemeLinkersTranslating the example programWhat does a linker do?Why linkers?Executable and linkable format (ELF)ELF object file formatSlide 9Example C programMerging .o files into an executableRelocating symbols and resolving external referencesm.o relocation infoa.o relocation info (.text)a.o relocation info (.data)Executable after relocation and external reference resolution (.text)Executable after relocation and external reference resolution (.data)Strong and weak symbolsLinker’s symbol rulesSymbol resolution examplesPackaging commonly used functionsStatic libraries (archives)Creating static librariesCommonly used librariesUsing static librariesLoading executable binariesShared librariesDynamically linked shared librariesThe complete pictureSystem-level Programming I: Building and running programsFeb. 22, 2000Topics•static linking•object files•static libraries•loading•dynamic linking of shared librariesclass11.ppt15-213“The course that gives CMU its Zip!”CS 213 S’00– 2 –class11.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 S’00– 3 –class11.pptLinkersLinker (ld)Translatorsm.cm.oTranslatorsa.ca.opseparately compiled relocatable object filesexecutable object file (contains code and data for all functions defined in m.c and a.c)CS 213 S’00– 4 –class11.pptTranslating the example program Compiler driver coordinates all steps in the translation and linking process. •Typically included with each compilation system (e.g., gcc)•Invokes preprocessor (cpp), compiler (cc1), assembler (as), and linker (ld).•Passes command line args to appropriate phases Example: 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 S’00– 5 –class11.pptWhat does a linker do?Merges object files•merges multiple relocatable (.o) object files into a single executable object 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 new absolute positions in the executable.•updates all references to these symbols to reflect their new positions.–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 S’00– 6 –class11.pptWhy 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 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 for the functions they actually use.CS 213 S’00– 7 –class11.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.out formats.CS 213 S’00– 8 –class11.pptELF object file formatElf header•magic number, type (.o, exec, .so), machine, byte ordering, etc.Program header table•page size, virtual addresses for memory segments (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 S’00– 9 –class11.pptELF object file format.symtab•symbol table•procedure and static variable names•section names and locations.rel.text•relocation info for .text section•addresses of instructions that will need to be modified in the executable•instructions for modifying..rel.data•relocation info for .data section•addresses of pointer data that will need to be modified in the merged executable.debug•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 S’00– 10 –class11.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 S’00– 11 –class11.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 S’00– 12 –class11.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 of local symbols x and yDef of local symbol 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 local symbols e,x,yDef oflocal symbol aCS 213 S’00– 13 –class11.pptm.o relocation infoDisassembly of section .text: 00000000
View Full Document