Skip to content

macOS: link clang_rt.osx to fix SME2 (__arm_tpidr2_*) link failures under rustc#498

Merged
HenryNdubuaku merged 7 commits intocactus-compute:mainfrom
yujonglee:fix/sme2-link-error
Mar 5, 2026
Merged

macOS: link clang_rt.osx to fix SME2 (__arm_tpidr2_*) link failures under rustc#498
HenryNdubuaku merged 7 commits intocactus-compute:mainfrom
yujonglee:fix/sme2-link-error

Conversation

@yujonglee
Copy link
Copy Markdown
Contributor

@yujonglee yujonglee commented Mar 5, 2026

  • First appearance

  • Symptom

    • cargo test / cargo build fails at the final link step on Apple Silicon with undefined symbols:
      • ___arm_tpidr2_save
      • ___arm_tpidr2_restore
      • (shown as ___arm_tpidr2_* by ld)
    • This shows up most often in CI (e.g. M1 runners) but can affect any macOS build that compiles the SME2 kernel.
  • Root Cause

    • The SME2 kernel (kernel_sme2.cpp) uses SME2 attributes/ABIs that reference __arm_tpidr2_* helper functions.
    • Those helpers are provided by Apple clang’s runtime archive libclang_rt.osx.a (inside the clang resource dir), not reliably by libSystem in a way Rust picks up automatically.
    • When linking through rustc, the final link invocation uses -nodefaultlibs, so the implicit “clang driver adds its runtime” behavior does not apply.
    • Result:
      • The object file(s) referencing __arm_tpidr2_* are present.
      • The defining archive is never linked.
      • The final link therefore fails.
  • What This PR Does

    • In rust/cactus-sys/build.rs, on macOS only:
      • runs clang --print-resource-dir
      • adds .../lib/darwin to the native search path
      • links libclang_rt.osx.a via
        cargo:rustc-link-lib=static=clang_rt.osx
    • This makes __arm_tpidr2_* resolution deterministic under Rust’s -nodefaultlibs linking.
  • Why Not “Allow Undefined” (-Wl,-U)

    • -Wl,-U,<sym> can make the link step succeed even if the symbol remains unresolved.
    • That risks a runtime crash if the SME2 path executes on hardware where cpu_has_sme2() is true but the symbols aren’t actually resolved in the final binary image.
    • This PR links the real definitions instead of deferring resolution.
  • Correct Conditions for SME2 to Actually Run

    • Compile-time (build) condition

      • The build must compile the SME2 kernel into the library (CMake BUILD_SME2 ends up true).
      • This is driven by the build toolchain’s SME2 support (headers/flags), not strictly “build machine is M4”.
    • Link-time condition (this change)

      • The final Rust link must include the clang runtime archive that defines __arm_tpidr2_* (libclang_rt.osx.a).
    • Runtime condition

      • The program must run on SME2-capable hardware and the runtime gate must allow it (cpu_has_sme2() true).
      • Otherwise the non-SME2 path is used.

@yujonglee yujonglee changed the title Set MACOSX_DEPLOYMENT_TARGET=14.0 for SME2 linker symbols Fix Rust binding related to SME2 Mar 5, 2026
Signed-off-by: Yujong Lee <[email protected]>
Signed-off-by: Yujong Lee <[email protected]>
Signed-off-by: Yujong Lee <[email protected]>
Signed-off-by: Yujong Lee <[email protected]>
@yujonglee yujonglee marked this pull request as draft March 5, 2026 13:20
Signed-off-by: Yujong Lee <[email protected]>
@yujonglee yujonglee changed the title Fix Rust binding related to SME2 macOS: link clang_rt.osx to fix SME2 (__arm_tpidr2_*) link failures under rustc Mar 5, 2026
@yujonglee yujonglee marked this pull request as ready for review March 5, 2026 13:55
@HenryNdubuaku HenryNdubuaku merged commit fd7ded8 into cactus-compute:main Mar 5, 2026
3 of 4 checks passed
@yujonglee yujonglee deleted the fix/sme2-link-error branch March 5, 2026 22:25
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