linking 15 213 introduc on to computer systems
play

Linking 15-213: Introduc;on to Computer Systems 13 th - PowerPoint PPT Presentation

Carnegie Mellon Linking 15-213: Introduc;on to Computer Systems 13 th Lecture, Oct. 13, 2015 Instructors: Randal E. Bryant and David R. OHallaron 1


  1. Carnegie Mellon Linking ¡ ¡ 15-­‑213: ¡Introduc;on ¡to ¡Computer ¡Systems ¡ 13 th ¡Lecture, ¡Oct. ¡13, ¡2015 ¡ Instructors: ¡ ¡ Randal ¡E. ¡Bryant ¡and ¡David ¡R. ¡O’Hallaron ¡ 1 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  2. Carnegie Mellon Today ¡ ¢ Linking ¡ ¢ Case ¡study: ¡Library ¡interposi7oning ¡ 2 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  3. � � � � 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; � } � } � sum.c main.c 3 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  4. 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) ¡ Separately ¡compiled ¡ main.o sum.o 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 ) ¡ 4 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  5. 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 ¡ 5 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  6. Carnegie Mellon Why ¡Linkers? ¡(cont) ¡ ¢ Reason ¡2: ¡Efficiency ¡ § Time: ¡Separate ¡compila;on ¡ § 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. ¡ 6 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  7. Carnegie Mellon What ¡Do ¡Linkers ¡Do? ¡ ¢ Step ¡1: ¡Symbol ¡resolu7on ¡ § 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 ¡loca;on ¡of ¡symbol. ¡ § During ¡symbol ¡resolu7on ¡step, ¡the ¡linker ¡associates ¡each ¡symbol ¡reference ¡ with ¡exactly ¡one ¡symbol ¡defini7on. ¡ 7 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  8. 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 ¡rela;ve ¡loca;ons ¡in ¡the ¡ .o ¡files ¡to ¡ their ¡final ¡absolute ¡memory ¡loca;ons ¡in ¡the ¡executable. ¡ § Updates ¡all ¡references ¡to ¡these ¡symbols ¡to ¡reflect ¡their ¡new ¡ posi;ons. ¡ Let’s ¡look ¡at ¡these ¡two ¡steps ¡in ¡more ¡detail…. ¡ 8 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  9. 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 ¡ 9 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  10. 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 ¡ 10 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

  11. 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 ¡ (required ¡for ¡executables) ¡ § Page ¡size, ¡virtual ¡addresses ¡memory ¡segments ¡ (sec;ons), ¡segment ¡sizes. ¡ .text ¡sec7on ¡ ¢ .text ¡sec7on ¡ .rodata ¡sec7on ¡ § Code ¡ .data ¡sec7on ¡ ¢ .rodata sec7on ¡ .bss ¡sec7on ¡ § Read ¡only ¡data: ¡jump ¡tables, ¡... ¡ .symtab sec7on ¡ ¢ .data ¡sec7on ¡ .rel.txt sec7on ¡ § Ini;alized ¡global ¡variables ¡ .rel.data sec7on ¡ ¢ .bss ¡sec7on ¡ .debug sec7on ¡ § Unini;alized ¡global ¡variables ¡ § “Block ¡Started ¡by ¡Symbol” ¡ Sec7on ¡header ¡table ¡ § “BeNer ¡Save ¡Space” ¡ § Has ¡sec;on ¡header ¡but ¡occupies ¡no ¡space ¡ 11 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡ ¡

  12. Carnegie Mellon ELF ¡Object ¡File ¡Format ¡(cont.) ¡ ¢ .symtab ¡sec7on ¡ 0 ¡ § Symbol ¡table ¡ ELF ¡header ¡ § Procedure ¡and ¡sta;c ¡variable ¡names ¡ Segment ¡header ¡table ¡ § Sec;on ¡names ¡and ¡loca;ons ¡ (required ¡for ¡executables) ¡ ¢ .rel.text ¡sec7on ¡ .text ¡sec7on ¡ § Reloca;on ¡info ¡for ¡ .text ¡ sec;on ¡ .rodata ¡sec7on ¡ § Addresses ¡of ¡instruc;ons ¡that ¡will ¡need ¡to ¡be ¡ modified ¡in ¡the ¡executable ¡ .data ¡sec7on ¡ § Instruc;ons ¡for ¡modifying. ¡ .bss ¡sec7on ¡ ¢ .rel.data ¡sec7on ¡ .symtab sec7on ¡ § Reloca;on ¡info ¡for ¡ .data ¡ sec;on ¡ § Addresses ¡of ¡pointer ¡data ¡that ¡will ¡need ¡to ¡be ¡ .rel.txt sec7on ¡ modified ¡in ¡the ¡merged ¡executable ¡ .rel.data sec7on ¡ ¢ .debug ¡sec7on ¡ .debug sec7on ¡ § Info ¡for ¡symbolic ¡debugging ¡( gcc -g ) ¡ ¢ Sec7on ¡header ¡table ¡ Sec7on ¡header ¡table ¡ § Offsets ¡and ¡sizes ¡of ¡each ¡sec;on ¡ 12 Bryant ¡and ¡O’Hallaron, ¡Computer ¡Systems: ¡A ¡Programmer’s ¡Perspec;ve, ¡Third ¡Edi;on ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend