Skip to content

Commit 1e25b4a

Browse files
authored
Don't trip up on non-code sections (#896)
* Don't trip up on non-code sections * Add test
1 parent 8e7f120 commit 1e25b4a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

espflash/src/image_format/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,57 @@ fn segments<'a>(elf: &'a ElfFile<'a>) -> Box<dyn Iterator<Item = Segment<'a>> +
227227
&& header.sh_type(Endianness::Little) == SHT_PROGBITS
228228
&& header.sh_offset.get(Endianness::Little) > 0
229229
&& section.address() > 0
230+
&& !is_empty(section.flags())
230231
})
231232
.flat_map(move |section| match section.data() {
232233
Ok(data) => Some(Segment::new(section.address() as u32, data)),
233234
_ => None,
234235
}),
235236
)
236237
}
238+
239+
fn is_empty(flags: object::SectionFlags) -> bool {
240+
match flags {
241+
object::SectionFlags::None => true,
242+
object::SectionFlags::Elf { sh_flags } => sh_flags == 0,
243+
_ => unreachable!(),
244+
}
245+
}
246+
247+
#[cfg(test)]
248+
mod test {
249+
use object::read::elf::ElfFile;
250+
251+
use super::segments;
252+
253+
#[test]
254+
fn test_overlapping_sections_are_removed() {
255+
let elf_data: Vec<u8> = std::fs::read(
256+
"tests/data/esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections",
257+
)
258+
.unwrap();
259+
260+
let elf = ElfFile::parse(elf_data.as_slice()).unwrap();
261+
let segments = segments(&elf).collect::<Vec<_>>();
262+
263+
let expected = [
264+
// (address, size)
265+
(0x3F400020, 256), // .rodata_desc
266+
(0x3F400120, 29152), // .rodata
267+
(0x3FFB0000, 3716), // .data
268+
(0x40080000, 1024), // .vectors
269+
(0x40080400, 5088), // .rwtext
270+
(0x400D0020, 62654), // .text
271+
];
272+
273+
assert_eq!(segments.len(), expected.len());
274+
275+
for seg in segments {
276+
let addr_and_len = (seg.addr, seg.size());
277+
assert!(
278+
expected.contains(&addr_and_len),
279+
"Unexpected section: {addr_and_len:x?}"
280+
)
281+
}
282+
}
283+
}

espflash/tests/data/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ And then build the elf file:
3434
```
3535
cargo build --release
3636
```
37+
38+
`esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections` is the ESP-HAL `gpio_unstable` test built for ESP32.
39+
This file is used in a unit test in espflash, and is not flashed as a HIL test.

0 commit comments

Comments
 (0)