Skip to content

Commit 84e1fbc

Browse files
committed
Add no_std AArch64 support for the QNX Neutrino (nto) 7.1 RTOS
This change allows to compile no_std applications for the QNX Neutrino realtime operating system for ARM 64 bit CPUs. Tested with QNX Neutrino 7.1.
1 parent b7b7f27 commit 84e1fbc

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::nto_qnx_base;
2+
use crate::spec::{Cc, LinkerFlavor, Lld, Target, TargetOptions};
3+
4+
pub fn target() -> Target {
5+
Target {
6+
llvm_target: "aarch64-unknown-unknown".into(),
7+
pointer_width: 64,
8+
// from: https://llvm.org/docs/LangRef.html#data-layout
9+
// e = little endian
10+
// m:e = ELF mangling: Private symbols get a .L prefix
11+
// i8:8:32 = 8-bit-integer, minimum_alignment=8, preferred_alignment=32
12+
// i16:16:32 = 16-bit-integer, minimum_alignment=16, preferred_alignment=32
13+
// i64:64 = 64-bit-integer, minimum_alignment=64, preferred_alignment=64
14+
// i128:128 = 128-bit-integer, minimum_alignment=128, preferred_alignment=128
15+
// n32:64 = 32 and 64 are native integer widths; Elements of this set are considered to support most general arithmetic operations efficiently.
16+
// S128 = 128 bits are the natural alignment of the stack in bits.
17+
data_layout: "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128".into(),
18+
arch: "aarch64".into(),
19+
options: TargetOptions {
20+
max_atomic_width: Some(128),
21+
pre_link_args: TargetOptions::link_args(
22+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
23+
&["-Vgcc_ntoaarch64le_cxx"],
24+
),
25+
..nto_qnx_base::opts()
26+
},
27+
}
28+
}

compiler/rustc_target/src/spec/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ mod linux_musl_base;
7474
mod linux_uclibc_base;
7575
mod msvc_base;
7676
mod netbsd_base;
77+
mod nto_qnx_base;
7778
mod openbsd_base;
7879
mod redox_base;
7980
mod solaris_base;
@@ -1242,6 +1243,9 @@ supported_targets! {
12421243
("x86_64-unknown-none", x86_64_unknown_none),
12431244

12441245
("mips64-openwrt-linux-musl", mips64_openwrt_linux_musl),
1246+
1247+
("aarch64-unknown-nto-qnx7.1.0", aarch64_unknown_nto_qnx_710),
1248+
("x86_64-pc-nto-qnx7.1.0", x86_64_pc_nto_qnx710),
12451249
}
12461250

