Skip to content

Commit 1daec06

Browse files
committed
Auto merge of #128004 - folkertdev:naked-fn-asm, r=Amanieu
codegen `#[naked]` functions using global asm tracking issue: #90957 Fixes #124375 This implements the approach suggested in the tracking issue: use the existing global assembly infrastructure to emit the body of `#[naked]` functions. The main advantage is that we now have full control over what gets generated, and are no longer dependent on LLVM not sneakily messing with our output (inlining, adding extra instructions, etc). I discussed this approach with `@Amanieu` and while I think the general direction is correct, there is probably a bunch of stuff that needs to change or move around here. I'll leave some inline comments on things that I'm not sure about. Combined with #127853, if both accepted, I think that resolves all steps from the tracking issue. r? `@Amanieu`
2 parents 21fe748 + 9aabef1 commit 1daec06

File tree

18 files changed

+635
-106
lines changed

18 files changed

+635
-106
lines changed

compiler/rustc_codegen_gcc/src/asm.rs

+7
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,13 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
867867
template_str.push_str("\n.popsection");
868868
self.context.add_top_level_asm(None, &template_str);
869869
}
870+
871+
fn mangled_name(&self, instance: Instance<'tcx>) -> String {
872+
// TODO(@Amanieu): Additional mangling is needed on
873+
// some targets to add a leading underscore (Mach-O)
874+
// or byte count suffixes (x86 Windows).
875+
self.tcx.symbol_name(instance).name.to_string()
876+
}
870877
}
871878

872879
fn modifier_to_gcc(

compiler/rustc_codegen_llvm/src/asm.rs

+8
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,14 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
442442
);
443443
}
444444
}
445+
446+
fn mangled_name(&self, instance: Instance<'tcx>) -> String {
447+
let llval = self.get_fn(instance);
448+
llvm::build_string(|s| unsafe {
449+
llvm::LLVMRustGetMangledName(llval, s);
450+
})
451+
.expect("symbol is not valid UTF-8")
452+
}
445453
}
446454

447455
pub(crate) fn inline_asm_call<'ll>(

compiler/rustc_codegen_llvm/src/attributes.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,9 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
395395
to_add.push(MemoryEffects::None.create_attr(cx.llcx));
396396
}
397397
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
398-
to_add.push(AttributeKind::Naked.create_attr(cx.llcx));
399-
// HACK(jubilee): "indirect branch tracking" works by attaching prologues to functions.
400-
// And it is a module-level attribute, so the alternative is pulling naked functions into
401-
// new LLVM modules. Otherwise LLVM's "naked" functions come with endbr prefixes per
402-
// https://github.com/rust-lang/rust/issues/98768
403-
to_add.push(AttributeKind::NoCfCheck.create_attr(cx.llcx));
404-
if llvm_util::get_version() < (19, 0, 0) {
405-
// Prior to LLVM 19, branch-target-enforcement was disabled by setting the attribute to
406-
// the string "false". Now it is disabled by absence of the attribute.
407-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "branch-target-enforcement", "false"));
408-
}
398+
// do nothing; a naked function is converted into an extern function
399+
// and a global assembly block. LLVM's support for naked functions is
400+
// not used.
409401
} else {
410402
// Do not set sanitizer attributes for naked functions.
411403
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
550550
}
551551
});
552552

553+
// naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
554+
// but not for the code generation backend because at that point the naked function will just be
555+
// a declaration, with a definition provided in global assembly.
556+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
557+
codegen_fn_attrs.inline = InlineAttr::Never;
558+
}
559+
553560
codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
554561
if !attr.has_name(sym::optimize) {
555562
return ia;
@@ -634,10 +641,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
634641
}
635642
}
636643

637-
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
638-
codegen_fn_attrs.inline = InlineAttr::Never;
639-
}
640-
641644
// Weak lang items have the same semantics as "std internal" symbols in the
642645
// sense that they're preserved through all our LTO passes and only
643646
// strippable by the linker.

