Skip to content

Instantly share code, notes, and snippets.

@kpcyrd
Created December 20, 2022 10:05
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 kpcyrd/b9eafe99ebfaa83e4e835634f30e1572 to your computer and use it in GitHub Desktop.
Save kpcyrd/b9eafe99ebfaa83e4e835634f30e1572 to your computer and use it in GitHub Desktop.
// RUSTFLAGS="-C panic=abort" cargo build --release --target x86_64-unknown-none && ./target/x86_64-unknown-none/debug/hello-world; echo $?
#![no_std]
#![no_main]
use core::arch::asm;
use core::slice;
pub const STDOUT_FILENO: u32 = 1;
/// This function is called on panic.
#[panic_handler]
unsafe fn panic(_info: &core::panic::PanicInfo) -> ! {
exit(1)
}
pub unsafe fn strlen(mut s: *const u8) -> usize {
let mut count = 0;
while *s != b'\0' {
count += 1;
s = s.add(1);
}
count
}
pub unsafe fn write(fd: u32, buf: *const u8, count: usize) {
let syscall_number: u64 = 1;
asm!(
"syscall",
inout("rax") syscall_number => _, // we don't check the return value
in("rdi") fd,
in("rsi") buf,
in("rdx") count,
lateout("rcx") _, lateout("r11") _,
options(nostack)
);
}
pub unsafe fn close(fd: u32) {
let syscall_number: u64 = 3;
asm!(
"syscall",
inout("rax") syscall_number => _,
in("rdi") fd,
options(nostack)
);
}
pub fn print(s: &[u8]) {
unsafe {
write(STDOUT_FILENO, s.as_ptr(), s.len());
}
}
pub fn println(s: &[u8]) {
print(s);
print(b"\n");
}
pub unsafe fn exit(code: i32) -> ! {
let syscall_number: u64 = 60;
asm!(
"syscall",
in("rax") syscall_number,
in("rdi") code,
options(noreturn)
)
}
#[no_mangle]
pub unsafe fn main(stack_top: *const u8) -> ! {
let argc = *(stack_top as *const u64);
let argv = stack_top.add(8) as *const *const u8;
print(b"# args:\n");
let args = slice::from_raw_parts(argv, argc as usize);
for &arg in args {
let arg = slice::from_raw_parts(arg, strlen(arg));
println(arg);
}
print(b"# environ:\n");
let mut envp = argv.add(argc as usize + 1) as *const *const u8;
while !(*envp).is_null() {
let var = slice::from_raw_parts(*envp, strlen(*envp));
println(var);
envp = envp.add(1);
}
exit(0)
}
#[no_mangle]
pub unsafe extern "C" fn _start() -> ! {
asm!("pop rax", "mov rdi, rsp", "call main", options(noreturn))
}
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment