|
|
|
|
|
by ChrisSD
806 days ago
|
|
A similar program on Windows is 3072 bytes. I compiled it using: rustc hello.rs -C panic=abort -C opt-level=3 -C link-arg=/entry:main
Here's the program: #![no_std]
#![no_main]
#[panic_handler]
fn panic_handler(_panic: &core::panic::PanicInfo<'_>) -> ! {
unsafe { ExitProcess(111) };
}
#[no_mangle]
pub extern "C" fn main() -> u32 {
let msg = b"Hello, world!\n";
unsafe {
let stdout = GetStdHandle(-11);
let mut written = 0;
WriteFile(
stdout,
msg.as_ptr(),
msg.len() as u32,
&mut written,
core::ptr::null_mut(),
);
}
0
}
#[link(name = "kernel32")]
extern "system" {
fn ExitProcess(uExitCode: u32) -> !;
fn GetStdHandle(nStdHandle: i32) -> isize;
fn WriteFile(
hFile: isize,
lpBuffer: *const u8,
nNumberOfBytesToWrite: u32,
lpNumberOfBytesWritten: *mut u32,
lpOverlapped: *mut (),
) -> i32;
}
I didn't bother much with the panic handler because there's no reason to in hello world. Though the binary contains a fair bit of padding still so it could have a few more things added to it without increasing the size. Alternatively I could shrink it a bit further by doing crimes but I'm not sure there's much point.It may be worth noting that the associated pdb (aka debug database) is 208,896 bytes. |
|