@@ -249,13 +249,26 @@ impl BootInformation {
249
249
self . get_tag :: < BasicMemoryInfoTag , _ > ( TagType :: BasicMeminfo )
250
250
}
251
251
252
- /// Search for the ELF Sections tag.
253
- pub fn elf_sections_tag ( & self ) -> Option < & ElfSectionsTag > {
252
+ /// Search for the ELF Sections.
253
+ ///
254
+ /// # Examples
255
+ ///
256
+ /// ```rust,no_run
257
+ /// # let boot_info = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
258
+ /// if let Some(sections) = boot_info.elf_sections() {
259
+ /// let mut total = 0;
260
+ /// for section in sections {
261
+ /// println!("Section: {:?}", section);
262
+ /// total += 1;
263
+ /// }
264
+ /// }
265
+ /// ```
266
+ pub fn elf_sections ( & self ) -> Option < ElfSectionIter > {
254
267
let tag = self . get_tag :: < ElfSectionsTag , _ > ( TagType :: ElfSections ) ;
255
- if let Some ( t ) = tag {
268
+ tag . map ( |t| {
256
269
assert ! ( ( t. entry_size * t. shndx) <= t. size) ;
257
- }
258
- tag
270
+ t . sections ( self . offset )
271
+ } )
259
272
}
260
273
261
274
/// Search for the Memory map tag.
@@ -467,20 +480,14 @@ impl fmt::Debug for BootInformation {
467
480
. field ( "module_tags" , & self . module_tags ( ) ) ;
468
481
// usually this is REALLY big (thousands of tags) => skip it here
469
482
470
- let elf_sections_tag_entries_count = self
471
- . elf_sections_tag ( )
472
- . map ( |x| x. sections ( self . offset ) . count ( ) )
473
- . unwrap_or ( 0 ) ;
483
+ let elf_sections_tag_entries_count = self . elf_sections ( ) . map ( |x| x. count ( ) ) . unwrap_or ( 0 ) ;
474
484
475
485
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
476
486
debug. field ( "elf_sections_tags (count)" , & elf_sections_tag_entries_count) ;
477
487
} else {
478
488
debug. field (
479
489
"elf_sections_tags" ,
480
- & self
481
- . elf_sections_tag ( )
482
- . map ( |x| x. sections ( self . offset ) )
483
- . unwrap_or_default ( ) ,
490
+ & self . elf_sections ( ) . unwrap_or_default ( ) ,
484
491
) ;
485
492
}
486
493
@@ -604,7 +611,7 @@ mod tests {
604
611
assert_eq ! ( addr, bi. start_address( ) ) ;
605
612
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
606
613
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
607
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
614
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
608
615
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
609
616
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
610
617
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -628,7 +635,7 @@ mod tests {
628
635
assert_eq ! ( addr, bi. start_address( ) ) ;
629
636
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
630
637
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
631
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
638
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
632
639
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
633
640
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
634
641
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -652,7 +659,7 @@ mod tests {
652
659
assert_eq ! ( addr, bi. start_address( ) ) ;
653
660
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
654
661
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
655
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
662
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
656
663
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
657
664
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
658
665
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -679,7 +686,7 @@ mod tests {
679
686
assert_eq ! ( addr, bi. start_address( ) ) ;
680
687
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
681
688
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
682
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
689
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
683
690
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
684
691
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
685
692
assert_eq ! (
@@ -1278,16 +1285,15 @@ mod tests {
1278
1285
assert_eq ! ( addr, bi. start_address( ) ) ;
1279
1286
assert_eq ! ( addr + bytes. len( ) , bi. end_address( ) ) ;
1280
1287
assert_eq ! ( bytes. len( ) , bi. total_size( ) ) ;
1281
- let es = bi. elf_sections_tag ( ) . unwrap ( ) ;
1282
- let mut s = es. sections ( bi. offset ) ;
1283
- let s1 = s. next ( ) . expect ( "Should have one more section" ) ;
1288
+ let mut es = bi. elf_sections ( ) . unwrap ( ) ;
1289
+ let s1 = es. next ( ) . expect ( "Should have one more section" ) ;
1284
1290
assert_eq ! ( ".rodata" , s1. name( ) . expect( "Should be valid utf-8" ) ) ;
1285
1291
assert_eq ! ( 0xFFFF_8000_0010_0000 , s1. start_address( ) ) ;
1286
1292
assert_eq ! ( 0xFFFF_8000_0010_3000 , s1. end_address( ) ) ;
1287
1293
assert_eq ! ( 0x0000_0000_0000_3000 , s1. size( ) ) ;
1288
1294
assert_eq ! ( ElfSectionFlags :: ALLOCATED , s1. flags( ) ) ;
1289
1295
assert_eq ! ( ElfSectionType :: ProgramSection , s1. section_type( ) ) ;
1290
- let s2 = s . next ( ) . expect ( "Should have one more section" ) ;
1296
+ let s2 = es . next ( ) . expect ( "Should have one more section" ) ;
1291
1297
assert_eq ! ( ".text" , s2. name( ) . expect( "Should be valid utf-8" ) ) ;
1292
1298
assert_eq ! ( 0xFFFF_8000_0010_3000 , s2. start_address( ) ) ;
1293
1299
assert_eq ! ( 0xFFFF_8000_0010_C000 , s2. end_address( ) ) ;
@@ -1297,7 +1303,7 @@ mod tests {
1297
1303
s2. flags( )
1298
1304
) ;
1299
1305
assert_eq ! ( ElfSectionType :: ProgramSection , s2. section_type( ) ) ;
1300
- let s3 = s . next ( ) . expect ( "Should have one more section" ) ;
1306
+ let s3 = es . next ( ) . expect ( "Should have one more section" ) ;
1301
1307
assert_eq ! ( ".data" , s3. name( ) . expect( "Should be valid utf-8" ) ) ;
1302
1308
assert_eq ! ( 0xFFFF_8000_0010_C000 , s3. start_address( ) ) ;
1303
1309
assert_eq ! ( 0xFFFF_8000_0010_E000 , s3. end_address( ) ) ;
@@ -1307,7 +1313,7 @@ mod tests {
1307
1313
s3. flags( )
1308
1314
) ;
1309
1315
assert_eq ! ( ElfSectionType :: ProgramSection , s3. section_type( ) ) ;
1310
- let s4 = s . next ( ) . expect ( "Should have one more section" ) ;
1316
+ let s4 = es . next ( ) . expect ( "Should have one more section" ) ;
1311
1317
assert_eq ! ( ".bss" , s4. name( ) . expect( "Should be valid utf-8" ) ) ;
1312
1318
assert_eq ! ( 0xFFFF_8000_0010_E000 , s4. start_address( ) ) ;
1313
1319
assert_eq ! ( 0xFFFF_8000_0011_3000 , s4. end_address( ) ) ;
@@ -1317,7 +1323,7 @@ mod tests {
1317
1323
s4. flags( )
1318
1324
) ;
1319
1325
assert_eq ! ( ElfSectionType :: Uninitialized , s4. section_type( ) ) ;
1320
- let s5 = s . next ( ) . expect ( "Should have one more section" ) ;
1326
+ let s5 = es . next ( ) . expect ( "Should have one more section" ) ;
1321
1327
assert_eq ! ( ".data.rel.ro" , s5. name( ) . expect( "Should be valid utf-8" ) ) ;
1322
1328
assert_eq ! ( 0xFFFF_8000_0011_3000 , s5. start_address( ) ) ;
1323
1329
assert_eq ! ( 0xFFFF_8000_0011_3000 , s5. end_address( ) ) ;
@@ -1327,28 +1333,28 @@ mod tests {
1327
1333
s5. flags( )
1328
1334
) ;
1329
1335
assert_eq ! ( ElfSectionType :: ProgramSection , s5. section_type( ) ) ;
1330
- let s6 = s . next ( ) . expect ( "Should have one more section" ) ;
1336
+ let s6 = es . next ( ) . expect ( "Should have one more section" ) ;
1331
1337
assert_eq ! ( ".symtab" , s6. name( ) . expect( "Should be valid utf-8" ) ) ;
1332
1338
assert_eq ! ( 0x0000_0000_0011_3000 , s6. start_address( ) ) ;
1333
1339
assert_eq ! ( 0x0000_0000_0011_5BE0 , s6. end_address( ) ) ;
1334
1340
assert_eq ! ( 0x0000_0000_0000_2BE0 , s6. size( ) ) ;
1335
1341
assert_eq ! ( ElfSectionFlags :: empty( ) , s6. flags( ) ) ;
1336
1342
assert_eq ! ( ElfSectionType :: LinkerSymbolTable , s6. section_type( ) ) ;
1337
- let s7 = s . next ( ) . expect ( "Should have one more section" ) ;
1343
+ let s7 = es . next ( ) . expect ( "Should have one more section" ) ;
1338
1344
assert_eq ! ( ".strtab" , s7. name( ) . expect( "Should be valid utf-8" ) ) ;
1339
1345
assert_eq ! ( 0x0000_0000_0011_5BE0 , s7. start_address( ) ) ;
1340
1346
assert_eq ! ( 0x0000_0000_0011_9371 , s7. end_address( ) ) ;
1341
1347
assert_eq ! ( 0x0000_0000_0000_3791 , s7. size( ) ) ;
1342
1348
assert_eq ! ( ElfSectionFlags :: empty( ) , s7. flags( ) ) ;
1343
1349
assert_eq ! ( ElfSectionType :: StringTable , s7. section_type( ) ) ;
1344
- let s8 = s . next ( ) . expect ( "Should have one more section" ) ;
1350
+ let s8 = es . next ( ) . expect ( "Should have one more section" ) ;
1345
1351
assert_eq ! ( ".shstrtab" , s8. name( ) . expect( "Should be valid utf-8" ) ) ;
1346
1352
assert_eq ! ( string_addr, s8. start_address( ) ) ;
1347
1353
assert_eq ! ( string_addr + string_bytes. len( ) as u64 , s8. end_address( ) ) ;
1348
1354
assert_eq ! ( string_bytes. len( ) as u64 , s8. size( ) ) ;
1349
1355
assert_eq ! ( ElfSectionFlags :: empty( ) , s8. flags( ) ) ;
1350
1356
assert_eq ! ( ElfSectionType :: StringTable , s8. section_type( ) ) ;
1351
- assert ! ( s . next( ) . is_none( ) ) ;
1357
+ assert ! ( es . next( ) . is_none( ) ) ;
1352
1358
let mut mm = bi. memory_map_tag ( ) . unwrap ( ) . available_memory_areas ( ) ;
1353
1359
let mm1 = mm. next ( ) . unwrap ( ) ;
1354
1360
assert_eq ! ( 0x00000000 , mm1. start_address( ) ) ;
@@ -1463,16 +1469,15 @@ mod tests {
1463
1469
assert_eq ! ( addr, bi. start_address( ) ) ;
1464
1470
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
1465
1471
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
1466
- let es = bi. elf_sections_tag ( ) . unwrap ( ) ;
1467
- let mut s = es. sections ( bi. offset ) ;
1468
- let s1 = s. next ( ) . expect ( "Should have one more section" ) ;
1472
+ let mut es = bi. elf_sections ( ) . unwrap ( ) ;
1473
+ let s1 = es. next ( ) . expect ( "Should have one more section" ) ;
1469
1474
assert_eq ! ( ".shstrtab" , s1. name( ) . expect( "Should be valid utf-8" ) ) ;
1470
1475
assert_eq ! ( string_addr, s1. start_address( ) ) ;
1471
1476
assert_eq ! ( string_addr + string_bytes. 0 . len( ) as u64 , s1. end_address( ) ) ;
1472
1477
assert_eq ! ( string_bytes. 0 . len( ) as u64 , s1. size( ) ) ;
1473
1478
assert_eq ! ( ElfSectionFlags :: empty( ) , s1. flags( ) ) ;
1474
1479
assert_eq ! ( ElfSectionType :: StringTable , s1. section_type( ) ) ;
1475
- assert ! ( s . next( ) . is_none( ) ) ;
1480
+ assert ! ( es . next( ) . is_none( ) ) ;
1476
1481
}
1477
1482
1478
1483
#[ test]
0 commit comments