|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use object::{Object, ObjectSection}; |
| 3 | +use typed_arena::Arena; |
| 4 | + |
| 5 | +use crate::obj::{Section, SectionKind}; |
| 6 | + |
| 7 | +/// Parse line information from DWARF 2+ sections. |
| 8 | +pub(crate) fn parse_line_info_dwarf2( |
| 9 | + obj_file: &object::File, |
| 10 | + sections: &mut [Section], |
| 11 | +) -> Result<()> { |
| 12 | + let arena_data = Arena::new(); |
| 13 | + let arena_relocations = Arena::new(); |
| 14 | + let endian = match obj_file.endianness() { |
| 15 | + object::Endianness::Little => gimli::RunTimeEndian::Little, |
| 16 | + object::Endianness::Big => gimli::RunTimeEndian::Big, |
| 17 | + }; |
| 18 | + let dwarf = gimli::Dwarf::load(|id: gimli::SectionId| -> Result<_> { |
| 19 | + load_file_section(id, obj_file, endian, &arena_data, &arena_relocations) |
| 20 | + }) |
| 21 | + .context("loading DWARF sections")?; |
| 22 | + |
| 23 | + let mut iter = dwarf.units(); |
| 24 | + if let Some(header) = iter.next().map_err(|e| gimli_error(e, "iterating over DWARF units"))? { |
| 25 | + let unit = dwarf.unit(header).map_err(|e| gimli_error(e, "loading DWARF unit"))?; |
| 26 | + if let Some(program) = unit.line_program.clone() { |
| 27 | + let mut text_sections = sections.iter_mut().filter(|s| s.kind == SectionKind::Code); |
| 28 | + let mut lines = text_sections.next().map(|section| &mut section.line_info); |
| 29 | + |
| 30 | + let mut rows = program.rows(); |
| 31 | + while let Some((_header, row)) = |
| 32 | + rows.next_row().map_err(|e| gimli_error(e, "loading program row"))? |
| 33 | + { |
| 34 | + if let (Some(line), Some(lines)) = (row.line(), &mut lines) { |
| 35 | + lines.insert(row.address(), line.get() as u32); |
| 36 | + } |
| 37 | + if row.end_sequence() { |
| 38 | + // The next row is the start of a new sequence, which means we must |
| 39 | + // advance to the next .text section. |
| 40 | + lines = text_sections.next().map(|section| &mut section.line_info); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + if iter.next().map_err(|e| gimli_error(e, "checking for next unit"))?.is_some() { |
| 46 | + log::warn!("Multiple units found in DWARF data, only processing the first"); |
| 47 | + } |
| 48 | + |
| 49 | + Ok(()) |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Default)] |
| 53 | +struct RelocationMap(object::read::RelocationMap); |
| 54 | + |
| 55 | +impl RelocationMap { |
| 56 | + fn add(&mut self, file: &object::File, section: &object::Section) { |
| 57 | + for (offset, relocation) in section.relocations() { |
| 58 | + if let Err(e) = self.0.add(file, offset, relocation) { |
| 59 | + log::error!( |
| 60 | + "Relocation error for section {} at offset 0x{:08x}: {}", |
| 61 | + section.name().unwrap(), |
| 62 | + offset, |
| 63 | + e |
| 64 | + ); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl gimli::read::Relocate for &'_ RelocationMap { |
| 71 | + fn relocate_address(&self, offset: usize, value: u64) -> gimli::Result<u64> { |
| 72 | + Ok(self.0.relocate(offset as u64, value)) |
| 73 | + } |
| 74 | + |
| 75 | + fn relocate_offset(&self, offset: usize, value: usize) -> gimli::Result<usize> { |
| 76 | + <usize as gimli::ReaderOffset>::from_u64(self.0.relocate(offset as u64, value as u64)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +type Relocate<'a, R> = gimli::RelocateReader<R, &'a RelocationMap>; |
| 81 | + |
| 82 | +fn load_file_section<'input, 'arena, Endian: gimli::Endianity>( |
| 83 | + id: gimli::SectionId, |
| 84 | + file: &object::File<'input>, |
| 85 | + endian: Endian, |
| 86 | + arena_data: &'arena Arena<alloc::borrow::Cow<'input, [u8]>>, |
| 87 | + arena_relocations: &'arena Arena<RelocationMap>, |
| 88 | +) -> Result<Relocate<'arena, gimli::EndianSlice<'arena, Endian>>> { |
| 89 | + let mut relocations = RelocationMap::default(); |
| 90 | + let data = match file.section_by_name(id.name()) { |
| 91 | + Some(ref section) => { |
| 92 | + relocations.add(file, section); |
| 93 | + section.uncompressed_data()? |
| 94 | + } |
| 95 | + // Use a non-zero capacity so that `ReaderOffsetId`s are unique. |
| 96 | + None => alloc::borrow::Cow::Owned(Vec::with_capacity(1)), |
| 97 | + }; |
| 98 | + let data_ref = arena_data.alloc(data); |
| 99 | + let section = gimli::EndianSlice::new(data_ref, endian); |
| 100 | + let relocations = arena_relocations.alloc(relocations); |
| 101 | + Ok(Relocate::new(section, relocations)) |
| 102 | +} |
| 103 | + |
| 104 | +fn gimli_error(e: gimli::Error, context: &str) -> anyhow::Error { |
| 105 | + anyhow::anyhow!("gimli error {context}: {e:?}") |
| 106 | +} |
0 commit comments