qld 0.1.1

A fast, parallel linker compatible with GNU ld, gold, lld and mold
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Identical code folding (`--icf=all`, `--icf=safe`), pipeline stage 8
//! second half: builds [`IcfInput`] from the live sections and applies the
//! result of [`fold_identical`].
//!
//! **Foldable** sections are live, allocated, read-only, non-TLS regular
//! sections with contents, outside `KEEP` output sections. The *key* that
//! must match exactly is `(type, flags, entry size, alignment, output
//! section, size)`, interned to a dense number.
//!
//! **Relocation targets** are input sections plus offsets (compared by
//! equivalence class), merged-string pieces (compared by output position,
//! since merging runs first), or symbols and absolute values (compared by
//! identity).
//!
//! **Address significance** for `--icf=safe`: a file's `.llvm_addrsig`
//! table when it has one; otherwise (GCC objects) an executable section is
//! significant when any relocation other than `R_X86_64_PLT32` refers to
//! it, and read-only data never is (`docs/optimizations.md`).
//!
//! Folded sections leave the output; references to them, their symbols and
//! their `.eh_frame` FDEs go to the kept section.

#![deny(clippy::arithmetic_side_effects)]

use std::sync::atomic::{AtomicBool, Ordering};

use hashbrown::HashMap;
use rayon::prelude::*;

use crate::diag::{Diagnostic, DiagnosticSink, Severity};
use crate::elf::read::Relocations;
use crate::elf::read::consts::{
    SHF_ALLOC, SHF_EXECINSTR, SHF_TLS, SHF_WRITE, SHT_NOBITS, x86_64::R_X86_64_PLT32,
};
use crate::error::{Error, Result};
use crate::ids::SectionId;
use crate::passes::{Csr, IcfInput, IcfMode, IcfReloc, IcfSection, IcfTarget, fold_identical};

use super::merge::Merged;
use super::object::SectionKind;
use super::place::Placement;
use super::refs::{Def, Refs};
use super::sections::NONE;

/// Section properties that must match for two sections to fold.
type Key = (u32, u64, u64, u64, u32, u64);

fn foldable_section<F: crate::elf::read::ElfFormat>(
    refs: &Refs<'_, '_, F>,
    placement: &Placement<'_>,
    id: SectionId,
) -> Option<Key> {
    if !refs.sections.is_live(id) {
        return None;
    }
    let (file, index) = refs.sections.locate(id)?;
    let section = refs.files.get(file)?.object.as_ref()?.section(index)?;
    let header = &section.header;
    let flags = header.sh_flags;
    if section.kind != SectionKind::Regular
        || flags & SHF_ALLOC == 0
        || flags & (SHF_WRITE | SHF_TLS) != 0
        || header.sh_type == SHT_NOBITS
        || header.sh_size == 0
    {
        return None;
    }
    let output = placement.output_of(id)?;
    if placement
        .outputs
        .get(output as usize)
        .is_none_or(|o| o.keep)
    {
        return None;
    }
    Some((
        header.sh_type,
        flags,
        header.sh_entsize,
        header.sh_addralign,
        output,
        header.sh_size,
    ))
}

/// Decodes the ULEB128 symbol indices of an `.llvm_addrsig` section.
fn addrsig_symbols(data: &[u8]) -> Vec<usize> {
    let mut out = Vec::new();
    let mut value = 0u64;
    let mut shift = 0u32;
    for &byte in data {
        if shift < 64 {
            value |= u64::from(byte & 0x7f) << shift;
        }
        if byte & 0x80 == 0 {
            if let Ok(index) = usize::try_from(value) {
                out.push(index);
            }
            value = 0;
            shift = 0;
        } else {
            shift = shift.saturating_add(7);
        }
    }
    out
}

