Skip to content

Commit 69630b3

Browse files
committed
multiboot2: Don't require an offset for the ELF sections
1 parent 09de523 commit 69630b3

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

multiboot2/src/elf_sections.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,7 @@ impl ElfSectionsTag {
3838
}
3939

4040
/// Get an iterator of loaded ELF sections.
41-
///
42-
/// # Examples
43-
///
44-
/// ```rust,no_run
45-
/// # let boot_info = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
46-
/// if let Some(elf_tag) = boot_info.elf_sections_tag() {
47-
/// let mut total = 0;
48-
/// for section in elf_tag.sections(0) {
49-
/// println!("Section: {:?}", section);
50-
/// total += 1;
51-
/// }
52-
/// }
53-
/// ```
54-
pub fn sections(&self, offset: usize) -> ElfSectionIter {
41+
pub(crate) fn sections(&self, offset: usize) -> ElfSectionIter {
5542
let string_section_offset = (self.shndx * self.entry_size) as isize;
5643
let string_section_ptr =
5744
unsafe { self.first_section().offset(string_section_offset) as *const _ };

multiboot2/src/lib.rs

+37-32
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,26 @@ impl BootInformation {
249249
self.get_tag::<BasicMemoryInfoTag, _>(TagType::BasicMeminfo)
250250
}
251251

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> {
254267
let tag = self.get_tag::<ElfSectionsTag, _>(TagType::ElfSections);
255-
if let Some(t) = tag {
268+
tag.map(|t| {
256269
assert!((t.entry_size * t.shndx) <= t.size);
257-
}
258-
tag
270+
t.sections(self.offset)
271+
})
259272
}
260273

261274
/// Search for the Memory map tag.
@@ -467,20 +480,14 @@ impl fmt::Debug for BootInformation {
467480
.field("module_tags", &self.module_tags());
468481
// usually this is REALLY big (thousands of tags) => skip it here
469482

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);
474484

475485
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
476486
debug.field("elf_sections_tags (count)", &elf_sections_tag_entries_count);
477487
} else {
478488
debug.field(
479489
"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(),
484491
);
485492
}
486493

@@ -604,7 +611,7 @@ mod tests {
604611
assert_eq!(addr, bi.start_address());
605612
assert_eq!(addr + bytes.0.len(), bi.end_address());
606613
assert_eq!(bytes.0.len(), bi.total_size());
607-
assert!(bi.elf_sections_tag().is_none());
614+
assert!(bi.elf_sections().is_none());
608615
assert!(bi.memory_map_tag().is_none());
609616
assert!(bi.module_tags().next().is_none());
610617
assert!(bi.boot_loader_name_tag().is_none());
@@ -628,7 +635,7 @@ mod tests {
628635
assert_eq!(addr, bi.start_address());
629636
assert_eq!(addr + bytes.0.len(), bi.end_address());
630637
assert_eq!(bytes.0.len(), bi.total_size());
631-
assert!(bi.elf_sections_tag().is_none());
638+
assert!(bi.elf_sections().is_none());
632639
assert!(bi.memory_map_tag().is_none());
633640
assert!(bi.module_tags().next().is_none());
634641
assert!(bi.boot_loader_name_tag().is_none());
@@ -652,7 +659,7 @@ mod tests {
652659
assert_eq!(addr, bi.start_address());
653660
assert_eq!(addr + bytes.0.len(), bi.end_address());
654661
assert_eq!(bytes.0.len(), bi.total_size());
655-
assert!(bi.elf_sections_tag().is_none());
662+
assert!(bi.elf_sections().is_none());
656663
assert!(bi.memory_map_tag().is_none());
657664
assert!(bi.module_tags().next().is_none());
658665
assert!(bi.boot_loader_name_tag().is_none());
@@ -679,7 +686,7 @@ mod tests {
679686
assert_eq!(addr, bi.start_address());
680687
assert_eq!(addr + bytes.0.len(), bi.end_address());
681688
assert_eq!(bytes.0.len(), bi.total_size());
682-
assert!(bi.elf_sections_tag().is_none());
689+
assert!(bi.elf_sections().is_none());
683690
assert!(bi.memory_map_tag().is_none());
684691
assert!(bi.module_tags().next().is_none());
685692
assert_eq!(
@@ -1278,16 +1285,15 @@ mod tests {
12781285
assert_eq!(addr, bi.start_address());
12791286
assert_eq!(addr + bytes.len(), bi.end_address());
12801287
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");
12841290
assert_eq!(".rodata", s1.name().expect("Should be valid utf-8"));
12851291
assert_eq!(0xFFFF_8000_0010_0000, s1.start_address());
12861292
assert_eq!(0xFFFF_8000_0010_3000, s1.end_address());
12871293
assert_eq!(0x0000_0000_0000_3000, s1.size());
12881294
assert_eq!(ElfSectionFlags::ALLOCATED, s1.flags());
12891295
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");
12911297
assert_eq!(".text", s2.name().expect("Should be valid utf-8"));
12921298
assert_eq!(0xFFFF_8000_0010_3000, s2.start_address());
12931299
assert_eq!(0xFFFF_8000_0010_C000, s2.end_address());
@@ -1297,7 +1303,7 @@ mod tests {
12971303
s2.flags()
12981304
);
12991305
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");
13011307
assert_eq!(".data", s3.name().expect("Should be valid utf-8"));
13021308
assert_eq!(0xFFFF_8000_0010_C000, s3.start_address());
13031309
assert_eq!(0xFFFF_8000_0010_E000, s3.end_address());
@@ -1307,7 +1313,7 @@ mod tests {
13071313
s3.flags()
13081314
);
13091315
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");
13111317
assert_eq!(".bss", s4.name().expect("Should be valid utf-8"));
13121318
assert_eq!(0xFFFF_8000_0010_E000, s4.start_address());
13131319
assert_eq!(0xFFFF_8000_0011_3000, s4.end_address());
@@ -1317,7 +1323,7 @@ mod tests {
13171323
s4.flags()
13181324
);
13191325
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");
13211327
assert_eq!(".data.rel.ro", s5.name().expect("Should be valid utf-8"));
13221328
assert_eq!(0xFFFF_8000_0011_3000, s5.start_address());
13231329
assert_eq!(0xFFFF_8000_0011_3000, s5.end_address());
@@ -1327,28 +1333,28 @@ mod tests {
13271333
s5.flags()
13281334
);
13291335
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");
13311337
assert_eq!(".symtab", s6.name().expect("Should be valid utf-8"));
13321338
assert_eq!(0x0000_0000_0011_3000, s6.start_address());
13331339
assert_eq!(0x0000_0000_0011_5BE0, s6.end_address());
13341340
assert_eq!(0x0000_0000_0000_2BE0, s6.size());
13351341
assert_eq!(ElfSectionFlags::empty(), s6.flags());
13361342
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");
13381344
assert_eq!(".strtab", s7.name().expect("Should be valid utf-8"));
13391345
assert_eq!(0x0000_0000_0011_5BE0, s7.start_address());
13401346
assert_eq!(0x0000_0000_0011_9371, s7.end_address());
13411347
assert_eq!(0x0000_0000_0000_3791, s7.size());
13421348
assert_eq!(ElfSectionFlags::empty(), s7.flags());
13431349
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");
13451351
assert_eq!(".shstrtab", s8.name().expect("Should be valid utf-8"));
13461352
assert_eq!(string_addr, s8.start_address());
13471353
assert_eq!(string_addr + string_bytes.len() as u64, s8.end_address());
13481354
assert_eq!(string_bytes.len() as u64, s8.size());
13491355
assert_eq!(ElfSectionFlags::empty(), s8.flags());
13501356
assert_eq!(ElfSectionType::StringTable, s8.section_type());
1351-
assert!(s.next().is_none());
1357+
assert!(es.next().is_none());
13521358
let mut mm = bi.memory_map_tag().unwrap().available_memory_areas();
13531359
let mm1 = mm.next().unwrap();
13541360
assert_eq!(0x00000000, mm1.start_address());
@@ -1463,16 +1469,15 @@ mod tests {
14631469
assert_eq!(addr, bi.start_address());
14641470
assert_eq!(addr + bytes.0.len(), bi.end_address());
14651471
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");
14691474
assert_eq!(".shstrtab", s1.name().expect("Should be valid utf-8"));
14701475
assert_eq!(string_addr, s1.start_address());
14711476
assert_eq!(string_addr + string_bytes.0.len() as u64, s1.end_address());
14721477
assert_eq!(string_bytes.0.len() as u64, s1.size());
14731478
assert_eq!(ElfSectionFlags::empty(), s1.flags());
14741479
assert_eq!(ElfSectionType::StringTable, s1.section_type());
1475-
assert!(s.next().is_none());
1480+
assert!(es.next().is_none());
14761481
}
14771482

14781483
#[test]

0 commit comments

Comments
 (0)