-
Notifications
You must be signed in to change notification settings - Fork 121
Implement sym operand support for global_asm! #1620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- 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]>
|
|
||
| // 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. |
There was a problem hiding this comment.
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.
| // For Mach-O, symbols need an underscore prefix | ||
| if binary_format == BinaryFormat::Macho { | ||
| generated_asm.push('_'); | ||
| } | ||
| generated_asm.push_str(symbol); |
There was a problem hiding this comment.
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.
Summary
This PR implements full support for
symoperands inglobal_asm!, addressing the issues mentioned in #1204.Changes
Mach-O symbol mangling: Added underscore prefix for symbol names on Darwin targets (macOS) in both
global_asmandinline_asmPrivate function handling: Created wrapper functions for
SymFnoperands inglobal_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.Added module access to
GlobalAsmContext: Required for creating wrapper functions in the Cranelift moduleTechnical Details
For
SymFnoperands: A wrapper function is created (similar to what inline_asm already does) that forwards calls to the actual function. The wrapper is exported withLinkage::Export.For
SymStaticoperands: The symbol is referenced directly with proper Mach-O underscore prefix when needed.Test
Added a test in
mini_core_hello_world.rsthat:sym_target()returning 42global_asm!withsymto create an assembly function that jumps to itsym_target()Fixes #1204
🤖 Generated with Claude Code