Created
August 19, 2022 14:58
-
-
Save gitcrtn/b38b721756d87551ae758271ce857945 to your computer and use it in GitHub Desktop.
Minimal no_std example for M1 Mac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust-nostd-helloworld" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[profile.dev] | |
panic = "abort" | |
[profile.release] | |
panic = "abort" | |
[target.aarch64-apple-darwin] | |
linker = "clang" | |
rustflags = ["-C", "link-args=-e __start -nostartfiles -fuse-ld=mold"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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