Refactor trampoline generation to use load_object_file.

This commit is contained in:
Nick Lewycky
2020-05-21 13:53:48 -07:00
parent 3764e7b9f8
commit c02cd76623

View File

@@ -1,4 +1,5 @@
use crate::config::LLVMConfig; use crate::config::LLVMConfig;
use crate::object_file::load_object_file;
use crate::translator::intrinsics::{func_type_to_llvm, Intrinsics}; use crate::translator::intrinsics::{func_type_to_llvm, Intrinsics};
use inkwell::{ use inkwell::{
context::Context, module::Linkage, passes::PassManager, targets::FileType, types::BasicType, context::Context, module::Linkage, passes::PassManager, targets::FileType, types::BasicType,
@@ -11,7 +12,7 @@ pub struct FuncTrampoline {
ctx: Context, ctx: Context,
} }
const FUNCTION_SECTION: &'static str = "wasmer_trampoline"; const FUNCTION_SECTION: &str = "wasmer_trampoline";
impl FuncTrampoline { impl FuncTrampoline {
pub fn new() -> Self { pub fn new() -> Self {
@@ -80,24 +81,34 @@ impl FuncTrampoline {
} }
*/ */
let object = memory_buffer.create_object_file().map_err(|()| { let mem_buf_slice = memory_buffer.as_slice();
CompileError::Codegen("failed to create object file from llvm ir".to_string()) let (function, sections) =
})?; load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| {
Err(CompileError::Codegen(format!(
let mut bytes = vec![]; "trampoline generation produced reference to unknown function {}",
for section in object.get_sections() { name
if section.get_name().map(std::ffi::CStr::to_bytes) == Some(FUNCTION_SECTION.as_bytes()) )))
{ })?;
bytes.extend(section.get_contents().to_vec()); if !sections.is_empty() {
break; return Err(CompileError::Codegen(
} "trampoline generation produced custom sections".into(),
));
} }
// TODO: remove debugging if !function.relocations.is_empty() {
//dbg!(&bytes); return Err(CompileError::Codegen(
"trampoline generation produced relocations".into(),
));
}
if !function.jt_offsets.is_empty() {
return Err(CompileError::Codegen(
"trampoline generation produced jump tables".into(),
));
}
// Ignore CompiledFunctionFrameInfo. Extra frame info isn't a problem.
Ok(FunctionBody { Ok(FunctionBody {
body: bytes, body: function.body.body,
unwind_info: None, unwind_info: function.body.unwind_info,
}) })
} }
} }