diff --git a/androscalpel/src/dex_writer.rs b/androscalpel/src/dex_writer.rs index 0124691..ee6a864 100644 --- a/androscalpel/src/dex_writer.rs +++ b/androscalpel/src/dex_writer.rs @@ -78,6 +78,8 @@ pub struct DexWriter { method_handles: Vec, /// The code_items sections. code_items: Vec, + /// The debug info items section. + debug_info_items: Vec, /// The map_list map_list: MapList, } @@ -136,6 +138,7 @@ impl Default for DexWriter { annotation_set_items: vec![], annotation_items: vec![], annotation_set_lists: vec![], + debug_info_items: vec![], } } } @@ -409,11 +412,21 @@ impl DexWriter { } else { Some(handler_list) }; + let debug_info_off = if code.debug_info.is_empty() { + 0 + } else { + let debug_info_off = self.section_manager.get_size(Section::DebugInfoItem); + let item = DebugInfoItem::deserialize_from_slice(&code.debug_info)?; + self.section_manager + .add_elt(Section::DebugInfoItem, Some(item.size())); + self.debug_info_items.push(item); + debug_info_off + 1 + }; let item = CodeItem { registers_size: code.registers_size, ins_size: code.ins_size, outs_size: code.outs_size, - debug_info_off: 0, // TODO: code.debug_info + debug_info_off, // linked in link_debug_info() insns: code.insns.clone(), tries, handlers, @@ -1332,7 +1345,7 @@ impl DexWriter { /// # Warning /// /// Linking can only occur once all sections are entirelly generated. - fn link_class_data_occurences(&mut self) -> Result<()> { + fn link_class_data_occurences(&mut self) { debug!("Link the class_data_item entries in class_def_items"); for class_def in self.class_defs_list.iter_mut() { // prelink value is set to offset in the section + 1 (to distinguish with 0) @@ -1341,7 +1354,6 @@ impl DexWriter { self.section_manager.get_offset(Section::ClassDataItem) - 1; } } - Ok(()) } /// Link the offsets of static_values_off in class_def_items. @@ -1349,7 +1361,7 @@ impl DexWriter { /// # Warning /// /// Linking can only occur once all sections are entirelly generated. - fn link_static_values(&mut self) -> Result<()> { + fn link_static_values(&mut self) { debug!("Link the static_values entries in class_def_items"); for class_def in self.class_defs_list.iter_mut() { if class_def.static_values_off != 0 { @@ -1357,7 +1369,6 @@ impl DexWriter { self.section_manager.get_offset(Section::EncodedArrayItem) - 1; } } - Ok(()) } /// Link the offsets of code item in class_data_items. @@ -1365,7 +1376,7 @@ impl DexWriter { /// # Warning /// /// Linking can only occur once all sections are entirelly generated. - fn link_code_item(&mut self) -> Result<()> { + fn link_code_item(&mut self) { debug!("Link the code_item entries in class_data_items"); for data in &mut self.class_data_list { for method in &mut data.direct_methods { @@ -1379,7 +1390,20 @@ impl DexWriter { } } } - Ok(()) + } + + /// Link the offset of debug info item in code items. + /// + /// # Warning + /// + /// Linking can only occur once all sections are entirelly generated. + fn link_debug_info(&mut self) { + debug!("Link the debug_info_off entries in code_items"); + for code in self.code_items.iter_mut() { + if code.debug_info_off != 0 { + code.debug_info_off += self.section_manager.get_offset(Section::DebugInfoItem); + } + } } /// Link all annotations objects. @@ -1387,7 +1411,7 @@ impl DexWriter { /// # Warning /// /// Linking can only occur once all sections are entirelly generated. - fn link_annotations(&mut self) -> Result<()> { + fn link_annotations(&mut self) { for annotation in self.annotations_directory_items.iter_mut() { if annotation.class_annotations_off != 0 { annotation.class_annotations_off += @@ -1427,7 +1451,6 @@ impl DexWriter { } } } - Ok(()) } fn write_dex_file(&mut self, writer: &mut dyn Write) -> Result<()> { @@ -1450,10 +1473,11 @@ impl DexWriter { self.link_header(); self.link_type_list_occurences()?; - self.link_class_data_occurences()?; - self.link_static_values()?; - self.link_code_item()?; - self.link_annotations()?; + self.link_class_data_occurences(); + self.link_static_values(); + self.link_code_item(); + self.link_debug_info(); + self.link_annotations(); debug!("Serialize the dex file"); // TODO: compute checksum, hash, ect @@ -1529,7 +1553,10 @@ impl DexWriter { for string in &self.string_data_list { string.serialize(writer)?; } - // TODO: DebugInfoItem, + // DebugInfoItem section + for debug_info in &self.debug_info_items { + debug_info.serialize(writer)?; + } // AnnotationItem section for annot in &self.annotation_items { annot.serialize(writer)?;