Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i686-unknown-uefi target #64334

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/librustc_target/spec/i686_unknown_uefi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This defines the ia32 target for UEFI systems as described in the UEFI specification. See the
// uefi-base module for generic UEFI options. On ia32 systems
// UEFI systems always run in protected-mode, have the interrupt-controller pre-configured and
// force a single-CPU execution.
// The cdecl ABI is used. It differs from the stdcall or fastcall ABI.
// "i686-unknown-windows" is used to get the minimal subset of windows-specific features.

use crate::spec::{LinkerFlavor, LldFlavor, Target, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::uefi_base::opts();
base.cpu = "pentium4".to_string();
base.max_atomic_width = Some(64);

// We disable MMX and SSE for now, even though UEFI allows using them. Problem is, you have to
// enable these CPU features explicitly before their first use, otherwise their instructions
// will trigger an exception. Rust does not inject any code that enables AVX/MMX/SSE
// instruction sets, so this must be done by the firmware. However, existing firmware is known
// to leave these uninitialized, thus triggering exceptions if we make use of them. Which is
// why we avoid them and instead use soft-floats. This is also what GRUB and friends did so
// far.
// If you initialize FP units yourself, you can override these flags with custom linker
// arguments, thus giving you access to full MMX/SSE acceleration.
base.features = "-mmx,-sse,+soft-float".to_string();

// UEFI mirrors the calling-conventions used on windows. In case of i686 this means small
// structs will be returned as int. This shouldn't matter much, since the restrictions placed
// by the UEFI specifications forbid any ABI to return structures.
base.abi_return_struct_as_int = true;

// Use -GNU here, because of the reason below:
// Backgound and Problem:
// If we use i686-unknown-windows, the LLVM IA32 MSVC generates compiler intrinsic
// _alldiv, _aulldiv, _allrem, _aullrem, _allmul, which will cause undefined symbol.
// A real issue is __aulldiv() is refered by __udivdi3() - udivmod_inner!(), from
// https://github.com/rust-lang-nursery/compiler-builtins.
// As result, rust-lld generates link error finally.
// Root-cause:
// In rust\src\llvm-project\llvm\lib\Target\X86\X86ISelLowering.cpp,
// we have below code to use MSVC intrinsics. It assumes MSVC target
// will link MSVC library. But that is NOT true in UEFI environment.
// UEFI does not link any MSVC or GCC standard library.
// if (Subtarget.isTargetKnownWindowsMSVC() ||
// Subtarget.isTargetWindowsItanium()) {
// // Setup Windows compiler runtime calls.
// setLibcallName(RTLIB::SDIV_I64, "_alldiv");
// setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
// setLibcallName(RTLIB::SREM_I64, "_allrem");
// setLibcallName(RTLIB::UREM_I64, "_aullrem");
// setLibcallName(RTLIB::MUL_I64, "_allmul");
// setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
// setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
// setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
// setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
// setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
// }
// The compiler intrisics should be implemented by compiler-builtins.
// Unfortunately, compiler-builtins has not provided those intrinsics yet. Such as:
// i386/divdi3.S
// i386/lshrdi3.S
// i386/moddi3.S
// i386/muldi3.S
// i386/udivdi3.S
// i386/umoddi3.S
// Possible solution:
// 1. Eliminate Intrinsics generation.
// 1.1 Choose differnt target to bypass isTargetKnownWindowsMSVC().
// 1.2 Remove the "Setup Windows compiler runtime calls" in LLVM
// 2. Implement Intrinsics.
// We evaluated all options.
// #2 is hard because we need implement the intrinsics (_aulldiv) generated
// from the other intrinscis (__udivdi3) implementation with the same
// functionality (udivmod_inner). If we let _aulldiv() call udivmod_inner!(),
// then we are in loop. We may have to find another way to implement udivmod_inner!().
// #1.2 may break the existing usage.
// #1.1 seems the simplest solution today.
// The IA32 -gnu calling convention is same as the one defined in UEFI specification.
// It uses cdecl, EAX/ECX/EDX as volatile register, and EAX/EDX as return value.
// We also checked the LLVM X86TargetLowering, the differences between -gnu and -msvc
// is fmodf(f32), longjmp() and TLS. None of them impacts the UEFI code.
// As a result, we choose -gnu for i686 version before those intrisics are implemented in
// compiler-builtins. After compiler-builtins implements all required intrinsics, we may
// remove -gnu and use the default one.
Ok(Target {
llvm_target: "i686-unknown-windows-gnu".to_string(),
target_endian: "little".to_string(),
target_pointer_width: "32".to_string(),
target_c_int_width: "32".to_string(),
data_layout: "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32".to_string(),
target_os: "uefi".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
arch: "x86".to_string(),
linker_flavor: LinkerFlavor::Lld(LldFlavor::Link),

options: base,
})
}
1 change: 1 addition & 0 deletions src/librustc_target/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ supported_targets! {
("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx),

("x86_64-unknown-uefi", x86_64_unknown_uefi),
("i686-unknown-uefi", i686_unknown_uefi),

("nvptx64-nvidia-cuda", nvptx64_nvidia_cuda),

Expand Down