qld 0.1.0

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! `--separate-debug-file[=FILE]`: debug information in a file of its own,
//! as mold writes it (GNU ld 2.46 has no such option).
//!
//! The output keeps everything but the debug sections: the non-allocated
//! `.debug_*` sections, `.gdb_index`, `.symtab` and `.strtab` go to FILE
//! (by default the output path plus `.dbg`), and the output gains a
//! `.gnu_debuglink` section naming FILE (its file name only) with a CRC-32
//! that GDB checks when it loads FILE.
//!
//! FILE is laid out like the complete output, section for section, with
//! the same section and program headers, so its symbol table and debug
//! information describe the output as linked. Every section that is not
//! debug information becomes `SHT_NOBITS` (it keeps its address and size
//! but no bytes), except notes (the build-id note among them, with the
//! output's build ID) and `.shstrtab`.
//!
//! The CRC stored in `.gnu_debuglink` has to be known when the output is
//! written, and FILE comes after it, so the value is chosen first, as mold
//! does: the CRC-32 of the output's build ID, or of the output itself when
//! it has none; FILE then gets four trailing bytes that give it that CRC
//! ([`crc32_solve`]). Trailing bytes after the section header table are
//! ignored by ELF readers.

#![deny(clippy::arithmetic_side_effects)]

use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use rayon::prelude::*;

use crate::args::{BuildId, LinkOptions};
use crate::elf::read::consts::{PT_LOAD, PT_PHDR, SHF_ALLOC, SHT_NOBITS, SHT_NOTE};
use crate::error::{Error, Result};
use crate::ids::SectionId;

use super::layout::{self, Layout, Member, Trailer};
use super::place::Placement;
use super::rules::Synthetic;
use super::sections::Sections;

/// The debug file's path, when `--separate-debug-file` is given.
#[must_use]
pub fn debug_path(options: &LinkOptions) -> Option<PathBuf> {
    match &options.separate_debug_file {
        None => None,
        Some(Some(path)) => Some(path.clone()),
        Some(None) => {
            let mut path = options.output_path().into_os_string();
            path.push(".dbg");
            Some(PathBuf::from(path))
        }
    }
}

/// The file name `.gnu_debuglink` records.
fn link_name(path: &Path) -> &[u8] {
    path.file_name()
        .map_or(path.as_os_str(), |n| n)
        .as_encoded_bytes()
}

/// The size of `.gnu_debuglink` for `path`: the name, a NUL, padding to 4
/// bytes, and the CRC.
#[must_use]
pub fn debuglink_size(path: &Path) -> u64 {
    let name = (link_name(path).len() as u64).saturating_add(1);
    name.saturating_add(3).saturating_add(4) & !3
}

/// The contents of `.gnu_debuglink` for `path`, with a zero CRC.
#[must_use]
pub fn debuglink_contents(path: &Path) -> Vec<u8> {
    let mut out = vec![0u8; usize::try_from(debuglink_size(path)).unwrap_or(0)];
    let name = link_name(path);
    if let Some(dest) = out.get_mut(..name.len()) {
        dest.copy_from_slice(name);
    }
    out
}

/// Whether an output section goes to the debug file.
#[must_use]
pub fn is_debug_section(section: &layout::OutSection<'_>) -> bool {
    if section.flags & SHF_ALLOC != 0 {
        return false;
    }
    match section.trailer {
        Trailer::Symtab | Trailer::Strtab => true,
        Trailer::Generated => section.name != b".gnu_debuglink",
        Trailer::Shstrtab | Trailer::Rela(_) => false,
        Trailer::None => section.name.starts_with(b".debug") || section.name_prefix == b".z",
    }
}

/// A copy of `sections` without the input sections of the debug output
/// sections (non-allocated `.debug*`), for laying out the stripped output.
#[must_use]
pub fn stripped_sections(sections: &Sections, placement: &Placement<'_>) -> Sections {
    let mut live = sections.live.clone();
    for (index, slot) in live.iter_mut().enumerate() {
        if !*slot {
            continue;
        }
        let debug = placement
            .output_of(SectionId::new(index))
            .and_then(|o| placement.outputs.get(o as usize))
            .is_some_and(|o| o.flags & SHF_ALLOC == 0 && o.name.starts_with(b".debug"));
        if debug {
            *slot = false;
        }
    }
    Sections {
        base: sections.base.clone(),
        count: sections.count.clone(),
        owner: sections.owner.clone(),
        live,
        kind: sections.kind.clone(),
        fold_into: sections.fold_into.clone(),
    }
}

/// Turns the complete output's layout into the debug file's: sections that
/// are not debug information, notes or `.shstrtab` become `SHT_NOBITS`,
/// and file offsets are assigned again after the ELF and program headers,
/// allocated sections at offsets congruent to their addresses modulo the
/// page size. Program headers keep their addresses and memory sizes and
/// cover only the bytes left in the file, as `objcopy --only-keep-debug`
/// makes them.
pub fn to_debug_file(layout: &mut Layout<'_>) -> Result<()> {
    let headers = layout.phoff.max(layout.kind.ehdr_size()).saturating_add(
        layout
            .kind
            .phdr_size()
            .saturating_mul(layout.segments.len() as u64),
    );
    let page = layout
        .segments
        .iter()
        .filter(|s| s.p_type == PT_LOAD)
        .map(|s| s.align)
        .max()
        .unwrap_or(layout::DEFAULT_PAGE)
        .max(1);
    // The PT_LOAD of each allocated section; the one mapping the headers
    // is where the file starts.
    let load_of = |addr: u64| {
        layout.segments.iter().position(|g| {
            g.p_type == PT_LOAD && addr >= g.vaddr && addr < g.vaddr.saturating_add(g.memsz)
        })
    };
    let loads: Vec<Option<usize>> = layout
        .sections
        .iter()
        .map(|s| {
            (s.flags & SHF_ALLOC != 0)
                .then(|| load_of(s.addr))
                .flatten()
        })
        .collect();
    let mut current = layout
        .segments
        .iter()
        .position(|g| g.p_type == PT_LOAD && g.offset == 0);
    let mut offset = headers;
    for (section, load) in layout.sections.iter_mut().zip(loads) {
        let keep = is_debug_section(section)
            || section.sh_type == SHT_NOTE
            || section.trailer == Trailer::Shstrtab;
        if !keep {
            section.sh_type = SHT_NOBITS;
        }
        if section.sh_type == SHT_NOBITS {
            section.offset = offset;
            continue;
        }
        offset = if section.flags & SHF_ALLOC != 0 {
            // Congruent to the address modulo the page: the next such
            // offset in the segment being filled, else on a fresh page.
            let skew = section.addr.checked_rem(page).unwrap_or(0);
            let fresh = layout::add(layout::align_up(offset, page)?, skew)?;
            if load.is_some() && load == current {
                let earlier = fresh.saturating_sub(page);
                if earlier >= offset { earlier } else { fresh }
            } else {
                current = load;
                fresh
            }
        } else {
            layout::align_up(offset, section.align)?
        };
        section.offset = offset;
        offset = layout::add(offset, section.size)?;
    }
    layout.shoff = layout::align_up(offset, layout.kind.word_size())?;
    layout.file_size = layout::add(
        layout.shoff,
        layout
            .kind
            .shdr_size()
            .saturating_mul((layout.sections.len() as u64).saturating_add(1)),
    )?;
    // Synthetic parts (the build-id note) move with their sections.
    for (kind, _, file_offset, _) in &mut layout.synthetic {
        if let Some(offset) = member_offset(layout.sections.as_slice(), *kind) {
            *file_offset = offset;
        }
    }
    for segment in &mut layout.segments {
        if segment.p_type == PT_PHDR {
            continue;
        }
        let end = segment.vaddr.saturating_add(segment.memsz);
        let inside = |s: &&layout::OutSection<'_>| {
            s.flags & SHF_ALLOC != 0
                && s.addr >= segment.vaddr
                && s.addr < end.max(segment.vaddr.saturating_add(1))
        };
        let with_bytes = || {
            layout
                .sections
                .iter()
                .filter(inside)
                .filter(|s| s.sh_type != SHT_NOBITS)
        };
        // The segment that maps the headers keeps covering them.
        let headers_too = segment.offset == 0 && segment.p_type == PT_LOAD;
        let start = if headers_too {
            Some(0)
        } else {
            with_bytes()
                .next()
                .or_else(|| layout.sections.iter().find(inside))
                .map(|s| {
                    s.offset
                        .saturating_sub(s.addr.saturating_sub(segment.vaddr))
                })
        };
        segment.offset = start.unwrap_or(0);
        let last = with_bytes()
            .map(|s| s.offset.saturating_add(s.size))
            .max()
            .unwrap_or(0);
        let covered = if headers_too { last.max(headers) } else { last };
        segment.filesz = covered
            .saturating_sub(segment.offset)
            .min(segment.memsz.max(headers));
        if !headers_too && with_bytes().next().is_none() {
            segment.filesz = 0;
        }
    }
    layout.warnings.clear();
    Ok(())
}

