Skip to content

Rewrite: Add elf library for elf_sections.rs#292

Open
an-owl wants to merge 28 commits into
rust-osdev:mainfrom
an-owl:elf
Open

Rewrite: Add elf library for elf_sections.rs#292
an-owl wants to merge 28 commits into
rust-osdev:mainfrom
an-owl:elf

Conversation

@an-owl

@an-owl an-owl commented Apr 21, 2026

Copy link
Copy Markdown

This is a complete rewrite from #290, using elf::sections::SectionHeaderTable to construct the iterator. Unlike #290 however, I decided not to care about version compatibility. This removes or replaces a number of existing functions.

One change in particular is how to fetch the section name. The existing method in my opinion is flawed, if the ELF sections are relocated then it's not possible to get the name. I've changed this so the string table must be fetched separately, which can then be passed to a helper. This helper returns a &core::ffi::CStr instead of a &str, the ELF specification doesn't define the character encoding so we should not force it to be Unicode.

An important behavioral change that isn't obvious is that "unused" (SHT_NULL) sections are no longer skipped. This threw me for a loop during testing. If they're there it's probably there for a reason, so I think its a bad idea to skip it, especially if you can just use .filter(_).

@phip1611 phip1611 self-requested a review May 4, 2026 05:46
@an-owl an-owl changed the title Add elf library for elf_sections.rs Rewrite: Add elf library for elf_sections.rs May 14, 2026
@an-owl

an-owl commented Jun 3, 2026

Copy link
Copy Markdown
Author

Any progress on this?

@phip1611

phip1611 commented Jun 3, 2026

Copy link
Copy Markdown
Member

Hey! thanks for pinging me! Will look into this soon - I'll put it on my list

@phip1611 phip1611 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely - thanks! This is a great contribution! Only a few nits. Also, please update the changelog.

}
}

impl<'a> From<&'a ElfSectionsTag> for SectionHeaderTable<'a, NativeEndian> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about the value-add of this. It feels like a misuse of From - could you please add a motivation to the commit?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is that ElfSectionTag is effectively

struct ElfSectionTag {
    multiboot_data: ...,
    header_table: SectionHeaderTable,
}

So it would be nice to implement AsRef so it can be passed to functions taking impl AsRef<SectionHeaderTable>. But this causes a use-after-free because this SectionHeaderTable::new() does not return a reference.

Comment thread multiboot2/src/elf_sections.rs Outdated
entry_size: u32,
}
/// Extension trait for [SectionHeader] containing getters for rust-native types
pub trait ElfSectionExt {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is only one impl for this trait. You do this to extend the type SectionHeader which comes from the elf library, yes?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the idea, otherwise you need to cast the section type & flags through raw pointers. It also adds a method to fetch the section name, elf can only fetch the string table from elf::elfBytes or elf::ElfStream which obviously we don't have.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay - thanks!

};

let strtab_hdr = shdr_table.get(strtab_index).ok()?;
// todo: Should this check that `strtab_hdr.sh_type == elf::abi::SHT_STRTAB`?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about this todo?

@an-owl an-owl Jun 11, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have absolutely no strong feelings one way or another.

elf does this, But I'm not sure if we should bother checking. I feel it's the bootloaders responsibility to check this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine with me, we can leave this comment there for now

Comment thread multiboot2/src/elf_sections.rs Outdated
@phip1611

Copy link
Copy Markdown
Member

Merge remote-tracking branch 'origin/elf' into elf

You have a weird git history. Please clean up your git history without any merge commits and rebase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants