From 8353080712242b436bf79eb88ecdd2afb827d6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Quentin?= Date: Mon, 26 Feb 2024 13:52:57 +0100 Subject: [PATCH] Improve resolving non-code addresses (#603) * Improve resolving non-code addresses * CHANGELOG.md entry --- CHANGELOG.md | 1 + espflash/src/cli/monitor/symbols.rs | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ae89e07..2968a821 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Change the `hard_reset` sequence to fix Windows issues (#594) +- Improve resolving non-code addresses (#603) ### Changed - Non-linux-musl: Only list the available USB Ports by default (#590) diff --git a/espflash/src/cli/monitor/symbols.rs b/espflash/src/cli/monitor/symbols.rs index ebcc5e5f..c3bb545b 100644 --- a/espflash/src/cli/monitor/symbols.rs +++ b/espflash/src/cli/monitor/symbols.rs @@ -2,7 +2,7 @@ use std::error::Error; use addr2line::{ gimli::{EndianRcSlice, RunTimeEndian}, - object::{read::File, Object, ObjectSegment}, + object::{read::File, Object, ObjectSegment, ObjectSymbol}, Context, LookupResult, }; @@ -51,10 +51,22 @@ impl<'sym> Symbols<'sym> { .and_then(|name| name.demangle().map(|s| s.into_owned()).ok()) }) .or_else(|| { - self.file.symbol_map().get(addr).map(|sym| { - addr2line::demangle_auto(std::borrow::Cow::Borrowed(sym.name()), None) - .to_string() - }) + // Don't use `symbol_map().get(addr)` - it's documentation says "Get the symbol before the given address." which might be totally wrong + let symbol = self.file.symbols().find(|symbol| { + (symbol.address()..=(symbol.address() + symbol.size())).contains(&addr) + }); + + if let Some(symbol) = symbol { + match symbol.name() { + Ok(name) if !name.is_empty() => Some( + addr2line::demangle_auto(std::borrow::Cow::Borrowed(name), None) + .to_string(), + ), + _ => None, + } + } else { + None + } }) }