Skip to content

Commit 41afd5f

Browse files
committed
handle uefi and test assembly versus regular functions
1 parent cc1ebb5 commit 41afd5f

2 files changed

Lines changed: 107 additions & 2 deletions

File tree

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::ty::{Instance, Ty, TyCtxt, TypeVisitableExt};
88
use rustc_middle::{bug, ty};
99
use rustc_span::sym;
1010
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
11-
use rustc_target::spec::{Arch, BinaryFormat, Env};
11+
use rustc_target::spec::{Arch, BinaryFormat, Env, Os};
1212

1313
use crate::common;
1414
use crate::mir::AsmCodegenMethods;
@@ -268,6 +268,8 @@ fn prefix_and_suffix<'tcx>(
268268
// `.subsections_via_symbols` global directive. LLVM already enables this directive.
269269
if let Some(section) = &link_section {
270270
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
271+
} else {
272+
writeln!(begin, ".section __TEXT,__text,regular,pure_instructions").unwrap();
271273
}
272274
writeln!(begin, ".balign {align_bytes}").unwrap();
273275
write_linkage(&mut begin).unwrap();
@@ -313,7 +315,13 @@ fn prefix_and_suffix<'tcx>(
313315
Env::Msvc => {
314316
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
315317
}
316-
other => bug!("invalid coff env {other:?}"),
318+
Env::Unspecified => match &tcx.sess.target.options.os {
319+
Os::Uefi => {
320+
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
321+
}
322+
_ => bug!("unexpected coff target {}", tcx.sess.target.llvm_target),
323+
},
324+
other => bug!("unexpected coff env {other:?}"),
317325
}
318326
}
319327
write_linkage(&mut begin).unwrap();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//@ add-minicore
2+
//@ assembly-output: emit-asm
3+
//
4+
//@ revisions: linux-x86-gnu-fs-true linux-x86-gnu-fs-false
5+
//@[linux-x86-gnu-fs-true] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=true
6+
//@[linux-x86-gnu-fs-true] needs-llvm-components: x86
7+
//@[linux-x86-gnu-fs-false] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=false
8+
//@[linux-x86-gnu-fs-false] needs-llvm-components: x86
9+
//
10+
//@ revisions: macos-aarch64-fs-true macos-aarch64-fs-false
11+
//@[macos-aarch64-fs-true] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=true
12+
//@[macos-aarch64-fs-true] needs-llvm-components: aarch64
13+
//@[macos-aarch64-fs-false] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=false
14+
//@[macos-aarch64-fs-false] needs-llvm-components: aarch64
15+
//
16+
//@ revisions: windows-x86-gnu-fs-true windows-x86-gnu-fs-false
17+
//@[windows-x86-gnu-fs-true] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=true
18+
//@[windows-x86-gnu-fs-true] needs-llvm-components: x86
19+
//@[windows-x86-gnu-fs-false] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=false
20+
//@[windows-x86-gnu-fs-false] needs-llvm-components: x86
21+
//
22+
//@ revisions: windows-x86-msvc-fs-true windows-x86-msvc-fs-false
23+
//@[windows-x86-msvc-fs-true] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=true
24+
//@[windows-x86-msvc-fs-true] needs-llvm-components: x86
25+
//@[windows-x86-msvc-fs-false] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=false
26+
//@[windows-x86-msvc-fs-false] needs-llvm-components: x86
27+
//
28+
//@ revisions: x86-uefi-fs-true x86-uefi-fs-false
29+
//@[x86-uefi-fs-true] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=true
30+
//@[x86-uefi-fs-true] needs-llvm-components: x86
31+
//@[x86-uefi-fs-false] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=false
32+
//@[x86-uefi-fs-false] needs-llvm-components: x86
33+
34+
#![crate_type = "lib"]
35+
#![feature(no_core)]
36+
#![no_core]
37+
38+
// Tests that naked and non-naked functions emit the same directives when (not) using
39+
// -Zfunction-sections. This setting is ignored on macos, off by default on windows gnu,
40+
// and on by default in the remaining revisions tested here.
41+
42+
extern crate minicore;
43+
use minicore::*;
44+
45+
#[unsafe(naked)]
46+
#[unsafe(no_mangle)]
47+
extern "C" fn naked_ret() {
48+
// linux-x86-gnu-fs-true: .section .text.naked_ret,"ax",@progbits
49+
// linux-x86-gnu-fs-false: .text
50+
//
51+
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
52+
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
53+
//
54+
// NOTE: the regular function below adds `unique,0` at the end, but we have no way of generating
55+
// the unique ID to use there, so don't emit that part.
56+
//
57+
// windows-x86-gnu-fs-true: .section .text$naked_ret,"xr",one_only,naked_ret
58+
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,naked_ret
59+
// x86-uefi-fs-true: .section .text,"xr",one_only,naked_ret
60+
//
61+
// windows-x86-gnu-fs-false: .text
62+
// windows-x86-msvc-fs-false: .text
63+
// x86-uefi-fs-false: .text
64+
//
65+
// CHECK-LABEL: naked_ret:
66+
naked_asm!("ret")
67+
}
68+
69+
// Use a different section here so that `regular_ret` has to explicitly specify the section.
70+
#[link_section = cfg_select!(
71+
target_os = "macos" => "__FOO,bar",
72+
_ => ".bar",
73+
)]
74+
#[unsafe(no_mangle)]
75+
extern "C" fn omarker() -> i32 {
76+
// CHECK-LABEL: omarker:
77+
32
78+
}
79+
80+
#[unsafe(no_mangle)]
81+
extern "C" fn regular_ret() {
82+
// linux-x86-gnu-fs-true: .section .text.regular_ret,"ax",@progbits
83+
// linux-x86-gnu-fs-false: .text
84+
//
85+
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
86+
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
87+
//
88+
// windows-x86-gnu-fs-true: .section .text$regular_ret,"xr",one_only,regular_ret,unique,0
89+
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
90+
// x86-uefi-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
91+
//
92+
// windows-x86-gnu-fs-false: .text
93+
// windows-x86-msvc-fs-false: .text
94+
// x86-uefi-fs-false: .text
95+
//
96+
// CHECK-LABEL: regular_ret:
97+
}

0 commit comments

Comments
 (0)