Slide 1cs3410 Recap/QuizReview of Program LayoutAdd 1 to 100Slide 5Globals and LocalsGlobals and LocalsGlobals and LocalsBig PictureBig PictureSlide 11Big PictureSymbols and ReferencesObject fileExampleObjdump disassemblyObjdump symbolsSeparate CompilationSlide 19Big PictureLinkersLinker ExampleLinker ExampleLinker ExampleObject fileFile FormatsSlide 27Big PictureLoadersStatic LibrariesShared LibrariesDirect Function CallsIndirect Function CallsIndirect Function CallsDynamic LinkingBig PictureDynamic Shared ObjectsStatic and Dynamic LinkingAssemblers, Linkers, and LoadersSee: P&H Appendix B.3-4Kevin WalshCS 3410, Spring 2011Computer ScienceCornell University2cs3410 Recap/Quiz2int x = 10;x = 2 * x + 15;Ccompileraddi r5, r0, 10muli r5, r5, 2addi r5, r5, 15MIPSassembly001000000000010100000000000010100000000000000101001010000100000000100000101001010000000000001111machinecodeassemblerCPUCircuitsGatesTransistorsSilicon3Review of Program Layoutvector v = malloc(8);v->x = prompt(“enter x”);v->y = prompt(“enter y”);int c = pi + tnorm(v);print(“result”, c);calc.cint tnorm(vector v) { return abs(v->x)+abs(v->y);}math.cglobal variable: pientry point: promptentry point: printentry point: malloclib3410.o4Add 1 to 100int n = 100;int main (int argc, char* argv[ ]) {int i;int m = n;int count = 0;for (i = 1; i <= m; i++)count += i;printf ("Sum 1 to %d is %d\n", n, count);}[csug01] mipsel-linux-gcc –S add1To100.c5$L2: lw $2,24($fp) lw $3,28($fp) slt $2,$3,$2 bne $2,$0,$L3 lw $3,32($fp) lw $2,24($fp) addu $2,$3,$2 sw $2,32($fp) lw $2,24($fp) addiu $2,$2,1 sw $2,24($fp) b $L2$L3: la $4,$str0 lw $5,28($fp) lw $6,32($fp) jal printf move $sp,$fp lw $31,44($sp) lw $fp,40($sp) addiu $sp,$sp,48 j $31 .data .globl n .align 2 n: .word 100 .rdata .align 2$str0: .asciiz "Sum 1 to %d is %d\n" .text .align 2 .globl mainmain: addiu $sp,$sp,-48 sw $31,44($sp) sw $fp,40($sp) move $fp,$sp sw $4,48($fp) sw $5,52($fp) la $2,n lw $2,0($2) sw $2,28($fp) sw $0,32($fp) li $2,1 sw $2,24($fp)6Globals and Localsint n = 100;int main (int argc, char* argv[ ]) {int i, m = n, count = 0, *A = malloc(4 * m);for (i = 1; i <= m; i++) { count += i; A[i] = count; }printf ("Sum 1 to %d is %d\n", n, count);}Variables Visibility LifetimeLocationFunction-LocalGlobalDynamic7Globals and LocalsVariables Visibility LifetimeLocationFunction-LocalGlobalDynamicC Pointers can be troubleint *trouble(){ int a; …; return &a; }char *evil() { char s[20]; gets(s); return s; }int *bad() { s = malloc(20); … free(s); … return s; }(Can’t do this in Java, C#, ...)8Globals and LocalsVariables Visibility LifetimeLocationFunction-LocalGlobalDynamicC Pointers can be troubleint *trouble(){ int a; …; return &a; }char *evil() { char s[20]; gets(s); return s; }int *bad() { s = malloc(20); … free(s); … return s; }(Can’t do this in Java, C#, ...)9Big Picturecalc.cmath.cio.slibc.olibm.ocalc.smath.sio.ocalc.omath.ocalc.exeExecuting inMemory10Big PictureCompiler output is assembly filesAssembler output is obj filesLinker joins object files into one executableLoader brings it into memory and starts execution11Compilers and Assemblers12Big PictureOutput is obj files•Binary machine code, but not executable•May refer to external symbols•Each object file has illusion of its own address space–Addresses will need to be fixed latermath.c math.s math.o13Symbols and ReferencesGlobal labels: Externally visible “exported” symbols•Can be referenced from other object files•Exported functions, global variablesLocal labels: Internal visible only symbols•Only used within this object file•static functions, static variables, loop labels, …14Object fileHeader•Size and position of pieces of fileText Segment•instructionsData Segment•static data (local/global vars, strings, constants)Debugging Information•line number code address map, etc.Symbol Table•External (exported) references•Unresolved (imported) referencesObject File15Exampleint pi = 3;int e = 2;static int randomval = 7;extern char *username;extern int printf(char *str, …);int square(int x) { … }static int is_prime(int x) { … }int pick_prime() { … }int pick_random() { return randomval; }math.cgcc -S … math.cgcc -c … math.sobjdump --disassemble math.oobjdump --syms math.o16Objdump disassemblycsug01 ~$ mipsel-linux-objdump --disassemble math.o math.o: file format elf32-tradlittlemipsDisassembly of section .text:00000000 <pick_random>: 0: 27bdfff8 addiu sp,sp,-8 4: afbe0000 sws8,0(sp) 8: 03a0f021 moves8,sp c: 3c020000 lui v0,0x0 10: 8c420008 lw v0,8(v0) 14: 03c0e821 movesp,s8 18: 8fbe0000 lw s8,0(sp) 1c: 27bd0008 addiu sp,sp,8 20: 03e00008 jr ra 24: 00000000 nop00000028 <square>: 28: 27bdfff8 addiu sp,sp,-8 2c: afbe0000 sws8,0(sp) 30: 03a0f021 moves8,sp 34: afc40008 swa0,8(s8) …17Objdump symbolscsug01 ~$ mipsel-linux-objdump --syms math.omath.o: file format elf32-tradlittlemipsSYMBOL TABLE:00000000 l df *ABS* 00000000 math.c00000000 l d .text 00000000 .text00000000 l d .data 00000000 .data00000000 l d .bss 00000000 .bss00000000 l d .mdebug.abi32 00000000 .mdebug.abi3200000008 l O .data 00000004 randomval00000060 l F .text 00000028 is_prime00000000 l d .rodata 00000000 .rodata00000000 l d .comment 00000000 .comment00000000 g O .data 00000004 pi00000004 g O .data 00000004 e00000000 g F .text 00000028 pick_random00000028 g F .text 00000038 square00000088 g F .text 0000004c pick_prime00000000 *UND* 00000000 username00000000 *UND* 00000000 printf18Separate CompilationQ: Why separate compile/assemble and linking steps?A: Can recompile one object, then just relink.19Linkers20Big Picturecalc.cmath.cio.slibc.olibm.ocalc.smath.sio.ocalc.omath.ocalc.exeExecuting inMemory21LinkersLinker combines object files into an executable file•Relocate each object’s text and data segments•Resolve as-yet-unresolved symbols•Record top-level entry point in executable fileEnd result: a program on disk, ready to execute22Linker Example main.o...0C000000210350001b80050C4C040000210470020C000000...00 T main00 D uname*UND* printf*UND* pi40, JL, printf4C, LW/gp, pi54, JL, squaremath.o...210320400C0000001b3014023C04000034040000...20 T square00 D pi*UND* printf*UND* uname28, JL, printf30, LUI, uname34, LA,
View Full Document