Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Delete DWARF `Reader` trait, also use `gimli::UnitRef` (#11883)

Philip Craigbd7b59dad092
diff --git a/crates/cranelift/src/debug/gc.rs b/crates/cranelift/src/debug/gc.rs
index c51bfa6177..08e2ab90fe 100644
--- a/crates/cranelift/src/debug/gc.rs
+++ b/crates/cranelift/src/debug/gc.rs
@@ -85,9 +85,10 @@ fn build_unit_dependencies(
deps: &mut Dependencies,
) -> read::Result<()> {
let unit = dwarf.unit(header)?;
+ let unit = unit.unit_ref(dwarf);
let mut tree = unit.entries_tree(None)?;
let root = tree.root()?;
- build_die_dependencies(root, dwarf, &unit, at, deps)?;
+ build_die_dependencies(root, unit, at, deps)?;
Ok(())
}
@@ -144,20 +145,15 @@ fn has_die_back_edge(die: &read::DebuggingInformationEntry<Reader<'_>>) -> read:
fn has_valid_code_range(
die: &read::DebuggingInformationEntry<Reader<'_>>,
- dwarf: &read::Dwarf<Reader<'_>>,
- unit: &read::Unit<Reader<'_>>,
+ unit: read::UnitRef<Reader<'_>>,
at: &AddressTransform,
) -> read::Result<bool> {
match die.tag() {
constants::DW_TAG_subprogram => {
if let Some(ranges_attr) = die.attr_value(constants::DW_AT_ranges)? {
let offset = match ranges_attr {
- read::AttributeValue::RangeListsRef(val) => {
- dwarf.ranges_offset_from_raw(unit, val)
- }
- read::AttributeValue::DebugRngListsIndex(index) => {
- dwarf.ranges_offset(unit, index)?
- }
+ read::AttributeValue::RangeListsRef(val) => unit.ranges_offset_from_raw(val),
+ read::AttributeValue::DebugRngListsIndex(index) => unit.ranges_offset(index)?,
_ => return Ok(false),
};
let mut has_valid_base = if let Some(read::AttributeValue::Addr(low_pc)) =
@@ -167,7 +163,7 @@ fn has_valid_code_range(
} else {
None
};
- let mut it = dwarf.ranges.raw_ranges(offset, unit.encoding())?;
+ let mut it = unit.raw_ranges(offset)?;
while let Some(range) = it.next()? {
// If at least one of the range addresses can be converted,
// declaring code range as valid.
@@ -188,7 +184,7 @@ fn has_valid_code_range(
}
read::RawRngListEntry::StartxEndx { begin, .. }
| read::RawRngListEntry::StartxLength { begin, .. } => {
- let addr = dwarf.address(unit, begin)?;
+ let addr = unit.address(begin)?;
if at.can_translate_address(addr) {
return Ok(true);
}
@@ -197,7 +193,7 @@ fn has_valid_code_range(
has_valid_base = Some(at.can_translate_address(addr));
}
read::RawRngListEntry::BaseAddressx { addr } => {
- let addr = dwarf.address(unit, addr)?;
+ let addr = unit.address(addr)?;
has_valid_base = Some(at.can_translate_address(addr));
}
read::RawRngListEntry::OffsetPair { .. } => (),
@@ -208,7 +204,7 @@ fn has_valid_code_range(
if let read::AttributeValue::Addr(a) = low_pc {
return Ok(at.can_translate_address(a));
} else if let read::AttributeValue::DebugAddrIndex(i) = low_pc {
- let a = dwarf.debug_addr.get_address(4, unit.addr_base, i)?;
+ let a = unit.address(i)?;
return Ok(at.can_translate_address(a));
}
}
@@ -220,30 +216,29 @@ fn has_valid_code_range(
fn build_die_dependencies(
die: read::EntriesTreeNode<Reader<'_>>,
- dwarf: &read::Dwarf<Reader<'_>>,
- unit: &read::Unit<Reader<'_>>,
+ unit: read::UnitRef<Reader<'_>>,
at: &AddressTransform,
deps: &mut Dependencies,
) -> read::Result<()> {
let entry = die.entry();
- let offset = entry.offset().to_unit_section_offset(unit);
+ let offset = entry.offset().to_unit_section_offset(&unit);
let mut attrs = entry.attrs();
while let Some(attr) = attrs.next()? {
- build_attr_dependencies(&attr, offset, dwarf, unit, at, deps)?;
+ build_attr_dependencies(&attr, offset, unit, at, deps)?;
}
let mut children = die.children();
while let Some(child) = children.next()? {
let child_entry = child.entry();
- let child_offset = child_entry.offset().to_unit_section_offset(unit);
+ let child_offset = child_entry.offset().to_unit_section_offset(&unit);
deps.add_edge(child_offset, offset);
if has_die_back_edge(child_entry)? {
deps.add_edge(offset, child_offset);
}
- if has_valid_code_range(child_entry, dwarf, unit, at)? {
+ if has_valid_code_range(child_entry, unit, at)? {
deps.add_root(child_offset);
}
- build_die_dependencies(child, dwarf, unit, at, deps)?;
+ build_die_dependencies(child, unit, at, deps)?;
}
Ok(())
}
@@ -251,14 +246,13 @@ fn build_die_dependencies(
fn build_attr_dependencies(
attr: &read::Attribute<Reader<'_>>,
offset: UnitSectionOffset,
- _dwarf: &read::Dwarf<Reader<'_>>,
- unit: &read::Unit<Reader<'_>>,
+ unit: read::UnitRef<Reader<'_>>,
_at: &AddressTransform,
deps: &mut Dependencies,
) -> read::Result<()> {
match attr.value() {
read::AttributeValue::UnitRef(val) => {
- let ref_offset = val.to_unit_section_offset(unit);
+ let ref_offset = val.to_unit_section_offset(&unit);
deps.add_edge(offset, ref_offset);
}
read::AttributeValue::DebugInfoRef(val) => {
diff --git a/crates/cranelift/src/debug/transform/attr.rs b/crates/cranelift/src/debug/transform/attr.rs
index 0acb74894c..c360fd1cd3 100644
--- a/crates/cranelift/src/debug/transform/attr.rs
+++ b/crates/cranelift/src/debug/transform/attr.rs
@@ -10,7 +10,7 @@ use super::{TransformError, dbi_log};
use anyhow::{Error, bail};
use cranelift_codegen::isa::TargetIsa;
use gimli::{
- AttributeValue, DebugLineOffset, DebuggingInformationEntry, Dwarf, Unit, UnitOffset, write,
+ AttributeValue, DebugLineOffset, DebuggingInformationEntry, UnitOffset, UnitRef, write,
};
#[derive(Debug)]
@@ -47,10 +47,9 @@ fn is_exprloc_to_loclist_allowed(attr_name: gimli::constants::DwAt) -> bool {
}
pub(crate) fn clone_die_attributes<'a>(
- dwarf: &gimli::Dwarf<Reader<'a>>,
- unit: &Unit<Reader<'a>>,
+ unit: UnitRef<Reader<'a>>,
entry: &DebuggingInformationEntry<Reader<'a>>,
- addr_tr: &'a AddressTransform,
+ addr_tr: &AddressTransform,
frame_info: Option<&FunctionFrameInfo>,
out_unit: &mut write::Unit,
out_entry_id: write::UnitEntryId,
@@ -59,7 +58,7 @@ pub(crate) fn clone_die_attributes<'a>(
out_strings: &mut write::StringTable,
pending_die_refs: &mut PendingUnitRefs,
pending_di_refs: &mut PendingDebugInfoRefs,
- mut attr_context: EntryAttributesContext<'a>,
+ mut attr_context: EntryAttributesContext<'_>,
isa: &dyn TargetIsa,
) -> Result<(), Error> {
let unit_encoding = unit.encoding();
@@ -70,12 +69,12 @@ pub(crate) fn clone_die_attributes<'a>(
// FIXME for CU: currently address_transform operate on a single
// function range, and when CU spans multiple ranges the
// transformation may be incomplete.
- RangeInfoBuilder::from(dwarf, unit, entry)?
+ RangeInfoBuilder::from(unit, entry)?
};
range_info.build(addr_tr, out_unit, out_entry_id);
let mut is_obj_ptr = false;
- prepare_die_context(dwarf, unit, entry, &mut attr_context, &mut is_obj_ptr)?;
+ prepare_die_context(unit, entry, &mut attr_context, &mut is_obj_ptr)?;
let mut attrs = entry.attrs();
while let Some(attr) = attrs.next()? {
@@ -133,7 +132,7 @@ pub(crate) fn clone_die_attributes<'a>(
continue;
}
gimli::DW_AT_name => {
- let old_name: &str = &dwarf.attr_string(unit, attr.value())?.to_string_lossy();
+ let old_name: &str = &unit.attr_string(attr.value())?.to_string_lossy();
let new_name = format!("__{old_name}");
dbi_log!(
"Object pointer: renamed '{}' -> '{}'",
@@ -158,7 +157,7 @@ pub(crate) fn clone_die_attributes<'a>(
write::AttributeValue::Address(addr)
}
AttributeValue::DebugAddrIndex(i) => {
- let u = dwarf.address(unit, i)?;
+ let u = unit.address(i)?;
let addr = addr_tr.translate(u).unwrap_or(write::Address::Constant(0));
write::AttributeValue::Address(addr)
}
@@ -202,28 +201,18 @@ pub(crate) fn clone_die_attributes<'a>(
}
AttributeValue::String(d) => write::AttributeValue::String(d.to_vec()),
AttributeValue::DebugStrRef(_) | AttributeValue::DebugStrOffsetsIndex(_) => {
- let s = dwarf
- .attr_string(unit, attr_value)?
- .to_string_lossy()
- .into_owned();
+ let s = unit.attr_string(attr_value)?.to_vec();
write::AttributeValue::StringRef(out_strings.add(s))
}
AttributeValue::RangeListsRef(_) | AttributeValue::DebugRngListsIndex(_) => {
- let r = dwarf.attr_ranges_offset(unit, attr_value)?.unwrap();
- let range_info = RangeInfoBuilder::from_ranges_ref(dwarf, unit, r)?;
+ let r = unit.attr_ranges_offset(attr_value)?.unwrap();
+ let range_info = RangeInfoBuilder::from_ranges_ref(unit, r)?;
let range_list_id = range_info.build_ranges(addr_tr, &mut out_unit.ranges);
write::AttributeValue::RangeListRef(range_list_id)
}
AttributeValue::LocationListsRef(_) | AttributeValue::DebugLocListsIndex(_) => {
- let r = dwarf.attr_locations_offset(unit, attr_value)?.unwrap();
- let low_pc = 0;
- let mut locs = dwarf.locations.locations(
- r,
- unit_encoding,
- low_pc,
- &dwarf.debug_addr,
- unit.addr_base,
- )?;
+ let r = unit.attr_locations_offset(attr_value)?.unwrap();
+ let mut locs = unit.locations(r)?;
let frame_base =
if let EntryAttributesContext::Children { frame_base, .. } = attr_context {
frame_base
@@ -373,10 +362,9 @@ pub(crate) fn clone_die_attributes<'a>(
Ok(())
}
-fn prepare_die_context(
- dwarf: &Dwarf<Reader<'_>>,
- unit: &Unit<Reader<'_>>,
- entry: &DebuggingInformationEntry<Reader<'_>>,
+fn prepare_die_context<'a>(
+ unit: UnitRef<Reader<'a>>,
+ entry: &DebuggingInformationEntry<Reader<'a>>,
attr_context: &mut EntryAttributesContext<'_>,
is_obj_ptr: &mut bool,
) -> Result<(), Error> {
@@ -407,7 +395,7 @@ fn prepare_die_context(
subprogram.param_num += 1;
if subprogram.obj_ptr == entry.offset()
- || is_obj_ptr_param(dwarf, unit, entry, subprogram.param_num)?
+ || is_obj_ptr_param(unit, entry, subprogram.param_num)?
{
*is_obj_ptr = true;
}
@@ -418,10 +406,9 @@ fn prepare_die_context(
Ok(())
}
-fn is_obj_ptr_param(
- dwarf: &Dwarf<Reader<'_>>,
- unit: &Unit<Reader<'_>>,
- entry: &DebuggingInformationEntry<Reader<'_>>,
+fn is_obj_ptr_param<'a>(
+ unit: UnitRef<Reader<'a>>,
+ entry: &DebuggingInformationEntry<Reader<'a>>,
param_num: isize,
) -> Result<bool, Error> {
debug_assert!(entry.tag() == gimli::DW_TAG_formal_parameter);
@@ -437,7 +424,7 @@ fn is_obj_ptr_param(
{
// Either this has no name (declarations omit them), or its explicitly "this".
let name = entry.attr_value(gimli::DW_AT_name)?;
- if name.is_none() || dwarf.attr_string(unit, name.unwrap())?.slice().eq(b"this") {
+ if name.is_none() || unit.attr_string(name.unwrap())?.slice().eq(b"this") {
// Finally, a type check. We expect a pointer.
if let Some(type_attr) = entry.attr_value(gimli::DW_AT_type)? {
if let Some(type_die) = resolve_die_ref(unit, &type_attr)? {
diff --git a/crates/cranelift/src/debug/transform/debug_transform_logging.rs b/crates/cranelift/src/debug/transform/debug_transform_logging.rs
index 92c6f0ac82..eb33acb51d 100644
--- a/crates/cranelift/src/debug/transform/debug_transform_logging.rs
+++ b/crates/cranelift/src/debug/transform/debug_transform_logging.rs
@@ -2,7 +2,7 @@ use crate::{debug::Reader, translate::get_vmctx_value_label};
use core::fmt;
use cranelift_codegen::{LabelValueLoc, ValueLabelsRanges, ir::ValueLabel, isa::TargetIsa};
use gimli::{
- AttributeValue, DebuggingInformationEntry, Dwarf, LittleEndian, Unit, UnitOffset,
+ AttributeValue, DebuggingInformationEntry, LittleEndian, UnitOffset, UnitRef,
UnitSectionOffset, write,
};
@@ -22,11 +22,11 @@ macro_rules! dbi_log {
pub(crate) use dbi_log;
pub(crate) use dbi_log_enabled;
-pub struct CompileUnitSummary<'a> {
- unit: &'a Unit<Reader<'a>, usize>,
+pub struct CompileUnitSummary<'a, 'r> {
+ unit: UnitRef<'a, Reader<'r>>,
}
-impl<'a> fmt::Debug for CompileUnitSummary<'a> {
+impl<'a, 'r> fmt::Debug for CompileUnitSummary<'a, 'r> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let unit = self.unit;
let offs = get_offset_value(unit.header.offset());
@@ -44,55 +44,52 @@ impl<'a> fmt::Debug for CompileUnitSummary<'a> {
}
}
-pub fn log_get_cu_summary<'a>(unit: &'a Unit<Reader<'a>, usize>) -> CompileUnitSummary<'a> {
+pub fn log_get_cu_summary<'a, 'r>(unit: UnitRef<'a, Reader<'r>>) -> CompileUnitSummary<'a, 'r> {
CompileUnitSummary { unit }
}
-pub struct DieRefSummary<'a> {
- unit: &'a Unit<Reader<'a>, usize>,
+pub struct DieRefSummary<'a, 'r> {
+ unit: UnitRef<'a, Reader<'r>>,
unit_ref: UnitOffset,
}
-impl<'a> fmt::Debug for DieRefSummary<'a> {
+impl<'a, 'r> fmt::Debug for DieRefSummary<'a, 'r> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let section_offs = self.unit_ref.to_unit_section_offset(self.unit);
+ let section_offs = self.unit_ref.to_unit_section_offset(&self.unit);
let offs = get_offset_value(section_offs);
write!(f, "0x{offs:08x}")
}
}
-pub fn log_get_die_ref<'a>(
- unit: &'a Unit<Reader<'a>, usize>,
+pub fn log_get_die_ref<'a, 'r>(
+ unit: UnitRef<'a, Reader<'r>>,
unit_ref: UnitOffset,
-) -> DieRefSummary<'a> {
+) -> DieRefSummary<'a, 'r> {
DieRefSummary { unit, unit_ref }
}
-struct DieDetailedSummary<'a> {
- dwarf: &'a Dwarf<Reader<'a>>,
- unit: &'a Unit<Reader<'a>, usize>,
- die: &'a DebuggingInformationEntry<'a, 'a, Reader<'a>>,
+struct DieDetailedSummary<'a, 'r> {
+ unit: UnitRef<'a, Reader<'r>>,
+ die: &'a DebuggingInformationEntry<'a, 'a, Reader<'r>>,
}
-pub fn log_begin_input_die(
- dwarf: &Dwarf<Reader<'_>>,
- unit: &Unit<Reader<'_>, usize>,
- die: &DebuggingInformationEntry<Reader<'_>>,
+pub fn log_begin_input_die<'r>(
+ unit: UnitRef<Reader<'r>>,
+ die: &DebuggingInformationEntry<Reader<'r>>,
depth: isize,
) {
dbi_log!(
"=== Begin DIE at {:?} (depth = {}):\n{:?}",
log_get_die_ref(unit, die.offset()),
depth,
- DieDetailedSummary { dwarf, unit, die }
+ DieDetailedSummary { unit, die }
);
}
-impl<'a> fmt::Debug for DieDetailedSummary<'a> {
+impl<'a, 'r> fmt::Debug for DieDetailedSummary<'a, 'r> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let die = self.die;
let unit = self.unit;
- let dwarf = self.dwarf;
write!(f, "{}\n", die.tag())?;
let mut attrs = die.attrs();
@@ -104,7 +101,7 @@ impl<'a> fmt::Debug for DieDetailedSummary<'a> {
write!(f, "{addr:08x}")
}
AttributeValue::DebugAddrIndex(index) => {
- if let Some(addr) = dwarf.address(unit, index).ok() {
+ if let Some(addr) = unit.address(index).ok() {
write!(f, "{addr:08x}")
} else {
write!(f, "<error reading address at index: {}>", index.0)
@@ -123,18 +120,18 @@ impl<'a> fmt::Debug for DieDetailedSummary<'a> {
AttributeValue::String(_)
| AttributeValue::DebugStrRef(_)
| AttributeValue::DebugStrOffsetsIndex(_) => {
- if let Ok(s) = dwarf.attr_string(unit, attr_value) {
+ if let Ok(s) = unit.attr_string(attr_value) {
write!(f, "\"{}\"", &s.to_string_lossy())
} else {
write!(f, "<error reading string>")
}
}
AttributeValue::RangeListsRef(_) | AttributeValue::DebugRngListsIndex(_) => {
- let _ = dwarf.attr_ranges_offset(unit, attr_value);
+ let _ = unit.attr_ranges_offset(attr_value);
write!(f, "<TODO: rnglist dump>")
}
AttributeValue::LocationListsRef(_) | AttributeValue::DebugLocListsIndex(_) => {
- let _ = dwarf.attr_locations_offset(unit, attr_value);
+ let _ = unit.attr_locations_offset(attr_value);
write!(f, "<TODO: loclist dump>")
}
AttributeValue::Exprloc(_) => {
@@ -153,7 +150,7 @@ impl<'a> fmt::Debug for DieDetailedSummary<'a> {
AttributeValue::Inline(value) => write!(f, "{value}"),
AttributeValue::Ordering(value) => write!(f, "{value}"),
AttributeValue::UnitRef(offset) => {
- let section_offset = offset.to_unit_section_offset(unit);
+ let section_offset = offset.to_unit_section_offset(&unit);
write!(f, "0x{:08x}", get_offset_value(section_offset))
}
AttributeValue::DebugInfoRef(offset) => write!(f, "0x{:08x}", offset.0),
@@ -244,7 +241,7 @@ impl<'a> fmt::Debug for OutDieDetailedSummary<'a> {
pub fn log_end_output_die(
input_die: &DebuggingInformationEntry<Reader<'_>>,
- input_unit: &Unit<Reader<'_>, usize>,
+ input_unit: UnitRef<Reader<'_>>,
die_id: write::UnitEntryId,
unit: &write::Unit,
strings: &write::StringTable,
@@ -264,7 +261,7 @@ pub fn log_end_output_die(
pub fn log_end_output_die_skipped(
input_die: &DebuggingInformationEntry<Reader<'_>>,
- input_unit: &Unit<Reader<'_>, usize>,
+ input_unit: UnitRef<Reader<'_>>,
reason: &str,
depth: isize,
) {
diff --git a/crates/cranelift/src/debug/transform/line_program.rs b/crates/cranelift/src/debug/transform/line_program.rs
index 7fe706f6e3..c41c931ab2 100644
--- a/crates/cranelift/src/debug/transform/line_program.rs
+++ b/crates/cranelift/src/debug/transform/line_program.rs
@@ -1,7 +1,8 @@
+use super::TransformError;
use super::address_transform::AddressTransform;
-use super::{Reader, TransformError};
+use crate::debug::Reader;
use anyhow::{Error, bail};
-use gimli::{DebugLineOffset, LineEncoding, Unit, write};
+use gimli::{DebugLineOffset, LineEncoding, UnitRef, write};
use wasmtime_environ::DefinedFuncIndex;
#[derive(Debug)]
@@ -35,17 +36,13 @@ enum ReadLineProgramState {
IgnoreSequence,
}
-pub(crate) fn clone_line_program<R>(
- dwarf: &gimli::Dwarf<R>,
- unit: &Unit<R, R::Offset>,
- comp_name: Option<R>,
+pub(crate) fn clone_line_program(
+ unit: UnitRef<Reader<'_>>,
+ comp_name: Option<Reader<'_>>,
addr_tr: &AddressTransform,
out_encoding: gimli::Encoding,
out_strings: &mut write::StringTable,
-) -> Result<(write::LineProgram, DebugLineOffset, Vec<write::FileId>, u64), Error>
-where
- R: Reader,
-{
+) -> Result<(write::LineProgram, DebugLineOffset, Vec<write::FileId>, u64), Error> {
if let Some(program) = unit.line_program.clone() {
let header = program.header();
let offset = header.offset();
@@ -60,7 +57,7 @@ where
};
let out_comp_dir = match header.directory(0) {
Some(comp_dir) => clone_line_string(
- dwarf.attr_string(unit, comp_dir)?,
+ unit.attr_string(comp_dir)?,
gimli::DW_FORM_string,
out_strings,
)?,
@@ -83,7 +80,7 @@ where
dirs.push(out_program.default_directory());
for dir_attr in header.include_directories() {
let dir_id = out_program.add_directory(clone_line_string(
- dwarf.attr_string(unit, dir_attr.clone())?,
+ unit.attr_string(*dir_attr)?,
gimli::DW_FORM_string,
out_strings,
)?);
@@ -97,7 +94,7 @@ where
let dir_id = dirs[dir_index as usize];
let file_id = out_program.add_file(
clone_line_string(
- dwarf.attr_string(unit, file_entry.path_name())?,
+ unit.attr_string(file_entry.path_name())?,
gimli::DW_FORM_string,
out_strings,
)?,
@@ -245,21 +242,18 @@ where
}
}
-fn clone_line_string<R>(
- value: R,
+fn clone_line_string(
+ value: Reader<'_>,
form: gimli::DwForm,
out_strings: &mut write::StringTable,
-) -> Result<write::LineString, Error>
-where
- R: Reader,
-{
- let content = value.to_string_lossy()?.into_owned();
+) -> Result<write::LineString, Error> {
+ let content = value.to_vec();
Ok(match form {
gimli::DW_FORM_strp => {
let id = out_strings.add(content);
write::LineString::StringRef(id)
}
- gimli::DW_FORM_string => write::LineString::String(content.into()),
+ gimli::DW_FORM_string => write::LineString::String(content),
_ => bail!("DW_FORM_line_strp or other not supported"),
})
}
diff --git a/crates/cranelift/src/debug/transform/mod.rs b/crates/cranelift/src/debug/transform/mod.rs
index 2cf171bb8b..a988460cee 100644
--- a/crates/cranelift/src/debug/transform/mod.rs
+++ b/crates/cranelift/src/debug/transform/mod.rs
@@ -2,11 +2,11 @@ use self::debug_transform_logging::dbi_log;
use self::refs::DebugInfoRefsMap;
use self::simulate::generate_simulated_dwarf;
use self::unit::clone_unit;
-use crate::debug::Compilation;
use crate::debug::gc::build_dependencies;
+use crate::debug::{Compilation, Reader};
use anyhow::Error;
use cranelift_codegen::isa::TargetIsa;
-use gimli::{Dwarf, DwarfPackage, LittleEndian, Section, Unit, UnitSectionOffset, write};
+use gimli::{Dwarf, DwarfPackage, LittleEndian, Section, Unit, UnitRef, UnitSectionOffset, write};
use std::{collections::HashSet, fmt::Debug};
use synthetic::ModuleSyntheticUnit;
use thiserror::Error;
@@ -43,13 +43,6 @@ impl<'a> Compilation<'a> {
}
}
-pub(crate) trait Reader: gimli::Reader<Offset = usize> + Send + Sync {}
-
-impl<'input, Endian> Reader for gimli::EndianSlice<'input, Endian> where
- Endian: gimli::Endianity + Send + Sync
-{
-}
-
#[derive(Error, Debug)]
#[error("Debug info transform error: {0}")]
pub struct TransformError(&'static str);
@@ -202,23 +195,25 @@ pub fn transform_dwarf(
let mut iter = di.dwarf.debug_info.units();
while let Some(header) = iter.next().unwrap_or(None) {
let unit = di.dwarf.unit(header)?;
+ let unit = unit.unit_ref(&di.dwarf);
let mut split_unit = None;
- let mut split_dwarf = None;
let mut split_reachable = None;
if unit.dwo_id.is_some() {
if let Some(dwarf_package) = &dwarf_package {
if let Some((fused, fused_dwarf)) =
- replace_unit_from_split_dwarf(&unit, dwarf_package, &di.dwarf)
+ replace_unit_from_split_dwarf(unit, dwarf_package)
{
split_reachable =
Some(build_dependencies(&fused_dwarf, addr_tr)?.get_reachable());
- split_unit = Some(fused);
- split_dwarf = Some(fused_dwarf);
+ split_unit = Some((fused, fused_dwarf));
}
}
}
+ let split_unit = split_unit
+ .as_ref()
+ .map(|(split_unit, split_dwarf)| split_unit.unit_ref(split_dwarf));
let context = DebugInputContext {
reachable: split_reachable.as_ref().unwrap_or(&reachable),
};
@@ -226,9 +221,8 @@ pub fn transform_dwarf(
if let Some((id, ref_map, pending_refs)) = clone_unit(
compilation,
module,
- &unit,
- split_unit.as_ref(),
- split_dwarf.as_ref(),
+ unit,
+ split_unit,
&context,
&addr_tr,
out_encoding,
@@ -265,17 +259,13 @@ pub fn transform_dwarf(
}
fn replace_unit_from_split_dwarf<'a>(
- unit: &'a Unit<gimli::EndianSlice<'a, gimli::LittleEndian>, usize>,
- dwp: &DwarfPackage<gimli::EndianSlice<'a, gimli::LittleEndian>>,
- parent: &Dwarf<gimli::EndianSlice<'a, gimli::LittleEndian>>,
-) -> Option<(
- Unit<gimli::EndianSlice<'a, gimli::LittleEndian>, usize>,
- Dwarf<gimli::EndianSlice<'a, gimli::LittleEndian>>,
-)> {
+ unit: UnitRef<Reader<'a>>,
+ dwp: &DwarfPackage<Reader<'a>>,
+) -> Option<(Unit<Reader<'a>>, Dwarf<Reader<'a>>)> {
let dwo_id = unit.dwo_id?;
- let split_unit_dwarf = dwp.find_cu(dwo_id, parent).ok()??;
+ let split_unit_dwarf = dwp.find_cu(dwo_id, unit.dwarf).ok()??;
let unit_header = split_unit_dwarf.debug_info.units().next().ok()??;
let mut split_unit = split_unit_dwarf.unit(unit_header).ok()?;
- split_unit.copy_relocated_attributes(unit);
+ split_unit.copy_relocated_attributes(&unit);
Some((split_unit, split_unit_dwarf))
}
diff --git a/crates/cranelift/src/debug/transform/range_info_builder.rs b/crates/cranelift/src/debug/transform/range_info_builder.rs
index 776217beae..e334b1f70d 100644
--- a/crates/cranelift/src/debug/transform/range_info_builder.rs
+++ b/crates/cranelift/src/debug/transform/range_info_builder.rs
@@ -1,7 +1,7 @@
-use super::Reader;
use super::address_transform::AddressTransform;
+use crate::debug::Reader;
use anyhow::Error;
-use gimli::{AttributeValue, DebuggingInformationEntry, RangeListsOffset, Unit, write};
+use gimli::{AttributeValue, DebuggingInformationEntry, RangeListsOffset, UnitRef, write};
use wasmtime_environ::DefinedFuncIndex;
pub(crate) enum RangeInfoBuilder {
@@ -12,17 +12,13 @@ pub(crate) enum RangeInfoBuilder {
}
impl RangeInfoBuilder {
- pub(crate) fn from<R>(
- dwarf: &gimli::Dwarf<R>,
- unit: &Unit<R, R::Offset>,
- entry: &DebuggingInformationEntry<R>,
- ) -> Result<Self, Error>
- where
- R: Reader,
- {
+ pub(crate) fn from(
+ unit: UnitRef<Reader<'_>>,
+ entry: &DebuggingInformationEntry<Reader<'_>>,
+ ) -> Result<Self, Error> {
if let Some(AttributeValue::RangeListsRef(r)) = entry.attr_value(gimli::DW_AT_ranges)? {
- let r = dwarf.ranges_offset_from_raw(unit, r);
- return RangeInfoBuilder::from_ranges_ref(dwarf, unit, r);
+ let r = unit.ranges_offset_from_raw(r);
+ return RangeInfoBuilder::from_ranges_ref(unit, r);
};
let low_pc =
@@ -31,7 +27,7 @@ impl RangeInfoBuilder {
} else if let Some(AttributeValue::DebugAddrIndex(i)) =
entry.attr_value(gimli::DW_AT_low_pc)?
{
- dwarf.address(unit, i)?
+ unit.address(i)?
} else {
return Ok(RangeInfoBuilder::Undefined);
};
@@ -45,15 +41,11 @@ impl RangeInfoBuilder {
)
}
- pub(crate) fn from_ranges_ref<R>(
- dwarf: &gimli::Dwarf<R>,
- unit: &Unit<R, R::Offset>,
+ pub(crate) fn from_ranges_ref(
+ unit: UnitRef<Reader<'_>>,
ranges: RangeListsOffset,
- ) -> Result<Self, Error>
- where
- R: Reader,
- {
- let mut ranges = dwarf.ranges(unit, ranges)?;
+ ) -> Result<Self, Error> {
+ let mut ranges = unit.ranges(ranges)?;
let mut result = Vec::new();
while let Some(range) = ranges.next()? {
if range.begin >= range.end {
@@ -69,27 +61,23 @@ impl RangeInfoBuilder {
})
}
- pub(crate) fn from_subprogram_die<R>(
- dwarf: &gimli::Dwarf<R>,
- unit: &Unit<R, R::Offset>,
- entry: &DebuggingInformationEntry<R>,
+ pub(crate) fn from_subprogram_die(
+ unit: UnitRef<Reader<'_>>,
+ entry: &DebuggingInformationEntry<Reader<'_>>,
addr_tr: &AddressTransform,
- ) -> Result<Self, Error>
- where
- R: Reader,
- {
+ ) -> Result<Self, Error> {
let addr =
if let Some(AttributeValue::Addr(addr)) = entry.attr_value(gimli::DW_AT_low_pc)? {
addr
} else if let Some(AttributeValue::DebugAddrIndex(i)) =
entry.attr_value(gimli::DW_AT_low_pc)?
{
- dwarf.address(unit, i)?
+ unit.address(i)?
} else if let Some(AttributeValue::RangeListsRef(r)) =
entry.attr_value(gimli::DW_AT_ranges)?
{
- let r = dwarf.ranges_offset_from_raw(unit, r);
- let mut ranges = dwarf.ranges(unit, r)?;
+ let r = unit.ranges_offset_from_raw(r);
+ let mut ranges = unit.ranges(r)?;
if let Some(range) = ranges.next()? {
range.begin
} else {
diff --git a/crates/cranelift/src/debug/transform/unit.rs b/crates/cranelift/src/debug/transform/unit.rs
index 3bb5c0855d..698457bb9b 100644
--- a/crates/cranelift/src/debug/transform/unit.rs
+++ b/crates/cranelift/src/debug/transform/unit.rs
@@ -16,7 +16,7 @@ use anyhow::{Context, Error};
use cranelift_codegen::ir::Endianness;
use cranelift_codegen::isa::TargetIsa;
use gimli::write;
-use gimli::{AttributeValue, DebuggingInformationEntry, Dwarf, Unit};
+use gimli::{AttributeValue, DebuggingInformationEntry, UnitRef};
use std::collections::HashSet;
use wasmtime_environ::StaticModuleIndex;
use wasmtime_versioned_export_macros::versioned_stringify_ident;
@@ -57,29 +57,28 @@ impl<T> InheritedAttr<T> {
}
}
-fn get_base_type_name(
- type_entry: &DebuggingInformationEntry<Reader<'_>>,
- unit: &Unit<Reader<'_>>,
- dwarf: &Dwarf<Reader<'_>>,
+fn get_base_type_name<'a>(
+ type_entry: &DebuggingInformationEntry<Reader<'a>>,
+ unit: UnitRef<Reader<'a>>,
) -> Result<String, Error> {
// FIXME remove recursion.
if let Some(die_ref) = type_entry.attr_value(gimli::DW_AT_type)? {
if let Some(ref die) = resolve_die_ref(unit, &die_ref)? {
if let Some(value) = die.attr_value(gimli::DW_AT_name)? {
- return Ok(String::from(dwarf.attr_string(unit, value)?.to_string()?));
+ return Ok(String::from(unit.attr_string(value)?.to_string()?));
}
match die.tag() {
gimli::DW_TAG_const_type => {
- return Ok(format!("const {}", get_base_type_name(die, unit, dwarf)?));
+ return Ok(format!("const {}", get_base_type_name(die, unit)?));
}
gimli::DW_TAG_pointer_type => {
- return Ok(format!("{}*", get_base_type_name(die, unit, dwarf)?));
+ return Ok(format!("{}*", get_base_type_name(die, unit)?));
}
gimli::DW_TAG_reference_type => {
- return Ok(format!("{}&", get_base_type_name(die, unit, dwarf)?));
+ return Ok(format!("{}&", get_base_type_name(die, unit)?));
}
gimli::DW_TAG_array_type => {
- return Ok(format!("{}[]", get_base_type_name(die, unit, dwarf)?));
+ return Ok(format!("{}[]", get_base_type_name(die, unit)?));
}
_ => (),
}
@@ -110,14 +109,13 @@ enum WebAssemblyPtrKind {
///
/// Notice that "resolve_vmctx_memory_ptr" is external/builtin
/// subprogram that is not part of Wasm code.
-fn replace_pointer_type(
+fn replace_pointer_type<'a>(
parent_id: write::UnitEntryId,
kind: WebAssemblyPtrKind,
comp_unit: &mut write::Unit,
wasm_ptr_die_ref: write::Reference,
- pointer_type_entry: &DebuggingInformationEntry<Reader<'_>>,
- unit: &Unit<Reader<'_>, usize>,
- dwarf: &Dwarf<Reader<'_>>,
+ pointer_type_entry: &DebuggingInformationEntry<Reader<'a>>,
+ unit: UnitRef<Reader<'a>>,
out_strings: &mut write::StringTable,
pending_die_refs: &mut PendingUnitRefs,
) -> Result<write::UnitEntryId, Error> {
@@ -138,11 +136,11 @@ fn replace_pointer_type(
let name = match kind {
WebAssemblyPtrKind::Pointer => format!(
"WebAssemblyPtrWrapper<{}>",
- get_base_type_name(pointer_type_entry, unit, dwarf)?
+ get_base_type_name(pointer_type_entry, unit)?
),
WebAssemblyPtrKind::Reference => format!(
"WebAssemblyRefWrapper<{}>",
- get_base_type_name(pointer_type_entry, unit, dwarf)?
+ get_base_type_name(pointer_type_entry, unit)?
),
};
add_tag!(parent_id, gimli::DW_TAG_structure_type => wrapper_die as wrapper_die_id {
@@ -245,12 +243,11 @@ fn replace_pointer_type(
Ok(wrapper_die_id)
}
-pub(crate) fn clone_unit(
+pub(crate) fn clone_unit<'a>(
compilation: &mut Compilation<'_>,
module: StaticModuleIndex,
- skeleton_unit: &Unit<Reader<'_>>,
- split_unit: Option<&Unit<Reader<'_>>>,
- split_dwarf: Option<&Dwarf<Reader<'_>>>,
+ skeleton_unit: UnitRef<Reader<'a>>,
+ split_unit: Option<UnitRef<Reader<'a>>>,
context: &DebugInputContext,
addr_tr: &AddressTransform,
out_encoding: gimli::Encoding,
@@ -265,85 +262,73 @@ pub(crate) fn clone_unit(
let mut pending_di_refs = PendingDebugInfoRefs::new();
let mut stack = Vec::new();
- let skeleton_dwarf = &compilation.translations[module].debuginfo.dwarf;
-
// Iterate over all of this compilation unit's entries.
- let dwarf = split_dwarf.unwrap_or(skeleton_dwarf);
let unit = split_unit.unwrap_or(skeleton_unit);
let mut entries = unit.entries();
dbi_log!("Cloning CU {:?}", log_get_cu_summary(unit));
- let (mut out_unit, out_unit_id, file_map, file_index_base) = if let Some((depth_delta, entry)) =
- entries.next_dfs()?
- {
- assert_eq!(depth_delta, 0);
- let (out_line_program, debug_line_offset, file_map, file_index_base) = clone_line_program(
- skeleton_dwarf,
- skeleton_unit,
- unit.name,
- addr_tr,
- out_encoding,
- out_strings,
- )?;
-
- if entry.tag() == gimli::DW_TAG_compile_unit {
- log_begin_input_die(dwarf, unit, entry, 0);
- let out_unit_id = out_units.add(write::Unit::new(out_encoding, out_line_program));
- let out_unit = out_units.get_mut(out_unit_id);
-
- let out_root_id = out_unit.root();
- die_ref_map.insert(entry.offset(), out_root_id);
-
- clone_die_attributes(
- dwarf,
- &unit,
- entry,
- addr_tr,
- None,
- out_unit,
- out_root_id,
- None,
- None,
- out_strings,
- &mut pending_die_refs,
- &mut pending_di_refs,
- EntryAttributesContext::Root(Some(debug_line_offset)),
- isa,
- )?;
- if split_unit.is_some() {
- if let Some((_, skeleton_entry)) = skeleton_unit.entries().next_dfs()? {
- clone_die_attributes(
- skeleton_dwarf,
- skeleton_unit,
- skeleton_entry,
- addr_tr,
- None,
- out_unit,
- out_root_id,
- None,
- None,
- out_strings,
- &mut pending_die_refs,
- &mut pending_di_refs,
- EntryAttributesContext::Root(Some(debug_line_offset)),
- isa,
- )?;
+ let (mut out_unit, out_unit_id, file_map, file_index_base) =
+ if let Some((depth_delta, entry)) = entries.next_dfs()? {
+ assert_eq!(depth_delta, 0);
+ let (out_line_program, debug_line_offset, file_map, file_index_base) =
+ clone_line_program(skeleton_unit, unit.name, addr_tr, out_encoding, out_strings)?;
+
+ if entry.tag() == gimli::DW_TAG_compile_unit {
+ log_begin_input_die(unit, entry, 0);
+ let out_unit_id = out_units.add(write::Unit::new(out_encoding, out_line_program));
+ let out_unit = out_units.get_mut(out_unit_id);
+
+ let out_root_id = out_unit.root();
+ die_ref_map.insert(entry.offset(), out_root_id);
+
+ clone_die_attributes(
+ unit,
+ entry,
+ addr_tr,
+ None,
+ out_unit,
+ out_root_id,
+ None,
+ None,
+ out_strings,
+ &mut pending_die_refs,
+ &mut pending_di_refs,
+ EntryAttributesContext::Root(Some(debug_line_offset)),
+ isa,
+ )?;
+ if split_unit.is_some() {
+ if let Some((_, skeleton_entry)) = skeleton_unit.entries().next_dfs()? {
+ clone_die_attributes(
+ skeleton_unit,
+ skeleton_entry,
+ addr_tr,
+ None,
+ out_unit,
+ out_root_id,
+ None,
+ None,
+ out_strings,
+ &mut pending_die_refs,
+ &mut pending_di_refs,
+ EntryAttributesContext::Root(Some(debug_line_offset)),
+ isa,
+ )?;
+ }
}
- }
- log_end_output_die(entry, unit, out_root_id, out_unit, out_strings, 0);
- stack.push(out_root_id);
- (out_unit, out_unit_id, file_map, file_index_base)
+ log_end_output_die(entry, unit, out_root_id, out_unit, out_strings, 0);
+ stack.push(out_root_id);
+ (out_unit, out_unit_id, file_map, file_index_base)
+ } else {
+ // Can happen when the DWARF is split and we dont have the package/dwo files.
+ // This is a better user experience than errorring.
+ dbi_log!("... skipped: split DW_TAG_compile_unit entry missing");
+ return Ok(None); // empty:
+ }
} else {
- // Can happen when the DWARF is split and we dont have the package/dwo files.
- // This is a better user experience than errorring.
- dbi_log!("... skipped: split DW_TAG_compile_unit entry missing");
- return Ok(None); // empty:
- }
- } else {
- dbi_log!("... skipped: empty CU (no DW_TAG_compile_unit entry)");
- return Ok(None); // empty
- };
+ dbi_log!("... skipped: empty CU (no DW_TAG_compile_unit entry)");
+ return Ok(None); // empty
+ };
let mut current_depth = 0;
let mut skip_at_depth = None;
let mut current_frame_base = InheritedAttr::new();
@@ -352,7 +337,7 @@ pub(crate) fn clone_unit(
let mut current_subprogram = InheritedAttr::new();
while let Some((depth_delta, entry)) = entries.next_dfs()? {
current_depth += depth_delta;
- log_begin_input_die(dwarf, unit, entry, current_depth);
+ log_begin_input_die(unit, entry, current_depth);
// If `skip_at_depth` is `Some` then we previously decided to skip over
// a node and all it's children. Let A be the last node processed, B be
@@ -393,8 +378,7 @@ pub(crate) fn clone_unit(
current_value_range.update(new_stack_len);
current_subprogram.update(new_stack_len);
let range_builder = if entry.tag() == gimli::DW_TAG_subprogram {
- let range_builder =
- RangeInfoBuilder::from_subprogram_die(dwarf, &unit, entry, addr_tr)?;
+ let range_builder = RangeInfoBuilder::from_subprogram_die(unit, entry, addr_tr)?;
if let RangeInfoBuilder::Function(func) = range_builder {
let frame_info = compilation.function_frame_info(module, func);
current_value_range.push(new_stack_len, frame_info);
@@ -410,7 +394,7 @@ pub(crate) fn clone_unit(
let high_pc = entry.attr_value(gimli::DW_AT_high_pc)?;
let ranges = entry.attr_value(gimli::DW_AT_ranges)?;
if high_pc.is_some() || ranges.is_some() {
- let range_builder = RangeInfoBuilder::from(dwarf, &unit, entry)?;
+ let range_builder = RangeInfoBuilder::from(unit, entry)?;
current_scope_ranges.push(new_stack_len, range_builder.get_ranges(addr_tr));
Some(range_builder)
} else {
@@ -449,7 +433,6 @@ pub(crate) fn clone_unit(
out_module_synthetic_unit.wasm_ptr_die_ref(),
entry,
unit,
- dwarf,
out_strings,
&mut pending_die_refs,
)?;
@@ -467,8 +450,7 @@ pub(crate) fn clone_unit(
die_ref_map.insert(entry.offset(), out_die_id);
clone_die_attributes(
- dwarf,
- &unit,
+ unit,
entry,
addr_tr,
current_value_range.top(),
diff --git a/crates/cranelift/src/debug/transform/utils.rs b/crates/cranelift/src/debug/transform/utils.rs
index 447d5be3cb..05a780fdbb 100644
--- a/crates/cranelift/src/debug/transform/utils.rs
+++ b/crates/cranelift/src/debug/transform/utils.rs
@@ -4,7 +4,7 @@ use super::address_transform::AddressTransform;
use super::expression::{CompiledExpression, FunctionFrameInfo};
use anyhow::Error;
use cranelift_codegen::isa::TargetIsa;
-use gimli::{AttributeValue, DebuggingInformationEntry, Unit, write};
+use gimli::{AttributeValue, DebuggingInformationEntry, UnitRef, write};
pub(crate) fn append_vmctx_info(
comp_unit: &mut write::Unit,
@@ -48,12 +48,12 @@ pub(crate) fn append_vmctx_info(
Ok(())
}
-pub fn resolve_die_ref<'a>(
- unit: &'a Unit<Reader<'a>>,
- die_ref: &'a AttributeValue<Reader<'a>>,
-) -> Result<Option<DebuggingInformationEntry<'a, 'a, Reader<'a>>>, Error> {
+pub fn resolve_die_ref<'a, 'r>(
+ unit: UnitRef<'a, Reader<'r>>,
+ die_ref: &'a AttributeValue<Reader<'r>>,
+) -> Result<Option<DebuggingInformationEntry<'a, 'a, Reader<'r>>>, Error> {
let die = match die_ref {
- AttributeValue::UnitRef(unit_offs) => Some(unit.entry(*unit_offs)?),
+ AttributeValue::UnitRef(unit_offs) => Some(unit.unit.entry(*unit_offs)?),
// TODO-DebugInfo: support AttributeValue::DebugInfoRef. The trouble is that we don't have
// a fast way to go from a DI offset to a unit offset (which is needed to parse the DIE).
// We would likely need to maintain a cache.