Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support imports in wasm2obj #93

Merged
merged 1 commit into from
Apr 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion wasmtime-obj/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,36 @@ use cranelift_entity::EntityRef;
use faerie::{Artifact, Decl, Link};
use wasmtime_environ::{Compilation, Module, RelocationTarget, Relocations};

fn get_reloc_target_special_import_name(target: RelocationTarget) -> Option<&'static str> {
Some(match target {
RelocationTarget::Memory32Grow => &"wasmtime_memory32_grow",
RelocationTarget::ImportedMemory32Grow => &"wasmtime_memory32_grow",
RelocationTarget::Memory32Size => &"wasmtime_memory32_size",
RelocationTarget::ImportedMemory32Size => &"wasmtime_imported_memory32_size",
_ => return None,
})
}

/// Defines module functions
pub fn declare_functions(
obj: &mut Artifact,
module: &Module,
relocations: &Relocations,
) -> Result<(), String> {
for i in 0..module.imported_funcs.len() {
let string_name = format!("_wasm_function_{}", i);
obj.declare(string_name, Decl::function_import())
.map_err(|err| format!("{}", err))?;
}
for (_, function_relocs) in relocations.iter() {
for r in function_relocs {
let special_import_name = get_reloc_target_special_import_name(r.reloc_target);
if let Some(special_import_name) = special_import_name {
obj.declare(special_import_name, Decl::function_import())
.map_err(|err| format!("{}", err))?;
}
}
}
for (i, _function_relocs) in relocations.iter().rev() {
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
Expand Down Expand Up @@ -61,7 +85,18 @@ pub fn emit_functions(
})
.map_err(|err| format!("{}", err))?;
}
_ => panic!("relocations target not supported yet"),
RelocationTarget::Memory32Grow
| RelocationTarget::ImportedMemory32Grow
| RelocationTarget::Memory32Size
| RelocationTarget::ImportedMemory32Size => {
obj.link(Link {
from: &string_name,
to: get_reloc_target_special_import_name(r.reloc_target).expect("name"),
at: r.offset as u64,
})
.map_err(|err| format!("{}", err))?;
}
_ => panic!("relocations target not supported yet: {:?}", r.reloc_target),
};
}
}
Expand Down