From 8dbb580d504259dfdc86b7f46f1971e8fd0d6f9a Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Mon, 27 Jan 2025 21:53:11 +0800 Subject: [PATCH] Add X86 target support for NuttX * Implement `i686-unknown-nuttx` and `x86_64-unknown-nuttx` target definitions * Integrate new targets into the platform support documentation * Update tests to include new target revisions This change introduces support for 32-bit and 64-bit x86 architectures on the NuttX operating system, enhancing the range of platforms Rust can target. The targets are defined as tier 3, indicating that they are supported but not automatically tested by the Rust project. The `i686-unknown-nuttx` target uses the `pentium4` CPU and 32-bit pointers, while the `x86_64-unknown-nuttx` target uses the `x86-64` CPU and 64-bit pointers. Both targets disable dynamic linking and use inline stack probes for improved performance and reliability in a bare-metal environment. Additionally, this commit updates the platform support documentation to list the new targets and includes them in the test suite to ensure that they build correctly. Signed-off-by: Huang Qi --- compiler/rustc_target/src/spec/mod.rs | 2 + .../src/spec/targets/i686_unknown_nuttx.rs | 37 +++++++++++++++++ .../src/spec/targets/x86_64_unknown_nuttx.rs | 41 +++++++++++++++++++ src/doc/rustc/src/platform-support.md | 2 + src/doc/rustc/src/platform-support/nuttx.md | 2 + tests/assembly/targets/targets-elf.rs | 6 +++ 6 files changed, 90 insertions(+) create mode 100644 compiler/rustc_target/src/spec/targets/i686_unknown_nuttx.rs create mode 100644 compiler/rustc_target/src/spec/targets/x86_64_unknown_nuttx.rs diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index bcd2aff54bb11..2a061d263d425 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1977,6 +1977,8 @@ supported_targets! { ("x86_64-unknown-linux-none", x86_64_unknown_linux_none), + ("i686-unknown-nuttx", i686_unknown_nuttx), + ("x86_64-unknown-nuttx", x86_64_unknown_nuttx), ("thumbv6m-nuttx-eabi", thumbv6m_nuttx_eabi), ("thumbv7a-nuttx-eabi", thumbv7a_nuttx_eabi), ("thumbv7a-nuttx-eabihf", thumbv7a_nuttx_eabihf), diff --git a/compiler/rustc_target/src/spec/targets/i686_unknown_nuttx.rs b/compiler/rustc_target/src/spec/targets/i686_unknown_nuttx.rs new file mode 100644 index 0000000000000..c83a9859f0758 --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/i686_unknown_nuttx.rs @@ -0,0 +1,37 @@ +use crate::spec::{Cc, LinkerFlavor, Lld, RelocModel, StackProbeType, Target, TargetOptions, cvs}; + +pub(crate) fn target() -> Target { + let mut base = TargetOptions { + os: "nuttx".into(), + dynamic_linking: false, + families: cvs!["unix"], + no_default_libraries: true, + has_rpath: false, + position_independent_executables: false, + relocation_model: RelocModel::Static, + relro_level: crate::spec::RelroLevel::Full, + has_thread_local: true, + use_ctors_section: true, + ..Default::default() + }; + base.cpu = "pentium4".into(); + base.max_atomic_width = Some(64); + base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32"]); + base.stack_probes = StackProbeType::Inline; + + Target { + llvm_target: "i686-unknown-nuttx".into(), + metadata: crate::spec::TargetMetadata { + description: Some("NuttX/x86".into()), + tier: Some(3), + host_tools: Some(false), + std: Some(true), + }, + pointer_width: 32, + data_layout: "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-\ + i128:128-f64:32:64-f80:32-n8:16:32-S128" + .into(), + arch: "x86".into(), + options: base, + } +} diff --git a/compiler/rustc_target/src/spec/targets/x86_64_unknown_nuttx.rs b/compiler/rustc_target/src/spec/targets/x86_64_unknown_nuttx.rs new file mode 100644 index 0000000000000..5273abe83aec7 --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/x86_64_unknown_nuttx.rs @@ -0,0 +1,41 @@ +// Generic x86-64 target for bare-metal code - Floating point disabled +// +// Can be used in conjunction with the `target-feature` and +// `target-cpu` compiler flags to opt-in more hardware-specific +// features. + +use crate::spec::{RelocModel, StackProbeType, Target, TargetOptions, cvs}; + +pub(crate) fn target() -> Target { + let mut base = TargetOptions { + os: "nuttx".into(), + dynamic_linking: false, + families: cvs!["unix"], + no_default_libraries: true, + has_rpath: false, + position_independent_executables: false, + relocation_model: RelocModel::Static, + relro_level: crate::spec::RelroLevel::Full, + has_thread_local: true, + use_ctors_section: true, + ..Default::default() + }; + base.cpu = "x86-64".into(); + base.max_atomic_width = Some(64); + base.stack_probes = StackProbeType::Inline; + + Target { + llvm_target: "x86_64-unknown-nuttx".into(), + metadata: crate::spec::TargetMetadata { + description: Some("NuttX/x86_64".into()), + tier: Some(3), + host_tools: Some(false), + std: Some(true), + }, + pointer_width: 64, + data_layout: + "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(), + arch: "x86_64".into(), + options: base, + } +} diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index 8227dfa043e3a..d9ef356170995 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -314,6 +314,7 @@ target | std | host | notes `i686-unknown-haiku` | ✓ | ✓ | 32-bit Haiku [^x86_32-floats-return-ABI] [`i686-unknown-hurd-gnu`](platform-support/hurd.md) | ✓ | ✓ | 32-bit GNU/Hurd [^x86_32-floats-return-ABI] [`i686-unknown-netbsd`](platform-support/netbsd.md) | ✓ | ✓ | NetBSD/i386 with SSE2 [^x86_32-floats-return-ABI] +[`i686-unknown-nuttx`](platform-support/nuttx.md) | ✓ | | 32-bit x86 with NuttX [`i686-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | 32-bit OpenBSD [^x86_32-floats-return-ABI] [`i686-unknown-redox`](platform-support/redox.md) | ✓ | | i686 Redox OS `i686-uwp-windows-gnu` | ✓ | | [^x86_32-floats-return-ABI] @@ -417,6 +418,7 @@ target | std | host | notes `x86_64-unknown-l4re-uclibc` | ? | | [`x86_64-unknown-linux-none`](platform-support/x86_64-unknown-linux-none.md) | * | | 64-bit Linux with no libc [`x86_64-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | 64-bit OpenBSD +[`x86_64-unknown-nuttx`](platform-support/nuttx.md) | ✓ | | 64-bit x86 with NuttX [`x86_64-unknown-trusty`](platform-support/trusty.md) | ? | | `x86_64-uwp-windows-gnu` | ✓ | | [`x86_64-uwp-windows-msvc`](platform-support/uwp-windows-msvc.md) | ✓ | | diff --git a/src/doc/rustc/src/platform-support/nuttx.md b/src/doc/rustc/src/platform-support/nuttx.md index f76fe0887b5dd..126abcfd133a1 100644 --- a/src/doc/rustc/src/platform-support/nuttx.md +++ b/src/doc/rustc/src/platform-support/nuttx.md @@ -20,6 +20,8 @@ The target name follow this format: `ARCH[-VENDOR]-nuttx-ABI`, where `ARCH` is t The following target names are defined: +- `i686-unknown-nuttx` +- `x86_64-unknown-nuttx` - `aarch64-unknown-nuttx` - `armv7a-nuttx-eabi` - `armv7a-nuttx-eabihf` diff --git a/tests/assembly/targets/targets-elf.rs b/tests/assembly/targets/targets-elf.rs index 0ff886653a477..13a1dab548679 100644 --- a/tests/assembly/targets/targets-elf.rs +++ b/tests/assembly/targets/targets-elf.rs @@ -249,6 +249,9 @@ //@ revisions: i686_unknown_netbsd //@ [i686_unknown_netbsd] compile-flags: --target i686-unknown-netbsd //@ [i686_unknown_netbsd] needs-llvm-components: x86 +//@ revisions: i686_unknown_nuttx +//@ [i686_unknown_nuttx] compile-flags: --target i686-unknown-nuttx +//@ [i686_unknown_nuttx] needs-llvm-components: x86 //@ revisions: i686_unknown_openbsd //@ [i686_unknown_openbsd] compile-flags: --target i686-unknown-openbsd //@ [i686_unknown_openbsd] needs-llvm-components: x86 @@ -627,6 +630,9 @@ //@ revisions: x86_64_unknown_none //@ [x86_64_unknown_none] compile-flags: --target x86_64-unknown-none //@ [x86_64_unknown_none] needs-llvm-components: x86 +//@ revisions: x86_64_unknown_nuttx +//@ [x86_64_unknown_nuttx] compile-flags: --target x86_64-unknown-nuttx +//@ [x86_64_unknown_nuttx] needs-llvm-components: x86 //@ revisions: x86_64_unknown_openbsd //@ [x86_64_unknown_openbsd] compile-flags: --target x86_64-unknown-openbsd //@ [x86_64_unknown_openbsd] needs-llvm-components: x86