15 213 Introduction to Computer Systems object files linkers Topics Program Translation and Execution I Linking Sept 29 1998 class11 ppt executing program memory image executable object file memory image on disk source file A simplistic program translation scheme p c Translators p Loader p 2 CS 213 F 98 Problems efficiency small change requires complete recompilation modularity hard to share common functionality e g printf class11 ppt p1 c Translator p2 c libc o Translator libc c relocatable object files Separate compilation Translator p2 o executable object file contains code and data for all functions defined in libc c p1 o Static Linker p Loader 3 CS 213 F 98 Improves modularity and efficiency but still hard to package common functions class11 ppt Translator p1 c p2 o Translator p2 c static library of relocatable object files executable object file only contains code and data for libc functions that are called from p1 c and p2 c libc a Static libraries p1 o Static Linker p Loader 4 CS 213 F 98 Further improves modularity and efficiency but duplicates common functions in different processes class11 ppt p1 c Translator p2 c CS 213 F 98 libc functions called by p1 c and p2 c are loaded at runtime and shared among processes shared library of dynamically relocatable object files Shared libraries Translator p2 o libc so 5 Loader Dynamic Linker p Static Linker p1 o class11 ppt class11 ppt Translator p1 c p2 o Translator p2 c libc so CS 213 F 98 libm so libwhatever a The complete picture p1 o Static Linker p Loader Dynamic Linker running program 6 C Program char c 100 double d int n 20 i 5 c i z main int i 7 Headers Relocation info Symbol table 0 CS 213 F 98 text sections read only code and data data sections initialized static variables bss sections uninitialized static variables Block Started by Symbol Better Save Space Object file Object files Note local variables don t go in object files They are created at runtime on the stack class11 ppt Increasing addresses main zip main c int p x extern int x zip c int x 0xfe code c Example C program code int p srand p CS 213 F 98 8 zip code p class11 ppt Compiling and linking the example program cc v c main c cfe main c tmp ctmfaacgja ugen O1 tmp ctmfaacgja o tmp ctmcaacgja as1 O1 t m p ctmcaacgja o main o cc c zip c cc c code c 9 C runtime system CS 213 F 98 cc v Wl O0 o main main o zip o code o ld o main O0 call shared usr lib cmplrs cc crt0 o main o zip o code o lc Unix linker class11 ppt 0 0 0 0 Linker functions crt main p zip x code 10 0 CS 213 F 98 crt main zip code x p main 1 Resolving external references 2 Relocating symbol definitions and references crt0 o main o zip o code o class11 ppt Data Text Symbols main c extern int x zip c int x 0xfe code c 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 external main zip int p x CS 213 F 98 Ref to external symbol srand defined in libc so Def of local symbol x Ref to external symbol x 11 Ref to local symbol p Def of local symbol zip zip code p code int p srand p Ref to external symbol zip Def of local symbol p class11 ppt 0 12 CS 213 F 98 type and value of relevant symbols Symbol table which instructions and data items will need to be relocated Section relocation info program code and data Section data for each section in Section data address size number of relocation entries Section headers magic number size and number of sections flags byte ordering executable or relocatable shared calls shared library functions entry point gp value start and sizes of text data and bss segments File and optional headers Alpha ECOFF object files Symbol table Section relocation info Section data Section headers Optional header File header class11 ppt File format bss sbss pdata xdata lit4 lit8 lita rconst rdata sdata data init fini text 13 CS 213 F 98 process init deinit code machine instructions support for exceptions 4 byte literal pool 8 byte literal pool 8 byte literal address pool read only constants read only data small data large data uninitialized data small 4 byte bss Section data relocatable object files Runtime memory image bss segment uninitialized variables block started by symbol better save space data segment initialized variables text segment machine code class11 ppt main zip start 0x00 0 0x50 80 14 sp 16 sp gp 1 t12 gp 32704 gp ra 0 sp t12 32752 gp ra t12 zip gp 1 ra ra 0 sp zero zero v0 gp 32728 gp sp 16 sp zero ra 1 text lita CS 213 F 98 Question Why use gp relative referencing of data and functions resets gp to 32 832 Note ra pc t12 address stored at lita 0 Assembler assumes that gp 32 832 64K 128 2 Note that t12 pc 4 size 0x30 48 0x10 16 text section main o main 0x0 lda 0x4 ldah 0x8 lda 0xc stq 0x10 ldq 0x14 jsr 0x18 ldah 0x1c ldq 0x20 bis 0x24 lda 0x28 lda 0x2c ret class11 ppt text lita 15 Value 0x0 0x0 start 0x00 0 0x50 80 CS 213 F 98 Sclass Symtype Text Proc Undefined Proc size 0x30 48 0x10 16 Symbol table main o main zip Index Name 0 main 1 zip class11 ppt text 0x04 0x10 0x14 0x14 0x18 1 4 13 3 1 12 text lita start 0x00 0 0x50 80 CS 213 F 98 REFQUAD 64 bit reference to the symbol s virtual address HINT 14 bit jsr hint reference LITUSE identifies instance of a literal address previously loaded into a register LITERAL gp relative reference to literal in lita GPDISP instruction pair that sets gp value size 0x30 48 0x10 16 Relocation entries main o main zip Extern Name local local lita local R LU JSR extern zip local 16 REFQUAD extern zip GPDISP LITERAL LITUSE HINT GPDISP Vaddr Symndx Type lita 0x50 class11 ppt main lda ldah lda stq ldq jsr ldah ldq bis lda lda ret main zip 0x0 0x4 0x8 0xc 0x10 0x14 0x18 0x1c 0x20 0x24 0x28 0x2c start 0x00 0 0x50 80 17 CS 213 F 98 Notes 1 LITUSE at 0x14 allows jsr to be replaced by bsr Vaddr Symndx Type Extern Name text 0x04 4 GPDISP local 0x10 13 LITERAL local lita 0x14 3 LITUSE local R LU JSR 0x14 1 HINT extern zip 0x18 12 GPDISP local lita 0x50 1 REFQUAD extern zip size 0x30 48 0x10 16 Relocations main o text lita sp 16 sp gp 1 t12 gp 32704 gp ra 0 sp t12 32752 gp ra t12 zip gp 1 ra ra 0 sp zero zero v0 gp 32728 gp …
View Full Document