compiler/rustc_codegen_ssa/src/mir/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod coverageinfo;
2020
pub mod debuginfo;
2121
mod intrinsic;
2222
mod locals;
23+
mod naked_asm;
2324
pub mod operand;
2425
pub mod place;
2526
mod rvalue;
@@ -176,6 +177,11 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
176177
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
177178
debug!("fn_abi: {:?}", fn_abi);
178179

180+
if cx.tcx().codegen_fn_attrs(instance.def_id()).flags.contains(CodegenFnAttrFlags::NAKED) {
181+
crate::mir::naked_asm::codegen_naked_asm::<Bx>(cx, &mir, instance);
182+
return;
183+
}
184+
179185
let debug_context = cx.create_function_debug_context(instance, fn_abi, llfn, mir);
180186

181187
let start_llbb = Bx::append_block(cx, llfn, "start");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
use rustc_attr::InstructionSetAttr;
2+
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
3+
use rustc_middle::mir::{Body, InlineAsmOperand};
4+
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf};
5+
use rustc_middle::ty::{Instance, TyCtxt};
6+
use rustc_middle::{bug, ty};
7+
use rustc_span::sym;
8+
9+
use crate::common;
10+
use crate::traits::{AsmCodegenMethods, BuilderMethods, GlobalAsmOperandRef, MiscCodegenMethods};
11+
12+
pub(crate) fn codegen_naked_asm<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
13+
cx: &'a Bx::CodegenCx,
14+
mir: &Body<'tcx>,
15+
instance: Instance<'tcx>,
16+
) {
17+
let rustc_middle::mir::TerminatorKind::InlineAsm {
18+
asm_macro: _,
19+
template,
20+
ref operands,
21+
options,
22+
line_spans,
23+
targets: _,
24+
unwind: _,
25+
} = mir.basic_blocks.iter().next().unwrap().terminator().kind
26+
else {
27+
bug!("#[naked] functions should always terminate with an asm! block")
28+
};
29+
30+
let operands: Vec<_> =
31+
operands.iter().map(|op| inline_to_global_operand::<Bx>(cx, instance, op)).collect();
32+
33+
let item_data = cx.codegen_unit().items().get(&MonoItem::Fn(instance)).unwrap();
34+
let name = cx.mangled_name(instance);
35+
let (begin, end) = prefix_and_suffix(cx.tcx(), instance, &name, item_data);
36+
37+
let mut template_vec = Vec::new();
38+
template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(begin.into()));
39+
template_vec.extend(template.iter().cloned());
40+
template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(end.into()));
41+
42+
cx.codegen_global_asm(&template_vec, &operands, options, line_spans);
43+
}
44+
45+
fn inline_to_global_operand<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
46+
cx: &'a Bx::CodegenCx,
47+
instance: Instance<'tcx>,
48+
op: &InlineAsmOperand<'tcx>,
49+
) -> GlobalAsmOperandRef<'tcx> {
50+
match op {
51+
InlineAsmOperand::Const { value } => {
52+
let const_value = instance
53+
.instantiate_mir_and_normalize_erasing_regions(
54+
cx.tcx(),
55+
cx.typing_env(),
56+
ty::EarlyBinder::bind(value.const_),
57+
)
58+
.eval(cx.tcx(), cx.typing_env(), value.span)
59+
.expect("erroneous constant missed by mono item collection");
60+
61+
let mono_type = instance.instantiate_mir_and_normalize_erasing_regions(
62+
cx.tcx(),
63+
cx.typing_env(),
64+
ty::EarlyBinder::bind(value.ty()),
65+
);
66+
67+
let string = common::asm_const_to_str(
68+
cx.tcx(),
69+
value.span,
70+
const_value,
71+
cx.layout_of(mono_type),
72+
);
73+
74+
GlobalAsmOperandRef::Const { string }
75+
}
76+
InlineAsmOperand::SymFn { value } => {
77+
let mono_type = instance.instantiate_mir_and_normalize_erasing_regions(
78+
cx.tcx(),
79+
cx.typing_env(),
80+
ty::EarlyBinder::bind(value.ty()),
81+
);
82+
83+
let instance = match mono_type.kind() {
84+
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
85+
_ => bug!("asm sym is not a function"),
86+
};
87+
88+
GlobalAsmOperandRef::SymFn { instance }
89+
}
90+
InlineAsmOperand::SymStatic { def_id } => {
91+
GlobalAsmOperandRef::SymStatic { def_id: *def_id }
92+
}
93+
InlineAsmOperand::In { .. }
94+
| InlineAsmOperand::Out { .. }
95+
| InlineAsmOperand::InOut { .. }
96+
| InlineAsmOperand::Label { .. } => {
97+
bug!("invalid operand type for naked_asm!")
98+
}
99+
}
100+
}
101+
102+
enum AsmBinaryFormat {
103+
Elf,
104+
Macho,
105+
Coff,
106+
}
107+
108+
impl AsmBinaryFormat {
109+
fn from_target(target: &rustc_target::spec::Target) -> Self {
110+
if target.is_like_windows {
111+
Self::Coff
112+
} else if target.is_like_osx {
113+
Self::Macho
114+
} else {
115+
Self::Elf
116+
}
117+
}
118+
}
119+
120+
fn prefix_and_suffix<'tcx>(
121+
tcx: TyCtxt<'tcx>,
122+
instance: Instance<'tcx>,
123+
asm_name: &str,
124+
item_data: &MonoItemData,
125+
) -> (String, String) {
126+
use std::fmt::Write;
127+
128+
let asm_binary_format = AsmBinaryFormat::from_target(&tcx.sess.target);
129+
130+
let is_arm = tcx.sess.target.arch == "arm";
131+
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
132+
133+
let attrs = tcx.codegen_fn_attrs(instance.def_id());
134+
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
135+
let align = attrs.alignment.map(|a| a.bytes()).unwrap_or(4);
136+
137+
// See https://sourceware.org/binutils/docs/as/ARM-Directives.html for info on these directives.
138+
// In particular, `.arm` can also be written `.code 32` and `.thumb` as `.code 16`.
139+
let (arch_prefix, arch_suffix) = if is_arm {
140+
(
141+
match attrs.instruction_set {
142+
None => match is_thumb {
143+
true => ".thumb\n.thumb_func",
144+
false => ".arm",
145+
},
146+
Some(InstructionSetAttr::ArmT32) => ".thumb\n.thumb_func",
147+
Some(InstructionSetAttr::ArmA32) => ".arm",
148+
},
149+
match is_thumb {
150+
true => ".thumb",
151+
false => ".arm",
152+
},
153+
)
154+
} else {
155+
("", "")
156+
};
157+
158+
let emit_fatal = |msg| tcx.dcx().span_fatal(tcx.def_span(instance.def_id()), msg);
159+
160+
// see https://godbolt.org/z/cPK4sxKor.
161+
let write_linkage = |w: &mut String| -> std::fmt::Result {
162+
match item_data.linkage {
163+
Linkage::External => {
164+
writeln!(w, ".globl {asm_name}")?;
165+
}
166+
Linkage::LinkOnceAny | Linkage::LinkOnceODR | Linkage::WeakAny | Linkage::WeakODR => {
167+
match asm_binary_format {
168+
AsmBinaryFormat::Elf | AsmBinaryFormat::Coff => {
169+
writeln!(w, ".weak {asm_name}")?;
170+
}
171+
AsmBinaryFormat::Macho => {
172+
writeln!(w, ".globl {asm_name}")?;
173+
writeln!(w, ".weak_definition {asm_name}")?;
174+
}
175+
}
176+
}
177+
Linkage::Internal | Linkage::Private => {
178+
// write nothing
179+
}
180+
Linkage::Appending => emit_fatal("Only global variables can have appending linkage!"),
181+
Linkage::Common => emit_fatal("Functions may not have common linkage"),
182+
Linkage::AvailableExternally => {
183+
// this would make the function equal an extern definition
184+
emit_fatal("Functions may not have available_externally linkage")
185+
}
186+
Linkage::ExternalWeak => {
187+
// FIXME: actually this causes a SIGILL in LLVM
188+
emit_fatal("Functions may not have external weak linkage")
189+
}
190+
}
191+
192+
Ok(())
193+
};
194+
195+
let mut begin = String::new();
196+
let mut end = String::new();
197+
match asm_binary_format {
198+
AsmBinaryFormat::Elf => {
199+
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
200+
201+
let progbits = match is_arm {
202+
true => "%progbits",
203+
false => "@progbits",
204+
};
205+
206+
let function = match is_arm {
207+
true => "%function",
208+
false => "@function",
209+
};
210+
211+
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
212+
writeln!(begin, ".balign {align}").unwrap();
213+
write_linkage(&mut begin).unwrap();
214+
if let Visibility::Hidden = item_data.visibility {
215+
writeln!(begin, ".hidden {asm_name}").unwrap();
216+
}
217+
writeln!(begin, ".type {asm_name}, {function}").unwrap();
218+
if !arch_prefix.is_empty() {
219+
writeln!(begin, "{}", arch_prefix).unwrap();
220+
}
221+
writeln!(begin, "{asm_name}:").unwrap();
222+
223+
writeln!(end).unwrap();
224+
writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
225+
writeln!(end, ".popsection").unwrap();
226+
if !arch_suffix.is_empty() {
227+
writeln!(end, "{}", arch_suffix).unwrap();
228+
}
229+
}
230+
AsmBinaryFormat::Macho => {
231+
let section = link_section.unwrap_or("__TEXT,__text".to_string());
232+
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
233+
writeln!(begin, ".balign {align}").unwrap();
234+
write_linkage(&mut begin).unwrap();
235+
if let Visibility::Hidden = item_data.visibility {
236+
writeln!(begin, ".private_extern {asm_name}").unwrap();
237+
}
238+
writeln!(begin, "{asm_name}:").unwrap();
239+
240+
writeln!(end).unwrap();
241+
writeln!(end, ".popsection").unwrap();
242+
if !arch_suffix.is_empty() {
243+
writeln!(end, "{}", arch_suffix).unwrap();
244+
}
245+
}
246+
AsmBinaryFormat::Coff => {
247+
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
248+
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
249+
writeln!(begin, ".balign {align}").unwrap();
250+
write_linkage(&mut begin).unwrap();
251+
writeln!(begin, ".def {asm_name}").unwrap();
252+
writeln!(begin, ".scl 2").unwrap();
253+
writeln!(begin, ".type 32").unwrap();
254+
writeln!(begin, ".endef {asm_name}").unwrap();
255+
writeln!(begin, "{asm_name}:").unwrap();
256+
257+
writeln!(end).unwrap();
258+
writeln!(end, ".popsection").unwrap();
259+
if !arch_suffix.is_empty() {
260+
writeln!(end, "{}", arch_suffix).unwrap();
261+
}
262+
}
263+
}
264+
265+
(begin, end)
266+
}

