I'm trying to ensure I've exhaustively covered all possible imports in a wasm program (the idea being that I build the program and use wasm-nm to list all imports) in a wrapper (using wasmtime-go) that I'm building. Does anyone know how I can disable dead code elimination or some other mechanism to get these imports? Such as querying a crate to find all extern "C" decls, or functions with a wasm link attr?
I suppose the list of imports doesn't change often, so it's not really a big deal to hand-write the list, but now I feel nerd baited
@Alex Vidal -Clink-dead-code=yes
should do.
Doesn't seem to, but I haven't tried a minimal repro yet.
Ok. Just did a minimal repro that defines three wasm imports. Compiling with rustc --target=wasm32-wasi -g ./src/main.rs -C link-dead-code=yes
then running the result through wasm-nm
, the result is that there's no import defined for functions that are not used in my main function
// Compile with:
// rustc --target=wasm32-unknown-unknown -g main.rs -C link-dead-code=yes
// Check with:
// wasm-nm -i main.wasm
#[link(wasm_import_module="math")]
extern "C" {
#[link_name = "add"]
pub fn add(a: i32, b: i32) -> i32;
#[link_name = "mul"]
pub fn mul(a: i32, b: i32) -> i32;
}
#[no_mangle]
extern "C" {
pub fn top();
}
fn main() {
println!("Hello, world!");
// Comment this out and there's no `top` import in the wasm binary
unsafe { top() };
// Comment this out and there's no `add` import in the wasm binary
call_add();
}
fn call_add() {
unsafe { add(1, 2) };
}
// mul is never in the wasm binary because `call_mul` is never called
fn call_mul() {
unsafe { mul(1, 2) };
}
note, i also tried with -C opt-level=0 -C lto=no
Executables only export the Didn't notice which topic this is.main
function by default. The linker removes all functions that are not directly or indirectly used through an export.
(deleted)
I don't really know what will fix it.
Last updated: Nov 22 2024 at 16:03 UTC