Implement sym operand support for global_asm!#1620
Open
pannous wants to merge 2 commits intorust-lang:mainfrom
Open
Implement sym operand support for global_asm!#1620pannous wants to merge 2 commits intorust-lang:mainfrom
pannous wants to merge 2 commits intorust-lang:mainfrom
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>
bjorn3
reviewed
Jan 4, 2026
|
|
||
| // 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. |
Member
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.
bjorn3
reviewed
Jan 4, 2026
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); |
Member
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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