How pietroalbini/plinky works
Project Type and Purpose
• plinky is a systems‐level, command‐line ELF linker (compiler‐toolchain component) targeting x86 and x86-64 Linux.
• It’s structured as a Rust “workspace” with multiple crates providing:
• The main linker binary (plinky)
• ELF parsing & writing (plinky_elf)
• Archive reading (.a files) (plinky_ar)
• pkg‐config emulation (plinky_pkg_config)
• Diagnostics & pretty‐printing (plinky_diagnostics)
• Macros & utilities (plinky_macros, plinky_utils)
• A test harness (plinky_test_harness)
Key Architectural Components
a. CLI Frontend (plinky crate)
• Modules: cli::lexer, cli::parser
• Entry point: main.rs → parser → builds a CliInput structure
b. Input Loaders
• plinky_ar: reads .a archives, exposes object file streams
• plinky_pkg_config: simulates pkg‐config to discover linker flags
• plinky_elf: reads individual ELF object files, yields in‐memory representations
c. Intermediate Representation (plinky/src/repr)
• Uniform in‐memory model of segments, sections, symbols, relocations
d. Linker Core (plinky/src/linker.rs & passes/)
• Passes pipeline:
1. load_inputs (read objects, merge archives, run pkg‐config)
2. analyze_relocations, convert_modes
3. merge_sections, deduplicate, layout sections & segments
4. generate_dynamic, build_elf (sections, symbols, string tables, hash tables)
5. inject symbol table, GC sections, RELRO/stack flags
6. write_to_disk
e. Diagnostics & Debugging
• plinky_diagnostics: builders, contexts, widgets render errors/warnings
• debug_print: visual inspect of in‐memory ELF layout and GC data
f. Utilities & Macros
• plinky_utils: common algorithms (bitfields, Jaro similarity, shell quoting)
• plinky_macros / plinky_macros_quote: custom derive & parser utilities
Interactions & Data Flow
• User invokes plinky CLI → parse flags/inputs
• plinky uses plinky_pkg_config to expand –l flags
• plinky_ar opens archives → yields object file paths/streams
• plinky_elf parses each ELF object → yields ReprObject structs
• Passes sequentially transform a global ReprProgram:
• load_inputs → merge sections → analyze relocations → layout segments
• build_elf → produces final ELF bytes
• write_to_disk writes the output executable
• On errors, plinky_diagnostics formats and emits diagnostics
Architectural Patterns & Principles
• Modular crate decomposition (single‐responsibility crates)
• Pipeline/chain‐of‐responsibility for linker passes
• Clear separation between parsing, representation, transformation, and emission
• No heavy external build/run‐time dependencies (self‐contained)