Skip to content

Commit 0e522d6

Browse files
committed
naked functions: respect function_sections on windows
For `gnu` function_sections is off by default.
1 parent e8e4541 commit 0e522d6

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -278,8 +280,11 @@ fn prefix_and_suffix<'tcx>(
278280
writeln!(begin, ".type 32").unwrap();
279281
writeln!(begin, ".endef").unwrap();
280282

281-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
282-
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
283+
if let Some(section) = &link_section {
284+
writeln!(begin, ".pushsection {section},\"xr\"").unwrap()
285+
} else if function_sections {
286+
writeln!(begin, ".pushsection .text${asm_name},\"xr\"").unwrap()
287+
}
283288
write_linkage(&mut begin).unwrap();
284289
writeln!(begin, ".balign {align_bytes}").unwrap();
285290
writeln!(begin, "{asm_name}:").unwrap();

tests/codegen-llvm/naked-fn/naked-functions.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//@ add-minicore
2-
//@ revisions: linux win_x86 win_i686 macos thumb
2+
//@ revisions: linux win_x86_msvc win_x86_gnu win_i686_gnu macos thumb
33
//
44
//@[linux] compile-flags: --target x86_64-unknown-linux-gnu
55
//@[linux] needs-llvm-components: x86
6-
//@[win_x86] compile-flags: --target x86_64-pc-windows-gnu
7-
//@[win_x86] needs-llvm-components: x86
8-
//@[win_i686] compile-flags: --target i686-pc-windows-gnu
9-
//@[win_i686] needs-llvm-components: x86
6+
//@[win_x86_gnu] compile-flags: --target x86_64-pc-windows-gnu
7+
//@[win_x86_gnu] needs-llvm-components: x86
8+
//@[win_x86_msvc] compile-flags: --target x86_64-pc-windows-msvc
9+
//@[win_x86_msvc] needs-llvm-components: x86
10+
//@[win_i686_gnu] compile-flags: --target i686-pc-windows-gnu
11+
//@[win_i686_gnu] needs-llvm-components: x86
1012
//@[macos] compile-flags: --target aarch64-apple-darwin
1113
//@[macos] needs-llvm-components: aarch64
1214
//@[thumb] compile-flags: --target thumbv7em-none-eabi
@@ -23,6 +25,11 @@ use minicore::*;
2325
//
2426
// linux: .pushsection .text.naked_empty,\22ax\22, @progbits
2527
// macos: .pushsection __TEXT,__text,regular,pure_instructions
28+
//
29+
// win_x86_msvc: .pushsection .text$naked_empty,\22xr\22
30+
// win_x86_gnu-NOT: .pushsection
31+
// win_i686_gnu-NOT: .pushsection
32+
//
2633
// thumb: .pushsection .text.naked_empty,\22ax\22, %progbits
2734
//
2835
// linux, macos, thumb: .balign 4
@@ -35,12 +42,12 @@ use minicore::*;
3542
//
3643
// linux: .type naked_empty, @function
3744
//
38-
// win_x86: .def naked_empty
39-
// win_i686: .def _naked_empty
45+
// win_x86_msvc,win_x86_gnu: .def naked_empty
46+
// win_i686_gnu: .def _naked_empty
4047
//
41-
// win_x86,win_i686: .scl 2
42-
// win_x86,win_i686: .type 32
43-
// win_x86,win_i686: .endef
48+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
49+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
50+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
4451
//
4552
// win_x86: .pushsection .text.naked_empty,\22xr\22
4653
// win_i686: .pushsection .text._naked_empty,\22xr\22
@@ -59,7 +66,8 @@ use minicore::*;
5966
// linux,macos,win_x86,win_x86: ret
6067
// thumb: bx lr
6168
//
62-
// linux,macos,thumb: .popsection
69+
// linux,windows,win_x86_msvc,thumb: .popsection
70+
// win_x86_gnu-NOT,win_i686_gnu-NOT: .popsection
6371
//
6472
// thumb: .thumb
6573
//
@@ -82,6 +90,11 @@ pub extern "C" fn naked_empty() {
8290
//
8391
// linux: .pushsection .text.naked_with_args_and_return,\22ax\22, @progbits
8492
// macos: .pushsection __TEXT,__text,regular,pure_instructions
93+
//
94+
// win_x86_msvc: .pushsection .text$naked_with_args_and_return,\22xr\22
95+
// win_x86_gnu-NOT: .pushsection
96+
// win_i686_gnu-NOT: .pushsection
97+
//
8598
// thumb: .pushsection .text.naked_with_args_and_return,\22ax\22, %progbits
8699
//
87100
// linux, macos, thumb: .balign 4
@@ -94,12 +107,12 @@ pub extern "C" fn naked_empty() {
94107
//
95108
// linux: .type naked_with_args_and_return, @function
96109
//
97-
// win_x86: .def naked_with_args_and_return
98-
// win_i686: .def _naked_with_args_and_return
110+
// win_x86_msvc,win_x86_gnu: .def naked_with_args_and_return
111+
// win_i686_gnu: .def _naked_with_args_and_return
99112
//
100-
// win_x86,win_i686: .scl 2
101-
// win_x86,win_i686: .type 32
102-
// win_x86,win_i686: .endef
113+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
114+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
115+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
103116
//
104117
// win_x86: .pushsection .text.naked_with_args_and_return,\22xr\22
105118
// win_i686: .pushsection .text._naked_with_args_and_return,\22xr\22
@@ -115,14 +128,15 @@ pub extern "C" fn naked_empty() {
115128
//
116129
// CHECK-LABEL: naked_with_args_and_return:
117130
//
118-
// linux, win_x86,win_i686: lea rax, [rdi + rsi]
131+
// linux,win_x86_msvc,win_x86_gnu,win_i686_gnu: lea rax, [rdi + rsi]
119132
// macos: add x0, x0, x1
120133
// thumb: adds r0, r0, r1
121134
//
122135
// linux,macos,win_x86,win_i686: ret
123136
// thumb: bx lr
124137
//
125-
// linux,macos,thumb: .popsection
138+
// linux,windows,win_x86_msvc,thumb: .popsection
139+
// win_x86_gnu-NOT,win_i686_gnu-NOT: .popsection
126140
//
127141
// thumb: .thumb
128142
//
@@ -146,7 +160,7 @@ pub extern "C" fn naked_with_args_and_return(a: isize, b: isize) -> isize {
146160

147161
// linux: .pushsection .text.some_different_name,\22ax\22, @progbits
148162
// macos: .pushsection .text.some_different_name,regular,pure_instructions
149-
// win_x86,win_i686: .pushsection .text.some_different_name,\22xr\22
163+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .pushsection .text.some_different_name,\22xr\22
150164
// thumb: .pushsection .text.some_different_name,\22ax\22, %progbits
151165
// CHECK-LABEL: test_link_section:
152166
#[no_mangle]
@@ -163,15 +177,15 @@ pub extern "C" fn test_link_section() {
163177
}
164178
}
165179

166-
// win_x86: .def fastcall_cc
167-
// win_i686: .def @fastcall_cc@4
180+
// win_x86_msvc,win_x86_gnu: .def fastcall_cc
181+
// win_i686_gnu: .def @fastcall_cc@4
168182
//
169-
// win_x86,win_i686: .scl 2
170-
// win_x86,win_i686: .type 32
171-
// win_x86,win_i686: .endef
183+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
184+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
185+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
172186
//
173-
// win_x86-LABEL: fastcall_cc:
174-
// win_i686-LABEL: @fastcall_cc@4:
187+
// win_x86_msvc-LABEL,win_x86_gnu-LABEL: fastcall_cc:
188+
// win_i686_gnu-LABEL: @fastcall_cc@4:
175189
#[cfg(target_os = "windows")]
176190
#[no_mangle]
177191
#[unsafe(naked)]

0 commit comments

Comments
 (0)