Skip to content

Conversation

@pannous
Copy link

@pannous pannous commented Jan 4, 2026

Summary

This PR implements full support for sym operands in global_asm!, addressing the issues mentioned in #1204.

Changes

  1. Mach-O symbol mangling: Added underscore prefix for symbol names on Darwin targets (macOS) in both global_asm and inline_asm

  2. Private function handling: Created wrapper functions for SymFn operands in global_asm!. Functions may be private to the current codegen unit and thus not exported from the object file. The wrapper function is exported and can be referenced by the external assembler.

  3. Added module access to GlobalAsmContext: Required for creating wrapper functions in the Cranelift module

Technical Details

  • For SymFn operands: A wrapper function is created (similar to what inline_asm already does) that forwards calls to the actual function. The wrapper is exported with Linkage::Export.

  • For SymStatic operands: The symbol is referenced directly with proper Mach-O underscore prefix when needed.

Test

Added a test in mini_core_hello_world.rs that:

  • Defines a Rust function sym_target() returning 42
  • Uses global_asm! with sym to create an assembly function that jumps to it
  • Verifies the assembly function correctly calls through to sym_target()
  • Supports x86_64 and aarch64 on Linux and macOS

Fixes #1204

🤖 Generated with Claude Code

pannous and others added 2 commits January 4, 2026 19:40
- Add Mach-O underscore prefix for symbol names in both global_asm and inline_asm
- Create wrapper functions for global_asm sym operands to handle private functions
  that may not be exported from the current codegen unit
- Add module access to GlobalAsmContext for wrapper function creation
- Fixes rustc_codegen_cranelift#1204

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Test that global_asm! can reference Rust functions via sym operands.
This verifies both the Mach-O underscore prefix and the wrapper function
creation for potentially private functions.

Supports x86_64 and aarch64 on Linux and macOS.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Copilot AI review requested due to automatic review settings January 4, 2026 18:41

This comment was marked as spam.


// Pass a wrapper rather than the function itself as the function itself
// may not be exported from the main codegen unit and may thus be
// unreachable from the object file created by an external assembler.
Copy link
Member

Choose a reason for hiding this comment

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

The exact same issue exists for statics. IMO the proper solution is to change rustc to not make functions and statics referenced by inline assembly be private rather than working around it in rustc.

Comment on lines +579 to +583
// For Mach-O, symbols need an underscore prefix
if binary_format == BinaryFormat::Macho {
generated_asm.push('_');
}
generated_asm.push_str(symbol);
Copy link
Member

Choose a reason for hiding this comment

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

This makes sense, but should probably be pulled out into a helper function to avoid code duplication and to make it easier to support mangling for Windows in the future.

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.

Full asm!() support

2 participants