gsym-rs 0.1.6

Pure-Rust reader, writer, and Linux ELF/DWARF converter for LLVM GSYM
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Split DWARF: individual `.dwo` files and the `.dwp` packages that bundle them.

use std::process::Command;
use std::time::{Duration, Instant};

use gsym::Error;
use gsym::convert::{
    ConversionOptions, ConversionWarning, DiscoveryPolicy, ElfConverter, ElfInputs,
};
use object::endian::Endian;
use object::{Object, ObjectSection, ObjectSymbol};

use crate::elf::{convert, find_function};
use crate::tools::{required_tool, run};

const SPLIT_FUNCTIONS: &[&[u8]] = &[b"split_a", b"split_b", b"split_c", b"main"];

/// This caps `llvm-dwp`, which builds the fixtures: it has been seen to hang on
/// some inputs, and a hung packager should fail the test rather than the run.
/// The timeout does not cover the converter itself.
fn run_bounded(command: &mut Command, timeout: Duration) {
    let mut child = command.spawn().unwrap();
    let deadline = Instant::now() + timeout;
    loop {
        if let Some(status) = child.try_wait().unwrap() {
            assert!(status.success(), "command failed with {status}");
            return;
        }
        if Instant::now() >= deadline {
            child.kill().unwrap();
            child.wait().unwrap();
            panic!("command exceeded {timeout:?}");
        }
        std::thread::sleep(Duration::from_millis(10));
    }
}

#[test]
fn copies_skeleton_address_base_for_dwo_and_dwp() {
    let directory = tempfile::tempdir().unwrap();
    let image = compile_split_dwarf_image(directory.path());
    let dwo = dwo_files(directory.path()).remove(0);
    assert_uses_indexed_split_addresses(&image, &dwo);

    let report = ElfConverter::new(dwarf_only_options())
        .convert_path(&image)
        .unwrap();
    assert_eq!(report.stats.split_dwarf_units, 1);
    assert_function_addresses(&report, &image, SPLIT_FUNCTIONS);

    let dwp = image.with_extension("dwp");
    package_dwo_files(std::slice::from_ref(&dwo), &dwp);
    std::fs::rename(&dwo, dwo.with_extension("dwo.unavailable")).unwrap();

    let report = ElfConverter::new(dwarf_only_options())
        .convert_path(&image)
        .unwrap();
    assert_eq!(report.discovered_dwp.as_deref(), Some(dwp.as_path()));
    assert_eq!(report.stats.split_dwarf_units, 1);
    assert_function_addresses(&report, &image, SPLIT_FUNCTIONS);
}

#[test]
fn loose_dwo_selects_the_split_unit_with_the_matching_id() {
    let directory = tempfile::tempdir().unwrap();
    let image = compile_split_dwarf_image(directory.path());
    let dwo = dwo_files(directory.path()).remove(0);
    prepend_unrelated_split_unit(&dwo, directory.path());

    let report = ElfConverter::new(dwarf_only_options())
        .convert_path(&image)
        .unwrap();
    assert_eq!(report.stats.split_dwarf_units, 1);
    assert_function_addresses(&report, &image, SPLIT_FUNCTIONS);
}