compiler/rustc_codegen_ssa/src/mono_item.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_hir as hir;
2+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
23
use rustc_middle::mir::interpret::ErrorHandled;
34
use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
45
use rustc_middle::ty::Instance;
@@ -135,7 +136,13 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
135136
cx.predefine_static(def_id, linkage, visibility, symbol_name);
136137
}
137138
MonoItem::Fn(instance) => {
138-
cx.predefine_fn(instance, linkage, visibility, symbol_name);
139+
let attrs = cx.tcx().codegen_fn_attrs(instance.def_id());
140+
141+
if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
142+
// do not define this function; it will become a global assembly block
143+
} else {
144+
cx.predefine_fn(instance, linkage, visibility, symbol_name);
145+
};
139146
}
140147
MonoItem::GlobalAsm(..) => {}
141148
}

compiler/rustc_codegen_ssa/src/traits/asm.rs

+7
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ pub trait AsmCodegenMethods<'tcx> {
6868
options: InlineAsmOptions,
6969
line_spans: &[Span],
7070
);
71+
72+
/// The mangled name of this instance
73+
///
74+
/// Additional mangling is used on
75+
/// some targets to add a leading underscore (Mach-O)
76+
/// or byte count suffixes (x86 Windows).
77+
fn mangled_name(&self, instance: Instance<'tcx>) -> String;
7178
}

0 commit comments

Comments
 (0)