From 0e958f5a4ba435a826ca95adc61a79e084e90121 Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Sun, 12 Feb 2023 11:37:52 -0800 Subject: [PATCH] Fix debuginfo parsing: actually supply all needed sections to addr2line. --- src/frontend.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/ir/debug.rs | 14 +++++++------- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index 7a5bf97..ee6062f 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -21,11 +21,22 @@ pub fn wasm_to_ir(bytes: &[u8]) -> Result> { let parser = Parser::new(0); let mut next_func = 0; let mut dwarf = gimli::Dwarf::default(); + let mut extra_sections = ExtraSections::default(); for payload in parser.parse_all(bytes) { let payload = payload?; - handle_payload(&mut module, payload, &mut next_func, &mut dwarf)?; + handle_payload( + &mut module, + payload, + &mut next_func, + &mut dwarf, + &mut extra_sections, + )?; } - let debug_map = DebugMap::from_dwarf(dwarf, &mut module.debug); + dwarf.locations = + gimli::LocationLists::new(extra_sections.debug_loc, extra_sections.debug_loclists); + dwarf.ranges = + gimli::RangeLists::new(extra_sections.debug_ranges, extra_sections.debug_rnglists); + let debug_map = DebugMap::from_dwarf(dwarf, &mut module.debug)?; module.debug_map = debug_map; Ok(module) @@ -57,11 +68,20 @@ fn parse_init_expr<'a>(init_expr: &wasmparser::ConstExpr<'a>) -> Result { + debug_loc: gimli::DebugLoc>, + debug_loclists: gimli::DebugLocLists>, + debug_ranges: gimli::DebugRanges>, + debug_rnglists: gimli::DebugRngLists>, +} + fn handle_payload<'a>( module: &mut Module<'a>, payload: Payload<'a>, next_func: &mut usize, dwarf: &mut gimli::Dwarf>, + extra_sections: &mut ExtraSections<'a>, ) -> Result<()> { trace!("Wasm parser item: {:?}", payload); match payload { @@ -249,6 +269,21 @@ fn handle_payload<'a>( Payload::CustomSection(reader) if reader.name() == ".debug_types" => { dwarf.debug_types = gimli::DebugTypes::new(reader.data(), gimli::LittleEndian); } + Payload::CustomSection(reader) if reader.name() == ".debug_loc" => { + extra_sections.debug_loc = gimli::DebugLoc::new(reader.data(), gimli::LittleEndian); + } + Payload::CustomSection(reader) if reader.name() == ".debug_loclists" => { + extra_sections.debug_loclists = + gimli::DebugLocLists::new(reader.data(), gimli::LittleEndian); + } + Payload::CustomSection(reader) if reader.name() == ".debug_ranges" => { + extra_sections.debug_ranges = + gimli::DebugRanges::new(reader.data(), gimli::LittleEndian); + } + Payload::CustomSection(reader) if reader.name() == ".debug_rnglists" => { + extra_sections.debug_rnglists = + gimli::DebugRngLists::new(reader.data(), gimli::LittleEndian); + } Payload::CustomSection(_) => {} Payload::CodeSectionStart { .. } => {} Payload::Version { .. } => {} diff --git a/src/ir/debug.rs b/src/ir/debug.rs index 5602cca..7568243 100644 --- a/src/ir/debug.rs +++ b/src/ir/debug.rs @@ -55,8 +55,8 @@ impl DebugMap { pub(crate) fn from_dwarf( dwarf: gimli::Dwarf, debug: &mut Debug, - ) -> DebugMap { - let ctx = addr2line::Context::from_dwarf(dwarf).unwrap(); + ) -> anyhow::Result { + let ctx = addr2line::Context::from_dwarf(dwarf)?; let mut tuples = vec![]; let mut locs = ctx.find_location_range(0, u64::MAX).unwrap(); @@ -66,13 +66,13 @@ impl DebugMap { tuples.push((start as u32, end as u32, loc)); } - log::trace!("tuples:"); + println!("tuples:"); for &(start, end, loc) in &tuples { - log::trace!(" {:x} - {:x}: {}", start, end, loc); + println!(" {:x} - {:x}: {}", start, end, loc); } - log::trace!("files: {:?}", debug.source_files); - log::trace!("locs: {:?}", debug.source_locs); + println!("files: {:?}", debug.source_files); + println!("locs: {:?}", debug.source_locs); - DebugMap { tuples } + Ok(DebugMap { tuples }) } }