fn prepend_unrelated_split_unit(dwo: &std::path::Path, directory: &std::path::Path) {
    let dwo_bytes = std::fs::read(dwo).unwrap();
    let dwo_file = object::File::parse(&*dwo_bytes).unwrap();
    let endian = dwo_file.endianness();
    let info = directory.join("debug-info.dwo.bin");
    let abbrev = directory.join("debug-abbrev.dwo.bin");
    let original_info = dwo_file
        .section_by_name(".debug_info.dwo")
        .unwrap()
        .uncompressed_data()
        .unwrap();
    let mut abbreviations = dwo_file
        .section_by_name(".debug_abbrev.dwo")
        .unwrap()
        .uncompressed_data()
        .unwrap()
        .into_owned();
    let abbreviation_offset = u32::try_from(abbreviations.len()).unwrap();

    let mut combined_info = Vec::with_capacity(21 + original_info.len());
    combined_info.extend_from_slice(&endian.write_u32(17));
    combined_info.extend_from_slice(&endian.write_u16(5));
    combined_info.push(gimli::constants::DW_UT_split_compile.0);
    combined_info.push(8);
    combined_info.extend_from_slice(&endian.write_u32(abbreviation_offset));
    combined_info.extend_from_slice(&endian.write_u64(0x0123_4567_89ab_cdef));
    combined_info.push(1);
    combined_info.extend_from_slice(&original_info);
    std::fs::write(&info, combined_info).unwrap();

    abbreviations.extend_from_slice(&[
        1,
        u8::try_from(gimli::constants::DW_TAG_compile_unit.0).unwrap(),
        gimli::constants::DW_CHILDREN_no.0,
        0,
        0,
        0,
    ]);
    std::fs::write(&abbrev, abbreviations).unwrap();
    let rewritten = directory.join("multiple-units.dwo");
    run(Command::new("objcopy")
        .arg(format!(
            "--update-section=.debug_info.dwo={}",
            info.display()
        ))
        .arg(format!(
            "--update-section=.debug_abbrev.dwo={}",
            abbrev.display()
        ))
        .arg(dwo)
        .arg(&rewritten));
    std::fs::rename(rewritten, dwo).unwrap();
}

fn assert_uses_indexed_split_addresses(image: &std::path::Path, dwo: &std::path::Path) {
    let image_bytes = std::fs::read(image).unwrap();
    let image_file = object::File::parse(&*image_bytes).unwrap();
    let endian = if image_file.is_little_endian() {
        gimli::RunTimeEndian::Little
    } else {
        gimli::RunTimeEndian::Big
    };
    let image_sections = gimli::DwarfSections::load(|id| -> object::Result<Vec<u8>> {
        image_file.section_by_name(id.name()).map_or_else(
            || Ok(Vec::new()),
            |section| Ok(section.uncompressed_data()?.into_owned()),
        )
    })
    .unwrap();
    let image_dwarf = image_sections.borrow(|section| gimli::EndianSlice::new(section, endian));
    let skeleton_header = image_dwarf.units().next().unwrap().unwrap();
    let skeleton = image_dwarf.unit(skeleton_header).unwrap();
    assert_ne!(skeleton.addr_base.0, 0);

    let dwo_bytes = std::fs::read(dwo).unwrap();
    let dwo_file = object::File::parse(&*dwo_bytes).unwrap();
    let dwo_sections = gimli::DwarfSections::load(|id| -> object::Result<Vec<u8>> {
        let Some(name) = id.dwo_name() else {
            return Ok(Vec::new());
        };
        dwo_file.section_by_name(name).map_or_else(
            || Ok(Vec::new()),
            |section| Ok(section.uncompressed_data()?.into_owned()),
        )
    })
    .unwrap();
    let mut dwo_dwarf = dwo_sections.borrow(|section| gimli::EndianSlice::new(section, endian));
    dwo_dwarf.make_dwo(&image_dwarf);
    let split_header = dwo_dwarf.units().next().unwrap().unwrap();
    let split_unit = dwo_dwarf.unit(split_header).unwrap();
    let mut entries = split_unit.entries();
    let mut indexed_addresses = 0;
    while let Some(entry) = entries.next_dfs().unwrap() {
        if entry.tag() == gimli::constants::DW_TAG_subprogram
            && matches!(
                entry.attr_value(gimli::constants::DW_AT_low_pc),
                Some(gimli::AttributeValue::DebugAddrIndex(_))
            )
        {
            indexed_addresses += 1;
        }
    }
    assert_eq!(indexed_addresses, SPLIT_FUNCTIONS.len());
}

