support a few DWARF-5 only features (#1410)

Support a few DWARF-5 only features:

* read .debug_addr
* read .debug_rnglists
* read .debug_loclists when present
* add dwarf-5 test
* read .debug_addr
* read .debug_rnglists
* read .debug_loclists when present
* support .debug_line_str and .debug_str_offsets

Co-authored-by: Yury Delendik <ydelendik@mozilla.com>
This commit is contained in:
Gabor Greif
2020-04-28 00:27:22 +02:00
committed by GitHub
parent d6b1589926
commit 1639ed0e2e
12 changed files with 202 additions and 99 deletions

View File

@@ -4,12 +4,20 @@ use super::range_info_builder::RangeInfoBuilder;
use super::refs::{PendingDebugInfoRefs, PendingUnitRefs};
use super::{DebugInputContext, Reader, TransformError};
use anyhow::{bail, Error};
use gimli::{write, AttributeValue, DebugLineOffset, DebugStr, DebuggingInformationEntry};
use gimli::{
write, AttributeValue, DebugLineOffset, DebugLineStr, DebugStr, DebugStrOffsets,
DebuggingInformationEntry, Unit,
};
use wasmtime_environ::isa::TargetIsa;
#[derive(Debug)]
pub(crate) enum FileAttributeContext<'a> {
Root(Option<DebugLineOffset>),
Children(&'a Vec<write::FileId>, Option<&'a CompiledExpression<'a>>),
Children(
&'a Vec<write::FileId>,
u64,
Option<&'a CompiledExpression<'a>>,
),
}
fn is_exprloc_to_loclist_allowed(attr_name: gimli::constants::DwAt) -> bool {
@@ -28,11 +36,11 @@ fn is_exprloc_to_loclist_allowed(attr_name: gimli::constants::DwAt) -> bool {
}
pub(crate) fn clone_die_attributes<'a, R>(
unit: &Unit<R, R::Offset>,
entry: &DebuggingInformationEntry<R>,
context: &DebugInputContext<R>,
addr_tr: &'a AddressTransform,
frame_info: Option<&FunctionFrameInfo>,
unit_encoding: gimli::Encoding,
out_unit: &mut write::Unit,
current_scope_id: write::UnitEntryId,
subprogram_range_builder: Option<RangeInfoBuilder>,
@@ -49,23 +57,24 @@ where
{
let _tag = &entry.tag();
let endian = gimli::RunTimeEndian::Little;
let unit_encoding = unit.encoding();
let range_info = if let Some(subprogram_range_builder) = subprogram_range_builder {
subprogram_range_builder
} else if entry.tag() == gimli::DW_TAG_compile_unit {
// FIXME currently address_transform operate on a single func range,
// once it is fixed we can properly set DW_AT_ranges attribute.
// Using for now DW_AT_low_pc = 0.
RangeInfoBuilder::Position(0)
} else {
RangeInfoBuilder::from(entry, context, unit_encoding, cu_low_pc)?
// 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(unit, entry, context, cu_low_pc)?
};
range_info.build(addr_tr, out_unit, current_scope_id);
let mut attrs = entry.attrs();
while let Some(attr) = attrs.next()? {
let attr_value = match attr.value() {
AttributeValue::Addr(_) if attr.name() == gimli::DW_AT_low_pc => {
AttributeValue::Addr(_) | AttributeValue::DebugAddrIndex(_)
if attr.name() == gimli::DW_AT_low_pc =>
{
continue;
}
AttributeValue::Udata(_) if attr.name() == gimli::DW_AT_high_pc => {
@@ -77,11 +86,19 @@ where
AttributeValue::Exprloc(_) if attr.name() == gimli::DW_AT_frame_base => {
continue;
}
AttributeValue::DebugAddrBase(_) | AttributeValue::DebugStrOffsetsBase(_) => {
continue;
}
AttributeValue::Addr(u) => {
let addr = addr_tr.translate(u).unwrap_or(write::Address::Constant(0));
write::AttributeValue::Address(addr)
}
AttributeValue::DebugAddrIndex(i) => {
let u = context.debug_addr.get_address(4, unit.addr_base, i)?;
let addr = addr_tr.translate(u).unwrap_or(write::Address::Constant(0));
write::AttributeValue::Address(addr)
}
AttributeValue::Udata(u) => write::AttributeValue::Udata(u),
AttributeValue::Data1(d) => write::AttributeValue::Data1(d),
AttributeValue::Data2(d) => write::AttributeValue::Data2(d),
@@ -99,8 +116,8 @@ where
}
}
AttributeValue::FileIndex(i) => {
if let FileAttributeContext::Children(file_map, _) = file_context {
write::AttributeValue::FileIndex(Some(file_map[(i - 1) as usize]))
if let FileAttributeContext::Children(file_map, file_index_base, _) = file_context {
write::AttributeValue::FileIndex(Some(file_map[(i - file_index_base) as usize]))
} else {
return Err(TransformError("unexpected file index attribute").into());
}
@@ -109,9 +126,17 @@ where
let s = context.debug_str.get_str(str_offset)?.to_slice()?.to_vec();
write::AttributeValue::StringRef(out_strings.add(s))
}
AttributeValue::DebugStrOffsetsIndex(i) => {
let str_offset = context.debug_str_offsets.get_str_offset(
gimli::Format::Dwarf32,
unit.str_offsets_base,
i,
)?;
let s = context.debug_str.get_str(str_offset)?.to_slice()?.to_vec();
write::AttributeValue::StringRef(out_strings.add(s))
}
AttributeValue::RangeListsRef(r) => {
let range_info =
RangeInfoBuilder::from_ranges_ref(r, context, unit_encoding, cu_low_pc)?;
let range_info = RangeInfoBuilder::from_ranges_ref(unit, r, context, cu_low_pc)?;
let range_list_id = range_info.build_ranges(addr_tr, &mut out_unit.ranges);
write::AttributeValue::RangeListRef(range_list_id)
}
@@ -122,14 +147,14 @@ where
unit_encoding,
low_pc,
&context.debug_addr,
context.debug_addr_base,
unit.addr_base,
)?;
let frame_base = if let FileAttributeContext::Children(_, frame_base) = file_context
{
frame_base
} else {
None
};
let frame_base =
if let FileAttributeContext::Children(_, _, frame_base) = file_context {
frame_base
} else {
None
};
let mut result = None;
while let Some(loc) = locs.next()? {
if let Some(expr) =
@@ -166,12 +191,12 @@ where
write::AttributeValue::LocationListRef(list_id)
}
AttributeValue::Exprloc(ref expr) => {
let frame_base = if let FileAttributeContext::Children(_, frame_base) = file_context
{
frame_base
} else {
None
};
let frame_base =
if let FileAttributeContext::Children(_, _, frame_base) = file_context {
frame_base
} else {
None
};
if let Some(expr) = compile_expression(expr, unit_encoding, frame_base, isa)? {
if expr.is_simple() {
if let Some(expr) = expr.build() {
@@ -263,7 +288,10 @@ where
pub(crate) fn clone_attr_string<R>(
attr_value: &AttributeValue<R>,
form: gimli::DwForm,
unit: &Unit<R, R::Offset>,
debug_str: &DebugStr<R>,
debug_str_offsets: &DebugStrOffsets<R>,
debug_line_str: &DebugLineStr<R>,
out_strings: &mut write::StringTable,
) -> Result<write::LineString, Error>
where
@@ -273,6 +301,17 @@ where
AttributeValue::DebugStrRef(str_offset) => {
debug_str.get_str(*str_offset)?.to_slice()?.to_vec()
}
AttributeValue::DebugStrOffsetsIndex(i) => {
let str_offset = debug_str_offsets.get_str_offset(
gimli::Format::Dwarf32,
unit.str_offsets_base,
*i,
)?;
debug_str.get_str(str_offset)?.to_slice()?.to_vec()
}
AttributeValue::DebugLineStrRef(str_offset) => {
debug_line_str.get_str(*str_offset)?.to_slice()?.to_vec()
}
AttributeValue::String(b) => b.to_slice()?.to_vec(),
v => bail!("Unexpected attribute value: {:?}", v),
};

View File

@@ -3,7 +3,8 @@ use super::attr::clone_attr_string;
use super::{Reader, TransformError};
use anyhow::{Context, Error};
use gimli::{
write, DebugLine, DebugLineOffset, DebugStr, DebuggingInformationEntry, LineEncoding, Unit,
write, DebugLine, DebugLineOffset, DebugLineStr, DebugStr, DebugStrOffsets,
DebuggingInformationEntry, LineEncoding, Unit,
};
use more_asserts::assert_le;
use wasmtime_environ::entity::EntityRef;
@@ -46,9 +47,11 @@ pub(crate) fn clone_line_program<R>(
addr_tr: &AddressTransform,
out_encoding: gimli::Encoding,
debug_str: &DebugStr<R>,
debug_str_offsets: &DebugStrOffsets<R>,
debug_line_str: &DebugLineStr<R>,
debug_line: &DebugLine<R>,
out_strings: &mut write::StringTable,
) -> Result<(write::LineProgram, DebugLineOffset, Vec<write::FileId>), Error>
) -> Result<(write::LineProgram, DebugLineOffset, Vec<write::FileId>, u64), Error>
where
R: Reader,
{
@@ -63,13 +66,19 @@ where
let out_comp_dir = clone_attr_string(
comp_dir.as_ref().context("comp_dir")?,
gimli::DW_FORM_strp,
unit,
debug_str,
debug_str_offsets,
debug_line_str,
out_strings,
)?;
let out_comp_name = clone_attr_string(
comp_name.as_ref().context("comp_name")?,
gimli::DW_FORM_strp,
unit,
debug_str,
debug_str_offsets,
debug_line_str,
out_strings,
)?;
@@ -81,7 +90,8 @@ where
);
if let Ok(program) = program {
let header = program.header();
assert_le!(header.version(), 4, "not supported 5");
let file_index_base = if header.version() < 5 { 1 } else { 0 };
assert_le!(header.version(), 5, "not supported 6");
let line_encoding = LineEncoding {
minimum_instruction_length: header.minimum_instruction_length(),
maximum_operations_per_instruction: header.maximum_operations_per_instruction(),
@@ -102,7 +112,10 @@ where
let dir_id = out_program.add_directory(clone_attr_string(
dir_attr,
gimli::DW_FORM_string,
unit,
debug_str,
debug_str_offsets,
debug_line_str,
out_strings,
)?);
dirs.push(dir_id);
@@ -114,7 +127,10 @@ where
clone_attr_string(
&file_entry.path_name(),
gimli::DW_FORM_string,
unit,
debug_str,
debug_str_offsets,
debug_line_str,
out_strings,
)?,
dir_id,
@@ -238,7 +254,7 @@ where
};
out_program.row().address_offset = address_offset;
out_program.row().op_index = *op_index;
out_program.row().file = files[(file_index - 1) as usize];
out_program.row().file = files[(file_index - file_index_base) as usize];
out_program.row().line = *line;
out_program.row().column = *column;
out_program.row().discriminator = *discriminator;
@@ -255,7 +271,7 @@ where
let end_addr = (map.offset + map.len - 1) as u64;
out_program.end_sequence(end_addr);
}
Ok((out_program, offset, files))
Ok((out_program, offset, files, file_index_base))
} else {
Err(TransformError("Valid line program not found").into())
}

View File

@@ -5,8 +5,8 @@ use crate::gc::build_dependencies;
use crate::DebugInfoData;
use anyhow::Error;
use gimli::{
write, DebugAddr, DebugAddrBase, DebugLine, DebugStr, LocationLists, RangeLists,
UnitSectionOffset,
write, DebugAddr, DebugLine, DebugLineStr, DebugStr, DebugStrOffsets, LocationLists,
RangeLists, UnitSectionOffset,
};
use std::collections::HashSet;
use thiserror::Error;
@@ -38,9 +38,10 @@ where
R: Reader,
{
debug_str: &'a DebugStr<R>,
debug_str_offsets: &'a DebugStrOffsets<R>,
debug_line_str: &'a DebugLineStr<R>,
debug_line: &'a DebugLine<R>,
debug_addr: &'a DebugAddr<R>,
debug_addr_base: DebugAddrBase<R::Offset>,
rnglists: &'a RangeLists<R>,
loclists: &'a LocationLists<R>,
reachable: &'a HashSet<UnitSectionOffset>,
@@ -58,9 +59,10 @@ pub fn transform_dwarf(
let context = DebugInputContext {
debug_str: &di.dwarf.debug_str,
debug_str_offsets: &di.dwarf.debug_str_offsets,
debug_line_str: &di.dwarf.debug_line_str,
debug_line: &di.dwarf.debug_line,
debug_addr: &di.dwarf.debug_addr,
debug_addr_base: DebugAddrBase(0),
rnglists: &di.dwarf.ranges,
loclists: &di.dwarf.locations,
reachable: &reachable,

View File

@@ -1,7 +1,7 @@
use super::address_transform::AddressTransform;
use super::{DebugInputContext, Reader};
use anyhow::Error;
use gimli::{write, AttributeValue, DebuggingInformationEntry, RangeListsOffset};
use gimli::{write, AttributeValue, DebuggingInformationEntry, RangeListsOffset, Unit};
use more_asserts::assert_lt;
use wasmtime_environ::entity::EntityRef;
use wasmtime_environ::wasm::DefinedFuncIndex;
@@ -15,21 +15,25 @@ pub(crate) enum RangeInfoBuilder {
impl RangeInfoBuilder {
pub(crate) fn from<R>(
unit: &Unit<R, R::Offset>,
entry: &DebuggingInformationEntry<R>,
context: &DebugInputContext<R>,
unit_encoding: gimli::Encoding,
cu_low_pc: u64,
) -> Result<Self, Error>
where
R: Reader,
{
if let Some(AttributeValue::RangeListsRef(r)) = entry.attr_value(gimli::DW_AT_ranges)? {
return RangeInfoBuilder::from_ranges_ref(r, context, unit_encoding, cu_low_pc);
return RangeInfoBuilder::from_ranges_ref(unit, r, context, cu_low_pc);
};
let low_pc =
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)?
{
context.debug_addr.get_address(4, unit.addr_base, i)?
} else {
return Ok(RangeInfoBuilder::Undefined);
};
@@ -44,20 +48,21 @@ impl RangeInfoBuilder {
}
pub(crate) fn from_ranges_ref<R>(
unit: &Unit<R, R::Offset>,
ranges: RangeListsOffset,
context: &DebugInputContext<R>,
unit_encoding: gimli::Encoding,
cu_low_pc: u64,
) -> Result<Self, Error>
where
R: Reader,
{
let unit_encoding = unit.encoding();
let mut ranges = context.rnglists.ranges(
ranges,
unit_encoding,
cu_low_pc,
&context.debug_addr,
context.debug_addr_base,
unit.addr_base,
)?;
let mut result = Vec::new();
while let Some(range) = ranges.next()? {
@@ -75,18 +80,23 @@ impl RangeInfoBuilder {
}
pub(crate) fn from_subprogram_die<R>(
unit: &Unit<R, R::Offset>,
entry: &DebuggingInformationEntry<R>,
context: &DebugInputContext<R>,
unit_encoding: gimli::Encoding,
addr_tr: &AddressTransform,
cu_low_pc: u64,
) -> Result<Self, Error>
where
R: Reader,
{
let unit_encoding = unit.encoding();
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)?
{
context.debug_addr.get_address(4, unit.addr_base, i)?
} else if let Some(AttributeValue::RangeListsRef(r)) =
entry.attr_value(gimli::DW_AT_ranges)?
{
@@ -95,7 +105,7 @@ impl RangeInfoBuilder {
unit_encoding,
cu_low_pc,
&context.debug_addr,
context.debug_addr_base,
unit.addr_base,
)?;
if let Some(range) = ranges.next()? {
range.begin

View File

@@ -246,18 +246,21 @@ where
// Iterate over all of this compilation unit's entries.
let mut entries = unit.entries();
let (mut comp_unit, unit_id, file_map, cu_low_pc, wp_die_id, vmctx_die_id) =
let (mut comp_unit, unit_id, file_map, file_index_base, cu_low_pc, wp_die_id, vmctx_die_id) =
if let Some((depth_delta, entry)) = entries.next_dfs()? {
assert_eq!(depth_delta, 0);
let (out_line_program, debug_line_offset, file_map) = clone_line_program(
&unit,
entry,
addr_tr,
out_encoding,
context.debug_str,
context.debug_line,
out_strings,
)?;
let (out_line_program, debug_line_offset, file_map, file_index_base) =
clone_line_program(
&unit,
entry,
addr_tr,
out_encoding,
context.debug_str,
context.debug_str_offsets,
context.debug_line_str,
context.debug_line,
out_strings,
)?;
if entry.tag() == gimli::DW_TAG_compile_unit {
let unit_id = out_units.add(write::Unit::new(out_encoding, out_line_program));
@@ -270,17 +273,21 @@ where
entry.attr_value(gimli::DW_AT_low_pc)?
{
addr
} else if let Some(AttributeValue::DebugAddrIndex(i)) =
entry.attr_value(gimli::DW_AT_low_pc)?
{
context.debug_addr.get_address(4, unit.addr_base, i)?
} else {
// FIXME? return Err(TransformError("No low_pc for unit header").into());
0
};
clone_die_attributes(
&unit,
entry,
context,
addr_tr,
None,
unit.encoding(),
comp_unit,
root_id,
None,
@@ -301,6 +308,7 @@ where
comp_unit,
unit_id,
file_map,
file_index_base,
cu_low_pc,
wp_die_id,
vmctx_die_id,
@@ -342,13 +350,8 @@ where
current_scope_ranges.update(new_stack_len);
current_value_range.update(new_stack_len);
let range_builder = if entry.tag() == gimli::DW_TAG_subprogram {
let range_builder = RangeInfoBuilder::from_subprogram_die(
entry,
context,
unit.encoding(),
addr_tr,
cu_low_pc,
)?;
let range_builder =
RangeInfoBuilder::from_subprogram_die(&unit, entry, context, addr_tr, cu_low_pc)?;
if let RangeInfoBuilder::Function(func_index) = range_builder {
if let Some(frame_info) =
get_function_frame_info(module_info, func_index, value_ranges)
@@ -366,8 +369,7 @@ where
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(entry, context, unit.encoding(), cu_low_pc)?;
let range_builder = RangeInfoBuilder::from(&unit, entry, context, cu_low_pc)?;
current_scope_ranges.push(new_stack_len, range_builder.get_ranges(addr_tr));
Some(range_builder)
} else {
@@ -417,11 +419,11 @@ where
die_ref_map.insert(entry.offset(), die_id);
clone_die_attributes(
&unit,
entry,
context,
addr_tr,
current_value_range.top(),
unit.encoding(),
&mut comp_unit,
die_id,
range_builder,
@@ -430,7 +432,7 @@ where
out_strings,
&mut pending_die_refs,
&mut pending_di_refs,
FileAttributeContext::Children(&file_map, current_frame_base.top()),
FileAttributeContext::Children(&file_map, file_index_base, current_frame_base.top()),
isa,
)?;