Skip to content

Commit b606b8e

Browse files
committed
naked functions: respect function_sections on windows
For `gnu` function_sections is off by default.
1 parent 843f8ce commit b606b8e

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ fn prefix_and_suffix<'tcx>(
127127

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

131133
let attrs = tcx.codegen_instance_attrs(instance.def);
132134
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
@@ -256,8 +258,11 @@ fn prefix_and_suffix<'tcx>(
256258
}
257259
}
258260
BinaryFormat::Coff => {
259-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
260-
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
261+
if let Some(section) = &link_section {
262+
writeln!(begin, ".pushsection {section},\"xr\"").unwrap()
263+
} else if function_sections {
264+
writeln!(begin, ".pushsection .text${asm_name},\"xr\"").unwrap()
265+
}
261266
writeln!(begin, ".balign {align_bytes}").unwrap();
262267
write_linkage(&mut begin).unwrap();
263268
writeln!(begin, ".def {asm_name}").unwrap();
@@ -268,7 +273,9 @@ fn prefix_and_suffix<'tcx>(
268273

269274
writeln!(end).unwrap();
270275
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
271-
writeln!(end, ".popsection").unwrap();
276+
if link_section.is_some() || function_sections {
277+
writeln!(end, ".popsection").unwrap();
278+
}
272279
if !arch_suffix.is_empty() {
273280
writeln!(end, "{}", arch_suffix).unwrap();
274281
}

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

Lines changed: 36 additions & 30 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,8 +25,9 @@ use minicore::*;
2325
//
2426
// linux: .pushsection .text.naked_empty,\22ax\22, @progbits
2527
// macos: .pushsection __TEXT,__text,regular,pure_instructions
26-
// win_x86: .pushsection .text.naked_empty,\22xr\22
27-
// win_i686: .pushsection .text._naked_empty,\22xr\22
28+
// win_x86_msvc: .pushsection .text$naked_empty,\22xr\22
29+
// win_x86_gnu-NOT: .pushsection
30+
// win_i686_gnu-NOT: .pushsection
2831
// thumb: .pushsection .text.naked_empty,\22ax\22, %progbits
2932
//
3033
// CHECK: .balign 4
@@ -37,12 +40,12 @@ use minicore::*;
3740
//
3841
// linux: .type naked_empty, @function
3942
//
40-
// win_x86: .def naked_empty
41-
// win_i686: .def _naked_empty
43+
// win_x86_msvc,win_x86_gnu: .def naked_empty
44+
// win_i686_gnu: .def _naked_empty
4245
//
43-
// win_x86,win_i686: .scl 2
44-
// win_x86,win_i686: .type 32
45-
// win_x86,win_i686: .endef
46+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
47+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
48+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
4649
//
4750
// thumb: .type naked_empty, %function
4851
// thumb: .thumb
@@ -53,7 +56,8 @@ use minicore::*;
5356
// linux,macos,win: ret
5457
// thumb: bx lr
5558
//
56-
// CHECK: .popsection
59+
// linux,windows,win_x86_msvc,thumb: .popsection
60+
// win_x86_gnu-NOT,win_i686_gnu-NOT: .popsection
5761
//
5862
// thumb: .thumb
5963
//
@@ -73,8 +77,9 @@ pub extern "C" fn naked_empty() {
7377
//
7478
// linux: .pushsection .text.naked_with_args_and_return,\22ax\22, @progbits
7579
// macos: .pushsection __TEXT,__text,regular,pure_instructions
76-
// win_x86: .pushsection .text.naked_with_args_and_return,\22xr\22
77-
// win_i686: .pushsection .text._naked_with_args_and_return,\22xr\22
80+
// win_x86_msvc: .pushsection .text$naked_with_args_and_return,\22xr\22
81+
// win_x86_gnu-NOT: .pushsection
82+
// win_i686_gnu-NOT: .pushsection
7883
// thumb: .pushsection .text.naked_with_args_and_return,\22ax\22, %progbits
7984
//
8085
// CHECK: .balign 4
@@ -87,27 +92,28 @@ pub extern "C" fn naked_empty() {
8792
//
8893
// linux: .type naked_with_args_and_return, @function
8994
//
90-
// win_x86: .def naked_with_args_and_return
91-
// win_i686: .def _naked_with_args_and_return
95+
// win_x86_msvc,win_x86_gnu: .def naked_with_args_and_return
96+
// win_i686_gnu: .def _naked_with_args_and_return
9297
//
93-
// win_x86,win_i686: .scl 2
94-
// win_x86,win_i686: .type 32
95-
// win_x86,win_i686: .endef
98+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
99+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
100+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
96101
//
97102
// thumb: .type naked_with_args_and_return, %function
98103
// thumb: .thumb
99104
// thumb: .thumb_func
100105
//
101106
// CHECK-LABEL: naked_with_args_and_return:
102107
//
103-
// linux, win_x86,win_i686: lea rax, [rdi + rsi]
108+
// linux,win_x86_msvc,win_x86_gnu,win_i686_gnu: lea rax, [rdi + rsi]
104109
// macos: add x0, x0, x1
105110
// thumb: adds r0, r0, r1
106111
//
107112
// linux,macos,win: ret
108113
// thumb: bx lr
109114
//
110-
// CHECK: .popsection
115+
// linux,windows,win_x86_msvc,thumb: .popsection
116+
// win_x86_gnu-NOT,win_i686_gnu-NOT: .popsection
111117
//
112118
// thumb: .thumb
113119
//
@@ -134,7 +140,7 @@ pub extern "C" fn naked_with_args_and_return(a: isize, b: isize) -> isize {
134140

135141
// linux: .pushsection .text.some_different_name,\22ax\22, @progbits
136142
// macos: .pushsection .text.some_different_name,regular,pure_instructions
137-
// win_x86,win_i686: .pushsection .text.some_different_name,\22xr\22
143+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .pushsection .text.some_different_name,\22xr\22
138144
// thumb: .pushsection .text.some_different_name,\22ax\22, %progbits
139145
// CHECK-LABEL: test_link_section:
140146
#[no_mangle]
@@ -148,15 +154,15 @@ pub extern "C" fn test_link_section() {
148154
naked_asm!("bx lr");
149155
}
150156

151-
// win_x86: .def fastcall_cc
152-
// win_i686: .def @fastcall_cc@4
157+
// win_x86_msvc,win_x86_gnu: .def fastcall_cc
158+
// win_i686_gnu: .def @fastcall_cc@4
153159
//
154-
// win_x86,win_i686: .scl 2
155-
// win_x86,win_i686: .type 32
156-
// win_x86,win_i686: .endef
160+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .scl 2
161+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .type 32
162+
// win_x86_msvc,win_x86_gnu,win_i686_gnu: .endef
157163
//
158-
// win_x86-LABEL: fastcall_cc:
159-
// win_i686-LABEL: @fastcall_cc@4:
164+
// win_x86_msvc-LABEL,win_x86_gnu-LABEL: fastcall_cc:
165+
// win_i686_gnu-LABEL: @fastcall_cc@4:
160166
#[cfg(target_os = "windows")]
161167
#[no_mangle]
162168
#[unsafe(naked)]

0 commit comments

Comments
 (0)