diff --git a/lib/clif-backend/src/module.rs b/lib/clif-backend/src/module.rs index b50517f6e..596763db0 100644 --- a/lib/clif-backend/src/module.rs +++ b/lib/clif-backend/src/module.rs @@ -92,8 +92,9 @@ impl Module { func_assoc.iter_mut().for_each(|(_, sig_index)| { *sig_index = sig_registry.lookup_deduplicated_sigindex(*sig_index); }); - - let (func_resolver_builder, handler_data) = FuncResolverBuilder::new(isa, functions)?; + let imported_functions_len = self.module.imported_functions.len(); + let (func_resolver_builder, handler_data) = + FuncResolverBuilder::new(isa, functions, imported_functions_len)?; self.module.func_resolver = Box::new(func_resolver_builder.finalize()?); let trampolines = Trampolines::new(isa, &self.module); diff --git a/lib/clif-backend/src/resolver.rs b/lib/clif-backend/src/resolver.rs index 224dc6e91..8aca94ce0 100644 --- a/lib/clif-backend/src/resolver.rs +++ b/lib/clif-backend/src/resolver.rs @@ -14,7 +14,7 @@ use wasmer_runtime_core::{ sys::{Memory, Protect}, }, error::{CompileError, CompileResult}, - structures::Map, + structures::{Map, TypedIndex}, types::LocalFuncIndex, vm, vmcalls, }; @@ -23,12 +23,14 @@ use wasmer_runtime_core::{ pub struct FuncResolverBuilder { resolver: FuncResolver, relocations: Map>, + import_len: usize, } impl FuncResolverBuilder { pub fn new( isa: &isa::TargetIsa, function_bodies: Map, + import_len: usize, ) -> CompileResult<(Self, HandlerData)> { let mut compiled_functions: Vec> = Vec::with_capacity(function_bodies.len()); let mut relocations = Map::with_capacity(function_bodies.len()); @@ -100,6 +102,7 @@ impl FuncResolverBuilder { Self { resolver: FuncResolver { map, memory }, relocations, + import_len, }, handler_data, )) @@ -113,6 +116,10 @@ impl FuncResolverBuilder { // This will always be an internal function // because imported functions are not // called in this way. + // Adjust from wasm-wide function index to index of locally-defined functions only. + let local_func_index = + LocalFuncIndex::new(local_func_index.index() - self.import_len); + self.resolver.lookup(local_func_index).unwrap().as_ptr() as isize } RelocationType::LibCall(libcall) => match libcall {