Link MSVC default lib in core by ChrisDenton · Pull Request #122268 · rust-lang/rust (original) (raw)

$ cat nostd.rs
#![no_std]
#![crate_type="cdylib"]

#[no_mangle]
pub unsafe extern "C" fn do_stuff(ptr: *const u8, len: usize, out: *mut u8) {
    let a = core::slice::from_raw_parts(ptr, len);
    let b = core::slice::from_raw_parts_mut(out, len);
    b.copy_from_slice(a);
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}
$ rustc nostd.rs -C panic=abort -C opt-level=3
$ cat exe.rs
fn main() {
    let a = [1u8; 10];
    let mut b = [0u8; 10];
    unsafe { do_stuff(a.as_ptr(), a.len(), b.as_mut_ptr()) }
    println!("{:?}", b);
}

#[link(name = "nostd")]
extern "C" {
    fn do_stuff(ptr: *const u8, len: usize, out: *mut u8);
}

$ rustc exe.rs -L ./
$ LD_LIBRARY_PATH=./ ./exe
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

The produced libnostd.so seemed to contain glibc specific stuff and not just be a pure Linux binary.