Don't trip up on non-code sections (#896)

* Don't trip up on non-code sections

* Add test
This commit is contained in:
Dániel Buga 2025-06-20 11:12:35 +02:00 committed by GitHub
parent 8e7f120ba8
commit 1e25b4aed8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 0 deletions

View File

@ -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?}"
)
}
}
}

View File

@ -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.