fn dwarf_only_options() -> ConversionOptions {
    ConversionOptions {
        include_symbols: false,
        debuginfod_urls: Vec::new(),
        ..ConversionOptions::default()
    }
}

fn assert_function_addresses(
    report: &gsym::convert::ConversionReport,
    image: &std::path::Path,
    names: &[&[u8]],
) {
    let bytes = std::fs::read(image).unwrap();
    let file = object::File::parse(&*bytes).unwrap();
    for name in names {
        let symbol = file.symbol_by_name_bytes(name).unwrap();
        assert!(symbol.is_definition());
        let expected = symbol.address();
        let actual = find_function(report, name).map(|function| function.range.start);
        assert_eq!(actual, Some(expected), "{}", String::from_utf8_lossy(name));
    }
}

fn compile_split_dwarf_image(directory: &std::path::Path) -> std::path::PathBuf {
    let source = directory.join("split.c");
    let image = directory.join("split");
    std::fs::write(
        &source,
        "__attribute__((noinline)) int split_a(int x) { return x + 1; }\n__attribute__((noinline)) int split_b(int x) { return x + 2; }\n__attribute__((noinline)) int split_c(int x) { return x + 3; }\nint main(void) { return split_a(1) + split_b(2) + split_c(3); }\n",
    )
    .unwrap();
    run(Command::new("cc").current_dir(directory).args([
        "-g",
        "-gdwarf-5",
        "-O2",
        "-gsplit-dwarf",
        "-o",
        image.to_str().unwrap(),
        source.to_str().unwrap(),
    ]));
    assert!(!dwo_files(directory).is_empty());
    image
}

#[test]
fn disabled_discovery_ignores_individual_dwo_files() {
    let directory = tempfile::tempdir().unwrap();
    let image = compile_split_dwarf_image(directory.path());
    let options = ConversionOptions {
        include_symbols: false,
        discovery: DiscoveryPolicy::Disabled,
        ..ConversionOptions::default()
    };

    let report = ElfConverter::new(options).convert_path(&image).unwrap();
    assert!(
        report
            .warnings
            .iter()
            .any(|warning| matches!(warning, ConversionWarning::SplitDwarfUnavailable { .. }))
    );
    assert_eq!(report.stats.dwarf_functions, 0, "{:?}", report.warnings);
    assert_eq!(report.stats.split_dwarf_units, 0);
    assert!(report.builder.functions().is_empty());
}

#[test]
fn resolves_relative_compilation_directories_from_the_current_directory() {
    let current_directory = std::env::current_dir().unwrap();
    let directory = tempfile::Builder::new()
        .prefix("relative-comp-dir-")
        .tempdir_in(&current_directory)
        .unwrap();
    let build_directory = directory.path().join("build");
    let binary_directory = build_directory.join("bin/nested");
    std::fs::create_dir_all(&binary_directory).unwrap();
    let source = build_directory.join("relative.c");
    let image = binary_directory.join("relative");
    std::fs::write(
        &source,
        "__attribute__((noinline)) int relative_target(int x) { return x + 1; }\nint main(void) { return relative_target(1); }\n",
    )
    .unwrap();
    let relative_build_directory = build_directory.strip_prefix(&current_directory).unwrap();
    run(Command::new("cc")
        .current_dir(&build_directory)
        .arg("-g")
        .arg("-O0")
        .arg("-gsplit-dwarf")
        .arg(format!(
            "-fdebug-prefix-map={}={}",
            build_directory.display(),
            relative_build_directory.display()
        ))
        .args(["-o", "bin/nested/relative", "relative.c"]));
    assert!(image.with_extension("dwo").is_file());
    let shadow = binary_directory
        .join(relative_build_directory)
        .join("bin/nested/relative.dwo");
    std::fs::create_dir_all(shadow.parent().unwrap()).unwrap();
    std::fs::write(shadow, b"not a DWO file").unwrap();

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    assert!(report.stats.dwarf_functions > 0, "{:?}", report.warnings);
    assert!(
        find_function(&report, b"relative_target").is_some(),
        "{:?}",
        report.warnings
    );
}

#[test]
fn resolves_relative_compilation_directories_from_the_binary_directory() {
    let directory = tempfile::tempdir().unwrap();
    let compilation_directory = directory.path().join("dwo");
    std::fs::create_dir(&compilation_directory).unwrap();
    let source = compilation_directory.join("relative.c");
    let compiled_image = compilation_directory.join("relative");
    let image = directory.path().join("relative");
    std::fs::write(
        &source,
        "__attribute__((noinline)) int binary_relative_target(int x) { return x + 1; }\nint main(void) { return binary_relative_target(1); }\n",
    )
    .unwrap();
    run(Command::new("cc")
        .current_dir(&compilation_directory)
        .arg("-g")
        .arg("-O0")
        .arg("-gsplit-dwarf")
        .arg(format!(
            "-fdebug-prefix-map={}=dwo",
            compilation_directory.display()
        ))
        .args(["-o", "relative", "relative.c"]));
    assert!(compiled_image.with_extension("dwo").is_file());
    std::fs::rename(&compiled_image, &image).unwrap();

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    assert!(report.stats.dwarf_functions > 0, "{:?}", report.warnings);
    assert!(find_function(&report, b"binary_relative_target").is_some());
}

#[test]
fn imports_gcc_split_line_rows_and_dwo_file_tables() {
    for dwarf_version in ["-gdwarf-4", "-gdwarf-5"] {
        check_gcc_split_line_rows_and_dwo_file_table(dwarf_version);
    }
}

fn check_gcc_split_line_rows_and_dwo_file_table(dwarf_version: &str) {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("implicit-line-table.c");
    let image = directory.path().join("implicit-line-table");
    std::fs::write(
        &source,
        "static inline __attribute__((always_inline)) int implicit_inline(int x) { volatile int value = x + 3; return value; }\n__attribute__((noinline)) int implicit_target(int x) { return implicit_inline(x); }\nint main(void) { return implicit_target(1); }\n",
    )
    .unwrap();
    run(Command::new(required_tool("gcc"))
        .current_dir(directory.path())
        .args([
            "-g",
            dwarf_version,
            "-O2",
            "-gsplit-dwarf",
            "-o",
            "implicit-line-table",
            "implicit-line-table.c",
        ]));
    let dwo_files = dwo_files(directory.path());
    let [dwo] = dwo_files.as_slice() else {
        panic!("expected exactly one DWO file");
    };
    let info = run(Command::new(required_tool("x86_64-linux-gnu-readelf"))
        .arg("--debug-dump=info")
        .arg(dwo));
    let info = String::from_utf8_lossy(&info.stdout);
    assert!(
        !info.contains("DW_AT_stmt_list"),
        "fixture unexpectedly has DW_AT_stmt_list:\n{info}"
    );

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    let target = report
        .builder
        .functions()
        .iter()
        .find(|function| function.name == b"implicit_target" && function.inline.is_some())
        .unwrap();
    let inline = target.inline.as_ref().unwrap();
    let [child] = inline.children.as_slice() else {
        panic!("expected exactly one inline child");
    };
    assert!(
        target.lines.iter().any(|line| line.line == 1),
        "{dwarf_version}: missing an executable line row from the inlined body: {:?}",
        target.lines
    );
    assert_ne!(child.call_file, gsym::FileIndex::ZERO);
    assert!(
        report.warnings.iter().all(|warning| !matches!(
            warning,
            ConversionWarning::MissingInlineCallFile { .. }
                | ConversionWarning::InvalidDeclarationFile { .. }
        )),
        "{dwarf_version}: {:?}",
        report.warnings
    );
}

fn dwo_files(directory: &std::path::Path) -> Vec<std::path::PathBuf> {
    let mut files = directory
        .read_dir()
        .unwrap()
        .map(|entry| entry.unwrap().path())
        .filter(|path| path.extension().is_some_and(|extension| extension == "dwo"))
        .collect::<Vec<_>>();
    files.sort();
    files
}

