Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Merge #319
Browse files Browse the repository at this point in the history
319: Warn on target chip mismatch r=jonas-schievink a=Urhengulas

This PR makes `probe-run` emit a warning, in case the chip type (obtained from `probe-rs`) and the compilation target (obtained from the path to the elf file) do not match. The path will contain the compilation target if `probe-run` is called through `cargo run`. In other cases the path might not contain the compilation target. In these cases we take no action and just move on.

**Example**
```console
$ cargo rb hello -q
(HOST) WARN  Compilation target (thumbv8m.base-none-eabi) and core type (Armv7em) do not match. Your compilation target should be 'thumbv7em-none-eabi' (no FPU) or 'thumbv7em-none-eabihf' (with FPU).
...
```

Fixes #115.

**Todo**
- [x] Handle `CoreType::Armv8m`

Co-authored-by: Urhengulas <[email protected]>
  • Loading branch information
bors[bot] and Urhengulas authored Apr 14, 2022
2 parents a1b4062 + 19a59bc commit 5342459
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/elf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashSet, convert::TryInto, env, ops::Deref};
use std::{collections::HashSet, convert::TryInto, env, ops::Deref, path::Path};

use anyhow::{anyhow, bail};
use defmt_decoder::{Locations, Table};
Expand All @@ -11,15 +11,20 @@ use crate::cortexm;
pub(crate) struct Elf<'file> {
elf: ObjectFile<'file>,
symbols: Symbols,
pub(crate) live_functions: HashSet<&'file str>,
pub(crate) defmt_table: Option<Table>,
pub(crate) defmt_locations: Option<Locations>,

pub(crate) debug_frame: DebugFrame<'file>,
pub(crate) defmt_locations: Option<Locations>,
pub(crate) defmt_table: Option<Table>,
pub(crate) elf_path: &'file Path,
pub(crate) live_functions: HashSet<&'file str>,
pub(crate) vector_table: cortexm::VectorTable,
}

impl<'file> Elf<'file> {
pub(crate) fn parse(elf_bytes: &'file [u8]) -> Result<Self, anyhow::Error> {
pub(crate) fn parse(
elf_bytes: &'file [u8],
elf_path: &'file Path,
) -> Result<Self, anyhow::Error> {
let elf = ObjectFile::parse(elf_bytes)?;

let live_functions = extract_live_functions(&elf)?;
Expand All @@ -35,10 +40,11 @@ impl<'file> Elf<'file> {
Ok(Self {
elf,
symbols,
live_functions,
defmt_table,
defmt_locations,
debug_frame,
defmt_locations,
defmt_table,
elf_path,
live_functions,
vector_table,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn run_target_program(elf_path: &Path, chip_name: &str, opts: &cli::Opts) -> any
}

let elf_bytes = fs::read(elf_path)?;
let elf = &Elf::parse(&elf_bytes)?;
let elf = &Elf::parse(&elf_bytes, elf_path)?;

if let Some(cdp) = &opts.chip_description_path {
probe_rs::config::add_target_from_yaml(Path::new(cdp))?;
Expand Down
57 changes: 56 additions & 1 deletion src/target_info.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::{
convert::TryInto,
ops::{Range, RangeInclusive},
path::Path,
};

use object::{Object, ObjectSection as _};
use probe_rs::config::{MemoryRegion, RamRegion};
use probe_rs::{
config::Core,
config::{MemoryRegion, RamRegion},
CoreType,
};

use crate::elf::Elf;

Expand All @@ -24,6 +29,8 @@ pub(crate) struct StackInfo {
impl TargetInfo {
pub(crate) fn new(chip: &str, elf: &Elf) -> anyhow::Result<Self> {
let probe_target = probe_rs::config::get_target_by_name(chip)?;
check_processor_target_compatability(&probe_target.cores, elf.elf_path);

let active_ram_region =
extract_active_ram_region(&probe_target, elf.vector_table.initial_stack_pointer);
let stack_info = active_ram_region
Expand All @@ -38,6 +45,54 @@ impl TargetInfo {
}
}

/// Check if the compilation target and processor fit and emit a warning if not.
fn check_processor_target_compatability(cores: &[Core], elf_path: &Path) {
let target = elf_path.iter().find_map(|a| {
let b = a.to_string_lossy();
match b.starts_with("thumbv") {
true => Some(b),
false => None,
}
});
let target = match target {
Some(target) => target,
None => return, // NOTE(return) If probe-run is not called through `cargo run` the elf_path
// might not contain the compilation target. In that case we return early.
};

// NOTE(indexing): There *must* always be at least one core.
let core_type = cores[0].core_type;
let matches = match core_type {
CoreType::Armv6m => target == "thumbv6m-none-eabi",
CoreType::Armv7m => target == "thumbv7m-none-eabi",
CoreType::Armv7em => target == "thumbv7em-none-eabi" || target == "thumbv7em-none-eabihf",
CoreType::Armv8m => {
target == "thumbv8m.base-none-eabi"
|| target == "thumbv8m.main-none-eabi"
|| target == "thumbv8m.main-none-eabihf"
}
CoreType::Riscv => return, // NOTE(return) Since we do not get any info about instruction
// set support from probe-rs we do not know which compilation
// targets fit.
};

if matches {
return;
}
let recommendation = match core_type {
CoreType::Armv6m => "must be 'thumbv6m-none-eabi'",
CoreType::Armv7m => "should be 'thumbv7m-none-eabi'",
CoreType::Armv7em => {
"should be 'thumbv7em-none-eabi' (no FPU) or 'thumbv7em-none-eabihf' (with FPU)"
}
CoreType::Armv8m => {
"should be 'thumbv8m.base-none-eabi' (M23), 'thumbv8m.main-none-eabi' (M33 no FPU), or 'thumbv8m.main-none-eabihf' (M33 with FPU)"
}
CoreType::Riscv => unreachable!(),
};
log::warn!("Compilation target ({target}) and core type ({core_type:?}) do not match. Your compilation target {recommendation}.");
}

fn extract_active_ram_region(
target: &probe_rs::Target,
initial_stack_pointer: u32,
Expand Down

0 comments on commit 5342459

Please sign in to comment.