Sta C Linking
Total Page:16
File Type:pdf, Size:1020Kb
Carnegie Mellon Linking 15-213: Introduc;on to Computer Systems 13th Lecture, Oct. 13, 2015 Instructors: Randal E. Bryant and David R. O’Hallaron Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 1 Carnegie Mellon Today ¢ Linking ¢ Case study: Library interposioning Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 2 Carnegie Mellon Example C Program int sum(int *a, int n);! int sum(int *a, int n)! ! {! int array[2] = {1, 2};! int i, s = 0;! ! ! int main()! for (i = 0; i < n; i++) {! {! s += a[i];! int val = sum(array, 2);! }! return val;! return s;! }! }! main.c ! sum.c Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 3 Carnegie Mellon Sta7c Linking ¢ Programs are translated and linked using a compiler driver: § linux> gcc -Og -o prog main.c sum.c § linux> ./prog main.c sum.c Source files Translators Translators (cpp, cc1, as) (cpp, cc1, as) main.o sum.o Separately compiled relocatable object files Linker (ld) Fully linked executable object file prog (contains code and data for all func;ons defined in main.c and sum.c) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 4 Carnegie Mellon Why Linkers? ¢ Reason 1: Modularity § Program can be wriNen as a collec;on of smaller source files, rather than one monolithic mass. § Can build libraries of common func;ons (more on this later) § e.g., Math library, standard C library Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 5 Carnegie Mellon Why Linkers? (cont) ¢ Reason 2: Efficiency § Time: Separate compilaon § Change one source file, compile, and then relink. § No need to recompile other source files. § Space: Libraries § Common func;ons can be aggregated into a single file... § Yet executable files and running memory images contain only code for the func;ons they actually use. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 6 Carnegie Mellon What Do Linkers Do? ¢ Step 1: Symbol resoluon § Programs define and reference symbols (global variables and func;ons): § void swap() {…} /* define symbol swap */ § swap(); /* reference symbol swap */ § int *xp = &x; /* define symbol xp, reference x */ § Symbol defini;ons are stored in object file (by assembler) in symbol table. § Symbol table is an array of structs § Each entry includes name, size, and locaon of symbol. § During symbol resolu7on step, the linker associates each symbol reference with exactly one symbol definion. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 7 Carnegie Mellon What Do Linkers Do? (cont) ¢ Step 2: Reloca7on § Merges separate code and data sec;ons into single sec;ons § Relocates symbols from their relave locaons in the .o files to their final absolute memory locaons in the executable. § Updates all references to these symbols to reflect their new posions. Let’s look at these two steps in more detail…. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 8 Carnegie Mellon Three Kinds of Object Files (Modules) ¢ Relocatable object file (.o file) § Contains code and data in a form that can be combined with other relocatable object files to form executable object file. § Each .o file is produced from exactly one source (.c) file ¢ Executable object file (a.out file) § Contains code and data in a form that can be copied directly into memory and then executed. ¢ Shared object file (.so file) § Special type of relocatable object file that can be loaded into memory and linked dynamically, at either load ;me or run-;me. § Called Dynamic Link Libraries (DLLs) by Windows Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 9 Carnegie Mellon Executable and Linkable Format (ELF) ¢ Standard binary format for object files ¢ One unified format for § Relocatable object files (.o), § Executable object files (a.out) § Shared object files (.so) ¢ Generic name: ELF binaries Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 10 Carnegie Mellon ELF Object File Format ¢ Elf header § Word size, byte ordering, file type (.o, 0 exec, .so), machine type, etc. ELF header ¢ Segment header table Segment header table § Page size, virtual addresses memory segments (required for executables) (sec;ons), segment sizes. .text secon ¢ .text secon .rodata secon § Code .data secon ¢ .rodata sec7on .bss secon § Read only data: jump tables, ... .symtab sec7on ¢ .data secon .rel.txt sec7on § Ini;alized global variables .rel.data sec7on ¢ .bss secon .debug sec7on § Unini;alized global variables § “Block Started by Symbol” Sec7on header table § “BeNer Save Space” § Has sec;on header but occupies no space Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 11 Carnegie Mellon ELF Object File Format (cont.) ¢ .symtab secon 0 § Symbol table ELF header § Procedure and stac variable names Segment header table § Sec;on names and locaons (required for executables) ¢ .rel.text secon .text secon § Relocaon info for .text secon § Addresses of instruc;ons that will need to be .rodata secon modified in the executable .data secon § Instruc;ons for modifying. .bss secon ¢ .rel.data secon § Relocaon info for .data secon .symtab sec7on § Addresses of pointer data that will need to be .rel.txt sec7on modified in the merged executable .rel.data sec7on ¢ .debug secon .debug sec7on § Info for symbolic debugging (gcc -g) ¢ Sec7on header table Sec7on header table § Offsets and sizes of each sec;on Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 12 Carnegie Mellon Linker Symbols ¢ Global symbols § Symbols defined by module m that can be referenced by other modules. § E.g.: non-static C func;ons and non-static global variables. ¢ External symbols § Global symbols that are referenced by module m but defined by some other module. ¢ Local symbols § Symbols that are defined and referenced exclusively by module m. § E.g.: C func;ons and global variables defined with the static aribute. § Local linker symbols are not local program variables Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 13 Carnegie Mellon Step 1: Symbol Resolu7on Referencing a global… …that’s defined here int sum(int *a, int n);! int sum(int *a, int n)! ! {! int array[2] = {1, 2};! int i, s = 0;! ! ! int main()! for (i = 0; i < n; i++) {! {! s += a[i];! int val = sum(array, 2);! }! return val;! return s;! } main.c }! sum.c Defining a global Referencing Linker knows Linker knows a global… nothing of i or s nothing of val …that’s defined here Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 14 Carnegie Mellon Local Symbols ¢ Local non-stac C variables vs. local stac C variables § local non-stac C variables: stored on the stack § local stac C variables: stored in either .bss, or .data int f()! {! static int x = 0;! return x;! Compiler allocates space in .data for }! each definion of x ! int g()! Creates local symbols in the symbol {! table with unique names, e.g., x.1 and static int x = 1;! x.2. return x;! }! Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 15 Carnegie Mellon How Linker Resolves Duplicate Symbol Definions ¢ Program symbols are either strong or weak § Strong: procedures and ini;alized globals § Weak: unini;alized globals p1.c p2.c strong int foo=5; int foo; weak strong p1() { p2() { strong } } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 16 Carnegie Mellon Linker’s Symbol Rules ¢ Rule 1: Mul7ple strong symbols are not allowed § Each item can be defined only once § Otherwise: Linker error ¢ Rule 2: Given a strong symbol and mul7ple weak symbols, choose the strong symbol § References to the weak symbol resolve to the strong symbol ¢ Rule 3: If there are mul7ple weak symbols, pick an arbitrary one § Can override this with gcc –fno-common Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 17 Carnegie Mellon Linker Puzzles int x; p1() {} p1() {} Link ;me error: two strong symbols (p1) int x; int x; References to x will refer to the same p1() {} p2() {} unini;alized int. Is this what you really want? int x; double x; int y; p2() {} Writes to x in p2 might overwrite y! p1() {} Evil! int x=7; double x; Writes to x in p2 will overwrite y! int y=5; p2() {} Nasty! p1() {} int x=7; int x; References to x will refer to the same ini;alized p1() {} p2() {} variable. Nightmare scenario: two iden7cal weak structs, compiled by different compilers with different alignment rules. Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 18 Carnegie Mellon Global Variables ¢ Avoid if you can ¢ Otherwise § Use static if you can § Ini;alize if you define a global variable § Use extern if you reference an external global variable Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 19 Carnegie Mellon Step 2: Reloca7on Relocatable Object Files Executable Object File System code .text 0 Headers .data System data System code main() .text main.o swap() main() .text More system code int array[2]={1,2} .data System data sum.o .data int array[2]={1,2} sum() .text .symtab .debug Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspec;ve, Third Edi;on 20 Carnegie Mellon Reloca7on Entries int array[2] = {1, 2};! ! int main()! {! int val = sum(array, 2);! return val;! } main.c 0000000000000000 <main>:! 0: 48 83 ec 08 sub $0x8,%rsp! 4: be 02 00 00 00 mov $0x2,%esi! 9: bf 00 00 00 00 mov $0x0,%edi # %edi