add a way to provide imported functions during relocation

They are provided as a closure taking the module (&str) and function name (&str) as arguments,
returning an address (Option<isize>)
This commit is contained in:
Geoffroy Couprie
2018-11-15 16:41:10 +01:00
committed by Dan Gohman
parent d72ebe53d4
commit 7fca0792dd
2 changed files with 30 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
use cranelift_codegen::binemit::Reloc; use cranelift_codegen::binemit::Reloc;
use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::isa::TargetIsa;
use cranelift_entity::PrimaryMap; use cranelift_entity::{EntityRef, PrimaryMap};
use cranelift_wasm::{DefinedFuncIndex, MemoryIndex}; use cranelift_wasm::{DefinedFuncIndex, MemoryIndex};
use instance::Instance; use instance::Instance;
use memory::LinearMemory; use memory::LinearMemory;
@@ -14,34 +14,48 @@ use wasmtime_environ::{
/// Executes a module that has been translated with the `wasmtime-environ` environment /// Executes a module that has been translated with the `wasmtime-environ` environment
/// implementation. /// implementation.
pub fn compile_and_link_module<'data, 'module>( pub fn compile_and_link_module<'data, 'module, F>(
isa: &TargetIsa, isa: &TargetIsa,
translation: &ModuleTranslation<'data, 'module>, translation: &ModuleTranslation<'data, 'module>,
) -> Result<Compilation, String> { imports: F,
) -> Result<Compilation, String>
where
F: Fn(&str, &str) -> Option<isize>,
{
let (mut compilation, relocations) = compile_module(&translation, isa)?; let (mut compilation, relocations) = compile_module(&translation, isa)?;
// Apply relocations, now that we have virtual addresses for everything. // Apply relocations, now that we have virtual addresses for everything.
relocate(&mut compilation, &relocations, &translation.module); relocate(&mut compilation, &relocations, &translation.module, imports);
Ok(compilation) Ok(compilation)
} }
/// Performs the relocations inside the function bytecode, provided the necessary metadata /// Performs the relocations inside the function bytecode, provided the necessary metadata
fn relocate( fn relocate<F>(
compilation: &mut Compilation, compilation: &mut Compilation,
relocations: &PrimaryMap<DefinedFuncIndex, Vec<Relocation>>, relocations: &PrimaryMap<DefinedFuncIndex, Vec<Relocation>>,
module: &Module, module: &Module,
) { imports: F,
) where
F: Fn(&str, &str) -> Option<isize>,
{
// The relocations are relative to the relocation's address plus four bytes // The relocations are relative to the relocation's address plus four bytes
// TODO: Support architectures other than x64, and other reloc kinds. // TODO: Support architectures other than x64, and other reloc kinds.
for (i, function_relocs) in relocations.iter() { for (i, function_relocs) in relocations.iter() {
for r in function_relocs { for r in function_relocs {
let target_func_address: isize = match r.reloc_target { let target_func_address: isize = match r.reloc_target {
RelocationTarget::UserFunc(index) => { RelocationTarget::UserFunc(index) => match module.defined_func_index(index) {
compilation.functions[module.defined_func_index(index).expect( Some(f) => compilation.functions[f].as_ptr() as isize,
"relocation to imported function not supported yet", None => {
)].as_ptr() as isize let func = &module.imported_funcs[index.index()];
} match imports(&func.0, &func.1) {
Some(ptr) => ptr,
None => {
panic!("no provided import function for {}/{}", &func.0, &func.1)
}
}
}
},
RelocationTarget::GrowMemory => grow_memory as isize, RelocationTarget::GrowMemory => grow_memory as isize,
RelocationTarget::CurrentMemory => current_memory as isize, RelocationTarget::CurrentMemory => current_memory as isize,
}; };

View File

@@ -145,8 +145,12 @@ fn handle_module(args: &Args, path: PathBuf, isa: &TargetIsa) -> Result<(), Stri
} }
let mut module = Module::new(); let mut module = Module::new();
let environ = ModuleEnvironment::new(isa, &mut module); let environ = ModuleEnvironment::new(isa, &mut module);
let imports_resolver = |_env: &str, _function: &str| None;
let translation = environ.translate(&data).map_err(|e| e.to_string())?; let translation = environ.translate(&data).map_err(|e| e.to_string())?;
let instance = match compile_and_link_module(isa, &translation) {
let instance = match compile_and_link_module(isa, &translation, &imports_resolver) {
Ok(compilation) => { Ok(compilation) => {
let mut instance = Instance::new( let mut instance = Instance::new(
translation.module, translation.module,