diff --git a/src/frontend.rs b/src/frontend.rs index 891dcff..6b28d2c 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -358,18 +358,23 @@ fn handle_payload<'a>( } struct DebugLocReader<'a> { + code_section_offset: u32, locs: &'a [(u32, u32, SourceLoc)], } impl<'a> DebugLocReader<'a> { fn new(module: &'a Module, offset: usize) -> Self { DebugLocReader { - locs: module.debug_map.locs_from_offset(offset), + code_section_offset: u32::try_from(offset).unwrap(), + locs: &module.debug_map.tuples[..], } } fn get_loc(&mut self, offset: usize) -> SourceLoc { - let offset = u32::try_from(offset).unwrap(); + let offset = u32::try_from(offset) + .unwrap() + .checked_sub(self.code_section_offset) + .unwrap(); while self.locs.len() > 0 { let (start, len, loc) = self.locs[0]; if offset < start { diff --git a/src/ir/debug.rs b/src/ir/debug.rs index 969de8c..e7365ce 100644 --- a/src/ir/debug.rs +++ b/src/ir/debug.rs @@ -5,7 +5,6 @@ use crate::entity::EntityVec; use addr2line::gimli; use std::collections::hash_map::Entry as HashEntry; use std::collections::HashMap; -use std::convert::TryFrom; declare_entity!(SourceFile, "file"); declare_entity!(SourceLoc, "loc"); @@ -49,7 +48,9 @@ impl Debug { #[derive(Clone, Debug, Default)] pub struct DebugMap { - tuples: Vec<(u32, u32, SourceLoc)>, + /// Each tuple is `(start, len, loc)`. The `start` offset is + /// relative to the Wasm code section (*not* the entire file). + pub tuples: Vec<(u32, u32, SourceLoc)>, } impl DebugMap { @@ -79,21 +80,4 @@ impl DebugMap { Ok(DebugMap { tuples }) } - - pub(crate) fn locs_from_offset<'a>(&'a self, offset: usize) -> &'a [(u32, u32, SourceLoc)] { - let offset = u32::try_from(offset).unwrap(); - let start = match self.tuples.binary_search_by(|&(start, len, _)| { - if offset < start { - std::cmp::Ordering::Greater - } else if offset >= (start + len) { - std::cmp::Ordering::Less - } else { - std::cmp::Ordering::Equal - } - }) { - Ok(idx) => idx, - Err(first_after) => first_after, - }; - &self.tuples[start..] - } }