CS 134

Key Points: Dynamic Linking

Static vs. Dynamic Linking

  1. Static Linking Challenges:
    • Each program includes a complete copy of library code
    • Results in significant disk and memory waste
    • Updates to libraries require recompiling all programs that use them
    • Simple, but inefficient use of system resources
  2. Benefits of Dynamic Linking:
    • Library code is shared between programs
    • Updates can be made without recompiling
    • Reduced memory and disk usage (over static linking)
    • Supports runtime loading of library code
  3. Implementation Requirements:
    • Position-independent code (PIC) for libraries
    • System support for shared memory pages
    • Runtime symbol resolution
    • Library version management

Dynamic Linking Mechanisms

  1. Global Offset Table (GOT):
    • Table of function/data addresses
    • One per process
    • Updated at load time
    • Enables position-independent code
  2. Procedure Linkage Table (PLT):
    • Contains function stubs
    • Supports lazy binding
    • Reduces startup overhead
    • Maintains read-only code pages
  3. Symbol Resolution:
    • Performed by dynamic linker
    • Uses library search paths
    • Handles version compatibility
    • Supports symbol versioning

Advanced Features

  1. Lazy Binding:
    • Functions resolved on first use
    • Reduces program startup time
    • Uses PLT stub functions
    • Automatically handled by dynamic linker
  2. Library Versioning:
    • Multiple versions can coexist
    • Compilers record required library versions in programs
    • Symbol-level versioning possible
    • Maintains backward compatibility
  3. Runtime Loading:
    • Libraries can be loaded on demand
    • Supports plugin architectures
    • Uses dlopen/dlsym interface
    • Enables dynamic code extension

System Considerations

  1. Security Aspects:
    • Library search path security
    • LD_PRELOAD restrictions
    • Protected system directories
    • Signature verification on some systems
  2. Performance Impact:
    • Small overhead from indirect calls
    • Reduced memory usage through sharing
    • Startup time vs. lazy binding trade-off
    • Modern CPUs optimize indirect calls
  3. System Features:
    • Library path configuration
    • Version compatibility checking
    • Symbol interposition support
    • Debug and development tools

Remember

  • Dynamic linking saves memory by sharing library code
  • Indirect calls through GOT enable position-independent code
  • Lazy binding reduces program startup overhead
  • Library versioning maintains compatibility
  • Security measures protect against malicious libraries
  • Modern systems balance flexibility and performance
  • Runtime loading enables powerful plugin architectures

(When logged in, completion status appears here.)