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

Commit

Permalink
Remove analyze_vector_table() as the ELF file has that information.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jun 21, 2023
1 parent 8e718ef commit f01365b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/cortexm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ pub fn subroutine_eq(addr1: u32, addr2: u32) -> bool {
/// The contents of the vector table
#[derive(Debug)]
pub struct VectorTable {
// entry 0
// entry 0: Initial SP
pub initial_stack_pointer: u32,
// entry 1: Reset vector
pub reset: u32,
// entry 3: HardFault handler
pub hard_fault: u32,
}
11 changes: 4 additions & 7 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ pub struct Elf<'file> {
}

impl<'file> Elf<'file> {
pub fn parse(
elf_bytes: &'file [u8],
elf_path: &'file Path,
reset_fn_address: u32,
) -> Result<Self, anyhow::Error> {
pub 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 @@ -42,7 +38,7 @@ impl<'file> Elf<'file> {

let debug_frame = extract_debug_frame(&elf)?;

let symbols = extract_symbols(&elf, reset_fn_address)?;
let symbols = extract_symbols(&elf, vector_table.reset)?;

Ok(Self {
elf,
Expand Down Expand Up @@ -149,11 +145,12 @@ fn extract_vector_table(elf: &ObjectFile) -> anyhow::Result<cortexm::VectorTable
.chunks_exact(4)
.map(|chunk| u32::from_le_bytes(chunk.try_into().unwrap()));

if let (Some(initial_stack_pointer), Some(_reset), Some(_third), Some(hard_fault)) =
if let (Some(initial_stack_pointer), Some(reset), Some(_third), Some(hard_fault)) =
(words.next(), words.next(), words.next(), words.next())
{
Ok(cortexm::VectorTable {
initial_stack_pointer,
reset,
hard_fault,
})
} else {
Expand Down
16 changes: 2 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ fn run_target_program(elf_path: &Path, chip_name: &str, opts: &cli::Opts) -> any
core.reset_and_halt(TIMEOUT)?;

// gather information
let (stack_start, reset_fn_address) = analyze_vector_table(core)?;
let elf_bytes = fs::read(elf_path)?;
let elf = &Elf::parse(&elf_bytes, elf_path, reset_fn_address)?;
let target_info = TargetInfo::new(elf, memory_map, probe_target, stack_start)?;
let elf = &Elf::parse(&elf_bytes, elf_path)?;
let target_info = TargetInfo::new(elf, memory_map, probe_target)?;

// install stack canary
let canary = Canary::install(core, &target_info, elf, opts.measure_stack)?;
Expand Down Expand Up @@ -197,17 +196,6 @@ fn flashing_progress() -> flashing::FlashProgress {
})
}

/// Read stack-pointer and reset-handler-address from the vector table.
///
/// Assumes that the target was reset-halted.
///
/// Returns `(stack_start: u32, reset_fn_address: u32)`
fn analyze_vector_table(core: &mut Core) -> anyhow::Result<(u32, u32)> {
let mut ivt = [0; 2];
core.read_32(0, &mut ivt[..])?;
Ok((ivt[0], ivt[1]))
}

fn start_program(core: &mut Core, elf: &Elf) -> anyhow::Result<()> {
log::debug!("starting device");

Expand Down
3 changes: 1 addition & 2 deletions src/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl TargetInfo {
elf: &Elf,
memory_map: Vec<MemoryRegion>,
probe_target: probe_rs::Target,
stack_start: u32,
) -> anyhow::Result<Self> {
let active_ram_region =
extract_active_ram_region(&probe_target, elf.vector_table.initial_stack_pointer);
Expand All @@ -46,7 +45,7 @@ impl TargetInfo {
memory_map,
probe_target,
stack_info,
stack_start,
stack_start: elf.vector_table.initial_stack_pointer,
})
}
}
Expand Down

0 comments on commit f01365b

Please sign in to comment.