//! Initialize wasi-libc's cwd for every program that the world toolchain builds. #![no_std] #[used] #[unsafe(link_section = "component-type:sausage-exit")] static EXIT_INTERFACE: [u8; include_bytes!("exit-meta.bin").len()] = *include_bytes!("exit-meta.bin"); unsafe extern "C" { fn getenv(name: *const u8) -> *const u8; fn chdir(path: *const u8) -> i32; } #[link(wasm_import_module = "wasi:cli/exit@0.2.12")] unsafe extern "C" { #[link_name = "exit-with-code"] fn exit_with_code(code: u32); } /// Replace wasi-libc's success/failure conversion with the standard numeric exit interface. #[unsafe(no_mangle)] unsafe extern "C" fn _Exit(code: i32) -> ! { unsafe { exit_with_code((code as u32) & 255); } core::arch::wasm32::unreachable() } #[unsafe(no_mangle)] unsafe extern "C" fn _exit(code: i32) -> ! { unsafe { _Exit(code) } } extern "C" fn initialize() { // PWD is supplied by the process table, never inferred from a host directory. unsafe { let cwd = getenv(c"PWD".as_ptr().cast()); if !cwd.is_null() && chdir(cwd) != 0 { _exit(1); } } } #[used] #[unsafe(link_section = ".init_array")] static INITIALIZE: extern "C" fn() = initialize;