Skip to content

Commit a537d24

Browse files
committed
multiboot2: streamline API of ElfSectionsTag
1 parent 09acee9 commit a537d24

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

integration-test/bins/multiboot2_payload/src/verify/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ pub(self) fn print_memory_map(mbi: &BootInformation) -> anyhow::Result<()> {
4848

4949
pub(self) fn print_elf_info(mbi: &BootInformation) -> anyhow::Result<()> {
5050
let sections_iter = mbi
51-
.elf_sections()
51+
.elf_sections_tag()
5252
.ok_or("Should have elf sections")
53+
.map(|tag| tag.sections())
5354
.map_err(anyhow::Error::msg)?;
5455
println!("ELF sections:");
5556
for s in sections_iter {

multiboot2/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
## v0.23.1 (2024-10-21)
66

77
- Fix wrong tag ID when using `BootdevTag::new`
8+
- `BootInformation::elf_sections` is now deprecated and replaced by the newly
9+
- added `BootInformation::elf_sections_tag`. On the returned type, you can call
10+
`.sections()` to iterate the sections
811

912
## v0.23.0 (2024-09-17)
1013

multiboot2/src/boot_information.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'a> BootInformation<'a> {
254254
/// # use multiboot2::{BootInformation, BootInformationHeader};
255255
/// # let ptr = 0xdeadbeef as *const BootInformationHeader;
256256
/// # let boot_info = unsafe { BootInformation::load(ptr).unwrap() };
257-
/// if let Some(sections) = boot_info.elf_sections() {
257+
/// if let Some(sections) = boot_info.elf_sections_tag().map(|tag| tag.sections()) {
258258
/// let mut total = 0;
259259
/// for section in sections {
260260
/// println!("Section: {:?}", section);
@@ -263,14 +263,21 @@ impl<'a> BootInformation<'a> {
263263
/// }
264264
/// ```
265265
#[must_use]
266+
#[deprecated = "Use elf_sections_tag() instead and corresponding getters"]
266267
pub fn elf_sections(&self) -> Option<ElfSectionIter> {
267268
let tag = self.get_tag::<ElfSectionsTag>();
268269
tag.map(|t| {
269270
assert!((t.entry_size() * t.shndx()) <= t.header().size);
270-
t.sections_iter()
271+
t.sections()
271272
})
272273
}
273274

275+
/// Search for the [`ElfSectionsTag`].
276+
#[must_use]
277+
pub fn elf_sections_tag(&self) -> Option<&ElfSectionsTag> {
278+
self.get_tag()
279+
}
280+
274281
/// Search for the [`FramebufferTag`]. The result is `Some(Err(e))`, if the
275282
/// framebuffer type is unknown, while the framebuffer tag is present.
276283
#[must_use]

multiboot2/src/elf_sections.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ impl ElfSectionsTag {
3737
)
3838
}
3939

40-
/// Get an iterator of loaded ELF sections.
40+
/// Get an iterator over the ELF sections.
4141
#[must_use]
42-
pub(crate) const fn sections_iter(&self) -> ElfSectionIter {
42+
pub const fn sections(&self) -> ElfSectionIter {
4343
let string_section_offset = (self.shndx * self.entry_size) as isize;
4444
let string_section_ptr =
4545
unsafe { self.sections.as_ptr().offset(string_section_offset) as *const _ };
@@ -96,12 +96,12 @@ impl Debug for ElfSectionsTag {
9696
.field("number_of_sections", &self.number_of_sections)
9797
.field("entry_size", &self.entry_size)
9898
.field("shndx", &self.shndx)
99-
.field("sections", &self.sections_iter())
99+
.field("sections", &self.sections())
100100
.finish()
101101
}
102102
}
103103

104-
/// An iterator over some ELF sections.
104+
/// An iterator over [`ElfSection`]s.
105105
#[derive(Clone)]
106106
pub struct ElfSectionIter<'a> {
107107
current_section: *const u8,

multiboot2/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ mod tests {
168168
assert_eq!(addr, bi.start_address());
169169
assert_eq!(addr + bytes.0.len(), bi.end_address());
170170
assert_eq!(bytes.0.len(), bi.total_size());
171-
assert!(bi.elf_sections().is_none());
171+
assert!(bi.elf_sections_tag().is_none());
172172
assert!(bi.memory_map_tag().is_none());
173173
assert!(bi.module_tags().next().is_none());
174174
assert!(bi.boot_loader_name_tag().is_none());
@@ -191,7 +191,7 @@ mod tests {
191191
assert_eq!(addr, bi.start_address());
192192
assert_eq!(addr + bytes.0.len(), bi.end_address());
193193
assert_eq!(bytes.0.len(), bi.total_size());
194-
assert!(bi.elf_sections().is_none());
194+
assert!(bi.elf_sections_tag().is_none());
195195
assert!(bi.memory_map_tag().is_none());
196196
assert!(bi.module_tags().next().is_none());
197197
assert!(bi.boot_loader_name_tag().is_none());
@@ -214,7 +214,7 @@ mod tests {
214214
assert_eq!(addr, bi.start_address());
215215
assert_eq!(addr + bytes.0.len(), bi.end_address());
216216
assert_eq!(bytes.0.len(), bi.total_size());
217-
assert!(bi.elf_sections().is_none());
217+
assert!(bi.elf_sections_tag().is_none());
218218
assert!(bi.memory_map_tag().is_none());
219219
assert!(bi.module_tags().next().is_none());
220220
assert!(bi.boot_loader_name_tag().is_none());
@@ -240,7 +240,7 @@ mod tests {
240240
assert_eq!(addr, bi.start_address());
241241
assert_eq!(addr + bytes.0.len(), bi.end_address());
242242
assert_eq!(bytes.0.len(), bi.total_size());
243-
assert!(bi.elf_sections().is_none());
243+
assert!(bi.elf_sections_tag().is_none());
244244
assert!(bi.memory_map_tag().is_none());
245245
assert!(bi.module_tags().next().is_none());
246246
assert_eq!(
@@ -834,7 +834,7 @@ mod tests {
834834
assert_eq!(addr, bi.start_address());
835835
assert_eq!(addr + bytes.len(), bi.end_address());
836836
assert_eq!(bytes.len(), bi.total_size());
837-
let mut es = bi.elf_sections().unwrap();
837+
let mut es = bi.elf_sections_tag().unwrap().sections();
838838
let s1 = es.next().expect("Should have one more section");
839839
assert_eq!(".rodata", s1.name().expect("Should be valid utf-8"));
840840
assert_eq!(0xFFFF_8000_0010_0000, s1.start_address());
@@ -1022,7 +1022,7 @@ mod tests {
10221022
assert_eq!(addr, bi.start_address());
10231023
assert_eq!(addr + bytes.0.len(), bi.end_address());
10241024
assert_eq!(bytes.0.len(), bi.total_size());
1025-
let mut es = bi.elf_sections().unwrap();
1025+
let mut es = bi.elf_sections_tag().unwrap().sections();
10261026
let s1 = es.next().expect("Should have one more section");
10271027
assert_eq!(".shstrtab", s1.name().expect("Should be valid utf-8"));
10281028
assert_eq!(string_addr, s1.start_address());

0 commit comments

Comments
 (0)