Skip to content

Commit 74be459

Browse files
committed
Auto merge of #147811 - folkertdev:naked-function-sections, r=<try>
naked functions: respect `function-sections` try-job: dist-various-1 try-job: dist-various-2 try-job: aarch64-apple try-job: x86_64-mingw-1
2 parents 7af3402 + c186ad4 commit 74be459

7 files changed

Lines changed: 317 additions & 46 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{Instance, Ty, TyCtxt, TypeVisitableExt};
88
use rustc_middle::{bug, ty};
99
use rustc_span::sym;
1010
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
11-
use rustc_target::spec::{Arch, BinaryFormat};
11+
use rustc_target::spec::{Arch, BinaryFormat, Env, Os};
1212

1313
use crate::common;
1414
use crate::mir::AsmCodegenMethods;
@@ -128,6 +128,8 @@ fn prefix_and_suffix<'tcx>(
128128

129129
let is_arm = tcx.sess.target.arch == Arch::Arm;
130130
let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
131+
let function_sections =
132+
tcx.sess.opts.unstable_opts.function_sections.unwrap_or(tcx.sess.target.function_sections);
131133

132134
// If we're compiling the compiler-builtins crate, e.g., the equivalent of
133135
// compiler-rt, then we want to implicitly compile everything with hidden
@@ -218,8 +220,6 @@ fn prefix_and_suffix<'tcx>(
218220
let mut end = String::new();
219221
match asm_binary_format {
220222
BinaryFormat::Elf => {
221-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
222-
223223
let progbits = match is_arm {
224224
true => "%progbits",
225225
false => "@progbits",
@@ -230,7 +230,13 @@ fn prefix_and_suffix<'tcx>(
230230
false => "@function",
231231
};
232232

233-
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
233+
if let Some(section) = &link_section {
234+
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
235+
} else if function_sections {
236+
writeln!(begin, ".pushsection .text.{asm_name},\"ax\", {progbits}").unwrap();
237+
} else {
238+
writeln!(begin, ".text").unwrap();
239+
}
234240
writeln!(begin, ".balign {align_bytes}").unwrap();
235241
write_linkage(&mut begin).unwrap();
236242
match visibility {
@@ -249,14 +255,22 @@ fn prefix_and_suffix<'tcx>(
249255
// pattern match on assembly generated by LLVM.
250256
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
251257
writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
252-
writeln!(end, ".popsection").unwrap();
258+
if link_section.is_some() || function_sections {
259+
writeln!(end, ".popsection").unwrap();
260+
}
253261
if !arch_suffix.is_empty() {
254262
writeln!(end, "{}", arch_suffix).unwrap();
255263
}
256264
}
257265
BinaryFormat::MachO => {
258-
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
259-
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
266+
// NOTE: LLVM ignores `-Zfunction-sections` on macos. Instead the Mach-O symbol
267+
// subsection splitting feature is used, which can be enabled with the
268+
// `.subsections_via_symbols` global directive. LLVM already enables this directive.
269+
if let Some(section) = &link_section {
270+
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
271+
} else {
272+
writeln!(begin, ".section __TEXT,__text,regular,pure_instructions").unwrap();
273+
}
260274
writeln!(begin, ".balign {align_bytes}").unwrap();
261275
write_linkage(&mut begin).unwrap();
262276
match visibility {
@@ -267,7 +281,9 @@ fn prefix_and_suffix<'tcx>(
267281

268282
writeln!(end).unwrap();
269283
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
270-
writeln!(end, ".popsection").unwrap();
284+
if link_section.is_some() {
285+
writeln!(end, ".popsection").unwrap();
286+
}
271287
if !arch_suffix.is_empty() {
272288
writeln!(end, "{}", arch_suffix).unwrap();
273289
}
@@ -278,8 +294,36 @@ fn prefix_and_suffix<'tcx>(
278294
writeln!(begin, ".type 32").unwrap();
279295
writeln!(begin, ".endef").unwrap();
280296

281-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
282-
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
297+
if let Some(section) = &link_section {
298+
writeln!(begin, ".section {section},\"xr\"").unwrap()
299+
} else if !function_sections {
300+
// Function sections are enabled by default on MSVC and windows-gnullvm,
301+
// but disabled by default on GNU.
302+
writeln!(begin, ".text").unwrap();
303+
} else {
304+
// LLVM uses an extension to the section directive to support defining multiple
305+
// sections with the same name and comdat. It adds `unique,<id>` at the end of the
306+
// `.section` directive. We have no way of generating that unique ID here, so don't
307+
// emit it.
308+
//
309+
// See https://llvm.org/docs/Extensions.html#id2.
310+
match &tcx.sess.target.options.env {
311+
Env::Gnu => {
312+
writeln!(begin, ".section .text${asm_name},\"xr\",one_only,{asm_name}")
313+
.unwrap();
314+
}
315+
Env::Msvc => {
316+
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
317+
}
318+
Env::Unspecified => match &tcx.sess.target.options.os {
319+
Os::Uefi => {
320+
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
321+
}
322+
_ => bug!("unexpected coff target {}", tcx.sess.target.llvm_target),
323+
},
324+
other => bug!("unexpected coff env {other:?}"),
325+
}
326+
}
283327
write_linkage(&mut begin).unwrap();
284328
writeln!(begin, ".balign {align_bytes}").unwrap();
285329
writeln!(begin, "{asm_name}:").unwrap();

src/tools/run-make-support/src/external_deps/rustc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ impl Rustc {
449449
self.cmd.arg("-Zcodegen-source-order");
450450
self
451451
}
452+
453+
/// Specify `-Z function-sections={yes, no}`.
454+
pub fn function_sections(&mut self, enable: bool) -> &mut Self {
455+
let flag = match enable {
456+
true => "-Zfunction-sections=yes",
457+
false => "-Zfunction-sections=no",
458+
};
459+
self.cmd.arg(flag);
460+
self
461+
}
452462
}
453463

454464
/// Query the sysroot path corresponding `rustc --print=sysroot`.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//@ add-minicore
2+
//@ assembly-output: emit-asm
3+
//
4+
//@ revisions: linux-x86-gnu-fs-true linux-x86-gnu-fs-false
5+
//@[linux-x86-gnu-fs-true] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=true
6+
//@[linux-x86-gnu-fs-true] needs-llvm-components: x86
7+
//@[linux-x86-gnu-fs-false] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=false
8+
//@[linux-x86-gnu-fs-false] needs-llvm-components: x86
9+
//
10+
//@ revisions: macos-aarch64-fs-true macos-aarch64-fs-false
11+
//@[macos-aarch64-fs-true] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=true
12+
//@[macos-aarch64-fs-true] needs-llvm-components: aarch64
13+
//@[macos-aarch64-fs-false] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=false
14+
//@[macos-aarch64-fs-false] needs-llvm-components: aarch64
15+
//
16+
//@ revisions: windows-x86-gnu-fs-true windows-x86-gnu-fs-false
17+
//@[windows-x86-gnu-fs-true] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=true
18+
//@[windows-x86-gnu-fs-true] needs-llvm-components: x86
19+
//@[windows-x86-gnu-fs-false] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=false
20+
//@[windows-x86-gnu-fs-false] needs-llvm-components: x86
21+
//
22+
//@ revisions: windows-x86-msvc-fs-true windows-x86-msvc-fs-false
23+
//@[windows-x86-msvc-fs-true] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=true
24+
//@[windows-x86-msvc-fs-true] needs-llvm-components: x86
25+
//@[windows-x86-msvc-fs-false] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=false
26+
//@[windows-x86-msvc-fs-false] needs-llvm-components: x86
27+
//
28+
//@ revisions: x86-uefi-fs-true x86-uefi-fs-false
29+
//@[x86-uefi-fs-true] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=true
30+
//@[x86-uefi-fs-true] needs-llvm-components: x86
31+
//@[x86-uefi-fs-false] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=false
32+
//@[x86-uefi-fs-false] needs-llvm-components: x86
33+
34+
#![crate_type = "lib"]
35+
#![feature(no_core)]
36+
#![no_core]
37+
38+
// Tests that naked and non-naked functions emit the same directives when (not) using
39+
// -Zfunction-sections. This setting is ignored on macos, off by default on windows gnu,
40+
// and on by default in the remaining revisions tested here.
41+
42+
extern crate minicore;
43+
use minicore::*;
44+
45+
#[unsafe(naked)]
46+
#[unsafe(no_mangle)]
47+
extern "C" fn naked_ret() {
48+
// linux-x86-gnu-fs-true: .section .text.naked_ret,"ax",@progbits
49+
// linux-x86-gnu-fs-false: .text
50+
//
51+
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
52+
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
53+
//
54+
// NOTE: the regular function below adds `unique,0` at the end, but we have no way of generating
55+
// the unique ID to use there, so don't emit that part.
56+
//
57+
// windows-x86-gnu-fs-true: .section .text$naked_ret,"xr",one_only,naked_ret
58+
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,naked_ret
59+
// x86-uefi-fs-true: .section .text,"xr",one_only,naked_ret
60+
//
61+
// windows-x86-gnu-fs-false: .text
62+
// windows-x86-msvc-fs-false: .text
63+
// x86-uefi-fs-false: .text
64+
//
65+
// CHECK-LABEL: naked_ret:
66+
naked_asm!("ret")
67+
}
68+
69+
// Use a different section here so that `regular_ret` has to explicitly specify the section.
70+
#[link_section = cfg_select!(
71+
target_os = "macos" => "__FOO,bar",
72+
_ => ".bar",
73+
)]
74+
#[unsafe(no_mangle)]
75+
extern "C" fn omarker() -> i32 {
76+
// CHECK-LABEL: omarker:
77+
32
78+
}
79+
80+
#[unsafe(no_mangle)]
81+
extern "C" fn regular_ret() {
82+
// linux-x86-gnu-fs-true: .section .text.regular_ret,"ax",@progbits
83+
// linux-x86-gnu-fs-false: .text
84+
//
85+
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
86+
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
87+
//
88+
// windows-x86-gnu-fs-true: .section .text$regular_ret,"xr",one_only,regular_ret,unique,0
89+
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
90+
// x86-uefi-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
91+
//
92+
// windows-x86-gnu-fs-false: .text
93+
// windows-x86-msvc-fs-false: .text
94+
// x86-uefi-fs-false: .text
95+
//
96+
// CHECK-LABEL: regular_ret:
97+
}

tests/assembly-llvm/naked-functions/link-section-windows.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ revisions: windows-x86-gnu windows-x86-msvc
1+
//@ revisions: windows-x86-gnu windows-x86-msvc x86-uefi
22
//@ add-minicore
33
//@ assembly-output: emit-asm
44
//
@@ -7,6 +7,9 @@
77
//
88
//@[windows-x86-msvc] compile-flags: --target x86_64-pc-windows-msvc
99
//@[windows-x86-msvc] needs-llvm-components: x86
10+
//
11+
//@[x86-uefi] compile-flags: --target x86_64-unknown-uefi
12+
//@[x86-uefi] needs-llvm-components: x86
1013

1114
#![crate_type = "lib"]
1215
#![feature(no_core)]

0 commit comments

Comments
 (0)