From bec21b7384d05662938b461baa6c08f1dac1289c Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:02:11 +0800 Subject: [PATCH 1/9] Create aarch64_unknown_uefi.rs --- compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs new file mode 100644 index 0000000000000..8b137891791fe --- /dev/null +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -0,0 +1 @@ + From 210b2af35ac63fd278ee1b1bf86d161bd271e7de Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:03:44 +0800 Subject: [PATCH 2/9] Update mod.rs --- compiler/rustc_target/src/spec/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 4bffd6e8ddd35..db56b1366f8f2 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -877,6 +877,7 @@ supported_targets! { ("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx), + ("aarch64-unknown-uefi", aarch64-unknown-uefi), ("x86_64-unknown-uefi", x86_64_unknown_uefi), ("i686-unknown-uefi", i686_unknown_uefi), From b648ac62ca35738a7ef02cefad87f323b7d90dca Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:09:54 +0800 Subject: [PATCH 3/9] Update aarch64_unknown_uefi.rs --- .../src/spec/aarch64_unknown_uefi.rs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs index 8b137891791fe..c6112c98710cc 100644 --- a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -1 +1,22 @@ +// Copyright 2021 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// This defines the aarch64 target for UEFI systems as described in the UEFI specification. See the +// uefi-base module for generic UEFI options. On aarch64 systems (mostly called "arm64" in the spec) +// UEFI systems always run in long-mode, have the interrupt-controller pre-configured and force a +// single-CPU execution. +// The win64 ABI is used. It differs from the sysv64 ABI, so we must use a windows target with +// LLVM. "aarch64-unknown-windows" is used to get the minimal subset of windows-specific features. + +use spec::{LinkerFlavor, LldFlavor, Target, TargetResult}; + +pub fn target() -> TargetResult { + +} From 9a611987ee1157305a846e45905c2e6c792e911d Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:18:03 +0800 Subject: [PATCH 4/9] Update aarch64_unknown_uefi.rs --- .../src/spec/aarch64_unknown_uefi.rs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs index c6112c98710cc..ee6d3c291cf88 100644 --- a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -18,5 +18,40 @@ use spec::{LinkerFlavor, LldFlavor, Target, TargetResult}; pub fn target() -> TargetResult { + let mut base = super::uefi_base::opts(); + base.cpu = "aarch64"; + base.max_atomic_width = Some(64); + // We disable MMX and SSE for now. UEFI does not prevent these from being used, but there have + // been reports to GRUB that some firmware does not initialize the FP exception handlers + // properly. Therefore, using FP coprocessors will end you up at random memory locations when + // you throw FP exceptions. + // To be safe, we disable them for now and force soft-float. This can be revisited when we + // have more test coverage. Disabling FP served GRUB well so far, so it should be good for us + // as well. + base.features = "-mmx,-sse,+soft-float".to_string(); + + // UEFI systems run without a host OS, hence we cannot assume any code locality. We must tell + // LLVM to expect code to reference any address in the address-space. The "large" code-model + // places no locality-restrictions, so it fits well here. + base.code_model = Some("large".to_string()); + + // 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; + + Ok(Target { + llvm_target: "aarch64-unknown-windows".to_string(), + target_endian: "little".to_string(), + target_pointer_width: "64".to_string(), + target_c_int_width: "32".to_string(), + data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(), + target_os: "uefi".to_string(), + target_env: "".to_string(), + target_vendor: "unknown".to_string(), + arch: "x86_64".to_string(), + linker_flavor: LinkerFlavor::Lld(LldFlavor::Link), + + options: base, + }) } From f414502d46ae0b69f2cfabedb71136f240173650 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:24:15 +0800 Subject: [PATCH 5/9] Update aarch64_unknown_uefi.rs From 7c5508f62fbbebe305d4152fb85fc0ecd146e951 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:27:14 +0800 Subject: [PATCH 6/9] Update aarch64_unknown_uefi.rs --- .../src/spec/aarch64_unknown_uefi.rs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs index ee6d3c291cf88..a443c301bce4e 100644 --- a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -15,9 +15,9 @@ // The win64 ABI is used. It differs from the sysv64 ABI, so we must use a windows target with // LLVM. "aarch64-unknown-windows" is used to get the minimal subset of windows-specific features. -use spec::{LinkerFlavor, LldFlavor, Target, TargetResult}; +use crate::spec::{CodeModel, Target}; -pub fn target() -> TargetResult { +pub fn target() -> Target { let mut base = super::uefi_base::opts(); base.cpu = "aarch64"; base.max_atomic_width = Some(64); @@ -34,23 +34,13 @@ pub fn target() -> TargetResult { // UEFI systems run without a host OS, hence we cannot assume any code locality. We must tell // LLVM to expect code to reference any address in the address-space. The "large" code-model // places no locality-restrictions, so it fits well here. - base.code_model = Some("large".to_string()); - - // 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; + base.code_model = Some(CodeModel::Large); Ok(Target { llvm_target: "aarch64-unknown-windows".to_string(), - target_endian: "little".to_string(), - target_pointer_width: "64".to_string(), - target_c_int_width: "32".to_string(), + pointer_width: 64, data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".to_string(), - target_os: "uefi".to_string(), - target_env: "".to_string(), - target_vendor: "unknown".to_string(), - arch: "x86_64".to_string(), - linker_flavor: LinkerFlavor::Lld(LldFlavor::Link), + arch: "aarch64".to_string(), options: base, }) From dd0f8f9f7a3fdcbcc99eef9c44e46b65db4494c8 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:35:19 +0800 Subject: [PATCH 7/9] Update aarch64_unknown_uefi.rs --- .../rustc_target/src/spec/aarch64_unknown_uefi.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs index a443c301bce4e..7d6784717a832 100644 --- a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -15,10 +15,10 @@ // The win64 ABI is used. It differs from the sysv64 ABI, so we must use a windows target with // LLVM. "aarch64-unknown-windows" is used to get the minimal subset of windows-specific features. -use crate::spec::{CodeModel, Target}; +use crate::spec::{CodeModel, LinkerFlavor, LldFlavor, Target}; pub fn target() -> Target { - let mut base = super::uefi_base::opts(); + let mut base = super::uefi_msvc_base::opts(); base.cpu = "aarch64"; base.max_atomic_width = Some(64); @@ -31,6 +31,14 @@ pub fn target() -> Target { // as well. base.features = "-mmx,-sse,+soft-float".to_string(); + let pre_link_args_msvc = vec!["/machine:arm64"]; + + base.pre_link_args.get_mut(&LinkerFlavor::Msvc).unwrap().extend(pre_link_args_msvc.clone()); + base.pre_link_args + .get_mut(&LinkerFlavor::Lld(LldFlavor::Link)) + .unwrap() + .extend(pre_link_args_msvc); + // UEFI systems run without a host OS, hence we cannot assume any code locality. We must tell // LLVM to expect code to reference any address in the address-space. The "large" code-model // places no locality-restrictions, so it fits well here. From 06e90105875b9898b98b14d2f6c7e9c29f759ab6 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 11:37:13 +0800 Subject: [PATCH 8/9] Fix target name --- compiler/rustc_target/src/spec/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index db56b1366f8f2..de49eb4b1732b 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -877,7 +877,7 @@ supported_targets! { ("x86_64-fortanix-unknown-sgx", x86_64_fortanix_unknown_sgx), - ("aarch64-unknown-uefi", aarch64-unknown-uefi), + ("aarch64-unknown-uefi", aarch64_unknown_uefi), ("x86_64-unknown-uefi", x86_64_unknown_uefi), ("i686-unknown-uefi", i686_unknown_uefi), From b53b1284abed6c36a3cff498a2368070b9761d49 Mon Sep 17 00:00:00 2001 From: HTG-YT <39023054+HTG-YT@users.noreply.github.com> Date: Sun, 16 May 2021 13:12:30 +0800 Subject: [PATCH 9/9] Fix vec --- compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs index 7d6784717a832..45ec8234e9b14 100644 --- a/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs +++ b/compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs @@ -31,7 +31,7 @@ pub fn target() -> Target { // as well. base.features = "-mmx,-sse,+soft-float".to_string(); - let pre_link_args_msvc = vec!["/machine:arm64"]; + let pre_link_args_msvc = vec!["/machine:arm64".to_string()]; base.pre_link_args.get_mut(&LinkerFlavor::Msvc).unwrap().extend(pre_link_args_msvc.clone()); base.pre_link_args