Skip to content

std.elf: stronger typing #23600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dotcarmen
Copy link
Contributor

@dotcarmen dotcarmen commented Apr 18, 2025

supercedes #23178

This PR attempts to make std.elf a bit easier for API consumers:

  1. many constants have instead been converted to bitfields or enums where compatible and as appropriate
  2. ELF32_* and ELF64_* types have been moved/renamed into elf32 and elf64 namespaces
  3. all pub const x = switch (@sizeOf(usize)) { ... }; declarations have been consolidated into a single pub const native_elf which selects either elf32 or elf64
  4. some more utility functions/types for interacting with elf files

note: to optimize the reviewer's experience, i'll keep this PR as 2 commits: the first commit will be the API changes to std.elf, and the 2nd commit will be downstream changes

@dotcarmen
Copy link
Contributor Author

yes i know i still have to update references elsewhere in the codebase :)

2 questions i have:

  1. is it preferred to rename enum fields to lower case?
  2. is it preferred to have more descriptive names for the various types? (eg ET -> FileType)

@dotcarmen
Copy link
Contributor Author

also note that i didn't convert all groups of values to enums/bitfields - for example, AT_ constants can be compared against either u32 or u64 values. if it's preferred that these are also type-ified, I'd be happy to include that in this pr

@dotcarmen dotcarmen force-pushed the elf-stronger-typing branch 4 times, most recently from 3f37b69 to 4bf9545 Compare April 24, 2025 19:13
@dotcarmen dotcarmen force-pushed the elf-stronger-typing branch from 4bf9545 to 170ac8d Compare April 25, 2025 13:19
@dotcarmen dotcarmen force-pushed the elf-stronger-typing branch from 170ac8d to 63745ce Compare April 25, 2025 13:26
Comment on lines +450 to +455
pub fn sectionHeaders(
self: *const Header,
parse_source: anytype,
) SectionHeaders(@TypeOf(parse_source)) {
return .new(self, parse_source);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

header.section_header_iterator(file) -> header.sectionHeaders(file).iterator()

strtab_header: *const elf64.Shdr,
parse_source: anytype,
) StringTable(@TypeOf(parse_source)) {
_ = self;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

feels right to still have this be a method on Header, even though there's no use for self in this method... if it's preferred to add method Shdr.asStringTable instead then i can do that.

8 => Elf64_Relr,
else => @compileError("expected pointer size of 32 or 64"),

pub const SHF_OSBITS = packed struct(u8) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the SHF os/proc bitflag types are the most awkward api changes. feedback on a better pattern would be appreciated

Copy link
Contributor Author

@dotcarmen dotcarmen left a comment

Choose a reason for hiding this comment

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

lately i've been focusing on iterating the API re: my os project so i haven't been working on updating the API usage around the codebase. i'm getting to that soon :)

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.

1 participant