Skip to content

Commit

Permalink
write: add LineConvert
Browse files Browse the repository at this point in the history
This allow reuse of the implementation of `LineProgram::from` as part
of a more complex transformation.

Operation of `LineProgram::from` is mostly unchanged. The one difference
is that it now uses the string form chosen by `LineString::new`
instead of copying the form of the input.
  • Loading branch information
philipc committed Aug 22, 2024
1 parent f49dfeb commit b44faff
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 147 deletions.
24 changes: 24 additions & 0 deletions src/read/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,30 @@ impl<R: Reader> Dwarf<R> {
}
}

/// Return an attribute value as a string slice.
/// This only handles forms that are usable without an associated unit.
///
/// If the attribute value is one of:
///
/// - an inline `DW_FORM_string` string
/// - a `DW_FORM_strp` reference to an offset into the `.debug_str` section
/// - a `DW_FORM_strp_sup` reference to an offset into a supplementary
/// object file
/// - a `DW_FORM_line_strp` reference to an offset into the `.debug_line_str`
/// section
///
/// then return the attribute's string value. Returns an error if the attribute
/// value does not have a string form, or if a string form has an invalid value.
pub fn attr_line_string(&self, attr: AttributeValue<R>) -> Result<R> {
match attr {
AttributeValue::String(string) => Ok(string),
AttributeValue::DebugStrRef(offset) => self.string(offset),
AttributeValue::DebugStrRefSup(offset) => self.sup_string(offset),
AttributeValue::DebugLineStrRef(offset) => self.line_string(offset),
_ => Err(Error::ExpectedStringAttributeValue),
}
}

/// Return the address at the given index.
pub fn address(&self, unit: &Unit<R>, index: DebugAddrIndex<R::Offset>) -> Result<u64> {
self.debug_addr
Expand Down
14 changes: 12 additions & 2 deletions src/write/dwarf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use alloc::vec::Vec;

use crate::common::Encoding;
use crate::write::{
AbbreviationTable, LineProgram, LineStringTable, Result, Sections, StringTable, Unit,
UnitTable, Writer,
AbbreviationTable, LineProgram, LineString, LineStringTable, Result, Sections, StringTable,
Unit, UnitTable, Writer,
};

/// Writable DWARF information for more than one unit.
Expand Down Expand Up @@ -48,6 +48,11 @@ impl Dwarf {
}
Ok(())
}

/// Get a reference to the data for a line string.
pub fn get_line_string<'a>(&'a self, string: &'a LineString) -> &'a [u8] {
string.get(&self.strings, &self.line_strings)
}
}

/// Writable DWARF information for a single unit.
Expand Down Expand Up @@ -102,6 +107,11 @@ impl DwarfUnit {
abbrevs.write(&mut sections.debug_abbrev)?;
Ok(())
}

/// Get a reference to the data for a line string.
pub fn get_line_string<'a>(&'a self, string: &'a LineString) -> &'a [u8] {
string.get(&self.strings, &self.line_strings)
}
}

#[cfg(feature = "read")]
Expand Down
Loading

0 comments on commit b44faff

Please sign in to comment.