Skip to content

Commit 2ebd197

Browse files
committed
naked functions: respect function_sections on linux/macos
1 parent 2748bd7 commit 2ebd197

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ fn prefix_and_suffix<'tcx>(
203203
let mut end = String::new();
204204
match asm_binary_format {
205205
BinaryFormat::Elf => {
206-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
207-
208206
let progbits = match is_arm {
209207
true => "%progbits",
210208
false => "@progbits",
@@ -215,7 +213,11 @@ fn prefix_and_suffix<'tcx>(
215213
false => "@function",
216214
};
217215

218-
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
216+
if let Some(section) = &link_section {
217+
writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
218+
} else if function_sections {
219+
writeln!(begin, ".pushsection .text.{asm_name},\"ax\", {progbits}").unwrap();
220+
}
219221
writeln!(begin, ".balign {align_bytes}").unwrap();
220222
write_linkage(&mut begin).unwrap();
221223
match item_data.visibility {
@@ -234,14 +236,19 @@ fn prefix_and_suffix<'tcx>(
234236
// pattern match on assembly generated by LLVM.
235237
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
236238
writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
237-
writeln!(end, ".popsection").unwrap();
239+
if link_section.is_some() || function_sections {
240+
writeln!(end, ".popsection").unwrap();
241+
}
238242
if !arch_suffix.is_empty() {
239243
writeln!(end, "{}", arch_suffix).unwrap();
240244
}
241245
}
242246
BinaryFormat::MachO => {
243-
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
244-
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
247+
if let Some(section) = &link_section {
248+
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
249+
} else if function_sections {
250+
writeln!(begin, ".pushsection __TEXT,__text,regular,pure_instructions").unwrap();
251+
}
245252
writeln!(begin, ".balign {align_bytes}").unwrap();
246253
write_linkage(&mut begin).unwrap();
247254
match item_data.visibility {
@@ -252,7 +259,9 @@ fn prefix_and_suffix<'tcx>(
252259

253260
writeln!(end).unwrap();
254261
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
255-
writeln!(end, ".popsection").unwrap();
262+
if link_section.is_some() || function_sections {
263+
writeln!(end, ".popsection").unwrap();
264+
}
256265
if !arch_suffix.is_empty() {
257266
writeln!(end, "{}", arch_suffix).unwrap();
258267
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ add-core-stubs
2-
//@ revisions: linux win_x86_msvc win_x86_gnu win_i686_gnu macos thumb
2+
//@ revisions: linux win_x86_msvc win_x86_gnu win_i686_gnu macos macos-function-sections thumb
33
//
44
//@[linux] compile-flags: --target x86_64-unknown-linux-gnu
55
//@[linux] needs-llvm-components: x86
@@ -11,6 +11,8 @@
1111
//@[win_i686_gnu] needs-llvm-components: x86
1212
//@[macos] compile-flags: --target aarch64-apple-darwin
1313
//@[macos] needs-llvm-components: aarch64
14+
//@[macos-function-sections] compile-flags: --target aarch64-apple-darwin -Cfunction-sections
15+
//@[macos-function-sections] needs-llvm-components: aarch64
1416
//@[thumb] compile-flags: --target thumbv7em-none-eabi
1517
//@[thumb] needs-llvm-components: arm
1618

@@ -24,7 +26,8 @@ use minicore::*;
2426
// linux,win: .intel_syntax
2527
//
2628
// linux: .pushsection .text.naked_empty,\22ax\22, @progbits
27-
// macos: .pushsection __TEXT,__text,regular,pure_instructions
29+
// macos-NOT: .pushsection
30+
// macos-function-sections: .pushsection __TEXT,__text,regular,pure_instructions
2831
// win_x86_msvc: .pushsection .text$naked_empty,\22xr\22
2932
// win_x86_gnu-NOT: .pushsection
3033
// win_i686_gnu-NOT: .pushsection
@@ -76,7 +79,8 @@ pub extern "C" fn naked_empty() {
7679
// linux,win: .intel_syntax
7780
//
7881
// linux: .pushsection .text.naked_with_args_and_return,\22ax\22, @progbits
79-
// macos: .pushsection __TEXT,__text,regular,pure_instructions
82+
// macos-NOT: .pushsection
83+
// macos-function-sections: .pushsection __TEXT,__text,regular,pure_instructions
8084
// win_x86_msvc: .pushsection .text$naked_with_args_and_return,\22xr\22
8185
// win_x86_gnu-NOT: .pushsection
8286
// win_i686_gnu-NOT: .pushsection

0 commit comments

Comments
 (0)