#[test]
fn discovers_dwp_packages_after_individual_dwo_files_are_unavailable() {
    let dwp_tool = required_tool("llvm-dwp");
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("package.c");
    let image = directory.path().join("package");
    std::fs::write(
        &source,
        "__attribute__((noinline)) int packaged(int x) { return x + 7; }\nint main(void) { return packaged(1); }\n",
    )
    .unwrap();
    run(Command::new("cc").current_dir(directory.path()).args([
        "-g",
        "-gdwarf-4",
        "-O0",
        "-gsplit-dwarf",
        "-o",
        image.to_str().unwrap(),
        source.to_str().unwrap(),
    ]));
    let dwo = dwo_files(directory.path()).remove(0);
    let dwp = image.with_extension("dwp");
    run_bounded(
        Command::new(dwp_tool).args([dwo.to_str().unwrap(), "-o", dwp.to_str().unwrap()]),
        Duration::from_secs(10),
    );
    std::fs::rename(&dwo, dwo.with_extension("dwo.unavailable")).unwrap();

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    assert_eq!(report.discovered_dwp.as_deref(), Some(dwp.as_path()));
    assert!(report.stats.dwarf_functions > 0, "{:?}", report.warnings);
    assert_eq!(report.stats.split_dwarf_units, 1);
    assert!(
        report
            .builder
            .functions()
            .iter()
            .any(|function| function.name == b"packaged" && !function.lines.is_empty()),
        "{:?}",
        report.warnings
    );

    let disabled = ElfConverter::new(ConversionOptions {
        include_symbols: false,
        discovery: DiscoveryPolicy::Disabled,
        ..ConversionOptions::default()
    })
    .convert_path(&image)
    .unwrap();
    assert!(disabled.discovered_dwp.is_none());
    assert!(
        disabled
            .warnings
            .iter()
            .any(|warning| matches!(warning, ConversionWarning::SplitDwarfUnavailable { .. }))
    );
    assert_eq!(disabled.stats.dwarf_functions, 0, "{:?}", disabled.warnings);

    let symbols_only = ElfConverter::new(ConversionOptions {
        dwarf: None,
        ..ConversionOptions::default()
    })
    .convert_path(&image)
    .unwrap();
    assert!(symbols_only.discovered_dwp.is_none());
    assert_eq!(symbols_only.stats.dwarf_functions, 0);
}

fn compile_split_units(
    directory: &std::path::Path,
    dwarf_version: &str,
) -> (std::path::PathBuf, Vec<std::path::PathBuf>) {
    let first = directory.join(format!("package-{dwarf_version}-first.c"));
    let second = directory.join(format!("package-{dwarf_version}-second.c"));
    let image = directory.join(format!("package-{dwarf_version}"));
    std::fs::write(
        &first,
        "static inline __attribute__((always_inline)) int packaged_inline(int x) { volatile int value = x + 3; return value; }\n__attribute__((noinline)) int packaged_first(int x) { return packaged_inline(x); }\n",
    )
    .unwrap();
    std::fs::write(
        &second,
        "int packaged_first(int);\n__attribute__((noinline)) int packaged_second(int x) { return packaged_first(x) + 5; }\nint main(void) { return packaged_second(1); }\n",
    )
    .unwrap();
    run(Command::new("cc").current_dir(directory).args([
        "-g",
        dwarf_version,
        "-O1",
        "-gsplit-dwarf",
        "-o",
        image.to_str().unwrap(),
        first.to_str().unwrap(),
        second.to_str().unwrap(),
    ]));
    let dwo_files = dwo_files(directory);
    assert_eq!(dwo_files.len(), 2);
    (image, dwo_files)
}

