Skip to content

Add Mach-O section parsing#13869

Open
madsmtm wants to merge 1 commit into
bytecodealliance:mainfrom
madsmtm:parse-macho-section
Open

Add Mach-O section parsing#13869
madsmtm wants to merge 1 commit into
bytecodealliance:mainfrom
madsmtm:parse-macho-section

Conversation

@madsmtm

@madsmtm madsmtm commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Mach-O sections have a type and a number of attributes, and are grouped in segments. There is a canonical textual representation that allows specifying this with the assembly .section directive (mostly defined by LLVM's source).

Cranelift supported this with DataDescription::custom_segment_section by letting the user implement the parsing themselves, but this complicates the API (and will require breaking changes if we want to support specifying e.g. the stub size).

Instead, DataDescription::custom_section is now just a String, and cranelift-object implements the parsing of the section name into its components. This is the same "layering" that LLVM has chosen, and it seems to work well there.

This makes it easy for rustc_cranelift_backend to implement Rust's #[link_section = ...] to support things such as the ctor crate.

Discussed a bit in #13137, I changed my mind since then.
Fixes #8901.
Replaces rust-lang/rustc_codegen_cranelift#1648.

@madsmtm madsmtm requested a review from a team as a code owner July 13, 2026 14:03
@madsmtm madsmtm requested review from alexcrichton and removed request for a team July 13, 2026 14:03
@alexcrichton

Copy link
Copy Markdown
Member

@bjorn3 would you be able to review this?

@madsmtm madsmtm force-pushed the parse-macho-section branch 2 times, most recently from 936f10a to 9853ed2 Compare July 13, 2026 14:14
Mach-O sections have a type and a number of attributes, and are grouped
in segments. There is a canonical textual representation that allows
specifying this with the assembly .section directive (mostly defined by
LLVM's source).

Cranelift supported this with `DataDescription::custom_segment_section`
by letting the user implement the parsing themselves, but this
complicates the API (and will require breaking changes if we want to
support specifying e.g. the stub size).

Instead, `DataDescription::custom_section` is now just a `String`, and
`cranelift-object` implements the parsing of the section name into its
components. This is the same "layering" that LLVM has chosen, and it
seems to work well there.

This makes it easy for `rustc_cranelift_backend` to implement Rust's
`#[link_section = ...]` to support things such as the `ctor` crate.
@madsmtm madsmtm force-pushed the parse-macho-section branch from 9853ed2 to ab3b658 Compare July 13, 2026 14:15
@bjorn3

bjorn3 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Sure. Will see if I can do that later today.

binary_format: BinaryFormat,
) -> Result<(&str, &str, u32), anyhow::Error> {
match binary_format {
// See https://github.com/llvm/llvm-project/blob/main/llvm/lib/MC/MCSectionMachO.cpp

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LLVM's parsing is more lenient, they allow whitespace as well. I can implement that too if you want?

("literal_pointers", S_LITERAL_POINTERS),
("non_lazy_symbol_pointers", S_NON_LAZY_SYMBOL_POINTERS),
("lazy_symbol_pointers", S_LAZY_SYMBOL_POINTERS),
// ("symbol_stubs", S_SYMBOL_STUBS) (requires extra param stub size)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This requires a stub size (5th part of the custom section name) that should be set with:
https://docs.rs/object/latest/object/read/macho/trait.Section.html#method.symbol_stub_size

I can implement that too it if you want me to?

@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:module labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:module cranelift Issues related to the Cranelift code generator

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cranelift-module: Support marking data object as read-only C string

3 participants