Skip to content

Commit 872301b

Browse files
committed
naked functions: respect function_sections on linux/macos
1 parent 0e522d6 commit 872301b

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ fn prefix_and_suffix<'tcx>(
220220
let mut end = String::new();
221221
match asm_binary_format {
222222
BinaryFormat::Elf => {
223-
let section = link_section.unwrap_or_else(|| format!(".text.{asm_name}"));
224-
225223
let progbits = match is_arm {
226224
true => "%progbits",
227225
false => "@progbits",
@@ -232,7 +230,11 @@ fn prefix_and_suffix<'tcx>(
232230
false => "@function",
233231
};
234232

235-
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+
}
236238
writeln!(begin, ".balign {align_bytes}").unwrap();
237239
write_linkage(&mut begin).unwrap();
238240
match visibility {
@@ -251,14 +253,18 @@ fn prefix_and_suffix<'tcx>(
251253
// pattern match on assembly generated by LLVM.
252254
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
253255
writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
254-
writeln!(end, ".popsection").unwrap();
256+
if link_section.is_some() || function_sections {
257+
writeln!(end, ".popsection").unwrap();
258+
}
255259
if !arch_suffix.is_empty() {
256260
writeln!(end, "{}", arch_suffix).unwrap();
257261
}
258262
}
259263
BinaryFormat::MachO => {
260-
let section = link_section.unwrap_or_else(|| "__TEXT,__text".to_string());
261-
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
264+
// NOTE: LLVM ignores `-Zfunction-sections` on macos.
265+
if let Some(section) = &link_section {
266+
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
267+
}
262268
writeln!(begin, ".balign {align_bytes}").unwrap();
263269
write_linkage(&mut begin).unwrap();
264270
match visibility {
@@ -269,7 +275,9 @@ fn prefix_and_suffix<'tcx>(
269275

270276
writeln!(end).unwrap();
271277
writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
272-
writeln!(end, ".popsection").unwrap();
278+
if link_section.is_some() {
279+
writeln!(end, ".popsection").unwrap();
280+
}
273281
if !arch_suffix.is_empty() {
274282
writeln!(end, "{}", arch_suffix).unwrap();
275283
}

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_x86,win_i686: .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
//
2929
// win_x86_msvc: .pushsection .text$naked_empty,\22xr\22
3030
// win_x86_gnu-NOT: .pushsection
@@ -89,7 +89,7 @@ pub extern "C" fn naked_empty() {
8989
// linux,win_x86,win_i686: .intel_syntax
9090
//
9191
// linux: .pushsection .text.naked_with_args_and_return,\22ax\22, @progbits
92-
// macos: .pushsection __TEXT,__text,regular,pure_instructions
92+
// macos-NOT: .pushsection
9393
//
9494
// win_x86_msvc: .pushsection .text$naked_with_args_and_return,\22xr\22
9595
// win_x86_gnu-NOT: .pushsection

0 commit comments

Comments
 (0)