fn package_dwo_files(dwo_files: &[std::path::PathBuf], output: &std::path::Path) {
    let mut command = Command::new(required_tool("llvm-dwp"));
    command
        .args(dwo_files)
        .args(["-o", output.to_str().unwrap()]);
    run_bounded(&mut command, Duration::from_secs(15));
}

#[test]
fn imports_multi_unit_dwarf4_and_dwarf5_packages() {
    for dwarf_version in ["-gdwarf-4", "-gdwarf-5"] {
        let directory = tempfile::tempdir().unwrap();
        let (image, dwo_files) = compile_split_units(directory.path(), dwarf_version);
        let dwp = image.with_extension("dwp");
        package_dwo_files(&dwo_files, &dwp);
        for dwo in dwo_files {
            std::fs::rename(&dwo, dwo.with_extension("dwo.unavailable")).unwrap();
        }

        let report = ElfConverter::new(dwarf_only_options())
            .convert_path(&image)
            .unwrap();
        let first = report
            .builder
            .functions()
            .iter()
            .find(|function| function.name == b"packaged_first" && function.inline.is_some())
            .unwrap();
        let second = report
            .builder
            .functions()
            .iter()
            .find(|function| function.name == b"packaged_second" && !function.lines.is_empty())
            .unwrap();
        assert!(
            !first.lines.is_empty(),
            "{dwarf_version}: {:?}",
            report.warnings
        );
        assert!(!second.lines.is_empty());
        assert_function_addresses(
            &report,
            &image,
            &[b"packaged_first", b"packaged_second", b"main"],
        );
        let [inline] = first.inline.as_ref().unwrap().children.as_slice() else {
            panic!("expected one packaged inline child for {dwarf_version}");
        };
        assert_ne!(inline.call_file, gsym::FileIndex::ZERO);
        assert!(
            report.warnings.iter().all(|warning| !matches!(
                warning,
                ConversionWarning::MissingInlineCallFile { .. }
                    | ConversionWarning::InvalidDeclarationFile { .. }
            )),
            "{dwarf_version}: {:?}",
            report.warnings
        );
    }
}

#[test]
fn missing_dwp_unit_is_reported_without_aborting_symbol_conversion() {
    let directory = tempfile::tempdir().unwrap();
    let (image, dwo_files) = compile_split_units(directory.path(), "-gdwarf-5");
    let dwp = image.with_extension("dwp");
    package_dwo_files(&dwo_files[..1], &dwp);
    for dwo in dwo_files {
        std::fs::rename(&dwo, dwo.with_extension("dwo.unavailable")).unwrap();
    }

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    assert!(report.stats.symbol_functions > 0);
    assert!(report.warnings.iter().any(|warning| matches!(
        warning,
        ConversionWarning::SplitDwarfUnavailable { reasons, .. }
            if reasons.iter().any(|reason| reason.contains("absent from the DWP index"))
    )));
}

#[test]
fn malformed_dwp_indexes_fail_with_a_bounded_error() {
    let directory = tempfile::tempdir().unwrap();
    let (image, dwo_files) = compile_split_units(directory.path(), "-gdwarf-5");
    let dwp = image.with_extension("dwp");
    package_dwo_files(&dwo_files, &dwp);
    let index = directory.path().join("cu-index.bin");
    run(Command::new("objcopy")
        .arg(format!(
            "--dump-section=.debug_cu_index={}",
            index.display()
        ))
        .arg(&dwp));
    let mut index_bytes = std::fs::read(&index).unwrap();
    index_bytes.truncate(index_bytes.len() / 2);
    std::fs::write(&index, index_bytes).unwrap();
    let malformed = directory.path().join("malformed.dwp");
    run(Command::new("objcopy")
        .arg(format!(
            "--update-section=.debug_cu_index={}",
            index.display()
        ))
        .arg(&dwp)
        .arg(&malformed));

    let image_bytes = std::fs::read(image).unwrap();
    let dwp_bytes = std::fs::read(malformed).unwrap();
    let error = convert(ElfInputs::new(&image_bytes).with_dwp(&dwp_bytes))
        .expect_err("a truncated DWP index must be rejected");
    assert!(matches!(error, Error::Dwarf { .. }));
}

