Skip to content

Commit 962e143

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

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
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

+38-28
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.
@@ -468,8 +481,8 @@ impl fmt::Debug for BootInformation {
468481
// usually this is REALLY big (thousands of tags) => skip it here
469482

470483
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())
473486
.unwrap_or(0);
474487

475488
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
@@ -478,8 +491,7 @@ impl fmt::Debug for BootInformation {
478491
debug.field(
479492
"elf_sections_tags",
480493
&self
481-
.elf_sections_tag()
482-
.map(|x| x.sections(self.offset))
494+
.elf_sections()
483495
.unwrap_or_default(),
484496
);
485497
}
@@ -604,7 +616,7 @@ mod tests {
604616
assert_eq!(addr, bi.start_address());
605617
assert_eq!(addr + bytes.0.len(), bi.end_address());
606618
assert_eq!(bytes.0.len(), bi.total_size());
607-
assert!(bi.elf_sections_tag().is_none());
619+
assert!(bi.elf_sections().is_none());
608620
assert!(bi.memory_map_tag().is_none());
609621
assert!(bi.module_tags().next().is_none());
610622
assert!(bi.boot_loader_name_tag().is_none());
@@ -628,7 +640,7 @@ mod tests {
628640
assert_eq!(addr, bi.start_address());
629641
assert_eq!(addr + bytes.0.len(), bi.end_address());
630642
assert_eq!(bytes.0.len(), bi.total_size());
631-
assert!(bi.elf_sections_tag().is_none());
643+
assert!(bi.elf_sections().is_none());
632644
assert!(bi.memory_map_tag().is_none());
633645
assert!(bi.module_tags().next().is_none());
634646
assert!(bi.boot_loader_name_tag().is_none());
@@ -652,7 +664,7 @@ mod tests {
652664
assert_eq!(addr, bi.start_address());
653665
assert_eq!(addr + bytes.0.len(), bi.end_address());
654666
assert_eq!(bytes.0.len(), bi.total_size());
655-
assert!(bi.elf_sections_tag().is_none());
667+
assert!(bi.elf_sections().is_none());
656668
assert!(bi.memory_map_tag().is_none());
657669
assert!(bi.module_tags().next().is_none());
658670
assert!(bi.boot_loader_name_tag().is_none());
@@ -679,7 +691,7 @@ mod tests {
679691
assert_eq!(addr, bi.start_address());
680692
assert_eq!(addr + bytes.0.len(), bi.end_address());
681693
assert_eq!(bytes.0.len(), bi.total_size());
682-
assert!(bi.elf_sections_tag().is_none());
694+
assert!(bi.elf_sections().is_none());
683695
assert!(bi.memory_map_tag().is_none());
684696
assert!(bi.module_tags().next().is_none());
685697
assert_eq!(
@@ -1278,16 +1290,15 @@ mod tests {
12781290
assert_eq!(addr, bi.start_address());
12791291
assert_eq!(addr + bytes.len(), bi.end_address());
12801292
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");
12841295
assert_eq!(".rodata", s1.name().expect("Should be valid utf-8"));
12851296
assert_eq!(0xFFFF_8000_0010_0000, s1.start_address());
12861297
assert_eq!(0xFFFF_8000_0010_3000, s1.end_address());
12871298
assert_eq!(0x0000_0000_0000_3000, s1.size());
12881299
assert_eq!(ElfSectionFlags::ALLOCATED, s1.flags());
12891300
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");
12911302
assert_eq!(".text", s2.name().expect("Should be valid utf-8"));
12921303
assert_eq!(0xFFFF_8000_0010_3000, s2.start_address());
12931304
assert_eq!(0xFFFF_8000_0010_C000, s2.end_address());
@@ -1297,7 +1308,7 @@ mod tests {
12971308
s2.flags()
12981309
);
12991310
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");
13011312
assert_eq!(".data", s3.name().expect("Should be valid utf-8"));
13021313
assert_eq!(0xFFFF_8000_0010_C000, s3.start_address());
13031314
assert_eq!(0xFFFF_8000_0010_E000, s3.end_address());
@@ -1307,7 +1318,7 @@ mod tests {
13071318
s3.flags()
13081319
);
13091320
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");
13111322
assert_eq!(".bss", s4.name().expect("Should be valid utf-8"));
13121323
assert_eq!(0xFFFF_8000_0010_E000, s4.start_address());
13131324
assert_eq!(0xFFFF_8000_0011_3000, s4.end_address());
@@ -1317,7 +1328,7 @@ mod tests {
13171328
s4.flags()
13181329
);
13191330
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");
13211332
assert_eq!(".data.rel.ro", s5.name().expect("Should be valid utf-8"));
13221333
assert_eq!(0xFFFF_8000_0011_3000, s5.start_address());
13231334
assert_eq!(0xFFFF_8000_0011_3000, s5.end_address());
@@ -1327,28 +1338,28 @@ mod tests {
13271338
s5.flags()
13281339
);
13291340
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");
13311342
assert_eq!(".symtab", s6.name().expect("Should be valid utf-8"));
13321343
assert_eq!(0x0000_0000_0011_3000, s6.start_address());
13331344
assert_eq!(0x0000_0000_0011_5BE0, s6.end_address());
13341345
assert_eq!(0x0000_0000_0000_2BE0, s6.size());
13351346
assert_eq!(ElfSectionFlags::empty(), s6.flags());
13361347
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");
13381349
assert_eq!(".strtab", s7.name().expect("Should be valid utf-8"));
13391350
assert_eq!(0x0000_0000_0011_5BE0, s7.start_address());
13401351
assert_eq!(0x0000_0000_0011_9371, s7.end_address());
13411352
assert_eq!(0x0000_0000_0000_3791, s7.size());
13421353
assert_eq!(ElfSectionFlags::empty(), s7.flags());
13431354
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");
13451356
assert_eq!(".shstrtab", s8.name().expect("Should be valid utf-8"));
13461357
assert_eq!(string_addr, s8.start_address());
13471358
assert_eq!(string_addr + string_bytes.len() as u64, s8.end_address());
13481359
assert_eq!(string_bytes.len() as u64, s8.size());
13491360
assert_eq!(ElfSectionFlags::empty(), s8.flags());
13501361
assert_eq!(ElfSectionType::StringTable, s8.section_type());
1351-
assert!(s.next().is_none());
1362+
assert!(es.next().is_none());
13521363
let mut mm = bi.memory_map_tag().unwrap().available_memory_areas();
13531364
let mm1 = mm.next().unwrap();
13541365
assert_eq!(0x00000000, mm1.start_address());
@@ -1463,16 +1474,15 @@ mod tests {
14631474
assert_eq!(addr, bi.start_address());
14641475
assert_eq!(addr + bytes.0.len(), bi.end_address());
14651476
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");
14691479
assert_eq!(".shstrtab", s1.name().expect("Should be valid utf-8"));
14701480
assert_eq!(string_addr, s1.start_address());
14711481
assert_eq!(string_addr + string_bytes.0.len() as u64, s1.end_address());
14721482
assert_eq!(string_bytes.0.len() as u64, s1.size());
14731483
assert_eq!(ElfSectionFlags::empty(), s1.flags());
14741484
assert_eq!(ElfSectionType::StringTable, s1.section_type());
1475-
assert!(s.next().is_none());
1485+
assert!(es.next().is_none());
14761486
}
14771487

14781488
#[test]

0 commit comments

Comments
 (0)