Skip to content

Allow passing Mach-O flags to set_segment_section#13137

Merged
alexcrichton merged 1 commit intobytecodealliance:mainfrom
madsmtm:allow-macho-flags
Apr 20, 2026
Merged

Allow passing Mach-O flags to set_segment_section#13137
alexcrichton merged 1 commit intobytecodealliance:mainfrom
madsmtm:allow-macho-flags

Conversation

@madsmtm
Copy link
Copy Markdown
Contributor

@madsmtm madsmtm commented Apr 19, 2026

This enables rustc_codegen_cranelift to control these, see rust-lang/rustc_codegen_cranelift#1648.

This enables `rustc_codegen_cranelift` to control these.
@madsmtm madsmtm requested a review from a team as a code owner April 19, 2026 12:14
@madsmtm madsmtm requested review from cfallin and removed request for a team April 19, 2026 12:14
Comment on lines +63 to +64
/// Object file segment, section and Mach-O flags.
pub custom_segment_section: Option<(String, String, u32)>,
Copy link
Copy Markdown
Contributor Author

@madsmtm madsmtm Apr 19, 2026

Choose a reason for hiding this comment

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

Another design for this could be to take:

segment: String,
section: String,
macho_section_type: String,
macho_section_attributes: Vec<String>,

And then have the "pure_instructions" -> S_ATTR_PURE_INSTRUCTIONS mappings in cranelift-object? What would you prefer?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe directly use object::write::SectionFlags and then or those flags with the ones cranelift-object would set by default?

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.

But that would introduce a dependency on object to cranelift-module?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ah, right this is cranelift-module, not cranelift-object.

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.

A third alternative would be:

custom_segment_section: Option<String>,

And do the entire parsing inside cranelift-object.

@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:module labels Apr 19, 2026
Copy link
Copy Markdown
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me -- thanks!

@cfallin cfallin added this pull request to the merge queue Apr 20, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 20, 2026
@alexcrichton alexcrichton added this pull request to the merge queue Apr 20, 2026
Merged via the queue into bytecodealliance:main with commit 2947208 Apr 20, 2026
48 checks passed
@madsmtm madsmtm deleted the allow-macho-flags branch April 20, 2026 19:04

match self.object.section_flags_mut(section) {
SectionFlags::MachO { flags } => {
*flags = *macho_flags;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually shouldn't this be |=? As is this would overwrite the default flags even when no flags are passed.

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.

I considered that, but there are no default flags for (SectionKind::Data, SectionKind::ReadOnlyData and SectionKind::ReadOnlyDataWithRel), and if there were, it would break if we tried to |= the section type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would skipping the flag set if macho_flags is 0 makes sense?

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.

Maybe we could do:

let section_type = *macho_flags & SECTION_TYPE;
let section_attributes = *macho_flags & SECTION_ATTRIBUTES;
let current_attributes = (*flags & SECTION_ATTRIBUTES);
*flags = section_type | current_attributes | section_attributes;

But I feared that might be confusing too, if I request only no_dead_strip, it might be weird if I got other attributes too.

Though we should probably have an assert that *flags is 0.

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.

I opened #13153 for adding a debug assertion instead

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.

4 participants