Hey,
The overall goal is to compile a simple kernel for my raspberry pi zero which simply blinks a LED. I have created a C version and I am compiling it with
arm-none-eabi-gcc -O -Wall -nostdlib -nostartfiles -ffreestanding -march=armv6 -std=gnu99 -c -o blink.o blink.c
I am trying to mirror this in Rust and therefor created a very simple project skeleton:
#![feature(asm)]
#![no_std]
#![no_main]
use core::panic::PanicInfo;
#[no_mangle]
pub extern "C" fn _start() -> ! {
loop {
unsafe { asm!("mov r0, r0"); }
}
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
.. which basically does nothing but loop indefinitely.
I added the option panic = "abort"
to the Cargo.toml
to avoid implementing the eh_personality
item, pretty much following the popular blog by Philipp Oppermann.
But I am struggling to get the build done mainly because of the following issues:
- I am not sure about the target: After some research the target
arm-unknown-linux-gnueabihf
seems to be the correct one. The problem is I rather need anone
target instead oflinux
but I cannot really find anything else? - I managed to compile the raw version above with the following build command:
cargo rustc -- --target=arm-unknown-linux-gnueabihf -C linker=arm-none-eabi-gcc -C link-arg=-nostartfiles
but apparently this is not a good thing to do (I suppose it is fine, when we are actually compiling for the required none
target?). Also, the resulting file is different from the one generated by C-version:
file target/debug/rust-blink
target/debug/rust-blink: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /usr/lib/ld.so.1, with debug_info, not stripped
file blink.elf
blink.elf: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
file blink.o
blink.o: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), not stripped
I am not really sure how to fix this.
Also this whole compilation fails as soon as I add a simple for-loop. This seems to be somewhat unexpected to me, but maybe this is the world of no_std
?
I was also trying to skip the linking stage completely but I could not make it work. I was trying to add --emit=obj
but cargo invokes the linker anyways.
Any help is much appreciated