@@ -227,10 +227,57 @@ fn segments<'a>(elf: &'a ElfFile<'a>) -> Box<dyn Iterator<Item = Segment<'a>> +
227
227
&& header. sh_type ( Endianness :: Little ) == SHT_PROGBITS
228
228
&& header. sh_offset . get ( Endianness :: Little ) > 0
229
229
&& section. address ( ) > 0
230
+ && !is_empty ( section. flags ( ) )
230
231
} )
231
232
. flat_map ( move |section| match section. data ( ) {
232
233
Ok ( data) => Some ( Segment :: new ( section. address ( ) as u32 , data) ) ,
233
234
_ => None ,
234
235
} ) ,
235
236
)
236
237
}
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
+ }
0 commit comments