Implement sym operand support for global_asm! - #1620
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
|
|
||
| // 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.
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.
There was a problem hiding this comment.
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.
In order to do this, do we need to modify rustc or can we do this with the codegen? just getting better clarification on what to do.
There was a problem hiding this comment.
This requires modifying rustc. In particular the codegen unit partitioning code needs to be modified to treat references to symbols by global_asm!()/inline_asm!() as if they are from a different codegen unit and thus never mark them as private. Might also need some handling for cross-crate inlineable functions to give them a unique name when making them public.
| // 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.
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.
|
☔ The latest upstream changes (possibly d9bd396) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Any progress on this pr? |
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