diff options
| author | Josh Ferrell <josh@vector35.com> | 2024-07-05 10:54:07 -0400 |
|---|---|---|
| committer | Josh Ferrell <josh@vector35.com> | 2024-07-05 12:20:33 -0400 |
| commit | 98db31c6f2442aac6b3d6687e0200fa61acc8d3b (patch) | |
| tree | 04151649dbede086dacd0858075e00e90b03936c /rust/examples | |
| parent | e37ee79562dd5f850144d0d93ab513db9cf4785c (diff) | |
Remove some unnecessary clones
Diffstat (limited to 'rust/examples')
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/die_handlers.rs | 24 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs | 35 | ||||
| -rw-r--r-- | rust/examples/dwarf/dwarf_import/src/types.rs | 5 |
3 files changed, 34 insertions, 30 deletions
diff --git a/rust/examples/dwarf/dwarf_import/src/die_handlers.rs b/rust/examples/dwarf/dwarf_import/src/die_handlers.rs index bcc6be66..24ef30fb 100644 --- a/rust/examples/dwarf/dwarf_import/src/die_handlers.rs +++ b/rust/examples/dwarf/dwarf_import/src/die_handlers.rs @@ -131,7 +131,7 @@ pub(crate) fn handle_enum<R: Reader<Offset = usize>>( pub(crate) fn handle_typedef( debug_info_builder: &mut DebugInfoBuilder, entry_type: Option<TypeUID>, - typedef_name: String, + typedef_name: &String, ) -> (Option<Ref<Type>>, bool) { // All base types have: // DW_AT_name @@ -140,12 +140,8 @@ pub(crate) fn handle_typedef( // This will fail in the case where we have a typedef to a type that doesn't exist (failed to parse, incomplete, etc) if let Some(entry_type_offset) = entry_type { - if let Some((name, t)) = debug_info_builder.get_type(entry_type_offset) { - if typedef_name == name { - return (Some(t), false); - } else if typedef_name != name { - return (Some(t), true); - } + if let Some(t) = debug_info_builder.get_type(entry_type_offset) { + return (Some(t.get_type()), typedef_name != t.get_name()); } } @@ -172,7 +168,7 @@ pub(crate) fn handle_pointer<R: Reader<Offset = usize>>( if let Some(pointer_size) = get_size_as_usize(entry) { if let Some(entry_type_offset) = entry_type { - let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1; + let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type(); Some(Type::pointer_of_width( parent_type.as_ref(), pointer_size, @@ -190,7 +186,7 @@ pub(crate) fn handle_pointer<R: Reader<Offset = usize>>( )) } } else if let Some(entry_type_offset) = entry_type { - let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1; + let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type(); Some(Type::pointer_of_width( parent_type.as_ref(), debug_info_builder_context.default_address_size(), @@ -228,7 +224,7 @@ pub(crate) fn handle_array<R: Reader<Offset = usize>>( // For multidimensional arrays, DW_TAG_subrange_type or DW_TAG_enumeration_type if let Some(entry_type_offset) = entry_type { - let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1; + let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type(); let mut tree = unit.entries_tree(Some(entry.offset())).unwrap(); let mut children = tree.root().unwrap().children(); @@ -289,7 +285,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>( debug_info_builder .get_type(entry_type_offset) .expect("Subroutine return type was not processed") - .1 + .get_type() } None => Type::void(), }; @@ -326,7 +322,7 @@ pub(crate) fn handle_function<R: Reader<Offset = usize>>( debug_info_builder_context.get_name(unit, child.entry()), ) } { - let child_type = debug_info_builder.get_type(child_uid).unwrap().1; + let child_type = debug_info_builder.get_type(child_uid).unwrap().get_type(); parameters.push(FunctionParameter::new(child_type, name, None)); } } else if child.entry().tag() == constants::DW_TAG_unspecified_parameters { @@ -358,7 +354,7 @@ pub(crate) fn handle_const( // ?DW_AT_type if let Some(entry_type_offset) = entry_type { - let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1; + let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type(); Some((*parent_type).to_builder().set_const(true).finalize()) } else { Some(TypeBuilder::void().set_const(true).finalize()) @@ -378,7 +374,7 @@ pub(crate) fn handle_volatile( // ?DW_AT_type if let Some(entry_type_offset) = entry_type { - let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().1; + let parent_type = debug_info_builder.get_type(entry_type_offset).unwrap().get_type(); Some((*parent_type).to_builder().set_volatile(true).finalize()) } else { Some(TypeBuilder::void().set_volatile(true).finalize()) diff --git a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs index f3cc7b08..8317843d 100644 --- a/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs +++ b/rust/examples/dwarf/dwarf_import/src/dwarfdebuginfo.rs @@ -101,6 +101,16 @@ pub(crate) struct DebugType { commit: bool, } +impl DebugType { + pub fn get_name(&self) -> &String { + &self.name + } + + pub fn get_type(&self) -> Ref<Type> { + self.t.clone() + } +} + pub(crate) struct DebugInfoBuilderContext<R: Reader<Offset = usize>> { dwarf: Dwarf<R>, units: Vec<Unit<R>>, @@ -321,15 +331,12 @@ impl DebugInfoBuilder { self.types.remove(&type_uid); } - // TODO : Non-copy? - pub(crate) fn get_type(&self, type_uid: TypeUID) -> Option<(String, Ref<Type>)> { - self.types - .get(&type_uid) - .map(|type_ref_ref| (type_ref_ref.name.clone(), type_ref_ref.t.clone())) + pub(crate) fn get_type(&self, type_uid: TypeUID) -> Option<&DebugType> { + self.types.get(&type_uid) } pub(crate) fn contains_type(&self, type_uid: TypeUID) -> bool { - self.types.get(&type_uid).is_some() + self.types.contains_key(&type_uid) } @@ -364,7 +371,7 @@ impl DebugInfoBuilder { // Either get the known type or use a 0 confidence void type so we at least get the name applied let t = match type_uid { - Some(uid) => Conf::new(self.get_type(uid).unwrap().1, 128), + Some(uid) => Conf::new(self.get_type(uid).unwrap().get_type(), 128), None => Conf::new(Type::void(), 0) }; let function = &mut self.functions[function_index]; @@ -405,14 +412,14 @@ impl DebugInfoBuilder { if let Some((_existing_name, existing_type_uid)) = self.data_variables.insert(address, (name, type_uid)) { - let existing_type = self.get_type(existing_type_uid).unwrap().1; - let new_type = self.get_type(type_uid).unwrap().1; + let existing_type = self.get_type(existing_type_uid).unwrap().get_type(); + let new_type = self.get_type(type_uid).unwrap().get_type(); if existing_type_uid != type_uid || existing_type != new_type { warn!("DWARF info contains duplicate data variable definition. Overwriting data variable at 0x{:08x} (`{}`) with `{}`", address, - self.get_type(existing_type_uid).unwrap().1, - self.get_type(type_uid).unwrap().1 + existing_type, + new_type ); } } @@ -432,7 +439,7 @@ impl DebugInfoBuilder { for (&address, (name, type_uid)) in &self.data_variables { assert!(debug_info.add_data_variable( address, - &self.get_type(*type_uid).unwrap().1, + &self.get_type(*type_uid).unwrap().t, name.clone(), &[] // TODO : Components )); @@ -441,7 +448,7 @@ impl DebugInfoBuilder { fn get_function_type(&self, function: &FunctionInfoBuilder) -> Ref<Type> { let return_type = match function.return_type { - Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().1.clone(), 0), + Some(return_type_id) => Conf::new(self.get_type(return_type_id).unwrap().get_type(), 0), _ => Conf::new(binaryninja::types::Type::void(), 0), }; @@ -451,7 +458,7 @@ impl DebugInfoBuilder { .filter_map(|parameter| match parameter { Some((name, 0)) => Some(FunctionParameter::new(Type::void(), name.clone(), None)), Some((name, uid)) => Some(FunctionParameter::new( - self.get_type(*uid).unwrap().1, + self.get_type(*uid).unwrap().get_type(), name.clone(), None, )), diff --git a/rust/examples/dwarf/dwarf_import/src/types.rs b/rust/examples/dwarf/dwarf_import/src/types.rs index 67d0110e..ccc44f4e 100644 --- a/rust/examples/dwarf/dwarf_import/src/types.rs +++ b/rust/examples/dwarf/dwarf_import/src/types.rs @@ -160,7 +160,8 @@ fn do_structure_parse<R: Reader<Offset = usize>>( debug_info_builder_context, debug_info_builder, ) { - if let Some((_, child_type)) = debug_info_builder.get_type(child_type_id) { + if let Some(t) = debug_info_builder.get_type(child_type_id) { + let child_type = t.get_type(); if let Some(child_name) = debug_info_builder_context .get_name(unit, child.entry()) .map_or( @@ -331,7 +332,7 @@ pub(crate) fn get_type<R: Reader<Offset = usize>>( // Basic types constants::DW_TAG_typedef => { if let Some(name) = debug_info_builder_context.get_name(unit, entry) { - handle_typedef(debug_info_builder, entry_type, name) + handle_typedef(debug_info_builder, entry_type, &name) } else { (None, false) } |
