Linker Puzzles 15 213 The course that gives CMU its Zip Linking Oct 15 2002 Topics n Static linking n Object files n Static libraries n Loading n Dynamic linking of shared libraries int x p1 p1 int x p1 int x p2 int x int y p1 double x p2 int x 7 int y 5 p1 double x p2 int x 7 p1 int x p2 15 213 F 02 2 class15 ppt A Simplistic Program Translation Scheme m c A Better Scheme Using a Linker m c ASCII source file Translator p Binary executable object file memory image on disk a c Translators Translators m o a o Separately compiled relocatable object files Linker ld p Problems Efficiency small change requires complete recompilation Modularity hard to share common functions e g printf Solution Static linker or linker 3 15 213 F 02 4 Executable object file contains code and data for all functions defined in m c and a c 15 213 F 02 Translating the Example Program What Does a Linker Do Compiler driver coordinates all steps in the translation and linking process Merges object files n Typically included with each compilation system e g gcc n Invokes preprocessor cpp compiler cc1 assembler as and linker ld n n Resolves external references n Relocates symbols Example create executable p from m c and a c 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 15 213 F 02 Modularity n Program can be written as a collection of smaller source files rather than one monolithic mass n Updates all references to these symbols to reflect their new positions code a data int xp x reference to symbol a reference to symbol x 15 213 F 02 6 Standard binary format for object files Derives from AT T System V Unix n Later adopted by BSD Unix variants and Linux One unified format for Efficiency Time n Relocatable object files o n Executable object files Shared object files so n l Change one source file compile and then relink Generic name ELF binaries l No need to recompile other source files n Relocates symbols from their relative locations in the o files to new absolute positions in the executable l References can be in either code or data Can build libraries of common functions more on this later l e g Math library standard C library n n Executable and Linkable Format ELF Why Linkers n As part of the merging process resolves external references l External reference reference to a symbol defined in another object file Passes command line arguments to appropriate phases 5 Merges multiple relocatable o object files into a single executable object file that can loaded and executed by the loader Better support for shared libraries than old a out formats Space l Libraries of common functions can be aggregated into a single file l Yet executable files and running memory images contain only code for the functions they actually use 7 15 213 F 02 8 15 213 F 02 ELF Object File Format ELF Object File Format cont Elf header n ELF header Magic number type o exec so machine byte ordering etc text section Page size virtual addresses memory segments sections segment sizes data section text section n n n n n n n n Initialized static data rel data n Uninitialized static data Block Started by Symbol Better Save Space Has section header but occupies no space Section header table required for relocatables n n int e 7 int main int r a exit 0 data section bss section symtab rel text Relocation info for data section Addresses of pointer data that will need to be modified in the merged executable rel data debug Section header table required for relocatables Info for symbolic debugging gcc g 15 213 F 02 Merging Relocatable Object Files into an Executable Object File Relocatable Object Files a c extern int e int ep e int x 15 int y system code text system data data Executable Object File 0 headers system code main int a return ep x y m o a o 11 Relocation info for text section Addresses of instructions that will need to be modified in the executable Instructions for modifying 10 Example C Program m c text section debug section n 15 213 F 02 9 Program header table required for executables rel data rel data section debug 0 ELF header Symbol table Procedure and static variable names Section names and locations rel text rel text section symtab rel txt bss section n n bss section Code data section n symtab section Program header table required for executables Program header table n 0 15 213 F 02 12 main text int e 7 data a text int ep e int x 15 int y data bss text a more system code system data int e 7 int ep e int x 15 uninitialized data symtab debug data bss 15 213 F 02 Relocating Symbols and Resolving External References n Symbols are lexical entities that name functions and variables n n Each symbol has a value typically a memory address Code consists of symbol definitions and references n References can be either local or external m c Def of local symbol e a c extern int e int e 7 int main int r a exit 0 Def of local symbol Ref to external symbol exit Ref to external ep defined in libc so symbol a int ep e int x 15 int y int a return ep x y Disassembly of section text int ep e int x 15 int y 00000000 a 0 55 1 8b 15 00 00 00 00 6 int a return ep x y 15 7 a1 00 00 00 00 c e 10 12 17 89 03 89 03 00 18 19 5d c3 e5 02 ec 05 00 00 00 pushl movl 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 b main 0xb a e8 fc ff ff ff call b R 386 PC32 exit f 90 nop Disassembly of section data Defs of local symbols x and y a o Relocation Info text a c extern int e m c Ref to external symbol e Def of Refs of local local symbols ep x y symbol a 15 213 F 02 13 m o Relocation Info 00000000 e 0 07 00 00 00 source objdump 15 213 F 02 14 a o Relocation Info data a c extern int e int ep e int x 15 int y ebp 0x0 edx 3 R 386 32 ep movl 0x0 eax 8 R 386 …
View Full Document