thorin-dwp 0.11.0

Library for building DWARF packages from input DWARF objects and packages
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
pub extern crate object;

use std::{
    borrow::Cow,
    collections::HashSet,
    fmt,
    path::{Path, PathBuf},
    rc::Rc,
};

#[cfg(feature = "gc")]
use gimli::UnitType;
use gimli::{EndianSlice, Reader};
use hashbrown::HashMap;
use object::{write::Object as WritableObject, FileKind, Object, ObjectSection};
use tracing::{debug, trace};

use crate::{
    error::Result,
    ext::EndianityExt,
    index::Bucketable,
    package::{dwo_identifier_of_unit, DwarfObject, InProgressDwarfPackage, SessionHolder},
    relocate::{add_relocations, Relocate, RelocationMap},
};

mod error;
mod ext;
#[cfg(feature = "gc")]
#[path = "gc.rs"]
pub(crate) mod gc;
#[cfg(not(feature = "gc"))]
#[path = "nogc.rs"]
pub(crate) mod gc;
mod index;
mod package;
mod relocate;
mod strings;

pub use crate::error::Error;
pub use crate::package::DwoId;

/// `Session` is expected to be implemented by users of `thorin`, allowing users of `thorin` to
/// decide how to manage data, rather than `thorin` having arenas internally.
pub trait Session<Relocations> {
    /// Returns a reference to `data`'s contents with lifetime `'session`.
    fn alloc_data<'session>(&'session self, data: Vec<u8>) -> &'session [u8];

    /// Returns a reference to `data`'s contents with lifetime `'input`.
    ///
    /// If `Cow` is borrowed, then return the contained reference (`'input`). If `Cow` is owned,
    /// then calls `alloc_data` to return a reference of lifetime `'session`, which is guaranteed
    /// to be longer than `'input`, so can be returned.
    fn alloc_owned_cow<'input, 'session: 'input>(
        &'session self,
        data: Cow<'input, [u8]>,
    ) -> &'input [u8] {
        match data {
            Cow::Borrowed(data) => data,
            Cow::Owned(data) => self.alloc_data(data),
        }
    }

    /// Returns a reference to `relocation` with lifetime `'session`.
    fn alloc_relocation<'session>(&'session self, data: Relocations) -> &'session Relocations;

    /// Returns a reference to contents of file at `path` with lifetime `'session`.
    fn read_input<'session>(&'session self, path: &Path) -> std::io::Result<&'session [u8]>;
}

#[cfg_attr(not(feature = "gc"), allow(dead_code))]
struct ExecutableData<'session>(
    Rc<gimli::Dwarf<Relocate<'session, gimli::EndianSlice<'session, gimli::RunTimeEndian>>>>,
);

#[cfg_attr(not(feature = "gc"), allow(dead_code))]
struct DwoData {
    addr_size: u8,
    addr_base: gimli::DebugAddrBase<usize>,
    ranges_base: gimli::DebugRngListsBase<usize>,
}

#[cfg_attr(not(feature = "gc"), allow(dead_code))]
#[derive(Default)]
struct GarbageCollectionData<'session> {
    executable_data: HashMap<PathBuf, ExecutableData<'session>>,
    dwo_data: HashMap<DwoId, Vec<(PathBuf, DwoData)>>,
}

