handle uefi and test assembly versus regular functions

This commit is contained in:
Folkert de Vries
2026-04-16 11:36:55 +02:00
parent cc1ebb5e20
commit 41afd5f8d6
2 changed files with 107 additions and 2 deletions
@@ -8,7 +8,7 @@
use rustc_middle::{bug, ty};
use rustc_span::sym;
use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
use rustc_target::spec::{Arch, BinaryFormat, Env};
use rustc_target::spec::{Arch, BinaryFormat, Env, Os};
use crate::common;
use crate::mir::AsmCodegenMethods;
@@ -268,6 +268,8 @@ fn prefix_and_suffix<'tcx>(
// `.subsections_via_symbols` global directive. LLVM already enables this directive.
if let Some(section) = &link_section {
writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
} else {
writeln!(begin, ".section __TEXT,__text,regular,pure_instructions").unwrap();
}
writeln!(begin, ".balign {align_bytes}").unwrap();
write_linkage(&mut begin).unwrap();
@@ -313,7 +315,13 @@ fn prefix_and_suffix<'tcx>(
Env::Msvc => {
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
}
other => bug!("invalid coff env {other:?}"),
Env::Unspecified => match &tcx.sess.target.options.os {
Os::Uefi => {
writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
}
_ => bug!("unexpected coff target {}", tcx.sess.target.llvm_target),
},
other => bug!("unexpected coff env {other:?}"),
}
}
write_linkage(&mut begin).unwrap();
@@ -0,0 +1,97 @@
//@ add-minicore
//@ assembly-output: emit-asm
//
//@ revisions: linux-x86-gnu-fs-true linux-x86-gnu-fs-false
//@[linux-x86-gnu-fs-true] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=true
//@[linux-x86-gnu-fs-true] needs-llvm-components: x86
//@[linux-x86-gnu-fs-false] compile-flags: --target x86_64-unknown-linux-gnu -Zfunction-sections=false
//@[linux-x86-gnu-fs-false] needs-llvm-components: x86
//
//@ revisions: macos-aarch64-fs-true macos-aarch64-fs-false
//@[macos-aarch64-fs-true] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=true
//@[macos-aarch64-fs-true] needs-llvm-components: aarch64
//@[macos-aarch64-fs-false] compile-flags: --target aarch64-apple-darwin -Zfunction-sections=false
//@[macos-aarch64-fs-false] needs-llvm-components: aarch64
//
//@ revisions: windows-x86-gnu-fs-true windows-x86-gnu-fs-false
//@[windows-x86-gnu-fs-true] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=true
//@[windows-x86-gnu-fs-true] needs-llvm-components: x86
//@[windows-x86-gnu-fs-false] compile-flags: --target x86_64-pc-windows-gnu -Zfunction-sections=false
//@[windows-x86-gnu-fs-false] needs-llvm-components: x86
//
//@ revisions: windows-x86-msvc-fs-true windows-x86-msvc-fs-false
//@[windows-x86-msvc-fs-true] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=true
//@[windows-x86-msvc-fs-true] needs-llvm-components: x86
//@[windows-x86-msvc-fs-false] compile-flags: --target x86_64-pc-windows-msvc -Zfunction-sections=false
//@[windows-x86-msvc-fs-false] needs-llvm-components: x86
//
//@ revisions: x86-uefi-fs-true x86-uefi-fs-false
//@[x86-uefi-fs-true] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=true
//@[x86-uefi-fs-true] needs-llvm-components: x86
//@[x86-uefi-fs-false] compile-flags: --target x86_64-unknown-uefi -Zfunction-sections=false
//@[x86-uefi-fs-false] needs-llvm-components: x86
#![crate_type = "lib"]
#![feature(no_core)]
#![no_core]
// Tests that naked and non-naked functions emit the same directives when (not) using
// -Zfunction-sections. This setting is ignored on macos, off by default on windows gnu,
// and on by default in the remaining revisions tested here.
extern crate minicore;
use minicore::*;
#[unsafe(naked)]
#[unsafe(no_mangle)]
extern "C" fn naked_ret() {
// linux-x86-gnu-fs-true: .section .text.naked_ret,"ax",@progbits
// linux-x86-gnu-fs-false: .text
//
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
//
// NOTE: the regular function below adds `unique,0` at the end, but we have no way of generating
// the unique ID to use there, so don't emit that part.
//
// windows-x86-gnu-fs-true: .section .text$naked_ret,"xr",one_only,naked_ret
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,naked_ret
// x86-uefi-fs-true: .section .text,"xr",one_only,naked_ret
//
// windows-x86-gnu-fs-false: .text
// windows-x86-msvc-fs-false: .text
// x86-uefi-fs-false: .text
//
// CHECK-LABEL: naked_ret:
naked_asm!("ret")
}
// Use a different section here so that `regular_ret` has to explicitly specify the section.
#[link_section = cfg_select!(
target_os = "macos" => "__FOO,bar",
_ => ".bar",
)]
#[unsafe(no_mangle)]
extern "C" fn omarker() -> i32 {
// CHECK-LABEL: omarker:
32
}
#[unsafe(no_mangle)]
extern "C" fn regular_ret() {
// linux-x86-gnu-fs-true: .section .text.regular_ret,"ax",@progbits
// linux-x86-gnu-fs-false: .text
//
// macos-aarch64-fs-true: .section __TEXT,__text,regular,pure_instructions
// macos-aarch64-fs-false: .section __TEXT,__text,regular,pure_instructions
//
// windows-x86-gnu-fs-true: .section .text$regular_ret,"xr",one_only,regular_ret,unique,0
// windows-x86-msvc-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
// x86-uefi-fs-true: .section .text,"xr",one_only,regular_ret,unique,0
//
// windows-x86-gnu-fs-false: .text
// windows-x86-msvc-fs-false: .text
// x86-uefi-fs-false: .text
//
// CHECK-LABEL: regular_ret:
}