From cfc758900d702b96b497d418812c8eed704f36fe Mon Sep 17 00:00:00 2001 From: Chris Fallin Date: Mon, 13 Feb 2023 17:42:43 -0800 Subject: [PATCH] WIP. --- src/frontend.rs | 18 ++++++++---------- src/ir/debug.rs | 11 +++++++++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index 6b28d2c..d31c49f 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -36,7 +36,7 @@ pub fn wasm_to_ir(bytes: &[u8]) -> Result> { 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)?; + let debug_map = DebugMap::from_dwarf(dwarf, &mut module.debug, extra_sections.code_offset)?; module.debug_map = debug_map; Ok(module) @@ -74,6 +74,7 @@ struct ExtraSections<'a> { debug_loclists: gimli::DebugLocLists>, debug_ranges: gimli::DebugRanges>, debug_rnglists: gimli::DebugRngLists>, + code_offset: u32, } fn handle_payload<'a>( @@ -171,6 +172,7 @@ fn handle_payload<'a>( } } Payload::CodeSectionEntry(body) => { + extra_sections.code_offset = body.range().start as u32; let func_idx = Func::new(*next_func); *next_func += 1; @@ -358,23 +360,20 @@ fn handle_payload<'a>( } struct DebugLocReader<'a> { - code_section_offset: u32, + code_offset: u32, locs: &'a [(u32, u32, SourceLoc)], } impl<'a> DebugLocReader<'a> { - fn new(module: &'a Module, offset: usize) -> Self { + fn new(module: &'a Module) -> Self { DebugLocReader { - code_section_offset: u32::try_from(offset).unwrap(), + code_offset: module.debug_map.code_offset, locs: &module.debug_map.tuples[..], } } fn get_loc(&mut self, offset: usize) -> SourceLoc { - let offset = u32::try_from(offset) - .unwrap() - .checked_sub(self.code_section_offset) - .unwrap(); + let offset = u32::try_from(offset).unwrap(); while self.locs.len() > 0 { let (start, len, loc) = self.locs[0]; if offset < start { @@ -396,8 +395,7 @@ pub(crate) fn parse_body<'a>( ) -> Result { let mut ret: FunctionBody = FunctionBody::default(); - let start_offset = body.range().start; - let mut debug_locs = DebugLocReader::new(module, start_offset); + let mut debug_locs = DebugLocReader::new(module); for ¶m in &module.signatures[my_sig].params[..] { ret.locals.push(param.into()); diff --git a/src/ir/debug.rs b/src/ir/debug.rs index e7365ce..b19b484 100644 --- a/src/ir/debug.rs +++ b/src/ir/debug.rs @@ -48,8 +48,10 @@ impl Debug { #[derive(Clone, Debug, Default)] pub struct DebugMap { + /// Offset of code section relative to the Wasm file start. + pub code_offset: u32, /// Each tuple is `(start, len, loc)`. The `start` offset is - /// relative to the Wasm code section (*not* the entire file). + /// relative to the code section. pub tuples: Vec<(u32, u32, SourceLoc)>, } @@ -57,6 +59,7 @@ impl DebugMap { pub(crate) fn from_dwarf( dwarf: gimli::Dwarf, debug: &mut Debug, + code_offset: u32, ) -> anyhow::Result { let ctx = addr2line::Context::from_dwarf(dwarf)?; let mut tuples = vec![]; @@ -65,6 +68,7 @@ impl DebugMap { while let Some((start, len, loc)) = locs.next() { let file = debug.intern_file(loc.file.unwrap_or("")); let loc = debug.intern_loc(file, loc.line.unwrap_or(0), loc.column.unwrap_or(0)); + log::trace!("tuple: loc {} start {:x} len {:x}", loc, start, len); tuples.push((start as u32, len as u32, loc)); } tuples.sort(); @@ -78,6 +82,9 @@ impl DebugMap { retain }); - Ok(DebugMap { tuples }) + Ok(DebugMap { + code_offset, + tuples, + }) } }