Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check defmt version before demangling symbols #193

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,24 @@ pub struct Table {
entries: BTreeMap<usize, TableEntry>,
}

/// Checks if the version encoded in the symbol table is compatible with this version of the
/// `decoder` crate
pub fn check_version(version: &str) -> Result<(), String> {
if version != DEFMT_VERSION {
return Err(format!(
"defmt version mismatch (firmware is using {}, host is using {}); \
are you using the same git version of defmt and related tools?
Try `cargo install`-ing the latest git version of `probe-run` AND updating your project's `Cargo.lock` with `cargo update`",
version, DEFMT_VERSION,
));
}

Ok(())
}

impl Table {
pub fn new(entries: BTreeMap<usize, TableEntry>, version: &str) -> Result<Self, String> {
if version != DEFMT_VERSION {
return Err(format!(
"defmt version mismatch (firmware is using {}, host is using {}); \
are you using the same git version of defmt and related tools?",
version, DEFMT_VERSION,
));
}
check_version(version)?;
jonas-schievink marked this conversation as resolved.
Show resolved Hide resolved

Ok(Self { entries })
}
Expand Down
31 changes: 21 additions & 10 deletions elf2table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ use object::{Object, ObjectSection};
/// This function returns `None` if the ELF file contains no `.defmt` section
pub fn parse(elf: &[u8]) -> Result<Option<Table>, anyhow::Error> {
let elf = object::File::parse(elf)?;
// find the index of the `.defmt` section
let defmt_shndx = if let Some(section) = elf.section_by_name(".defmt") {
section.index()
} else {
return Ok(None);
};

let mut map = BTreeMap::new();
// first pass to extract the `_defmt_version`
let mut version = None;
for (_, entry) in elf.symbols() {
let name = match entry.name() {
Expand All @@ -52,6 +45,26 @@ pub fn parse(elf: &[u8]) -> Result<Option<Table>, anyhow::Error> {
}
version = Some(new_version);
}
}

let version = version.ok_or_else(|| anyhow!("defmt version symbol not found"))?;

defmt_decoder::check_version(version).map_err(anyhow::Error::msg)?;

// find the index of the `.defmt` section
let defmt_shndx = if let Some(section) = elf.section_by_name(".defmt") {
section.index()
} else {
return Ok(None);
};

// second pass to demangle symbols
let mut map = BTreeMap::new();
for (_, entry) in elf.symbols() {
let name = match entry.name() {
Some(name) => name,
None => continue,
};

if entry.section_index() == Some(defmt_shndx) {
let sym = symbol::Symbol::demangle(name)?;
Expand All @@ -67,8 +80,6 @@ pub fn parse(elf: &[u8]) -> Result<Option<Table>, anyhow::Error> {
}
}

let version = version.ok_or_else(|| anyhow!("defmt version symbol not found"))?;

Table::new(map, version)
.map_err(anyhow::Error::msg)
.map(Some)
Expand Down