Skip to content

Commit 5afbf73

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

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 15 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,18 @@ 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+
// NOTE: LLVM ignores `-Zfunction-sections` on macos.
248+
if let Some(section) = &link_section {
249+
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
250+
}
245251
writeln!(begin, ".balign {align_bytes}").unwrap();
246252
write_linkage(&mut begin).unwrap();
247253
match item_data.visibility {
@@ -252,7 +258,9 @@ fn prefix_and_suffix<'tcx>(
252258

253259
writeln!(end).unwrap();
254260
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
255-
writeln!(end, ".popsection").unwrap();
261+
if link_section.is_some() || function_sections {
262+
writeln!(end, ".popsection").unwrap();
263+
}
256264
if !arch_suffix.is_empty() {
257265
writeln!(end, "{}", arch_suffix).unwrap();
258266
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use minicore::*;
2424
// linux,win: .intel_syntax
2525
//
2626
// linux: .pushsection .text.naked_empty,\22ax\22, @progbits
27-
// macos: .pushsection __TEXT,__text,regular,pure_instructions
27+
// macos-NOT: .pushsection
2828
// win_x86_msvc: .pushsection .text$naked_empty,\22xr\22
2929
// win_x86_gnu-NOT: .pushsection
3030
// win_i686_gnu-NOT: .pushsection
@@ -76,7 +76,7 @@ pub extern "C" fn naked_empty() {
7676
// linux,win: .intel_syntax
7777
//
7878
// linux: .pushsection .text.naked_with_args_and_return,\22ax\22, @progbits
79-
// macos: .pushsection __TEXT,__text,regular,pure_instructions
79+
// macos-NOT: .pushsection
8080
// win_x86_msvc: .pushsection .text$naked_with_args_and_return,\22xr\22
8181
// win_x86_gnu-NOT: .pushsection
8282
// win_i686_gnu-NOT: .pushsection

0 commit comments

Comments
 (0)