Skip to content

Commit 1fe0a85

Browse files
committed
Add rv64IM
1 parent 693f365 commit 1fe0a85

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

compiler/rustc_target/src/spec/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,7 @@ supported_targets! {
16861686
("riscv32imac-unknown-xous-elf", riscv32imac_unknown_xous_elf),
16871687
("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu),
16881688
("riscv32gc-unknown-linux-musl", riscv32gc_unknown_linux_musl),
1689+
("riscv64im-unknown-none-elf", riscv64im_unknown_none_elf),
16891690
("riscv64imac-unknown-none-elf", riscv64imac_unknown_none_elf),
16901691
("riscv64gc-unknown-none-elf", riscv64gc_unknown_none_elf),
16911692
("riscv64gc-unknown-linux-gnu", riscv64gc_unknown_linux_gnu),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use crate::spec::{
2+
Arch, Cc, CodeModel, LinkerFlavor, Lld, PanicStrategy, RelocModel, Target, TargetMetadata,
3+
TargetOptions,
4+
};
5+
6+
pub(crate) fn target() -> Target {
7+
Target {
8+
data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".into(),
9+
llvm_target: "riscv64".into(),
10+
metadata: TargetMetadata {
11+
description: Some("Bare RISC-V (RV64IM ISA)".into()),
12+
tier: Some(3),
13+
host_tools: Some(false),
14+
std: Some(false),
15+
},
16+
pointer_width: 64,
17+
arch: Arch::RiscV64,
18+
19+
options: TargetOptions {
20+
linker_flavor: LinkerFlavor::Gnu(Cc::No, Lld::Yes),
21+
linker: Some("rust-lld".into()),
22+
cpu: "generic-rv64".into(),
23+
max_atomic_width: Some(64),
24+
atomic_cas: false,
25+
features: "+m,+forced-atomics".into(),
26+
llvm_abiname: "lp64".into(),
27+
panic_strategy: PanicStrategy::Abort,
28+
relocation_model: RelocModel::Static,
29+
code_model: Some(CodeModel::Medium),
30+
emit_debug_gdb_scripts: false,
31+
eh_frame_header: false,
32+
..Default::default()
33+
},
34+
}
35+
}

src/doc/rustc/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
- [riscv32i\*-unknown-none-elf](platform-support/riscv32-unknown-none-elf.md)
113113
- [riscv32im-risc0-zkvm-elf](platform-support/riscv32im-risc0-zkvm-elf.md)
114114
- [riscv32imac-unknown-xous-elf](platform-support/riscv32imac-unknown-xous-elf.md)
115+
- [riscv64im-unknown-none-elf](platform-support/riscv64im-unknown-none-elf.md)
115116
- [riscv64gc-unknown-linux-gnu](platform-support/riscv64gc-unknown-linux-gnu.md)
116117
- [riscv64gc-unknown-linux-musl](platform-support/riscv64gc-unknown-linux-musl.md)
117118
- [riscv64a23-unknown-linux-gnu](platform-support/riscv64a23-unknown-linux-gnu.md)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ target | std | notes
184184
[`riscv32imc-unknown-none-elf`](platform-support/riscv32-unknown-none-elf.md) | * | Bare RISC-V (RV32IMC ISA)
185185
[`riscv64gc-unknown-linux-musl`](platform-support/riscv64gc-unknown-linux-musl.md) | ✓ |RISC-V Linux (kernel 4.20+, musl 1.2.5)
186186
`riscv64gc-unknown-none-elf` | * | Bare RISC-V (RV64IMAFDC ISA)
187+
[`riscv64im-unknown-none-elf`](platform-support/riscv64im-unknown-none-elf.md) | * | Bare RISC-V (RV64IM ISA)
187188
`riscv64imac-unknown-none-elf` | * | Bare RISC-V (RV64IMAC ISA)
188189
`sparc64-unknown-linux-gnu` | ✓ | SPARC Linux (kernel 4.4+, glibc 2.23)
189190
[`thumbv6m-none-eabi`](platform-support/thumbv6m-none-eabi.md) | * | Bare Armv6-M
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# `riscv64im-unknown-none-elf`
2+
3+
**Tier: 3**
4+
5+
Bare-metal target for RISC-V CPUs with the RV64IM ISA.
6+
7+
## Target maintainers
8+
9+
* Rust Embedded Working Group, [RISC-V team](https://github.com/rust-embedded/wg#the-risc-v-team)
10+
11+
## Requirements
12+
13+
This target is cross-compiled and uses static linking. The target supports `core` and `alloc`, but not `std`.
14+
15+
The target does not support atomic compare-and-swap operations, as the RV64IM ISA lacks the "A" (Atomics) extension. Atomic operations are emulated using the `+forced-atomics` feature.
16+
17+
No external toolchain is required and the default `rust-lld` linker works, but you must specify a linker script. The [`riscv-rt`] crate provides suitable linker scripts. The [`riscv-rust-quickstart`] repository gives examples of RISC-V bare-metal projects.
18+
19+
[`riscv-rt`]: https://crates.io/crates/riscv-rt
20+
[`riscv-rust-quickstart`]: https://github.com/riscv-rust/riscv-rust-quickstart
21+
22+
## Building the target
23+
24+
This target is included in Rust and can be installed via `rustup`:
25+
26+
```bash
27+
rustup target add riscv64im-unknown-none-elf
28+
```
29+
30+
## Building Rust programs
31+
32+
Build using the standard Cargo workflow:
33+
34+
```bash
35+
cargo build --target riscv64im-unknown-none-elf
36+
```
37+
38+
You will need to provide a linker script. The [`riscv-rt`] crate handles this automatically when used as a dependency.
39+
40+
## Testing
41+
42+
This is a cross-compiled `no-std` target, which must be run either in a simulator or by programming onto suitable hardware. It is not possible to run the Rust test-suite on this target.
43+
44+
You can test the target in QEMU with:
45+
46+
```bash
47+
qemu-system-riscv64 -machine virt -cpu rv64,a=false,c=false -nographic -semihosting -kernel your-binary
48+
```
49+
50+
Note: You must explicitly disable the 'a' (atomics) and 'c' (compressed) extensions when using QEMU to accurately emulate an RV64IM-only CPU.
51+
52+
## Cross-compilation toolchains and C code
53+
54+
This target supports C code. If interlinking with C or C++, you may need to use `riscv64-unknown-elf-gcc` with the appropriate `-march=rv64im -mabi=lp64` flags as a linker instead of `rust-lld`.

tests/assembly-llvm/targets/targets-elf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@
520520
//@ revisions: riscv64gc_unknown_redox
521521
//@ [riscv64gc_unknown_redox] compile-flags: --target riscv64gc-unknown-redox
522522
//@ [riscv64gc_unknown_redox] needs-llvm-components: riscv
523+
//@ revisions: riscv64im_unknown_none_elf
524+
//@ [riscv64im_unknown_none_elf] compile-flags: --target riscv64im-unknown-none-elf
525+
//@ [riscv64im_unknown_none_elf] needs-llvm-components: riscv
523526
//@ revisions: riscv64imac_unknown_none_elf
524527
//@ [riscv64imac_unknown_none_elf] compile-flags: --target riscv64imac-unknown-none-elf
525528
//@ [riscv64imac_unknown_none_elf] needs-llvm-components: riscv

0 commit comments

Comments
 (0)