@@ -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.
@@ -468,8 +481,8 @@ impl fmt::Debug for BootInformation {
468
481
// usually this is REALLY big (thousands of tags) => skip it here
469
482
470
483
let elf_sections_tag_entries_count = self
471
- . elf_sections_tag ( )
472
- . map ( |x| x. sections ( self . offset ) . count ( ) )
484
+ . elf_sections ( )
485
+ . map ( |x| x. count ( ) )
473
486
. unwrap_or ( 0 ) ;
474
487
475
488
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
@@ -478,8 +491,7 @@ impl fmt::Debug for BootInformation {
478
491
debug. field (
479
492
"elf_sections_tags" ,
480
493
& self
481
- . elf_sections_tag ( )
482
- . map ( |x| x. sections ( self . offset ) )
494
+ . elf_sections ( )
483
495
. unwrap_or_default ( ) ,
484
496
) ;
485
497
}
@@ -604,7 +616,7 @@ mod tests {
604
616
assert_eq ! ( addr, bi. start_address( ) ) ;
605
617
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
606
618
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
607
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
619
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
608
620
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
609
621
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
610
622
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -628,7 +640,7 @@ mod tests {
628
640
assert_eq ! ( addr, bi. start_address( ) ) ;
629
641
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
630
642
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
631
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
643
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
632
644
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
633
645
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
634
646
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -652,7 +664,7 @@ mod tests {
652
664
assert_eq ! ( addr, bi. start_address( ) ) ;
653
665
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
654
666
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
655
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
667
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
656
668
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
657
669
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
658
670
assert ! ( bi. boot_loader_name_tag( ) . is_none( ) ) ;
@@ -679,7 +691,7 @@ mod tests {
679
691
assert_eq ! ( addr, bi. start_address( ) ) ;
680
692
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
681
693
assert_eq ! ( bytes. 0 . len( ) , bi. total_size( ) ) ;
682
- assert ! ( bi. elf_sections_tag ( ) . is_none( ) ) ;
694
+ assert ! ( bi. elf_sections ( ) . is_none( ) ) ;
683
695
assert ! ( bi. memory_map_tag( ) . is_none( ) ) ;
684
696
assert ! ( bi. module_tags( ) . next( ) . is_none( ) ) ;
685
697
assert_eq ! (
@@ -1278,16 +1290,15 @@ mod tests {
1278
1290
assert_eq ! ( addr, bi. start_address( ) ) ;
1279
1291
assert_eq ! ( addr + bytes. len( ) , bi. end_address( ) ) ;
1280
1292
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" ) ;
1293
+ let mut es = bi. elf_sections ( ) . unwrap ( ) ;
1294
+ let s1 = es. next ( ) . expect ( "Should have one more section" ) ;
1284
1295
assert_eq ! ( ".rodata" , s1. name( ) . expect( "Should be valid utf-8" ) ) ;
1285
1296
assert_eq ! ( 0xFFFF_8000_0010_0000 , s1. start_address( ) ) ;
1286
1297
assert_eq ! ( 0xFFFF_8000_0010_3000 , s1. end_address( ) ) ;
1287
1298
assert_eq ! ( 0x0000_0000_0000_3000 , s1. size( ) ) ;
1288
1299
assert_eq ! ( ElfSectionFlags :: ALLOCATED , s1. flags( ) ) ;
1289
1300
assert_eq ! ( ElfSectionType :: ProgramSection , s1. section_type( ) ) ;
1290
- let s2 = s . next ( ) . expect ( "Should have one more section" ) ;
1301
+ let s2 = es . next ( ) . expect ( "Should have one more section" ) ;
1291
1302
assert_eq ! ( ".text" , s2. name( ) . expect( "Should be valid utf-8" ) ) ;
1292
1303
assert_eq ! ( 0xFFFF_8000_0010_3000 , s2. start_address( ) ) ;
1293
1304
assert_eq ! ( 0xFFFF_8000_0010_C000 , s2. end_address( ) ) ;
@@ -1297,7 +1308,7 @@ mod tests {
1297
1308
s2. flags( )
1298
1309
) ;
1299
1310
assert_eq ! ( ElfSectionType :: ProgramSection , s2. section_type( ) ) ;
1300
- let s3 = s . next ( ) . expect ( "Should have one more section" ) ;
1311
+ let s3 = es . next ( ) . expect ( "Should have one more section" ) ;
1301
1312
assert_eq ! ( ".data" , s3. name( ) . expect( "Should be valid utf-8" ) ) ;
1302
1313
assert_eq ! ( 0xFFFF_8000_0010_C000 , s3. start_address( ) ) ;
1303
1314
assert_eq ! ( 0xFFFF_8000_0010_E000 , s3. end_address( ) ) ;
@@ -1307,7 +1318,7 @@ mod tests {
1307
1318
s3. flags( )
1308
1319
) ;
1309
1320
assert_eq ! ( ElfSectionType :: ProgramSection , s3. section_type( ) ) ;
1310
- let s4 = s . next ( ) . expect ( "Should have one more section" ) ;
1321
+ let s4 = es . next ( ) . expect ( "Should have one more section" ) ;
1311
1322
assert_eq ! ( ".bss" , s4. name( ) . expect( "Should be valid utf-8" ) ) ;
1312
1323
assert_eq ! ( 0xFFFF_8000_0010_E000 , s4. start_address( ) ) ;
1313
1324
assert_eq ! ( 0xFFFF_8000_0011_3000 , s4. end_address( ) ) ;
@@ -1317,7 +1328,7 @@ mod tests {
1317
1328
s4. flags( )
1318
1329
) ;
1319
1330
assert_eq ! ( ElfSectionType :: Uninitialized , s4. section_type( ) ) ;
1320
- let s5 = s . next ( ) . expect ( "Should have one more section" ) ;
1331
+ let s5 = es . next ( ) . expect ( "Should have one more section" ) ;
1321
1332
assert_eq ! ( ".data.rel.ro" , s5. name( ) . expect( "Should be valid utf-8" ) ) ;
1322
1333
assert_eq ! ( 0xFFFF_8000_0011_3000 , s5. start_address( ) ) ;
1323
1334
assert_eq ! ( 0xFFFF_8000_0011_3000 , s5. end_address( ) ) ;
@@ -1327,28 +1338,28 @@ mod tests {
1327
1338
s5. flags( )
1328
1339
) ;
1329
1340
assert_eq ! ( ElfSectionType :: ProgramSection , s5. section_type( ) ) ;
1330
- let s6 = s . next ( ) . expect ( "Should have one more section" ) ;
1341
+ let s6 = es . next ( ) . expect ( "Should have one more section" ) ;
1331
1342
assert_eq ! ( ".symtab" , s6. name( ) . expect( "Should be valid utf-8" ) ) ;
1332
1343
assert_eq ! ( 0x0000_0000_0011_3000 , s6. start_address( ) ) ;
1333
1344
assert_eq ! ( 0x0000_0000_0011_5BE0 , s6. end_address( ) ) ;
1334
1345
assert_eq ! ( 0x0000_0000_0000_2BE0 , s6. size( ) ) ;
1335
1346
assert_eq ! ( ElfSectionFlags :: empty( ) , s6. flags( ) ) ;
1336
1347
assert_eq ! ( ElfSectionType :: LinkerSymbolTable , s6. section_type( ) ) ;
1337
- let s7 = s . next ( ) . expect ( "Should have one more section" ) ;
1348
+ let s7 = es . next ( ) . expect ( "Should have one more section" ) ;
1338
1349
assert_eq ! ( ".strtab" , s7. name( ) . expect( "Should be valid utf-8" ) ) ;
1339
1350
assert_eq ! ( 0x0000_0000_0011_5BE0 , s7. start_address( ) ) ;
1340
1351
assert_eq ! ( 0x0000_0000_0011_9371 , s7. end_address( ) ) ;
1341
1352
assert_eq ! ( 0x0000_0000_0000_3791 , s7. size( ) ) ;
1342
1353
assert_eq ! ( ElfSectionFlags :: empty( ) , s7. flags( ) ) ;
1343
1354
assert_eq ! ( ElfSectionType :: StringTable , s7. section_type( ) ) ;
1344
- let s8 = s . next ( ) . expect ( "Should have one more section" ) ;
1355
+ let s8 = es . next ( ) . expect ( "Should have one more section" ) ;
1345
1356
assert_eq ! ( ".shstrtab" , s8. name( ) . expect( "Should be valid utf-8" ) ) ;
1346
1357
assert_eq ! ( string_addr, s8. start_address( ) ) ;
1347
1358
assert_eq ! ( string_addr + string_bytes. len( ) as u64 , s8. end_address( ) ) ;
1348
1359
assert_eq ! ( string_bytes. len( ) as u64 , s8. size( ) ) ;
1349
1360
assert_eq ! ( ElfSectionFlags :: empty( ) , s8. flags( ) ) ;
1350
1361
assert_eq ! ( ElfSectionType :: StringTable , s8. section_type( ) ) ;
1351
- assert ! ( s . next( ) . is_none( ) ) ;
1362
+ assert ! ( es . next( ) . is_none( ) ) ;
1352
1363
let mut mm = bi. memory_map_tag ( ) . unwrap ( ) . available_memory_areas ( ) ;
1353
1364
let mm1 = mm. next ( ) . unwrap ( ) ;
1354
1365
assert_eq ! ( 0x00000000 , mm1. start_address( ) ) ;
@@ -1463,16 +1474,15 @@ mod tests {
1463
1474
assert_eq ! ( addr, bi. start_address( ) ) ;
1464
1475
assert_eq ! ( addr + bytes. 0 . len( ) , bi. end_address( ) ) ;
1465
1476
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" ) ;
1477
+ let mut es = bi. elf_sections ( ) . unwrap ( ) ;
1478
+ let s1 = es. next ( ) . expect ( "Should have one more section" ) ;
1469
1479
assert_eq ! ( ".shstrtab" , s1. name( ) . expect( "Should be valid utf-8" ) ) ;
1470
1480
assert_eq ! ( string_addr, s1. start_address( ) ) ;
1471
1481
assert_eq ! ( string_addr + string_bytes. 0 . len( ) as u64 , s1. end_address( ) ) ;
1472
1482
assert_eq ! ( string_bytes. 0 . len( ) as u64 , s1. size( ) ) ;
1473
1483
assert_eq ! ( ElfSectionFlags :: empty( ) , s1. flags( ) ) ;
1474
1484
assert_eq ! ( ElfSectionType :: StringTable , s1. section_type( ) ) ;
1475
- assert ! ( s . next( ) . is_none( ) ) ;
1485
+ assert ! ( es . next( ) . is_none( ) ) ;
1476
1486
}
1477
1487
1478
1488
#[ test]
0 commit comments