/// Runs ICF and marks folded sections in `refs.sections`' fold map, which
/// the caller installs. Returns `fold_into` (with [`NONE`] for sections that
/// are not folded).
///
/// # Errors
///
/// Returns [`Error::Internal`] if the pass rejects its input.
pub fn fold<F: crate::elf::read::ElfFormat>(
    refs: &Refs<'_, '_, F>,
    placement: &Placement<'_>,
    merged: &Merged<'_, '_>,
    mode: IcfMode,
    arch: crate::elf::arch::Arch,
    print: bool,
    diagnostics: &dyn DiagnosticSink,
) -> Result<Vec<u32>> {
    let total = refs.sections.len();

    // Keys, interned in section order.
    let raw_keys: Vec<Option<Key>> = (0..total)
        .into_par_iter()
        .map(|index| foldable_section(refs, placement, SectionId::new(index)))
        .collect();
    let mut interned: HashMap<Key, u64, foldhash::fast::FixedState> =
        HashMap::with_hasher(foldhash::fast::FixedState::with_seed(0x0069_6366));
    let keys: Vec<Option<u64>> = raw_keys
        .iter()
        .map(|key| {
            key.map(|k| {
                let next = u64::try_from(interned.len()).unwrap_or(u64::MAX);
                *interned.entry(k).or_insert(next)
            })
        })
        .collect();

    // Address significance.
    let significant: Vec<AtomicBool> = (0..total).map(|_| AtomicBool::new(false)).collect();
    refs.files
        .par_iter()
        .enumerate()
        .for_each(|(file_index, file)| {
            let Some(object) = &file.object else {
                return;
            };
            if object.addrsig != 0
                && let Some(section) = object.section(object.addrsig)
                && let Ok(data) = object.elf.section_data(&section.header)
            {
                for symbol in addrsig_symbols(data) {
                    if let Some(target) = refs.target(file_index, symbol)
                        && let Some(id) = refs.target_section(&target)
                        && let Some(flag) = significant.get(id.index())
                    {
                        flag.store(true, Ordering::Relaxed);
                    }
                }
            }
            for (index, section) in object.sections.iter().enumerate() {
                let Some(id) = refs
                    .sections
                    .id(file_index, u32::try_from(index).unwrap_or(NONE))
                else {
                    continue;
                };
                if section.relocs == 0 || !refs.sections.is_live(id) {
                    continue;
                }
                let Some(Ok(Some(relocations))) = object
                    .section(section.relocs)
                    .map(|r| object.elf.relocation_section(section.relocs, &r.header))
                else {
                    continue;
                };
                for index in 0..relocations.relocations.len() {
                    let Some(rel) = relocations.relocations.get(index) else {
                        continue;
                    };
                    if rel.r_type == R_X86_64_PLT32 {
                        continue;
                    }
                    let Some(target) = refs.target(file_index, rel.symbol as usize) else {
                        continue;
                    };
                    let Some(target_id) = refs.target_section(&target) else {
                        continue;
                    };
                    let target_file = refs
                        .files
                        .get(refs.sections.locate(target_id).map_or(0, |l| l.0));
                    let has_addrsig = target_file
                        .and_then(|f| f.object.as_ref())
                        .is_some_and(|o| o.addrsig != 0);
                    if !has_addrsig && let Some(flag) = significant.get(target_id.index()) {
                        flag.store(true, Ordering::Relaxed);
                    }
                }
            }
        });

    let sections: Vec<IcfSection<'_>> = (0..total)
        .into_par_iter()
        .map(|index| {
            let id = SectionId::new(index);
            let key = keys.get(index).copied().flatten();
            let located = refs.sections.locate(id);
            let section = located.and_then(|(file, section)| {
                refs.files.get(file)?.object.as_ref()?.section(section)
            });
            let contents = match (key, section, located) {
                (Some(_), Some(section), Some((file, _))) => refs
                    .files
                    .get(file)
                    .and_then(|f| f.object.as_ref())
                    .and_then(|o| o.elf.section_data(&section.header).ok())
                    .unwrap_or_default(),
                _ => &[],
            };
            let executable = section.is_some_and(|s| s.header.sh_flags & SHF_EXECINSTR != 0);
            IcfSection {
                contents,
                key: key.unwrap_or(u64::MAX),
                foldable: key.is_some(),
                address_significant: executable
                    && significant
                        .get(index)
                        .is_some_and(|f| f.load(Ordering::Relaxed)),
            }
        })
        .collect();

    let relocs = Csr::build_parallel(
        total,
        IcfReloc {
            offset: 0,
            kind: 0,
            addend: 0,
            target: IcfTarget::Value(0),
        },
        |row| {
            if sections.get(row).is_none_or(|s| !s.foldable) {
                return 0;
            }
            section_relocs(refs, SectionId::new(row)).map_or(0, |(_, _, relocs)| relocs.len())
        },
        |row, slot| {
            if sections.get(row).is_none_or(|s| !s.foldable) {
                return 0;
            }
            let Some((file, data, relocations)) = section_relocs(refs, SectionId::new(row)) else {
                return 0;
            };
            let mut written = 0usize;
            let mut fill = |rel: crate::elf::read::Relocation, out: &mut IcfReloc| {
                let Some(target) = refs.target(file, rel.symbol as usize) else {
                    return;
                };
                let (target, addend) = icf_target(refs, merged, &target, rel.addend);
                *out = IcfReloc {
                    offset: rel.offset,
                    kind: rel.r_type,
                    addend,
                    target,
                };
                written = written.saturating_add(1);
            };
            match relocations {
                Relocations::Rela(relas) => {
                    for (rel, out) in relas.iter().zip(slot.iter_mut()) {
                        fill(rel, out);
                    }
                }
                Relocations::Rel(rels) => {
                    for (rel, out) in rels.iter().zip(slot.iter_mut()) {
                        let addend = arch.implicit_addend(rel.r_type, data, rel.offset);
                        fill(crate::elf::read::Relocation { addend, ..rel }, out);
                    }
                }
            }
            written
        },
    )
    .map_err(|e| Error::Internal(format!("ICF relocations: {e}")))?;
    let input =
        IcfInput::new(sections, relocs).map_err(|e| Error::Internal(format!("ICF input: {e}")))?;
    let result = fold_identical(&input, mode);

    if print {
        for group in result.report().groups() {
            let mut diagnostic = Diagnostic::new(
                Severity::Note,
                format!("selected section {}", describe(refs, group.kept)),
            );
            for &folded in group.folded {
                diagnostic = diagnostic.note(format!(
                    "removing identical section {}",
                    describe(refs, folded)
                ));
            }
            diagnostics.emit(diagnostic.order(u64::from(group.kept.as_u32())));
        }
    }

    Ok(result
        .fold_into()
        .par_iter()
        .enumerate()
        .map(|(index, rep)| {
            if rep.index() == index {
                NONE
            } else {
                rep.as_u32()
            }
        })
        .collect())
}