/// The file offset of synthetic part `kind` in `sections`.
fn member_offset(sections: &[layout::OutSection<'_>], kind: Synthetic) -> Option<u64> {
    sections.iter().find_map(|section| {
        section
            .members
            .iter()
            .find(|p| p.member == Member::Synthetic(kind))
            .map(|p| section.offset.saturating_add(p.offset))
    })
}

/// The file offset of the build ID bytes of a layout, if it has a build-id
/// note.
#[must_use]
pub fn build_id_offset(layout: &Layout<'_>) -> Option<u64> {
    member_offset(&layout.sections, Synthetic::BuildId).map(|o| o.saturating_add(16))
}

/// The size of the build ID `kind` writes.
fn build_id_size(kind: &BuildId) -> usize {
    match kind {
        BuildId::None => 0,
        BuildId::Fast | BuildId::Md5 | BuildId::Uuid => 16,
        BuildId::Hex(bytes) => bytes.len(),
        _ => 20,
    }
}

/// Finishes the output: stores the CRC `.gnu_debuglink` promises (see the
/// module documentation) and returns it with the output's build ID.
///
/// # Errors
///
/// Returns I/O errors.
pub fn finish_output<F: crate::elf::read::ElfFormat>(
    path: &Path,
    layout: &Layout<'_>,
    options: &LinkOptions,
) -> Result<(u32, Option<Vec<u8>>)> {
    let io = |e| Error::io(path, e);
    let mut file = File::options()
        .read(true)
        .write(true)
        .open(path)
        .map_err(io)?;
    let size = build_id_size(&options.build_id);
    let build_id = match build_id_offset(layout) {
        Some(offset) if size > 0 => {
            let mut bytes = vec![0u8; size];
            file.seek(SeekFrom::Start(offset)).map_err(io)?;
            file.read_exact(&mut bytes).map_err(io)?;
            Some(bytes)
        }
        _ => None,
    };
    let crc = match &build_id {
        Some(bytes) => crc32(bytes),
        None => crc32_file(&mut file).map_err(io)?,
    };
    let link = layout
        .sections
        .iter()
        .find(|s| s.trailer == Trailer::Generated && s.name == b".gnu_debuglink")
        .ok_or_else(|| Error::Internal("no .gnu_debuglink section".into()))?;
    let at = link
        .offset
        .checked_add(link.size)
        .and_then(|end| end.checked_sub(4))
        .ok_or_else(|| Error::Internal(".gnu_debuglink too small".into()))?;
    file.seek(SeekFrom::Start(at)).map_err(io)?;
    // The CRC is a word of the output, in its byte order.
    file.write_all(&<F::Endian as crate::elf::read::Endian>::put_u32(crc))
        .map_err(io)?;
    Ok((crc, build_id))
}

/// Finishes the debug file: copies the output's build ID into its note and
/// appends the four bytes that make its CRC-32 `crc`.
///
/// # Errors
///
/// Returns I/O errors.
pub fn finish_debug_file(
    path: &Path,
    layout: &Layout<'_>,
    build_id: Option<&[u8]>,
    crc: u32,
) -> Result<()> {
    let io = |e| Error::io(path, e);
    let mut file = File::options()
        .read(true)
        .write(true)
        .open(path)
        .map_err(io)?;
    if let (Some(bytes), Some(offset)) = (build_id, build_id_offset(layout)) {
        file.seek(SeekFrom::Start(offset)).map_err(io)?;
        file.write_all(bytes).map_err(io)?;
    }
    let current = crc32_file(&mut file).map_err(io)?;
    file.seek(SeekFrom::End(0)).map_err(io)?;
    file.write_all(&crc32_solve(current, crc)).map_err(io)?;
    Ok(())
}

// --- CRC-32 (ISO-HDLC, the polynomial of zlib and `.gnu_debuglink`) ---

/// The reflected CRC-32 polynomial.
const POLY: u32 = 0xedb8_8320;

/// Slicing-by-8 tables.
const TABLES: [[u32; 256]; 8] = make_tables();

// Bounded loops over fixed-size tables.
#[allow(clippy::arithmetic_side_effects)]
const fn make_tables() -> [[u32; 256]; 8] {
    let mut tables = [[0u32; 256]; 8];
    let mut i = 0;
    while i < 256 {
        let mut crc = i as u32;
        let mut bit = 0;
        while bit < 8 {
            crc = if crc & 1 != 0 {
                (crc >> 1) ^ POLY
            } else {
                crc >> 1
            };
            bit += 1;
        }
        tables[0][i] = crc;
        i += 1;
    }
    let mut t = 1;
    while t < 8 {
        let mut i = 0;
        while i < 256 {
            let previous = tables[t - 1][i];
            tables[t][i] = (previous >> 8) ^ tables[0][(previous & 0xff) as usize];
            i += 1;
        }
        t += 1;
    }
    tables
}

/// Continues CRC-32 `crc` (of the data so far) over `data`.
fn crc32_update(crc: u32, data: &[u8]) -> u32 {
    let mut crc = !crc;
    let (blocks, rest) = data.as_chunks::<8>();
    for block in blocks {
        let low = crc ^ u32::from_le_bytes([block[0], block[1], block[2], block[3]]);
        let high = u32::from_le_bytes([block[4], block[5], block[6], block[7]]);
        crc = TABLES[7][(low & 0xff) as usize]
            ^ TABLES[6][((low >> 8) & 0xff) as usize]
            ^ TABLES[5][((low >> 16) & 0xff) as usize]
            ^ TABLES[4][(low >> 24) as usize]
            ^ TABLES[3][(high & 0xff) as usize]
            ^ TABLES[2][((high >> 8) & 0xff) as usize]
            ^ TABLES[1][((high >> 16) & 0xff) as usize]
            ^ TABLES[0][(high >> 24) as usize];
    }
    for &byte in rest {
        crc = (crc >> 8) ^ TABLES[0][((crc ^ u32::from(byte)) & 0xff) as usize];
    }
    !crc
}

/// The CRC-32 of `data`.
#[must_use]
pub fn crc32(data: &[u8]) -> u32 {
    crc32_update(0, data)
}

/// Multiplies two polynomials modulo the CRC polynomial (bit-reflected).
fn gf2_multiply(a: u32, mut b: u32) -> u32 {
    let mut product = 0u32;
    let mut bit = 1u32 << 31;
    while bit != 0 {
        if a & bit != 0 {
            product ^= b;
        }
        b = if b & 1 != 0 { (b >> 1) ^ POLY } else { b >> 1 };
        bit >>= 1;
    }
    product
}

/// `x^(8 * len)` modulo the CRC polynomial.
fn x_to_the_bytes(len: u64) -> u32 {
    // x^(2^k) for k = 3 (one byte), squared as len's bits are consumed.
    let mut power = 1u32 << (31 - 8); // x^8
    let mut result = 1u32 << 31; // x^0
    let mut len = len;
    while len != 0 {
        if len & 1 != 0 {
            result = gf2_multiply(result, power);
        }
        power = gf2_multiply(power, power);
        len >>= 1;
    }
    result
}

/// The CRC-32 of `a ++ b` from the CRCs of `a` and `b` and `b`'s length
/// (zlib's `crc32_combine`).
#[must_use]
pub fn crc32_combine(a: u32, b: u32, b_len: u64) -> u32 {
    gf2_multiply(x_to_the_bytes(b_len), a) ^ b
}

/// The four bytes that, appended to data whose CRC-32 is `current`, make
/// the CRC-32 `desired`: the CRC register after them must be `!desired`,
/// so the register is run backwards through 32 zero bits (multiplying by
/// x^-32) and the difference from the current register is what to append.
#[must_use]
pub fn crc32_solve(current: u32, desired: u32) -> [u8; 4] {
    let mut register = !desired;
    for _ in 0..32 {
        // One step back: undo the shift and the conditional XOR.
        register = if register & 0x8000_0000 != 0 {
            ((register ^ POLY) << 1) | 1
        } else {
            register << 1
        };
    }
    (register ^ !current).to_le_bytes()
}

/// Bytes read and checksummed per task.
const CRC_BLOCK: usize = 4 << 20;

/// The CRC-32 of a whole file, read in blocks that are checksummed in
/// parallel and combined.
fn crc32_file(file: &mut File) -> std::io::Result<u32> {
    file.seek(SeekFrom::Start(0))?;
    let mut crc = 0u32;
    let batch = rayon::current_num_threads().clamp(1, 16);
    let mut buffers: Vec<Vec<u8>> = (0..batch).map(|_| vec![0u8; CRC_BLOCK]).collect();
    loop {
        let mut filled = Vec::with_capacity(batch);
        for buffer in &mut buffers {
            let mut len = 0usize;
            while len < buffer.len() {
                let Some(rest) = buffer.get_mut(len..) else {
                    break;
                };
                match file.read(rest)? {
                    0 => break,
                    n => len = len.saturating_add(n),
                }
            }
            filled.push(len);
            if len < CRC_BLOCK {
                break;
            }
        }
        let parts: Vec<(u32, u64)> = buffers
            .par_iter()
            .zip(filled.par_iter())
            .map(|(buffer, &len)| {
                let data = buffer.get(..len).unwrap_or_default();
                (crc32(data), len as u64)
            })
            .collect();
        for (part, len) in parts {
            crc = crc32_combine(crc, part, len);
        }
        if filled.last().is_none_or(|&len| len < CRC_BLOCK) {
            return Ok(crc);
        }
    }
}

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

    #[test]
    fn crc32_matches_zlib() {
        assert_eq!(crc32(b""), 0);
        assert_eq!(crc32(b"123456789"), 0xcbf4_3926);
        let data: Vec<u8> = (0..1000u32).map(|i| (i * 7 + 3) as u8).collect();
        let (a, b) = data.split_at(377);
        assert_eq!(
            crc32_combine(crc32(a), crc32(b), b.len() as u64),
            crc32(&data)
        );
        assert_eq!(crc32_update(crc32(a), b), crc32(&data));
    }

    #[test]
    fn crc32_solve_forges_the_crc() {
        for (data, desired) in [(&b"hello"[..], 0xdead_beef), (b"", 0), (b"x", 0x1234_5678)] {
            let mut forged = data.to_vec();
            forged.extend_from_slice(&crc32_solve(crc32(data), desired));
            assert_eq!(crc32(&forged), desired);
        }
    }

    #[test]
    fn debuglink_is_padded() {
        assert_eq!(debuglink_size(Path::new("dir/a.dbg")), 12);
        assert_eq!(debuglink_size(Path::new("abc")), 8);
        let contents = debuglink_contents(Path::new("dir/a.dbg"));
        assert_eq!(&contents, b"a.dbg\0\0\0\0\0\0\0");
    }
}