|
| 1 | +use binaryninja::binary_view::{BinaryView, BinaryViewBase}; |
| 2 | +use binaryninja::Endianness; |
| 3 | +use gimli::{DebugInfoOffset, RunTimeEndian, UnitSectionOffset}; |
| 4 | +use std::borrow::Cow; |
| 5 | +use std::collections::BTreeMap; |
| 6 | + |
| 7 | +pub struct DebugLineInfo { |
| 8 | + pub file: String, |
| 9 | + pub line_number: u64, |
| 10 | +} |
| 11 | + |
| 12 | +impl std::fmt::Display for DebugLineInfo { |
| 13 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 14 | + write!(f, "{}:{}", self.file, self.line_number) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +pub struct DebugLineState { |
| 19 | + sections: gimli::DwarfSections<Cow<'static, [u8]>>, |
| 20 | + endian: RunTimeEndian, |
| 21 | + address_mapping: BTreeMap<u64, UnitSectionOffset>, |
| 22 | +} |
| 23 | + |
| 24 | +impl DebugLineState { |
| 25 | + // TODO: Support supplementary files and networked files. |
| 26 | + pub fn new(view: &BinaryView) -> Result<Self, Box<dyn std::error::Error>> { |
| 27 | + let section_reader = |
| 28 | + |name: &str| -> Result<Cow<'static, [u8]>, Box<dyn std::error::Error>> { |
| 29 | + let mut section = view.section_by_name(name); |
| 30 | + if section.is_none() { |
| 31 | + // Looks like macho uses underscores instead of dots for section names. |
| 32 | + let alt_name = name.replace(".", "__"); |
| 33 | + section = view.section_by_name(alt_name); |
| 34 | + } |
| 35 | + |
| 36 | + match section { |
| 37 | + Some(section) => Ok(Cow::Owned(view.read_vec(section.start(), section.len()))), |
| 38 | + // TODO: Instead of failing, we return empty section so we can continue. |
| 39 | + None => Ok(Cow::Owned(Vec::new())), |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + let loader = |section: gimli::SectionId| section_reader(section.name()); |
| 44 | + let dwarf_sections = gimli::DwarfSections::load(loader)?; |
| 45 | + |
| 46 | + let endian = match view.default_endianness() { |
| 47 | + Endianness::LittleEndian => RunTimeEndian::Little, |
| 48 | + Endianness::BigEndian => RunTimeEndian::Big, |
| 49 | + }; |
| 50 | + |
| 51 | + let borrow_section = |section| gimli::EndianSlice::new(Cow::as_ref(section), endian); |
| 52 | + let dwarf = dwarf_sections.borrow(borrow_section); |
| 53 | + |
| 54 | + // TODO: The row.address() will not be adjusted for the view image base. |
| 55 | + // let base_address = view.image_base(); |
| 56 | + let mut address_mapping = BTreeMap::new(); |
| 57 | + let mut units = dwarf.units(); |
| 58 | + while let Ok(Some(unit_header)) = units.next() { |
| 59 | + let Ok(unit) = dwarf.unit(unit_header) else { |
| 60 | + continue; |
| 61 | + }; |
| 62 | + let Some(line_program) = unit.line_program.as_ref() else { |
| 63 | + continue; |
| 64 | + }; |
| 65 | + |
| 66 | + let mut program_rows = line_program.clone().rows(); |
| 67 | + while let Ok(Some((_, row))) = program_rows.next_row() { |
| 68 | + if row.end_sequence() { |
| 69 | + continue; |
| 70 | + } |
| 71 | + address_mapping.insert(row.address(), unit_header.offset()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Ok(Self { |
| 76 | + sections: dwarf_sections, |
| 77 | + endian, |
| 78 | + address_mapping, |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + pub fn line_info(&self, address: u64) -> Option<DebugLineInfo> { |
| 83 | + // TODO: This can approximte the lie number, and should be more refined before shipping, most of |
| 84 | + // TODO: it to do with the address mapping being a tree and we selecting for the closest. |
| 85 | + let (_, unit_offset) = self.address_mapping.range(..=address).next_back()?; |
| 86 | + let borrow_section = |section| gimli::EndianSlice::new(Cow::as_ref(section), self.endian); |
| 87 | + let dwarf = self.sections.borrow(borrow_section); |
| 88 | + let unit_offset = DebugInfoOffset(unit_offset.0); |
| 89 | + let unit_header = dwarf.debug_info.header_from_offset(unit_offset).ok()?; |
| 90 | + let unit = dwarf.unit(unit_header).ok()?; |
| 91 | + |
| 92 | + let line_program = unit.line_program.as_ref()?; |
| 93 | + let mut rows = line_program.clone().rows(); |
| 94 | + |
| 95 | + let mut current_file_index = 0; |
| 96 | + let mut current_line = None; |
| 97 | + while let Ok(Some((_, row))) = rows.next_row() { |
| 98 | + if row.address() > address { |
| 99 | + break; |
| 100 | + } |
| 101 | + if row.end_sequence() { |
| 102 | + continue; |
| 103 | + } |
| 104 | + current_file_index = row.file_index(); |
| 105 | + if let Some(line) = row.line() { |
| 106 | + current_line = Some(line.get()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + let header = line_program.header(); |
| 111 | + let file = header.file(current_file_index)?; |
| 112 | + let mut file_name = "<unknown file>".to_string(); |
| 113 | + if let Ok(attr_str) = dwarf.attr_string(&unit, file.path_name()) { |
| 114 | + file_name = attr_str.to_string_lossy().into_owned(); |
| 115 | + } |
| 116 | + |
| 117 | + Some(DebugLineInfo { |
| 118 | + file: file_name, |
| 119 | + line_number: current_line?, |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments