Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,20 @@ impl TypeInfo {
}

pub(crate) fn get_reference<'a>(&'a self, types: &'a HashMap<usize, TypeInfo>) -> &'a Self {
if let DbgDataType::TypeRef(dbginfo_offset, _) = &self.datatype {
types.get(dbginfo_offset).unwrap_or(self)
} else {
self
let mut current: &'a TypeInfo = self;
loop {
match &current.datatype {
DbgDataType::TypeRef(dbginfo_offset, _) => {
// Try to follow the reference; if missing, stop and return current
let next = types.get(dbginfo_offset);

match next {
Some(t) => current = t,
None => return current, // broken ref: stop here
}
}
_ => return current, // reached a concrete type
}
}
}

Expand Down
84 changes: 84 additions & 0 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ fn find_membertype<'a>(
}

let elementaddr = address + (multi_index as u64 * stride);
let arraytype = arraytype.get_reference(&debug_data.types);
find_membertype(
arraytype,
debug_data,
Expand Down Expand Up @@ -552,6 +553,89 @@ mod test {
assert!(result3.is_err());
}

#[test]
fn test_find_symbol_in_nested_typerefs() {
let mut dbgdata = DebugData {
types: HashMap::new(),
typenames: HashMap::new(),
variables: IndexMap::new(),
demangled_names: HashMap::new(),
unit_names: Vec::new(),
sections: HashMap::new(),
};

dbgdata.types.insert(
86352,
TypeInfo {
name: Some("shorttype".to_string()),
datatype: DbgDataType::Sint16,
unit_idx: 0,
dbginfo_offset: 86352,
},
);
dbgdata.types.insert(
86353,
TypeInfo {
name: Some("consttype".to_string()),
datatype: DbgDataType::TypeRef(86352, 2),
unit_idx: 0,
dbginfo_offset: 86353,
},
);
dbgdata.types.insert(
86310,
TypeInfo {
name: Some("constshortarraytype".to_string()),
unit_idx: 0,
datatype: DbgDataType::Array {
size: 20,
dim: vec![10],
stride:2,
arraytype: Box::new(TypeInfo {
name: Some("constshorttype".to_string()),
unit_idx: 0,
datatype: DbgDataType::TypeRef(86353, 2),
dbginfo_offset: 86309,
})
},
dbginfo_offset: 86310,
},
);
dbgdata.variables.insert(
"variable".to_string(),
vec![crate::debuginfo::VarInfo {
address: 0x00ca_fe00,
typeref: 86310,
unit_idx: 0,
function: None,
namespaces: vec![],
}],
);

// try the different array indexing notations
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable._0_", &dbgdata);
assert!(result.is_ok());
let symbolinfo = result.unwrap();
assert!(symbolinfo.address == 0x00ca_fe00);
assert!(matches!(symbolinfo.typeinfo.datatype, DbgDataType::Sint16));
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable[0]", &dbgdata);
assert!(result.is_ok());
assert!(result.unwrap().address == 0x00ca_fe00);
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable[1]", &dbgdata);
assert!(result.is_ok());
assert!(result.unwrap().address == 0x00ca_fe02);
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable[9]", &dbgdata);
assert!(result.is_ok());
assert!(result.unwrap().address == 0x00ca_fe12);

// there should not be a result if the symbol name contains extra unmatched components
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable[0].three", &dbgdata);
assert!(result.is_err());
// there should not be a result if the symbol name goes outside array boundaries
let result: Result<SymbolInfo<'_>, String> = find_symbol("variable[10]", &dbgdata);
assert!(result.is_err());
}

#[test]
fn test_select_varinfo() {
let mut debug_data = DebugData {
Expand Down
Loading