Skip to content

Commit 7787bd9

Browse files
committed
fix macho section specifier & windows test
1 parent bc4aad3 commit 7787bd9

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ fn prefix_and_suffix<'tcx>(
295295
if let Some(section) = &link_section {
296296
writeln!(begin, ".section {section},\"xr\"").unwrap()
297297
} else if !function_sections {
298-
// Function sections are enabled by default on MSVC, but disabled by default on GNU.
298+
// Function sections are enabled by default on MSVC and windows-gnullvm,
299+
// but disabled by default on GNU.
299300
writeln!(begin, ".text").unwrap();
300301
} else {
301302
// LLVM uses an extension to the section directive to support defining multiple

src/tools/run-make-support/src/external_deps/rustc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,16 @@ impl Rustc {
449449
self.cmd.arg("-Zcodegen-source-order");
450450
self
451451
}
452+
453+
/// Specify `-Z function-sections={yes, no}`.
454+
pub fn function_sections(&mut self, enable: bool) -> &mut Self {
455+
let flag = match enable {
456+
true => "-Zfunction-sections=yes",
457+
false => "-Zfunction-sections=no",
458+
};
459+
self.cmd.arg(flag);
460+
self
461+
}
452462
}
453463

454464
/// Query the sysroot path corresponding `rustc --print=sysroot`.
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(cfg_target_object_format)]
12
use std::arch::naked_asm;
23

34
#[unsafe(naked)]
@@ -6,19 +7,36 @@ extern "C" fn used() {
67
naked_asm!("ret")
78
}
89

10+
#[unsafe(no_mangle)]
11+
extern "C" fn unused_clothed() -> i32 {
12+
42
13+
}
14+
915
#[unsafe(naked)]
1016
#[unsafe(no_mangle)]
1117
extern "C" fn unused() {
1218
naked_asm!("ret")
1319
}
1420

1521
#[unsafe(naked)]
16-
#[unsafe(link_section = "foobar")]
22+
#[unsafe(link_section = cfg_select!(
23+
target_object_format = "mach-o" => "__TEXT,foobar",
24+
_ => ".foobar",
25+
))]
1726
#[unsafe(no_mangle)]
1827
extern "C" fn unused_link_section() {
1928
naked_asm!("ret")
2029
}
2130

31+
#[unsafe(link_section = cfg_select!(
32+
target_object_format = "mach-o" => "__TEXT,baz",
33+
_ => ".baz",
34+
))]
35+
#[unsafe(no_mangle)]
36+
extern "C" fn unused_link_section_clothed() -> i32 {
37+
43
38+
}
39+
2240
fn main() {
2341
used();
2442
}

tests/run-make/naked-dead-code-elimination/rmake.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@ use run_make_support::symbols::object_contains_any_symbol;
44
use run_make_support::{bin_name, rustc};
55

66
fn main() {
7-
rustc().input("main.rs").opt().run();
8-
let mut unused = vec!["unused", "unused_link_section"];
9-
assert!(!object_contains_any_symbol(bin_name("main"), &unused));
7+
rustc().input("main.rs").opt().function_sections(true).run();
8+
9+
let bin = bin_name("main");
10+
11+
// Check that the naked symbol is eliminated when the "clothed" one is.
12+
13+
assert_eq!(
14+
object_contains_any_symbol(&bin, &["unused_clothed"]),
15+
object_contains_any_symbol(&bin, &["unused"])
16+
);
17+
18+
assert_eq!(
19+
object_contains_any_symbol(&bin, &["unused_link_section_clothed"]),
20+
object_contains_any_symbol(&bin, &["unused_link_section"])
21+
);
1022
}

0 commit comments

Comments
 (0)