# rui314/mold architecture

> Architecture diagram of the GitHub repository rui314/mold, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rui314/mold
- Repository: https://github.com/rui314/mold

## Overview

mold is a high-performance, cross-platform ELF linker. The principal workflow takes linker arguments and object files, resolves their sections and symbols, applies link-time transformations and target-specific relocation logic, then emits an executable or shared library. The graph also preserves documented multi-architecture support and indexed subsystems such as LTO, garbage collection, identical code folding, and debug-index generation. Some internal links are dashed because the sampled excerpts and index identify modules but do not expose their runtime call sites.

Main entry points: [`src/driver.rs`](https://github.com/rui314/mold/blob/main/src/driver.rs) (Link driver), [`src/input_files.rs`](https://github.com/rui314/mold/blob/main/src/input_files.rs) (Input files), [`src/passes.rs`](https://github.com/rui314/mold/blob/main/src/passes.rs) (Link passes), [`src/output_file.rs`](https://github.com/rui314/mold/blob/main/src/output_file.rs) (Output file)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Link entry"]
  node_cli["CLI entry<br/>[main.rs]"]
  node_arguments["Argument parsing<br/>[cmdline.rs]"]
  node_driver["Link driver<br/>[driver.rs]"]
end

subgraph group_analysis["Input analysis"]
  node_inputs["Input files<br/>[input_files.rs]"]
  node_mapped["Mapped files<br/>[mapped_file.rs]"]
  node_sections["Input sections<br/>[input_sections.rs]"]
  node_symbols[("Symbol resolution<br/>[symbol.rs]")]
  node_linkscript["Linker scripts<br/>[linker_script.rs]"]
end

subgraph group_linking["Link construction"]
  node_passes["Link passes<br/>[passes.rs]"]
  node_gc["Section garbage collection<br/>[gc_sections.rs]"]
  node_icf["Identical code folding<br/>[icf.rs]"]
  node_lto["Link-time optimization<br/>[lto.rs]"]
  node_target["Target backend<br/>[mod.rs]"]
  node_thunks["Range thunks<br/>[thunks.rs]"]
end

subgraph group_backend["Output backend"]
  node_merged["Merged sections<br/>[merged.rs]"]
  node_output_sections["Output sections<br/>[output_section.rs]"]
  node_chunks["ELF output chunks<br/>[mod.rs]"]
  node_output["Output file<br/>[output_file.rs]"]
  node_debug_index["GDB debug index<br/>[gdb_index.rs]"]
end

node_caller(("Build invocation"))

node_caller -.->|"invokes"| node_cli
node_cli -.->|"starts link"| node_driver
node_driver -.->|"parses options"| node_arguments
node_driver -.->|"loads inputs"| node_inputs
node_inputs -->|"maps files"| node_mapped
node_inputs -->|"parses sections"| node_sections
node_inputs -->|"reads symbols"| node_symbols
node_driver -.->|"uses scripts"| node_linkscript
node_driver -.->|"runs passes"| node_passes
node_passes -.->|"performs collection"| node_gc
node_passes -.->|"performs folding"| node_icf
node_passes -.->|"invokes LTO"| node_lto
node_sections -->|"deduplicates fragments"| node_merged
node_sections -->|"provides members"| node_output_sections
node_target -->|"defines target hooks"| node_thunks
node_output_sections -->|"uses thunks"| node_thunks
node_driver -.->|"selects target"| node_target
node_merged -.->|"writes data"| node_output
node_output_sections -.->|"writes sections"| node_output
node_chunks -.->|"writes ELF chunks"| node_output
node_driver -.->|"generates index"| node_debug_index
node_caller -->|"receives linked ELF"| node_output

click node_cli "https://github.com/rui314/mold/blob/main/cli/main.rs"
click node_arguments "https://github.com/rui314/mold/blob/main/src/cmdline.rs"
click node_driver "https://github.com/rui314/mold/blob/main/src/driver.rs"
click node_inputs "https://github.com/rui314/mold/blob/main/src/input_files.rs"
click node_mapped "https://github.com/rui314/mold/blob/main/src/mapped_file.rs"
click node_sections "https://github.com/rui314/mold/blob/main/src/input_sections.rs"
click node_symbols "https://github.com/rui314/mold/blob/main/src/symbol.rs"
click node_linkscript "https://github.com/rui314/mold/blob/main/src/linker_script.rs"
click node_passes "https://github.com/rui314/mold/blob/main/src/passes.rs"
click node_gc "https://github.com/rui314/mold/blob/main/src/gc_sections.rs"
click node_icf "https://github.com/rui314/mold/blob/main/src/icf.rs"
click node_lto "https://github.com/rui314/mold/blob/main/src/lto.rs"
click node_target "https://github.com/rui314/mold/blob/main/src/target/mod.rs"
click node_thunks "https://github.com/rui314/mold/blob/main/src/thunks.rs"
click node_merged "https://github.com/rui314/mold/blob/main/src/chunks/merged.rs"
click node_output_sections "https://github.com/rui314/mold/blob/main/src/chunks/output_section.rs"
click node_chunks "https://github.com/rui314/mold/blob/main/src/chunks/mod.rs"
click node_output "https://github.com/rui314/mold/blob/main/src/output_file.rs"
click node_debug_index "https://github.com/rui314/mold/blob/main/src/gdb_index.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_arguments,node_driver toneBlue
class node_inputs,node_mapped,node_sections,node_symbols,node_linkscript toneAmber
class node_passes,node_gc,node_icf,node_lto,node_target,node_thunks toneMint
class node_merged,node_output_sections,node_chunks,node_output,node_debug_index toneRose
class node_caller toneIndigo
```

## Components

### Link entry

- [CLI entry](https://github.com/rui314/mold/blob/main/cli/main.rs): `cli/main.rs`
- [Argument parsing](https://github.com/rui314/mold/blob/main/src/cmdline.rs): `src/cmdline.rs`
- [Link driver](https://github.com/rui314/mold/blob/main/src/driver.rs): `src/driver.rs`

### Input analysis

- [Input files](https://github.com/rui314/mold/blob/main/src/input_files.rs): `src/input_files.rs`
- [Mapped files](https://github.com/rui314/mold/blob/main/src/mapped_file.rs): `src/mapped_file.rs`
- [Input sections](https://github.com/rui314/mold/blob/main/src/input_sections.rs): `src/input_sections.rs`
- [Symbol resolution](https://github.com/rui314/mold/blob/main/src/symbol.rs): `src/symbol.rs`
- [Linker scripts](https://github.com/rui314/mold/blob/main/src/linker_script.rs): `src/linker_script.rs`

### Link construction

- [Link passes](https://github.com/rui314/mold/blob/main/src/passes.rs): `src/passes.rs`
- [Section garbage collection](https://github.com/rui314/mold/blob/main/src/gc_sections.rs): `src/gc_sections.rs`
- [Identical code folding](https://github.com/rui314/mold/blob/main/src/icf.rs): `src/icf.rs`
- [Link-time optimization](https://github.com/rui314/mold/blob/main/src/lto.rs): `src/lto.rs`
- [Target backend](https://github.com/rui314/mold/blob/main/src/target/mod.rs): `src/target/mod.rs`
- [Range thunks](https://github.com/rui314/mold/blob/main/src/thunks.rs): `src/thunks.rs`

### Output backend

- [Merged sections](https://github.com/rui314/mold/blob/main/src/chunks/merged.rs): `src/chunks/merged.rs`
- [Output sections](https://github.com/rui314/mold/blob/main/src/chunks/output_section.rs): `src/chunks/output_section.rs`
- [ELF output chunks](https://github.com/rui314/mold/blob/main/src/chunks/mod.rs): `src/chunks/mod.rs`
- [Output file](https://github.com/rui314/mold/blob/main/src/output_file.rs): `src/output_file.rs`
- [GDB debug index](https://github.com/rui314/mold/blob/main/src/gdb_index.rs): `src/gdb_index.rs`

### Other components

- **Build invocation**

## Connections

- Build invocation → CLI entry: invokes
- CLI entry → Link driver: starts link
- Link driver → Argument parsing: parses options
- Link driver → Input files: loads inputs
- Input files → Mapped files: maps files (evidence: `src/input_files.rs`)
- Input files → Input sections: parses sections (evidence: `src/input_files.rs`)
- Input files → Symbol resolution: reads symbols (evidence: `src/input_files.rs`)
- Link driver → Linker scripts: uses scripts
- Link driver → Link passes: runs passes
- Link passes → Section garbage collection: performs collection
- Link passes → Identical code folding: performs folding
- Link passes → Link-time optimization: invokes LTO
- Input sections → Merged sections: deduplicates fragments (evidence: `src/chunks/merged.rs`)
- Input sections → Output sections: provides members (evidence: `src/chunks/output_section.rs`)
- Target backend → Range thunks: defines target hooks (evidence: `src/target/mod.rs`)
- Output sections → Range thunks: uses thunks (evidence: `src/chunks/output_section.rs`)
- Link driver → Target backend: selects target
- Merged sections → Output file: writes data (evidence: `src/chunks/merged.rs`)
- Output sections → Output file: writes sections (evidence: `src/chunks/output_section.rs`)
- ELF output chunks → Output file: writes ELF chunks
- Link driver → GDB debug index: generates index
- Build invocation → Output file: receives linked ELF (evidence: `README.md`)
