Guy
Guy

Reputation: 21

Rust Bare-Metal Cross-Compilation for AArch64: can't find crate for `core`

I am trying to cross-compile an example rust code as library for bare-metal AArch64 on Linux (KDE-Neon). Unfortunately it does not work. This is my example rust code (lib.rs):

#![no_std]

#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
    a / 2
}

According to [1] I first installed rustup with:

sudo snap install rustup --classic

Afterwards, I followed [2] and ran:

rustup toolchain list
rustup install stable
rustup default stable

Then I followed [1] and [3] and ran:

rustup target add aarch64-unknown-none

However when I try to compile afterwards, I doesn't work, neither with rustc nor with cargo:

rustc:

rustc --crate-type=lib lib.rs --target=aarch64-unknown-none
error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-unknown-none` target may not be installed

error: aborting due to previous error

cargo:

Cargo.toml:

[package]
name = "rust_baremetal_lib"
version = "0.1.0"
edition = "2018"

[lib]
name = "rust_baremetal_lib"
path = "src/lib.rs"
crate-type = ["staticlib"]

[dependencies]
cargo build --lib --target=aarch64-unknown-none
   Compiling rust_baremetal_lib v0.1.0 (/home/kilian/code/rust_link/rust_baremetal_lib)
error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-unknown-none` target may not be installed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `rust_baremetal_lib`

To learn more, run the command again with --verbose.

To me it looks like rustc and cargo cannot find the core library, although it should be installed, as seen when running rustc --print:

rustc --print target-list|grep arch64-unknown-none
aarch64-unknown-none
aarch64-unknown-none-softfloat

I already looked on the internet but didn't find any clues unfortunately. I hope someone can help me find the issue!

[1] https://rust-lang.github.io/rustup/cross-compilation.html

[2] No default toolchain configured after installing rustup

[3] https://doc.rust-lang.org/nightly/rustc/platform-support.html

Upvotes: 2

Views: 4371

Answers (1)

Guy
Guy

Reputation: 21

The problem seemed to result from a broken rust installation. I removed all packages related to rust that I could found via apt and snap. Afterwards I reinstalled rust via the recommended way [1]. Then I again ran:

rustup target add aarch64-unknown-none

Afterwards Cargo complained, that a "panic handler" was missing in my example code, so I inserted one, following [2]. My example code now looks like this:

#![no_std]

use core::{
    panic::PanicInfo,
};

#[no_mangle]
pub extern "C" fn double_value (a : u32) -> u32
{
    a * 2
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {
        continue;
    }
}

Finally I can now cross-compile this example to an AArch64 bare metal static library with:

cargo build --lib --target=aarch64-unknown-none

[1] https://www.rust-lang.org/tools/install

[2] https://interrupt.memfault.com/blog/zero-to-main-rust-1

Upvotes: 0

Related Questions