15 213 The course that gives CMU its Zip Linking February 20 2001 Topics class16 ppt static linking object files static libraries loading dynamic linking of shared libraries A simplistic program translation scheme ASCII source file m c Translator binary executable object file memory image on disk p Problems efficiency small change requires complete recompilation modularity hard to share common functions e g printf Solution static linker or linker class16 ppt 2 CS 213 S 01 Linkers m c a c Translators Translators m o a o separately compiled relocatable object files Linker ld p class16 ppt executable object file contains code and data for all functions defined in m c and a c 3 CS 213 S 01 Translating 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 class16 ppt 4 CS 213 S 01 What 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 ref to symbol a code a data int xp x ref to symbol x because of this modifying linking is sometimes called link editing class16 ppt 5 CS 213 S 01 Why 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 class16 ppt 6 CS 213 S 01 Executable and linkable format ELF Standard binary format for object files Derives from AT T System V Unix later adopted by BSD Unix variants and Linux One unified format for relocatable object files o executable object files and shared object files so generic name ELF binaries Better support for shared libraries than old a out formats class16 ppt 7 CS 213 S 01 ELF object file format Elf 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 text section data section symtab data section rel txt initialized static data rel data bss section uninitialized static data Block Started by Symbol Better Save Space has section header but occupies no space class16 ppt Program header table required for executables bss section code ELF header 8 debug Section header table required for relocatables CS 213 S 01 0 ELF 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 need to be modified in the executable instructions for modifying rel data section relocation info for data section addresses of pointer data that will need to be modified in the merged executable debug section info for symbolic debugging gcc g class16 ppt 9 ELF header Program header table required for executables text section data section bss section symtab rel text rel data debug Section header table required for relocatables CS 213 S 01 0 Example C program m c a c extern int e int e 7 int ep e int x 15 int y int main int r a exit 0 class16 ppt int a return ep x y 10 CS 213 S 01 Merging o files into an executable Relocatable object files Executable object file 0 system code text system data data bss headers system code main m o a o main text int e 7 data a text int ep e int x 15 int y data class16 ppt text a more system code system data int e 7 int ep e int x 15 uninitialized data symtab debug bss 11 CS 213 S 01 data bss Relocating symbols and resolving external references Symbols 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 m c Def of local symbol e a c extern int e int e 7 int ep e int main int x 15 int r a int y exit 0 Def of int a local return ep x y symbol Ref to external symbol exit Ref to external ep defined in symbol a Def of Refs of local libc so local symbols e x y symbol a class16 ppt 12 CS 213 S 01 Ref to external symbol e Defs of local symbols x and y m o relocation info m c int e 7 Disassembly of section text int main int r a exit 0 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 objdump class16 ppt 13 CS 213 S 01 a o relocation info text a c extern int e Disassembly of section text int ep e int x 15 int y 00000000 a 0 55 1 8b 15 00 00 00 6 00 int a return ep x y 7 c e 10 12 17 18 19 class16 ppt a1 00 00 00 00 89 03 89 03 00 5d c3 14 e5 02 ec 05 00 00 00 pushl movl ebp 0x0 edx 3 R 386 32 ep movl 0x0 eax 8 R 386 32 x movl esp ebp addl edx eax movl ebp esp addl 0x0 eax 14 R 386 32 popl ebp ret CS 213 S 01 y a o relocation info data a c extern int e int ep e int x 15 int y int a return ep x y class16 ppt Disassembly of section data 00000000 ep 0 00 00 00 00 0 R 386 32 00000004 x 4 0f 00 00 00 15 CS 213 S 01 e Executable after relocation and …
View Full Document