diff --git a/espflash/src/image_format/mod.rs b/espflash/src/image_format/mod.rs index ab4b4fa..a1af7a2 100644 --- a/espflash/src/image_format/mod.rs +++ b/espflash/src/image_format/mod.rs @@ -227,6 +227,7 @@ fn segments<'a>(elf: &'a ElfFile<'a>) -> Box> + && 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> + }), ) } + +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 = 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::>(); + + 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?}" + ) + } + } +} diff --git a/espflash/tests/data/README.md b/espflash/tests/data/README.md index fcbbfbb..9569731 100644 --- a/espflash/tests/data/README.md +++ b/espflash/tests/data/README.md @@ -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. diff --git a/espflash/tests/data/esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections b/espflash/tests/data/esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections new file mode 100644 index 0000000..8189f28 Binary files /dev/null and b/espflash/tests/data/esp_hal_binary_with_overlapping_defmt_and_embedded_test_sections differ