/// The relocations of section `id`: its file, its contents (empty where
/// the addends are explicit) and the relocations themselves.
fn section_relocs<'a, F: crate::elf::read::ElfFormat>(
    refs: &Refs<'_, 'a, F>,
    id: SectionId,
) -> Option<(usize, &'a [u8], Relocations<'a, F>)> {
    let (file, index) = refs.sections.locate(id)?;
    let object = refs.files.get(file)?.object.as_ref()?;
    let section = object.section(index)?;
    if section.relocs == 0 {
        return None;
    }
    let header = object.section(section.relocs)?.header;
    let relocations = object
        .elf
        .relocation_section(section.relocs, &header)
        .ok()??
        .relocations;
    // `SHT_REL`: the addends are in the fields the relocations patch, so
    // folding compares them from the section's contents.
    let data = match relocations {
        Relocations::Rela(_) => &[][..],
        Relocations::Rel(_) => object.section_data(section).ok()?,
    };
    Some((file, data, relocations))
}

fn icf_target<F: crate::elf::read::ElfFormat>(
    refs: &Refs<'_, '_, F>,
    merged: &Merged<'_, '_>,
    target: &super::refs::Target,
    addend: i64,
) -> (IcfTarget, i64) {
    if target.is_ifunc()
        && let Some(id) = target.global
    {
        return (IcfTarget::Symbol(id), addend);
    }
    match target.def {
        Def::Section {
            file,
            section,
            value,
        } => {
            let Some(id) = refs.sections.id(file, section) else {
                return (IcfTarget::Value(u64::MAX), addend);
            };
            if let Some(group) = merged.group_of(id) {
                let (offset, addend) = if target.is_section_symbol() {
                    (value.checked_add_signed(addend).unwrap_or(0), 0)
                } else {
                    (value, addend)
                };
                let piece = merged.offset_in_group(id, offset).unwrap_or(u64::MAX);
                // Group numbers are small; pieces fit in the low 48 bits of
                // any real section.
                return (IcfTarget::Value((u64::from(group) << 48) ^ piece), addend);
            }
            (
                IcfTarget::Section {
                    section: id,
                    offset: value,
                },
                addend,
            )
        }
        Def::Absolute(value) => (IcfTarget::Value(value), addend),
        Def::Common(id) | Def::Linker(id) | Def::Shared(id) => (IcfTarget::Symbol(id), addend),
        Def::Undefined { .. } => match target.global {
            Some(id) => (IcfTarget::Symbol(id), addend),
            None => (IcfTarget::Value(0), addend),
        },
    }
}

fn describe<F: crate::elf::read::ElfFormat>(refs: &Refs<'_, '_, F>, id: SectionId) -> String {
    let Some((file, index)) = refs.sections.locate(id) else {
        return format!("section {}", id.as_u32());
    };
    let Some(input) = refs.files.get(file) else {
        return format!("section {}", id.as_u32());
    };
    let name = input
        .object
        .as_ref()
        .and_then(|o| o.section(index))
        .map_or_else(String::new, |s| {
            String::from_utf8_lossy(s.name).into_owned()
        });
    format!("{}:({name})", input.display())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn decodes_addrsig() {
        assert_eq!(addrsig_symbols(&[1, 0x85, 0x01, 3]), vec![1, 133, 3]);
        assert_eq!(addrsig_symbols(&[0xff; 12]), Vec::<usize>::new());
    }
}