Program Translation and Execution I:LinkingSept. 29, 1998Topics• object files• linkersclass11.ppt15-213Introduction to Computer SystemsCS 213 F’98– 2 –class11.pptA simplistic program translation schemeProblems:• efficiency: small change requires complete recompilation• modularity: hard to share common functionality (e.g. printf)Translatorsp.cpLoaderpsource fileexecutable object file(memory image on disk)executing program(memory image)CS 213 F’98– 3 –class11.pptSeparate compilationStatic LinkerTranslatorp1.cp1.oTranslatorp2.cp2.opLoaderImproves modularity and efficiency, but still hard to package common functionsTranslatorlibc.clibc.orelocatableobject filesexecutable object file(contains code anddata for all functionsdefined in libc.c)CS 213 F’98– 4 –class11.pptStatic librariesTranslatorp1.cp1.oTranslatorp2.cp2.o libc.astatic libraryof relocatableobject filesexecutable object file(only contains code and data forlibc functions that are calledfrom p1.c and p2.c)Further improves modularity and efficiency, but duplicatescommon functions in different processesStatic LinkerpLoaderCS 213 F’98– 5 –class11.pptShared libraries libc functions called by p1.cand p2.c are loaded at run-time and shared amongprocesses.shared library of dynamicallyrelocatable object filesTranslatorp1.cp1.oTranslatorp2.cp2.olibc.soStatic LinkerpLoader/Dynamic LinkerCS 213 F’98– 6 –class11.pptThe complete pictureTranslatorp1.cp1.oTranslatorp2.cp2.olibc.soStatic LinkerpLoader/Dynamic Linkerlibwhatever.arunning programlibm.soCS 213 F’98– 7 –class11.pptObject fileschar c[100];double d;int n=20; main() { int i; i=5; c[i] = ‘z’;}HeadersRelocation infoSymbol tabletext sections:read-only code and datadata sectionsinitialized static variables0bss sectionsuninitialized static variablesBlock Started by SymbolBetter Save SpaceC Program Object fileNote: local variables don’t go inobject files. They are created atruntime on the stack.IncreasingaddressesCS 213 F’98– 8 –class11.pptExample C programmain() { zip();}int x=0xfe;code(int *p) { srand(*p);}extern int x;int *p = &x;zip() { code(p);}main.c: zip.c: code.c:CS 213 F’98– 9 –class11.pptCompiling and linking the example program% cc -v -c main.ccfe main.c > /tmp/ctmfaacgja ugen -O1 /tmp/ctmfaacgja -o /tmp/ctmcaacgjaas1 -O1 /tmp/ctmcaacgja -o main.o% cc -c zip.c% cc -c code.c% cc -v -Wl,-O0 -o main main.o zip.o code.old -o main -O0 -call_shared /usr/lib/cmplrs/cc/crt0.o main.o zip.o code.o -lc Unix linkerC runtime systemCS 213 F’98– 10 –class11.pptLinker functionsmain()1. Resolving external references2. Relocating symbol definitions and referencesmain.o*pzip()zip.oxcode()code.oxcrtmain()zip()0000crtcrt0.o0code()*pmainDataTextCS 213 F’98– 11 –class11.pptSymbols• Lexical entities that correspond to functions and global variables• Each symbol has a value (address)• Code consists of symbol definitions and references• References can be either local or externalmain() { zip();}int x=0xfe;code(int *p) { srand(*p);}extern int x;int *p = &x;zip() { code(p);}main.c: zip.c: code.c:Ref to external symbol zipDef of local symbol zipDef oflocalsymbol pRef to external symbol xDef of local symbol xRef to externalsymbol srand(defined in libc.so)Ref to local symbol pCS 213 F’98– 12 –class11.pptAlpha ECOFF object filesFile headerOptional headerSection headersSection dataSection relocation infoSymbol table0File and optional headers• magic number, size and number ofsections, flags–byte ordering, executable or relocatable?,shared?, calls shared library functions?• entry point, gp value, start and sizes oftext, data, and bss segmentsSection headers• for each section in “Section data”:address, size, number of relocation entriesSection data• program code and dataSection relocation info• which instructions and data items willneed to be relocated?Symbol table• type and value of relevant symbolsCS 213 F’98– 13 –class11.pptSection data (relocatable object files).bss.sbss.sdata.lit4.lit8.lita.data.init/.fini.text.pdata/.xdata.rdata.rconstbss segmentuninitialized variables(block started by symbol)(better save space!)data segmentinitialized variablestext segmentmachine codeuninitialized datasmall (4-byte) bsssmall data4-byte literal pool8-byte literal pool8-byte literal address poolread only dataread only constantslarge datasupport for exceptionsmachine instructionsprocess init/deinit code(Runtime memory image) (File format)CS 213 F’98– 14 –class11.ppt.text section (main.o)main: 0x0: lda sp, -16(sp) 0x4: ldah gp, 1(t12) 0x8: lda gp, -32704(gp) 0xc: stq ra, 0(sp) 0x10: ldq t12, -32752(gp) 0x14: jsr ra, (t12), zip 0x18: ldah gp, 1(ra) 0x1c: ldq ra, 0(sp) 0x20: bis zero, zero, v0 0x24: lda gp, -32728(gp) 0x28: lda sp, 16(sp) 0x2c: ret zero, (ra), 1main() { zip();}.text.lita0x00 (0)0x50 (80)start0x30 (48)0x10 (16)sizeAssembler assumes that gp=32,832=(64K+128)/2(Note that t12 = pc-4)t12 = address stored at .lita+0resets gp to 32,832(Note: ra = pc)Question: Why use gp-relative referencing of data and functions?CS 213 F’98– 15 –class11.pptSymbol table (main.o)[Index] Name Value Sclass Symtype[0] main 0x0 Text Proc[1] zip 0x0 Undefined Proc main() { zip();}.text.lita0x00 (0)0x50 (80)start0x30 (48)0x10 (16)sizeCS 213 F’98– 16 –class11.pptRelocation entries (main.o)Vaddr Symndx Type Extern Name.text:0x04 4 GPDISP local0x10 13 LITERAL local .lita0x14 3 LITUSE local R_LU_JSR0x14 1 HINT extern zip0x18 12 GPDISP local .lita:0x50 1 REFQUAD extern zipmain() { zip();}.text.lita0x00 (0)0x50 (80)start0x30 (48)0x10 (16)sizeGPDISP : instruction pair thatsets gp valueLITERAL: gp-relativereference to literal in .litaLITUSE: identifies instance ofa literal address previouslyloaded into a registerHINT: 14 bit jsr hint referenceREFQUAD: 64-bit reference tothe symbol’s virtual addressCS 213 F’98– 17 –class11.ppt Relocations (main.o)main: 0x0: lda sp, -16(sp) 0x4: ldah gp, 1(t12) 0x8: lda gp, -32704(gp) 0xc: stq ra, 0(sp) 0x10: ldq t12, -32752(gp) 0x14: jsr ra, (t12), zip 0x18: ldah gp, 1(ra) 0x1c: ldq ra, 0(sp) 0x20: bis zero, zero, v0 0x24: lda gp, -32728(gp) 0x28: lda sp, 16(sp) 0x2c: ret zero, (ra), 1main() { zip();}.text.lita0x00 (0)0x50 (80)start0x30
View Full Document