12471251
/// Cow-Vec-Str: Cow<'static, [Cow<'static, str>]>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::spec::{cvs, RelroLevel, TargetOptions};
2+
3+
pub fn opts() -> TargetOptions {
4+
TargetOptions {
5+
crt_static_respected: true,
6+
dynamic_linking: true,
7+
env: "nto71".into(),
8+
executables: true,
9+
families: cvs!["unix"],
10+
has_rpath: true,
11+
has_thread_local: false,
12+
linker: Some("qcc".into()),
13+
os: "nto".into(),
14+
position_independent_executables: true,
15+
static_position_independent_executables: true,
16+
relro_level: RelroLevel::Full,
17+
..Default::default()
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use super::nto_qnx_base;
2+
use crate::spec::{Cc, LinkerFlavor, Lld, Target, TargetOptions};
3+
4+
pub fn target() -> Target {
5+
Target {
6+
llvm_target: "x86_64-pc-unknown".into(),
7+
pointer_width: 64,
8+
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
9+
.into(),
10+
arch: "x86_64".into(),
11+
options: TargetOptions {
12+
cpu: "x86-64".into(),
13+
max_atomic_width: Some(64),
14+
pre_link_args: TargetOptions::link_args(
15+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
16+
&["-Vgcc_ntox86_64_cxx"],
17+
),
18+
..nto_qnx_base::opts()
19+
},
20+
}
21+
}

src/doc/rustc/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [nvptx64-nvidia-cuda](platform-support/nvptx64-nvidia-cuda.md)
3434
- [riscv32imac-unknown-xous-elf](platform-support/riscv32imac-unknown-xous-elf.md)
3535
- [*-pc-windows-gnullvm](platform-support/pc-windows-gnullvm.md)
36+
- [\*-nto-qnx-\*](platform-support/nto-qnx.md)
3637
- [*-unknown-openbsd](platform-support/openbsd.md)
3738
- [\*-unknown-uefi](platform-support/unknown-uefi.md)
3839
- [wasm64-unknown-unknown](platform-support/wasm64-unknown-unknown.md)

src/doc/rustc/src/platform-support.md

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ target | std | host | notes
214214
[`aarch64-kmc-solid_asp3`](platform-support/kmc-solid.md) | ✓ | | ARM64 SOLID with TOPPERS/ASP3
215215
[`aarch64-nintendo-switch-freestanding`](platform-support/aarch64-nintendo-switch-freestanding.md) | * | | ARM64 Nintendo Switch, Horizon
216216
[`aarch64-pc-windows-gnullvm`](platform-support/pc-windows-gnullvm.md) | ✓ | ✓ |
217+
[`aarch64-unknown-nto-qnx7.1.0`](platform-support/nto-qnx.md) | ? | | ARM64 QNX Neutrino 7.1 RTOS |
217218
`aarch64-unknown-freebsd` | ✓ | ✓ | ARM64 FreeBSD
218219
`aarch64-unknown-hermit` | ✓ | | ARM64 HermitCore
219220
`aarch64-unknown-linux-gnu_ilp32` | ✓ | ✓ | ARM64 Linux (ILP32 ABI)
@@ -303,6 +304,7 @@ target | std | host | notes
303304
`x86_64-apple-ios-macabi` | ✓ | | Apple Catalyst on x86_64
304305
`x86_64-apple-tvos` | * | | x86 64-bit tvOS
305306
[`x86_64-apple-watchos-sim`](platform-support/apple-watchos.md) | ✓ | | x86 64-bit Apple WatchOS simulator
307+
[`x86_64-pc-nto-qnx7.1.0`](platform-support/nto-qnx.md) | ? | | x86 64-bit QNX Neutrino 7.1 RTOS |
306308
[`x86_64-pc-windows-gnullvm`](platform-support/pc-windows-gnullvm.md) | ✓ | ✓ |
307309
`x86_64-pc-windows-msvc` | * | | 64-bit Windows XP support
308310
`x86_64-sun-solaris` | ? | | Deprecated target for 64-bit Solaris 10/11, illumos
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# nto-qnx
2+
3+
**Tier: 3**
4+
5+
[BlackBerry® QNX®][BlackBerry] Neutrino (nto) Real-time operating system.
6+
The support has been implemented jointly by [Elektrobit Automotive GmbH][Elektrobit]
7+
and [BlackBerry][BlackBerry].
8+
9+
[BlackBerry]: https://blackberry.qnx.com
10+
[Elektrobit]: https://www.elektrobit.com
11+
12+
## Target maintainers
13+
14+
- Florian Bartels, `[email protected]`, https://github.com/flba-eb
15+
16+
## Requirements
17+
18+
Currently, only cross-compilation for QNX Neutrino on AArch64 and x86_64 are supported (little endian).
19+
Adding other architectures that are supported by QNX Neutrino is possible.
20+
21+
The standard library does not yet support QNX Neutrino. Therefore, only `no_std` code can
22+
be compiled.
23+
24+
`core` and `alloc` (with default allocator) are supported.
25+
26+
Applications must link against `libc.so` (see example). This is required because applications
27+
always link against the `crt` library and `crt` depends on `libc.so`.
28+
29+
The correct version of `qcc` must be available by setting the `$PATH` variable (e.g. by sourcing `qnxsdp-env.sh` of the
30+
QNX Neutrino toolchain).
31+
32+
### Small example application
33+
34+
```rust
35+
#![no_std]
36+
#![no_main]
37+
#![feature(lang_items)]
38+
39+
// We must always link against libc, even if no external functions are used
40+
// "extern C" - Block can be empty but must be present
41+
#[link(name = "c")]
42+
extern "C" {
43+
pub fn printf(format: *const core::ffi::c_char, ...) -> core::ffi::c_int;
44+
}
45+
46+
#[no_mangle]
47+
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
48+
const HELLO: &'static str = "Hello World, the answer is %d\n\0";
49+
unsafe {
50+
printf(HELLO.as_ptr() as *const _, 42);
51+
}
52+
0
53+
}
54+
55+
use core::panic::PanicInfo;
56+
57+
#[panic_handler]
58+
fn panic(_panic: &PanicInfo<'_>) -> ! {
59+
loop {}
60+
}
61+
62+
#[lang = "eh_personality"]
63+
#[no_mangle]
64+
pub extern "C" fn rust_eh_personality() {}
65+
```
66+
67+
The QNX Neutrino support of Rust has been tested with QNX Neutrino 7.1.
68+
69+
There are no further known requirements.
70+
71+
## Conditional compilation
72+
73+
For conditional compilation, following QNX Neutrino specific attributes are defined:
74+
75+
- `target_os` = `"nto"`
76+
- `target_env` = `"nto71"` (for QNX Neutrino 7.1)
77+
78+
## Building the target
79+
80+
1. Create a `config.toml`
81+
82+
Example content:
83+
84+
```toml
85+
profile = "compiler"
86+
changelog-seen = 2
87+
```
88+
89+
2. Compile the Rust toolchain for an `x86_64-unknown-linux-gnu` host (for both `aarch64` and `x86_64` targets)
90+
91+
Run the following:
92+
93+
```bash
94+
env \
95+
CC_aarch64-unknown-nto-qnx7.1.0="qcc" \
96+
CFLAGS_aarch64-unknown-nto-qnx7.1.0="-Vgcc_ntoaarch64le_cxx" \
97+
CXX_aarch64-unknown-nto-qnx7.1.0="qcc" \
98+
AR_aarch64_unknown_nto_qnx7.1.0="ntoaarch64-ar" \
99+
CC_x86_64-pc-nto-qnx7.1.0="qcc" \
100+
CFLAGS_x86_64-pc-nto-qnx7.1.0="-Vgcc_ntox86_64_cxx" \
101+
CXX_x86_64-pc-nto-qnx7.1.0="qcc" \
102+
AR_x86_64_pc_nto_qnx7.1.0="ntox86_64-ar" \
103+
./x.py build --target aarch64-unknown-nto-qnx7.1.0 --target x86_64-pc-nto-qnx7.1.0 --target x86_64-unknown-linux-gnu rustc library/core library/alloc/
104+
```
105+
106+
## Building Rust programs
107+
108+
Rust does not yet ship pre-compiled artifacts for this target. To compile for this target, you must either build Rust with the target enabled (see "Building the target" above), or build your own copy of `core` by using
109+
`build-std` or similar.
110+
111+
## Testing
112+
113+
Compiled executables can directly be run on QNX Neutrino.
114+
115+
## Cross-compilation toolchains and C code
116+
117+
Compiling C code requires the same environment variables to be set as compiling the Rust toolchain (see above), to ensure `qcc` is used with proper arguments. To ensure compatibility, do not specify any further arguments that for example change calling conventions or memory layout.

0 commit comments

Comments
 (0)