mirror of
https://github.com/esp-rs/espflash.git
synced 2026-03-13 17:37:49 +00:00
Don't trip up on non-code sections (#896)
* Don't trip up on non-code sections * Add test
This commit is contained in:
parent
8e7f120ba8
commit
1e25b4aed8
@ -227,6 +227,7 @@ fn segments<'a>(elf: &'a ElfFile<'a>) -> Box<dyn Iterator<Item = Segment<'a>> +
|
||||
&& header.sh_type(Endianness::Little) == SHT_PROGBITS
|
||||
&& header.sh_offset.get(Endianness::Little) > 0
|
||||
&& section.address() > 0
|
||||
&& !is_empty(section.flags())
|
||||
})
|
||||
.flat_map(move |section| match section.data() {
|
||||
Ok(data) => Some(Segment::new(section.address() as u32, data)),
|
||||
@ -234,3 +235,49 @@ fn segments<'a>(elf: &'a ElfFile<'a>) -> Box<dyn Iterator<Item = Segment<'a>> +
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn is_empty(flags: object::SectionFlags) -> bool {
|
||||
match flags {
|
||||
object::SectionFlags::None => true,
|
||||
object::SectionFlags::Elf { sh_flags } => sh_flags == 0,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use object::read::elf::ElfFile;
|
||||
|
||||
use super::segments;
|
||||
|
||||
#[test]
|
||||
fn test_overlapping_sections_are_removed() {
|
||||
let elf_data: Vec<u8> = std::fs::read(
|
||||
"tests/data/esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let elf = ElfFile::parse(elf_data.as_slice()).unwrap();
|
||||
let segments = segments(&elf).collect::<Vec<_>>();
|
||||
|
||||
let expected = [
|
||||
// (address, size)
|
||||
(0x3F400020, 256), // .rodata_desc
|
||||
(0x3F400120, 29152), // .rodata
|
||||
(0x3FFB0000, 3716), // .data
|
||||
(0x40080000, 1024), // .vectors
|
||||
(0x40080400, 5088), // .rwtext
|
||||
(0x400D0020, 62654), // .text
|
||||
];
|
||||
|
||||
assert_eq!(segments.len(), expected.len());
|
||||
|
||||
for seg in segments {
|
||||
let addr_and_len = (seg.addr, seg.size());
|
||||
assert!(
|
||||
expected.contains(&addr_and_len),
|
||||
"Unexpected section: {addr_and_len:x?}"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,3 +34,6 @@ And then build the elf file:
|
||||
```
|
||||
cargo build --release
|
||||
```
|
||||
|
||||
`esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections` is the ESP-HAL `gpio_unstable` test built for ESP32.
|
||||
This file is used in a unit test in espflash, and is not flashed as a HIL test.
|
||||
|
||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user