Skip to content

Instantly share code, notes, and snippets.

@gitcrtn
Created August 19, 2022 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitcrtn/b38b721756d87551ae758271ce857945 to your computer and use it in GitHub Desktop.
Save gitcrtn/b38b721756d87551ae758271ce857945 to your computer and use it in GitHub Desktop.
Minimal no_std example for M1 Mac
[package]
name = "rust-nostd-helloworld"
version = "0.1.0"
edition = "2021"
[dependencies]
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"
[target.aarch64-apple-darwin]
linker = "clang"
rustflags = ["-C", "link-args=-e __start -nostartfiles -fuse-ld=mold"]
#![no_std]
#![no_main]
#![feature(lang_items)]
use core::panic::PanicInfo;
#[no_mangle]
pub extern "C" fn _start() -> usize {
let sys_message = "Hello world from no_std!\n";
say_hello(sys_message);
0
}
#[link(name = "c")]
extern "C" {
fn write(fd: u32, buf: *const u8, count: usize) -> i32;
}
fn say_hello(message: &str) {
let msg_ptr = message.as_ptr();
let len = message.len();
let _ = unsafe { write(1, msg_ptr, len) };
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment