Skip to content

add support for mod_init_funcs (was compat: rust-ctor fails to trigger with Cranelift on MacOS, but works with LLVM) #1588

Closed

Description

@stewartadam

I noticed when switching to the cranelift backend that my typetag types stopped working:
dtolnay/typetag#35

I'm not sure what exactly is to blame, but figured it warranted reporting as a compat issue. Through some subsequent debugging, I've identified noticed an issue specific to Cranelift on MacOS where the rust-ctor crate to fails to trigger #[ctor]-annotated code on module initialization: mmastrac/linktime#339

Other thoughts:

Activity

  1. changed the title [-]Cranelift on MacOS fails to[/-] [+]compat: rust-ctor fails to trigger with Cranelift on MacOS, but works with LLVM[/+] on Jul 8, 2025
  2. bjorn3 commented on Jul 8, 2025

    @bjorn3
    Member

    Looks like on Mach-O constructor sections should get the S_MOD_INIT_FUNC_POINTERS flag. It seems like LLVM sets this flag for __DATA,__mod_init_func even if you miss the mod_init_funcs flag specified in the #[link_section]. I suspect this is a side-effect of https://github.com/llvm/llvm-project/blob/2e8e254d18f51b6ca898bf0b1e4d12109b5b16c7/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L1292-L1304 combined with merging the flags of identically named sections. The rust-ctor crate should probably use __DATA,__mod_init_func,mod_init_funcs as #[link_section]. That alone won't fix this issue though as handling for mod_init_funcs hasn't been added at

    let (segment_name, section_name) = if tcx.sess.target.is_like_darwin {
    // See https://github.com/llvm/llvm-project/blob/main/llvm/lib/MC/MCSectionMachO.cpp
    let mut parts = section_name.as_str().split(',');
    let Some(segment_name) = parts.next() else {
    tcx.dcx().fatal(format!(
    "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
    section_name
    ));
    };
    let Some(section_name) = parts.next() else {
    tcx.dcx().fatal(format!(
    "#[link_section = \"{}\"] is not valid for macos target: must be segment and section separated by comma",
    section_name
    ));
    };
    if section_name.len() > 16 {
    tcx.dcx().fatal(format!(
    "#[link_section = \"{}\"] is not valid for macos target: section name bigger than 16 bytes",
    section_name
    ));
    }
    let section_type = parts.next().unwrap_or("regular");
    if section_type != "regular" && section_type != "cstring_literals" {
    tcx.dcx().fatal(format!(
    "#[link_section = \"{}\"] is not supported: unsupported section type {}",
    section_name, section_type,
    ));
    }
    let _attrs = parts.next();
    if parts.next().is_some() {
    tcx.dcx().fatal(format!(
    "#[link_section = \"{}\"] is not valid for macos target: too many components",
    section_name
    ));
    }
    // FIXME(bytecodealliance/wasmtime#8901) set S_CSTRING_LITERALS section type when
    // cstring_literals is specified
    (segment_name, section_name)
    yet. This also needs a Cranelift change to be able to set this flag in the object file in the first place.

  3. changed the title [-]compat: rust-ctor fails to trigger with Cranelift on MacOS, but works with LLVM[/-] [+]add support for mod_init_funcs (was compat: rust-ctor fails to trigger with Cranelift on MacOS, but works with LLVM)[/+] on Jul 8, 2025
  4. added a commit that references this issue on Aug 10, 2025
  5. mmastrac commented on Aug 10, 2025

    @mmastrac

    ctor now emits the full flag (fixed in mmastrac/linktime#344).

  6. added a commit that references this issue on Feb 20, 2026
    3303ebb
  7. bjorn3 commented on Aug 24, 2026

    @bjorn3
    Member

    Fixed by 2bc05d4.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-macosOperating system: MacOS

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions