☠️
smadi0x86 Playground
  • 💀Welcome to smadi0x86 Playground
    • 🍷Resources
    • 🚬Projects
    • 🎓Certifications
    • 📌Pinned
    • ❓Questions
    • 📞Contact
  • 🏞️Cloud Native
    • Docker
      • Quick Reference
      • Introduction
      • Containers
      • Images
      • Storage & Volumes
      • Security
      • Cheatsheet
    • Git
    • Serverless Framework
    • YAML
  • 🔨Software Engineering
    • System Design
    • Environment Variables
    • JSON Web Tokens
  • 👾Architecture
    • C Language
      • Introduction
      • Calling Conventions
      • GCC Compilation
      • Libraries & Linking
      • I/O
      • Files
      • Pointers
      • Dynamic Memory Allocation
      • Data Types
      • Strings Manipulation
      • Bit Manipulation
      • Pre-processors
      • Macros
      • Type Qualifiers
    • C/C++ Build Systems
      • Fundamentals for Linking
      • Symbolic Linking
      • Cross-Platform Compilation
      • CMake for Building and Linking
      • Shared Libraries
      • Dynamic Linking and Dependency Management
    • Operating Systems
      • OS & Architecture
      • Processes
      • CPU Scheduling
      • Memory Management
  • 🛩️Cyber Warfare
    • Flight Physics
    • Communication
      • PWM & PPM
      • MAVLink
  • 🏴‍☠️Offensive Security
    • Active Directory
      • Introduction
    • Web Attacks
      • Server Side
        • OS Command Injection
        • Information Disclosure
        • Directory Traversal
        • Business Logic
        • Authentication
        • File Upload
        • SSRF
      • Client Side
        • CSRF
        • XSS
    • Recon
      • Active
        • Host discovery
        • Nmap
        • Mass Scan
      • Passive
        • Metadata
      • Web Applications
        • Discovery
        • Subdomains & Directories
        • SSL Certs
        • CMS
        • WAF Detection
      • Firewall Evasion
  • Binary Exploitation
    • Stack Smashing
      • x86
      • x86_64
    • pwntools
      • Processes and Communication
      • Logging and Context
      • Cyclic
      • Packing
      • ELF
      • ROP
  • 😈Advanced Persistent Threat
    • C2
      • Sliver
    • Malware
      • Windows Internals
        • PEB
      • Academy
        • Basics
      • Sektor7
        • Essentials
  • 💌Certifications
    • AWS Certified Cloud Practitioner (CLF-C01)
      • Cloud Foundations
      • Domain 1: Cloud Concepts
      • Domain 2: Security and Compliance
      • Domain 3: Technology
      • Domain 4: Billing and Pricing
    • AWS Certified Solutions Architect - Associate (SAA-C03)
      • Foundation
    • Certified Kubernetes Administrator (CKA)
      • Core Concepts
      • Scheduling
      • Logging & Monitoring
      • Application Lifecycle Management
      • Cluster Maintenance
      • Security
      • Storage
      • Networking
      • Design Kubernetes Cluster
      • Kubernetes The Kubeadm Way
      • Troubleshooting
      • JSONPATH
      • Lightning Lab
      • Mock Exams
      • Killer Shell
    • Certified Kubernetes Security (CKS)
      • Foundation
      • Cluster Setup
      • Cluster Hardening
      • Supply Chain Security
      • Runtime Security
      • System Hardening
      • Killer Shell
    • (KGAC-101) Kong Gateway Foundations
      • Introduction to APIs and API Management
      • Introduction to Kong Gateway
      • Getting Started with Kong Enterprise
      • Getting Started with Kong Konnect
      • Introduction to Kong Plugins
  • 📜Blog Posts
    • Modern Solutions For Preventing Ransomware Attacks
Powered by GitBook
On this page
  • Dynamic Linking
  • The Mechanics of Dynamic Linking
  • Symbol Resolution
  • Dependency Management
  • Example: Using CMake for Dependency Management with OpenCV
  • Advanced Dependency Management Techniques:
  • Versioning and Compatibility
  1. Architecture
  2. C/C++ Build Systems

Dynamic Linking and Dependency Management

Dynamic Linking

Dynamic linking allows programs to rely on shared libraries (.so on Linux, .dll on Windows, .dylib on macOS) at runtime instead of embedding library code directly in the executable.

This offers several advantages:

  • Reduced Executable Size: Programs only reference library functions, not the entire library code, leading to smaller executables.

  • Improved Code Reusability: Shared libraries can be used by multiple programs, promoting code efficiency and reducing code duplication.

  • Simplified Updates: Updates to a shared library can benefit all linked programs without recompiling them individually.

The Mechanics of Dynamic Linking

Creating Shared Libraries:

  • Compilers like GCC use the -fPIC flag to generate Position Independent Code (PIC) suitable for shared libraries. This ensures code can function from various memory addresses at runtime.

  • The linker combines object files and metadata into a single shared library file.

Loading at Runtime:

  • When a program starts and needs a shared library:

    • The dynamic linker searches for the library based on system-wide search paths or environment variables.

    • It loads the library into memory.

    • It performs symbol resolution, matching unresolved function and variable references in the program with definitions found in the loaded library.

Symbol Resolution

Shared libraries often depend on other shared libraries. The dynamic linker employs a recursive resolution process:

  • It identifies unresolved symbols in the program's dependency chain.

  • It searches for the required libraries based on information embedded within them.

  • This process continues until all symbols are resolved and the program can execute correctly.

Dependency Management

Managing dependencies across libraries and different project versions can be a hurdle. Missing libraries or version conflicts can cause runtime errors.

Strategies:

Package Managers: Platforms like Linux have package managers that automate library installation, versioning, and dependency resolution (e.g., apt-get, yum).

CMake's find_package: CMake simplifies dependency management by searching for and linking with shared libraries during the build process.

It offers features like:

  • Finding specific libraries (find_package(Threads REQUIRED)).

  • Configuring compiler include directories (target_include_directories).

  • Linking with libraries (target_link_libraries(MyProgram PRIVATE Threads::Threads)).

Example: Using CMake for Dependency Management with OpenCV

cmake_minimum_required(VERSION 3.0)

project(MyImageProcessingApp)

# Find and link with OpenCV library
find_package(OpenCV REQUIRED)

# Include OpenCV header directories
target_include_directories(MyProgram PRIVATE "${OpenCV_INCLUDE_DIRS}")

# Link the program with OpenCV libraries
target_link_libraries(MyProgram PRIVATE "${OpenCV_LIBRARIES}")

# Source files
add_executable(MyImageProcessingApp main.cpp image_processing.cpp)

Explanation:

  • find_package(OpenCV REQUIRED) attempts to find and link with the OpenCV library.

  • target_include_directories specifies the OpenCV header directory for the compiler.

  • target_link_libraries links the program with the OpenCV libraries.

Advanced Dependency Management Techniques:

Dependency Graph Visualization Tools: Tools like CMakeLists.txt Visualizer generate dependency graphs, providing a visual representation of library dependencies. This helps identify potential conflicts and optimize build processes.

External Project Management: For complex projects with external dependencies (e.g., from repositories), CMake can integrate with tools like FetchContent or ExternalProject. These tools automate downloading and building external projects as part of the build process.

Versioning and Compatibility

Shared libraries have versions (e.g., libopencv.so.4.8.0) to ensure compatibility with applications. Major version changes might introduce breaking changes, requiring program updates.

Tools like ldconfig (Linux) manage the dynamic linker cache, reflecting available library versions.

Dependency Management Tools: Package managers and CMake often provide mechanisms for specifying required library versions, helping to manage compatibility concerns.

PreviousShared LibrariesNextOperating Systems
👾