#[test]
fn dwp_index_cannot_pair_a_skeleton_with_the_wrong_split_unit() {
    let directory = tempfile::tempdir().unwrap();
    let (image, dwo_files) = compile_split_units(directory.path(), "-gdwarf-5");
    let dwp = image.with_extension("dwp");
    package_dwo_files(&dwo_files, &dwp);
    let index = directory.path().join("cu-index.bin");
    let dwp_bytes = std::fs::read(&dwp).unwrap();
    let dwp_file = object::File::parse(&*dwp_bytes).unwrap();
    let endian = dwp_file.endianness();
    let mut index_bytes = dwp_file
        .section_by_name(".debug_cu_index")
        .unwrap()
        .uncompressed_data()
        .unwrap()
        .into_owned();
    swap_first_two_dwp_index_rows(&mut index_bytes, endian);
    std::fs::write(&index, index_bytes).unwrap();
    let mismatched = directory.path().join("mismatched.dwp");
    run(Command::new("objcopy")
        .arg(format!(
            "--update-section=.debug_cu_index={}",
            index.display()
        ))
        .arg(&dwp)
        .arg(&mismatched));

    let image_bytes = std::fs::read(image).unwrap();
    let dwp_bytes = std::fs::read(mismatched).unwrap();
    let error = convert(ElfInputs::new(&image_bytes).with_dwp(&dwp_bytes))
        .expect_err("a DWP index must not redirect a skeleton to another split unit");
    assert!(matches!(
        error,
        Error::Malformed {
            context: "split DWARF unit",
            detail,
        } if detail.contains("ID mismatch")
    ));
}

fn swap_first_two_dwp_index_rows(index: &mut [u8], endian: object::Endianness) {
    let slot_count = endian.read_u32(index[12..16].try_into().unwrap()) as usize;
    let rows_start = 16 + slot_count * 8;
    let mut occupied = (0..slot_count).filter(|slot| {
        let start = 16 + slot * 8;
        endian.read_u64(index[start..start + 8].try_into().unwrap()) != 0
    });
    let first = occupied.next().expect("first occupied DWP index slot");
    let second = occupied.next().expect("second occupied DWP index slot");
    let first = rows_start + first * 4;
    let second = rows_start + second * 4;
    let first_row: [u8; 4] = index[first..first + 4].try_into().unwrap();
    let second_row: [u8; 4] = index[second..second + 4].try_into().unwrap();
    index[first..first + 4].copy_from_slice(&second_row);
    index[second..second + 4].copy_from_slice(&first_row);
}

#[test]
fn missing_split_dwarf_is_reported_without_aborting_symbol_conversion() {
    let directory = tempfile::tempdir().unwrap();
    let source = directory.path().join("missing-split.c");
    let image = directory.path().join("missing-split");
    std::fs::write(
        &source,
        "__attribute__((noinline)) int available_symbol(int x) { return x + 1; }\nint main(void) { return available_symbol(1); }\n",
    )
    .unwrap();
    run(Command::new("cc").current_dir(directory.path()).args([
        "-g",
        "-O1",
        "-gsplit-dwarf",
        "-o",
        image.to_str().unwrap(),
        source.to_str().unwrap(),
    ]));
    let dwo = dwo_files(directory.path()).remove(0);
    std::fs::rename(&dwo, dwo.with_extension("dwo.unavailable")).unwrap();

    let report = ElfConverter::new(ConversionOptions::default())
        .convert_path(&image)
        .unwrap();
    assert!(report.stats.symbol_functions > 0);
    assert!(
        report
            .warnings
            .iter()
            .any(|warning| matches!(warning, ConversionWarning::SplitDwarfUnavailable { .. }))
    );
}