linking 15 213 introduc0on to computer systems
play

Linking 15-213: Introduc0on to Computer Systems 11 th Lecture, - PowerPoint PPT Presentation

Carnegie Mellon Linking 15-213: Introduc0on to Computer Systems 11 th Lecture, Sept. 30, 2010 Instructors: Randy Bryant and Dave OHallaron 1 Carnegie Mellon Today


  1. Carnegie Mellon Linking ¡ 15-­‑213: ¡Introduc0on ¡to ¡Computer ¡Systems ¡ 11 th ¡Lecture, ¡Sept. ¡30, ¡2010 ¡ Instructors: ¡ ¡ Randy ¡Bryant ¡and ¡Dave ¡O’Hallaron ¡ 1

  2. Carnegie Mellon Today ¡  Linking ¡  Case ¡study: ¡Library ¡interposi7oning ¡ 2

  3. Carnegie Mellon Example ¡C ¡Program ¡ main.c swap.c int buf[2] = {1, 2}; extern int buf[]; int main() int *bufp0 = &buf[0]; { static int *bufp1; swap(); return 0; void swap() } { int temp; bufp1 = &buf[1]; temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } 3

  4. Carnegie Mellon Sta7c ¡Linking ¡  Programs ¡are ¡translated ¡and ¡linked ¡using ¡a ¡ compiler ¡driver : ¡  unix> gcc -O2 -g -o p main.c swap.c  unix> ./p main.c swap.c Source ¡files ¡ Translators ¡ Translators ¡ (cpp, ¡cc1, ¡as) ¡ (cpp, ¡cc1, ¡as) ¡ Separately ¡compiled ¡ main.o swap.o relocatable ¡object ¡files ¡ Linker ¡(ld) ¡ Fully ¡linked ¡executable ¡object ¡file ¡ p (contains ¡code ¡and ¡data ¡for ¡all ¡func;ons ¡ defined ¡in ¡ main.c and swap.c ) ¡ 4

  5. Carnegie Mellon Why ¡Linkers? ¡  Reason ¡1: ¡Modularity ¡  Program ¡can ¡be ¡wriKen ¡as ¡a ¡collec0on ¡of ¡smaller ¡source ¡files, ¡ rather ¡than ¡one ¡monolithic ¡mass. ¡  Can ¡build ¡libraries ¡of ¡common ¡func0ons ¡(more ¡on ¡this ¡later) ¡  e.g., ¡Math ¡library, ¡standard ¡C ¡library ¡ 5

  6. Carnegie Mellon Why ¡Linkers? ¡(cont) ¡  Reason ¡2: ¡Efficiency ¡  Time: ¡Separate ¡compila0on ¡  Change ¡one ¡source ¡file, ¡compile, ¡and ¡then ¡relink. ¡  No ¡need ¡to ¡recompile ¡other ¡source ¡files. ¡  Space: ¡Libraries ¡ ¡  Common ¡func0ons ¡can ¡be ¡aggregated ¡into ¡a ¡single ¡file... ¡  Yet ¡executable ¡files ¡and ¡running ¡memory ¡images ¡contain ¡only ¡ code ¡for ¡the ¡func0ons ¡they ¡actually ¡use. ¡ 6

  7. Carnegie Mellon What ¡Do ¡Linkers ¡Do? ¡  Step ¡1. ¡Symbol ¡resolu7on ¡  Programs ¡define ¡and ¡reference ¡ symbols ¡(variables ¡and ¡func0ons): ¡  void swap() {…} /* define symbol swap */  swap(); /* reference symbol a */  int *xp = &x; /* define symbol xp, reference x */ ¡  Symbol ¡defini0ons ¡are ¡stored ¡(by ¡compiler) ¡in ¡ symbol ¡table . ¡  Symbol ¡table ¡is ¡an ¡array ¡of ¡structs ¡  Each ¡entry ¡includes ¡name, ¡size, ¡and ¡loca0on ¡of ¡symbol. ¡  Linker ¡associates ¡each ¡symbol ¡reference ¡with ¡exactly ¡one ¡symbol ¡defini0on. ¡ 7

  8. Carnegie Mellon What ¡Do ¡Linkers ¡Do? ¡(cont) ¡  Step ¡2. ¡Reloca7on ¡  Merges ¡separate ¡code ¡and ¡data ¡sec0ons ¡into ¡single ¡sec0ons ¡  Relocates ¡symbols ¡from ¡their ¡rela0ve ¡loca0ons ¡in ¡the ¡ .o ¡files ¡to ¡ their ¡final ¡absolute ¡memory ¡loca0ons ¡in ¡the ¡executable. ¡  Updates ¡all ¡references ¡to ¡these ¡symbols ¡to ¡reflect ¡their ¡new ¡ posi0ons. ¡ 8

  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 ¡0me ¡or ¡run-­‑0me. ¡  Called ¡ Dynamic ¡Link ¡Libraries ¡(DLLs) ¡by ¡Windows ¡ 9

  10. Carnegie Mellon Executable ¡and ¡Linkable ¡Format ¡(ELF) ¡  Standard ¡binary ¡format ¡for ¡object ¡files ¡  Originally ¡proposed ¡by ¡AT&T ¡System ¡V ¡Unix ¡  Later ¡adopted ¡by ¡BSD ¡Unix ¡variants ¡and ¡Linux ¡  One ¡unified ¡format ¡for ¡ ¡  Relocatable ¡object ¡files ¡( .o ), ¡ ¡  Executable ¡object ¡files ¡ (a.out ) ¡  Shared ¡object ¡files ¡( .so ) ¡  Generic ¡name: ¡ELF ¡binaries ¡ 10

  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 ¡ (sec0ons), ¡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 ¡  Ini0alized ¡global ¡variables ¡ .rel.data sec7on ¡  .bss ¡sec7on ¡ .debug sec7on ¡  Unini0alized ¡global ¡variables ¡ Sec7on ¡header ¡table ¡  “Block ¡Started ¡by ¡Symbol” ¡  “BeKer ¡Save ¡Space” ¡  Has ¡sec0on ¡header ¡but ¡occupies ¡no ¡space ¡ 11

  12. Carnegie Mellon ELF ¡Object ¡File ¡Format ¡(cont.) ¡  .symtab ¡sec7on ¡ 0 ¡  Symbol ¡table ¡ ELF ¡header ¡  Procedure ¡and ¡sta0c ¡variable ¡names ¡ Segment ¡header ¡table ¡  Sec0on ¡names ¡and ¡loca0ons ¡ (required ¡for ¡executables) ¡  .rel.text ¡sec7on ¡ .text ¡sec7on ¡  Reloca0on ¡info ¡for ¡ .text ¡ sec0on ¡ .rodata ¡sec7on ¡  Addresses ¡of ¡instruc0ons ¡that ¡will ¡need ¡to ¡be ¡ modified ¡in ¡the ¡executable ¡ .data ¡sec7on ¡  Instruc0ons ¡for ¡modifying. ¡ .bss ¡sec7on ¡  .rel.data ¡sec7on ¡ .symtab sec7on ¡  Reloca0on ¡info ¡for ¡ .data ¡ sec0on ¡ .rel.txt sec7on ¡  Addresses ¡of ¡pointer ¡data ¡that ¡will ¡need ¡to ¡be ¡ 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 ¡sec0on ¡ 12

  13. Carnegie Mellon Linker ¡Symbols ¡ ¡  Global ¡symbols ¡  Symbols ¡defined ¡by ¡module ¡ m ¡that ¡can ¡be ¡referenced ¡by ¡other ¡modules. ¡  E.g.: ¡non-­‑ static ¡C ¡func0ons ¡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 ¡func0ons ¡and ¡variables ¡defined ¡with ¡the ¡ static aKribute. ¡  Local ¡linker ¡symbols ¡are ¡ not ¡local ¡program ¡variables ¡ 13

  14. Carnegie Mellon Resolving ¡Symbols ¡ Global ¡ External ¡ Local ¡ Global ¡ int buf[2] = {1, 2}; extern int buf[]; int main() int *bufp0 = &buf[0]; { static int *bufp1; swap(); Global ¡ return 0; void swap() } { main.c int temp; External ¡ Linker ¡knows ¡ bufp1 = &buf[1]; nothing ¡of ¡temp ¡ temp = *bufp0; *bufp0 = *bufp1; *bufp1 = temp; } swap.c 14

  15. Carnegie Mellon Reloca7ng ¡Code ¡and ¡Data ¡ Relocatable ¡Object ¡Files ¡ Executable ¡Object ¡File ¡ .text 0 ¡ System ¡code ¡ Headers ¡ .data System ¡data ¡ System ¡code ¡ main() .text main.o swap() .text main() More ¡system ¡code ¡ .data int buf[2]={1,2} System ¡data ¡ .data swap.o int buf[2]={1,2} int *bufp0=&buf[0] .text swap() .bss int *bufp1 .data .symtab int *bufp0=&buf[0] .debug static int *bufp1 .bss Even ¡though ¡private ¡to ¡swap, ¡requires ¡alloca7on ¡in ¡.bss ¡ 15

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