Skip to content

Commit 5d25b8f

Browse files
committed
Convert inline assembly sym operands into GCC input operands
This commit updates `<Builder as AsmBuilderMethods>::codegen_inline_asm` to convert `sym` operands into `"X" (&func_or_static)` input operands to indicate the dependency on the referenced symbols and prevent them from being eliminated. We follow the suit of the LLVM codegen with a mixture of its differing techniques for `asm!` and `global_asm!`. The codegen module generates input operands for the `sym` operands (as in `asm!` in cg_llvm). However, the codegen module replaces all placeholders with mangled symbol names before passing the assembly template string to the backend (as in `global_asm!` in cg_llvm), which means these input operands are never referenced in the final assembly template string. Unlike the LLVM codegen, the input operand constraint must be `X` instead of `s`. If the `s` constraint is used, GCC will employ checks to make sure that the operand can really be represented by a simple symbolic constant, thus rejecting symbols requiring GOT, etc. to resolve. Such checks are unnecessary for Rust `sym` as it's up to programmers to handle such complex cases, e.g., by manually appending GOT addressing modifiers to the substituted symbol names. Using the `X` constraint doesn't seem to generate any extra code, so this will not compromise the property of naked functions.
1 parent 4210fd4 commit 5d25b8f

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/asm.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::borrow::Cow;
1313
use crate::builder::Builder;
1414
use crate::context::CodegenCx;
1515
use crate::type_of::LayoutGccExt;
16+
use crate::callee::get_fn;
1617

1718

1819
// Rust asm! and GCC Extended Asm semantics differ substantially.
@@ -343,9 +344,24 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
343344
// processed in the previous pass
344345
}
345346

346-
InlineAsmOperandRef::Const { .. }
347-
| InlineAsmOperandRef::SymFn { .. }
348-
| InlineAsmOperandRef::SymStatic { .. } => {
347+
InlineAsmOperandRef::SymFn { instance } => {
348+
inputs.push(AsmInOperand {
349+
constraint: "X".into(),
350+
rust_idx,
351+
val: self.cx.rvalue_as_function(get_fn(self.cx, instance))
352+
.get_address(None),
353+
});
354+
}
355+
356+
InlineAsmOperandRef::SymStatic { def_id } => {
357+
inputs.push(AsmInOperand {
358+
constraint: "X".into(),
359+
rust_idx,
360+
val: self.cx.get_static(def_id).get_address(None),
361+
});
362+
}
363+
364+
InlineAsmOperandRef::Const { .. } => {
349365
// processed in the previous pass
350366
}
351367
}

0 commit comments

Comments
 (0)