--- tags: wasm title: WASM linking --- # Archlinux pacman -Syu wasi-libc wasi-compiler-rt wabt # Installer in Rust - `src/lib.rs` ```rust= #![no_std] #[panic_handler] fn panic(_:&::core::panic::PanicInfo) -> ! { loop{} } #[link(name = "module_hash", kind = "static")] extern { static MODULE_HASH: *const u8; } #[no_mangle] pub unsafe extern "C" fn get_hash() -> *const u8 { MODULE_HASH } ``` - Compile with ``` rustc -C opt-level=2 --target=wasm32-unknown-unknown --crate-type=staticlib --print native-static-libs --emit=obj -C panic=abort src/lib.rs -o installer.o ``` # Module hash object - `module_hash.c` ```c char const * MODULE_HASH = "deadbeef"; ``` - Compile with ``` clang -o module_hash.o --target=wasm32 -nostdlib -Wl,-r -Wl,--no-entry -Wl,--export-all module_hash.c ``` # Link all ``` wasm-ld --no-entry --export-all installer.o module_hash.o -o installer.wasm ``` The result is the following WAT. ```wat= (module (type (;0;) (func)) (type (;1;) (func (param i32))) (type (;2;) (func (result i32))) (func $__wasm_call_ctors (type 0)) (func $rust_begin_unwind (type 1) (param i32) loop ;; label = @1 br 0 (;@1;) end) (func $get_hash (type 2) (result i32) i32.const 0 i32.load offset=1036) (memory (;0;) 2) (global $__stack_pointer (mut i32) (i32.const 66576)) (global (;1;) i32 (i32.const 1036)) (global (;2;) i32 (i32.const 1024)) (global (;3;) i32 (i32.const 1040)) (global (;4;) i32 (i32.const 1024)) (global (;5;) i32 (i32.const 66576)) (global (;6;) i32 (i32.const 0)) (global (;7;) i32 (i32.const 1)) (export "memory" (memory 0)) (export "__wasm_call_ctors" (func $__wasm_call_ctors)) (export "rust_begin_unwind" (func $rust_begin_unwind)) (export "get_hash" (func $get_hash)) (export "MODULE_HASH" (global 1)) (; location of the module hash ;) (export "__dso_handle" (global 2)) (export "__data_end" (global 3)) (export "__global_base" (global 4)) (export "__heap_base" (global 5)) (export "__memory_base" (global 6)) (export "__table_base" (global 7)) (data $.rodata (i32.const 1024) "deadbeef\00") (data $.data (i32.const 1036) "\00\04\00\00")) ``` It looks legit so far. :smiley: