Skip to content

Commit

Permalink
Add handling for incomplete types in DWARF debug data
Browse files Browse the repository at this point in the history
C allows incomplete type declarations.
For example "struct foo;" only tells the compiler that his type exists, and
results in a stub type entry with the attribute DW_AT_declaration in the DWARF
data.
The DWARF typereader can now handle this case.

Fixes issue #38
  • Loading branch information
DanielT committed Sep 26, 2024
1 parent 2a9f70c commit c1100bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/dwarf/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,15 @@ pub(crate) fn get_type_attribute(
_ => Err("failed to get DIE tree".to_string()),
}
}

// get the DW_AT_declaration attribute
pub(crate) fn get_declaration_attribute(
entry: &DebuggingInformationEntry<SliceType, usize>,
) -> Option<bool> {
let decl_attr = get_attr_value(entry, gimli::constants::DW_AT_declaration)?;
if let gimli::AttributeValue::Flag(flag) = decl_attr {
Some(flag)
} else {
None
}
}
15 changes: 15 additions & 0 deletions src/dwarf/typereader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ impl DebugDataReader<'_> {
let entries_tree_node = entries_tree.root().map_err(|err| err.to_string())?;
let entry = entries_tree_node.entry();
let typename = get_name_attribute(entry, &self.dwarf, unit).ok();
let is_declaration = get_declaration_attribute(entry).unwrap_or(false);

if is_declaration {
// This is a declaration, not a definition. This happens when a type is declared but not defined
// e.g. "struct foo;" in a header file.
// We can't do anything with this - return a dummy type, and don't store it in the types map.
return Ok(TypeInfo {
datatype: DwarfDataType::Other(0),
name: typename,
unit_idx: current_unit,
dbginfo_offset: dbginfo_offset.0,
});
}

// track in-progress items to prevent infinite recursion
typereader_data.wip_items.push(WipItemInfo::new(
dbginfo_offset.0,
typename.clone(),
Expand Down

0 comments on commit c1100bd

Please sign in to comment.