#[cfg_attr(not(feature = "gc"), allow(dead_code))]
impl<'session> GarbageCollectionData<'session> {
    fn put_data_for_executable(&mut self, path: &Path, data: ExecutableData<'session>) {
        self.executable_data.insert(path.to_path_buf(), data);
    }
    fn put_data_for_dwo(&mut self, executable_path: &'_ Path, dwo_id: DwoId, data: DwoData) {
        self.dwo_data.entry(dwo_id).or_default().push((executable_path.to_path_buf(), data));
    }
    fn get_data_for_executable(&self, path: &'_ Path) -> Option<&ExecutableData<'session>> {
        self.executable_data.get(path)
    }
    fn get_data_for_dwo(
        &self,
        dwo_id: DwoId,
    ) -> Option<Vec<(&ExecutableData<'session>, &DwoData)>> {
        let entries = self.dwo_data.get(&dwo_id)?;
        let r = entries
            .iter()
            .filter_map(|(path, dwo_data)| {
                self.executable_data.get(path).map(|exec| (exec, dwo_data))
            })
            .collect::<Vec<_>>();
        if r.is_empty() {
            return None;
        }
        Some(r)
    }
}

fn dwarf_from_executable<'session>(
    sess: &'session impl Session<RelocationMap>,
    path: &'_ Path,
) -> Result<gimli::Dwarf<Relocate<'session, gimli::EndianSlice<'session, gimli::RunTimeEndian>>>> {
    let data = sess.read_input(path).map_err(Error::ReadInput)?;
    let obj = object::File::parse(data).map_err(Error::ParseObjectFile)?;

    let exec_endian = obj.endianness().as_runtime_endian();

    let mut load_section = |id: gimli::SectionId| -> Result<_> {
        let mut relocations = RelocationMap::default();
        let data = match obj.section_by_name(&id.name()) {
            Some(ref section) => {
                add_relocations(&mut relocations, &obj, section)?;
                section.compressed_data()?.decompress()?
            }
            // Use a non-zero capacity so that `ReaderOffsetId`s are unique.
            None => Cow::Owned(Vec::with_capacity(1)),
        };

        let data_ref = sess.alloc_owned_cow(data);
        let reader = EndianSlice::new(data_ref, exec_endian);
        let section = reader;
        let relocations = sess.alloc_relocation(relocations);
        Ok(Relocate { relocations, section, reader })
    };

    gimli::Dwarf::load(&mut load_section)
}

/// Should missing DWARF objects referenced by executables be skipped or result in an error?
///
/// Referenced objects that are still missing when the DWARF package is finished will result in
/// an error.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum MissingReferencedObjectBehaviour {
    /// Skip missing referenced DWARF objects - useful if this is expected, i.e. the path in the
    /// executable is wrong, but the referenced object will be found because it is an input.
    Skip,
    /// Error when encountering missing referenced DWARF objects.
    Error,
}

impl MissingReferencedObjectBehaviour {
    /// Should missing referenced objects be skipped?
    pub fn skip_missing(&self) -> bool {
        match *self {
            MissingReferencedObjectBehaviour::Skip => true,
            MissingReferencedObjectBehaviour::Error => false,
        }
    }
}

/// Builder for DWARF packages, add input objects/packages with `add_input_object` or input objects
/// referenced by an executable with `add_executable` before accessing the completed object with
/// `finish`.
pub struct DwarfPackage<'output, 'session: 'output, Sess: Session<RelocationMap>> {
    sess: &'session Sess,
    #[cfg(feature = "gc")]
    gc_data: Option<GarbageCollectionData<'session>>,
    maybe_in_progress: Option<InProgressDwarfPackage<'output>>,
    targets: HashSet<DwarfObject>,
}

impl<'output, 'session: 'output, Sess> fmt::Debug for DwarfPackage<'output, 'session, Sess>
where
    Sess: Session<RelocationMap>,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DwarfPackage")
            .field("in_progress", &self.maybe_in_progress)
            .field("target_count", &self.targets.len())
            .finish()
    }
}

impl<'output, 'session: 'output, Sess> DwarfPackage<'output, 'session, Sess>
where
    Sess: Session<RelocationMap>,
{
    /// Create a new `DwarfPackage` with the provided `Session` implementation.
    pub fn new(sess: &'session Sess) -> Self {
        Self {
            sess,
            #[cfg(feature = "gc")]
            gc_data: None,
            maybe_in_progress: None,
            targets: HashSet::new(),
        }
    }

    /// Add an input object to the in-progress package.
    #[tracing::instrument(level = "trace", skip(obj))]
    fn process_input_object<'input>(&mut self, obj: &'input object::File<'input>) -> Result<()> {
        if self.maybe_in_progress.is_none() {
            self.maybe_in_progress =
                Some(InProgressDwarfPackage::new(obj.architecture(), obj.endianness()));
        }

        let encoding = if let Some(section) = obj.section_by_name(".debug_info.dwo") {
            let data = section.compressed_data()?.decompress()?;
            let data_ref = self.sess.alloc_owned_cow(data);
            let debug_info = gimli::DebugInfo::new(data_ref, obj.endianness().as_runtime_endian());
            debug_info
                .units()
                .next()
                .map_err(Error::ParseUnitHeader)?
                .map(|root_header| root_header.encoding())
                .ok_or(Error::NoCompilationUnits)?
        } else {
            debug!("no `.debug_info.dwo` in input dwarf object");
            return Ok(());
        };

        let sess = self.sess;
        self.maybe_in_progress.as_mut().expect("`process_input_object` is broken").add_input_object(
            SessionHolder::SimpleSession(sess),
            obj,
            encoding,
        )
    }

    /// Calls F with the path of each dwo in the executable.
    fn iterate_executable_dwo<F>(
        &mut self,
        dwarf: &gimli::Dwarf<Relocate<gimli::EndianSlice<gimli::RunTimeEndian>>>,
        mut f: F,
    ) -> Result<()>
    where
        F: FnMut(&mut Self, &Path) -> Result<()>,
    {
        let mut iter = dwarf.units();
        while let Some(header) = iter.next().map_err(Error::ParseUnitHeader)? {
            let unit = dwarf.unit(header.clone()).map_err(Error::ParseUnit)?;

            let target = match dwo_identifier_of_unit(&dwarf.debug_abbrev, &unit.header)? {
                Some(target) => target,
                None => {
                    debug!("no target {:?}", header.offset());
                    continue;
                }
            };

            let dwo_name = {
                let mut cursor = unit.header.entries(&unit.abbreviations);
                cursor.next_dfs()?;
                let root = cursor.current().expect("unit w/out root debugging information entry");

                let dwo_name = if let Some(val) = root.attr_value(gimli::DW_AT_dwo_name) {
                    // DWARF 5
                    val
                } else if let Some(val) = root.attr_value(gimli::DW_AT_GNU_dwo_name) {
                    // GNU Extension
                    val
                } else {
                    return Err(Error::MissingDwoName(target.index()));
                };

                dwarf.attr_string(&unit, dwo_name)?.to_string()?.into_owned()
            };

            // Prepend the compilation directory if it exists.
            let mut path = if let Some(comp_dir) = &unit.comp_dir {
                PathBuf::from(comp_dir.to_string()?.into_owned())
            } else {
                PathBuf::new()
            };
            path.push(dwo_name);

            // Only add `DwoId`s to the targets, not `DebugTypeSignature`s. There doesn't
            // appear to be a "skeleton type unit" to find the corresponding unit of (there are
            // normal type units in an executable, but should we expect to find a corresponding
            // split type unit for those?).
            if matches!(target, DwarfObject::Compilation(_)) {
                // Input objects are processed first, if a DWARF object referenced by this
                // executable was already found then don't add it to the target and try to add it
                // again.
                if let Some(package) = &self.maybe_in_progress {
                    if package.contained_units().contains(&target) {
                        continue;
                    }
                }

                debug!(?target, "adding target");
                self.targets.insert(target);
            }

            f(self, &path)?;
        }

        Ok(())
    }

    /// Add input objects referenced by executable to the DWARF package.
    #[tracing::instrument(level = "trace")]
    pub fn add_executable(
        &mut self,
        path: &Path,
        missing_behaviour: MissingReferencedObjectBehaviour,
    ) -> Result<()> {
        let dwarf = dwarf_from_executable(self.sess, path)?;
        self.iterate_executable_dwo(&dwarf, |this, path| match this.add_input_object(path) {
            Ok(()) => Ok(()),
            Err(Error::ReadInput(..)) if missing_behaviour.skip_missing() => Ok(()),
            Err(e) => Err(e),
        })
    }

    fn iterate_object<F>(&mut self, path: &'_ Path, mut f: F) -> Result<()>
    where
        F: FnMut(&mut Self, &object::File) -> Result<()>,
    {
        let data = self.sess.read_input(&path).map_err(Error::ReadInput)?;

        let kind = FileKind::parse(data).map_err(Error::ParseFileKind)?;
        trace!(?kind);
        match kind {
            FileKind::Archive => {
                let archive = object::read::archive::ArchiveFile::parse(data)
                    .map_err(Error::ParseArchiveFile)?;

                for member in archive.members() {
                    let member = member.map_err(Error::ParseArchiveMember)?;
                    let data = member.data(data)?;

                    let kind = if let Ok(kind) = FileKind::parse(data) {
                        kind
                    } else {
                        trace!("skipping non-elf archive member");
                        continue;
                    };

                    trace!(?kind, "archive member");
                    match kind {
                        FileKind::Elf32 | FileKind::Elf64 => {
                            let obj = object::File::parse(data).map_err(Error::ParseObjectFile)?;
                            f(self, &obj)?;
                        }
                        _ => {
                            trace!("skipping non-elf archive member");
                        }
                    }
                }

                Ok(())
            }
            FileKind::Elf32 | FileKind::Elf64 => {
                let obj = object::File::parse(data).map_err(Error::ParseObjectFile)?;
                f(self, &obj)
            }
            _ => Err(Error::InvalidInputKind),
        }
    }

    /// Add an input object to the DWARF package.
    ///
    /// Input object must be an archive or an elf object.
    #[tracing::instrument(level = "trace")]
    pub fn add_input_object(&mut self, path: &Path) -> Result<()> {
        self.iterate_object(path, |this, obj| this.process_input_object(obj))
    }

    /// Returns the `object::write::Object` containing the created DWARF package.
    ///
    /// Returns an `Error::MissingReferencedUnit` if DWARF objects referenced by executables were
    /// not subsequently found.
    /// Returns an `Error::NoOutputObjectCreated` if no input objects or executables were provided.
    #[tracing::instrument(level = "trace")]
    pub fn finish(self) -> Result<WritableObject<'output>> {
        match self.maybe_in_progress {
            Some(package) => {
                if let Some(missing) = self.targets.difference(package.contained_units()).next() {
                    return Err(Error::MissingReferencedUnit(missing.index()));
                }

                package.finish()
            }
            None if !self.targets.is_empty() => {
                let first_missing_unit = self
                    .targets
                    .iter()
                    .next()
                    .copied()
                    .expect("non-empty map doesn't have first element");
                Err(Error::MissingReferencedUnit(first_missing_unit.index()))
            }
            None => Err(Error::NoOutputObjectCreated),
        }
    }

    #[cfg(feature = "gc")]
    #[tracing::instrument(level = "trace")]
    pub fn preprocess_gc_executable(&mut self, path: &Path) -> Result<()> {
        let dwarf = Rc::new(dwarf_from_executable(self.sess, path)?);
        let gc_data = self.gc_data.get_or_insert_default();
        gc_data.put_data_for_executable(path, ExecutableData(dwarf.clone()));

        let mut iter = dwarf.units();
        while let Some(header) = iter.next().map_err(Error::ParseUnitHeader)? {
            let unit = dwarf.unit(header.clone()).map_err(Error::ParseUnit)?;

            let target = match dwo_identifier_of_unit(&dwarf.debug_abbrev, &unit.header)? {
                Some(DwarfObject::Compilation(dwo_id)) => dwo_id,
                Some(_) => continue,
                None => {
                    debug!("skipping unit without DWO ID in executable skeleton");
                    continue;
                }
            };

            match header.type_() {
                UnitType::Skeleton(_) => {
                    // DWARF5: addr_base is in DW_AT_addr_base.
                    let mut cursor = unit.header.entries(&unit.abbreviations);
                    cursor.next_dfs()?;
                    if let Some(root) = cursor.current() {
                        if let Some(gimli::AttributeValue::DebugAddrBase(addr_base)) =
                            root.attr_value(gimli::DW_AT_addr_base)
                        {
                            let addr_size = header.address_size();
                            trace!(
                                ?target,
                                addr_size,
                                ?addr_base,
                                "found dwo data for DWARF5 skeleton CU"
                            );
                            gc_data.put_data_for_dwo(
                                path,
                                target,
                                DwoData {
                                    addr_size,
                                    addr_base,
                                    // Always 0 for DWARF 5.
                                    ranges_base: gimli::DebugRngListsBase(0),
                                },
                            );
                        }
                    }
                }
                UnitType::Compilation => {
                    // DWARF4 GNU extension: skeleton units have DW_AT_GNU_dwo_id,
                    // DW_AT_GNU_addr_base, and DW_AT_GNU_ranges_base. If no addr
                    // or ranges base is present, default to 0.
                    let mut cursor = unit.header.entries(&unit.abbreviations);
                    cursor.next_dfs()?;
                    if let Some(root) = cursor.current() {
                        // Check for DW_AT_GNU_dwo_id to identify skeleton units.
                        if let Some(gimli::AttributeValue::DwoId(_)) =
                            root.attr_value(gimli::constants::DW_AT_GNU_dwo_id)
                        {
                            // DW_AT_GNU_addr_base defaults to 0 if absent.
                            let addr_base = root
                                .attr_value(gimli::constants::DW_AT_GNU_addr_base)
                                .and_then(|v| {
                                    let gimli::AttributeValue::DebugAddrBase(base) = v else {
                                        return None;
                                    };
                                    Some(base)
                                })
                                .unwrap_or(gimli::DebugAddrBase(0));
                            // DW_AT_GNU_ranges_base defaults to 0 if absent.
                            let ranges_base = root
                                .attr_value(gimli::constants::DW_AT_GNU_ranges_base)
                                .and_then(|v| match v {
                                    gimli::AttributeValue::DebugRngListsBase(base) => Some(base),
                                    _ => None,
                                })
                                .unwrap_or(gimli::DebugRngListsBase(0));
                            let addr_size = header.address_size();
                            trace!(
                                ?target,
                                ?addr_base,
                                ?ranges_base,
                                addr_size,
                                "found dwo data for DWARF4 GNU skeleton CU"
                            );
                            gc_data.put_data_for_dwo(
                                path,
                                target,
                                DwoData { addr_size, addr_base, ranges_base },
                            );
                        }
                    }
                }
                _ => {}
            }
        }

        Ok(())
    }

    /// Add an input object to the in-progress package with GC.
    #[cfg(feature = "gc")]
    #[tracing::instrument(level = "trace", skip(obj))]
    fn process_gc_input_object<'input>(&mut self, obj: &'input object::File<'input>) -> Result<()> {
        if self.maybe_in_progress.is_none() {
            self.maybe_in_progress =
                Some(InProgressDwarfPackage::new(obj.architecture(), obj.endianness()));
        }

        let encoding = if let Some(section) = obj.section_by_name(".debug_info.dwo") {
            let data = section.compressed_data()?.decompress()?;
            let data_ref = self.sess.alloc_owned_cow(data);
            let debug_info = gimli::DebugInfo::new(data_ref, obj.endianness().as_runtime_endian());
            debug_info
                .units()
                .next()
                .map_err(Error::ParseUnitHeader)?
                .map(|root_header| root_header.encoding())
                .ok_or(Error::NoCompilationUnits)?
        } else {
            debug!("no `.debug_info.dwo` in input dwarf object");
            return Ok(());
        };

        let sess = self.sess;
        let gc_data = self.gc_data.as_ref().ok_or(Error::GcNotInitialized)?;
        self.maybe_in_progress.as_mut().expect("`process_input_object` is broken").add_input_object(
            SessionHolder::new_gc(sess, gc_data),
            obj,
            encoding,
        )
    }

    /// Add an input object to the DWARF package with GC.
    ///
    /// Input object must be an archive or an elf object.
    #[cfg(feature = "gc")]
    #[tracing::instrument(level = "trace")]
    pub fn add_gc_input_object(&mut self, path: &Path) -> Result<()> {
        self.iterate_object(path, |this, obj| this.process_gc_input_object(obj))
    }

    #[cfg(feature = "gc")]
    #[tracing::instrument(level = "trace")]
    pub fn add_gc_executable(
        &mut self,
        path: &Path,
        missing_behaviour: MissingReferencedObjectBehaviour,
    ) -> Result<()> {
        let dwarf = self.gc_data.as_ref().ok_or(Error::GcNotInitialized)?.get_data_for_executable(path)
            .expect("All executables passed to add_gc_executable() must have preprocess_gc_executable() called first.")
            .0
            .clone();
        self.iterate_executable_dwo(&dwarf, |this, path| match this.add_gc_input_object(path) {
            Ok(()) => Ok(()),
            Err(Error::ReadInput(..)) if missing_behaviour.skip_missing() => Ok(()),
            Err(e) => Err(e),
        })
    }
}