Build up list of per-function custom sections and build local relocations into them.

This commit is contained in:
Nick Lewycky
2020-05-01 16:31:26 -07:00
parent cf70a297d8
commit 4827434eef
2 changed files with 59 additions and 20 deletions

View File

@ -40,8 +40,8 @@ use wasm_common::{
use wasmer_compiler::wasmparser::{self, BinaryReader, MemoryImmediate, Operator};
use wasmer_compiler::{
to_wasm_error, wasm_unsupported, Addend, CodeOffset, CompileError, CompiledFunction,
CompiledFunctionUnwindInfo, CustomSection, FunctionAddressMap, FunctionBodyData, Relocation,
RelocationKind, RelocationTarget, SourceLoc, WasmResult,
CompiledFunctionUnwindInfo, CustomSection, CustomSectionProtection, FunctionAddressMap,
FunctionBodyData, Relocation, RelocationKind, RelocationTarget, SourceLoc, WasmResult,
};
use wasmer_runtime::libcalls::LibCall;
use wasmer_runtime::Module as WasmerCompilerModule;
@ -84,6 +84,7 @@ fn const_zero<'ctx>(ty: BasicTypeEnum<'ctx>) -> BasicValueEnum<'ctx> {
}
// Relocation against a per-function section.
#[derive(Debug)]
pub struct LocalRelocation {
pub kind: RelocationKind,
pub local_section_index: u32,
@ -308,6 +309,8 @@ impl FuncTranslator {
let mut bytes = vec![];
let mut relocations = vec![];
let mut local_relocations = vec![];
let mut required_custom_sections = vec![];
for section in object.get_sections() {
match section.get_name().map(std::ffi::CStr::to_bytes) {
Some(b"wasmer_function") => {
@ -342,26 +345,57 @@ impl FuncTranslator {
}
}
}
if reloc_target.is_none() {
unimplemented!("reference to non-user function {:?}", &target);
}
let reloc_target = reloc_target.unwrap();
if reloc_target.is_some() {
let reloc_target = reloc_target.unwrap();
let relocation = Relocation {
kind,
reloc_target,
offset: rel.get_offset() as u32,
// TODO: it appears the LLVM C API has no way to
// retrieve the addend.
addend: 0,
};
relocations.push(relocation);
relocations.push(Relocation {
kind,
reloc_target,
offset: rel.get_offset() as u32,
// TODO: it appears the LLVM C API has no way to
// retrieve the addend.
addend: 0,
});
} else {
if let Some(ref target) = &target {
let local_section_index = required_custom_sections.len() as u32;
required_custom_sections.push(target.clone());
local_relocations.push(LocalRelocation {
kind,
local_section_index,
offset: rel.get_offset() as u32,
// TODO: it appears the LLVM C API has no way to
// retrieve the addend.
addend: 0,
});
}
}
}
}
_ => {}
};
}
// TODO: verify that we see all of them
let mut custom_sections = vec![];
custom_sections.resize(
required_custom_sections.len(),
CustomSection {
protection: CustomSectionProtection::Read,
bytes: vec![],
},
);
for section in object.get_sections() {
if let Some(name) = section.get_name().map(std::ffi::CStr::to_bytes) {
if let Some(index) = required_custom_sections
.iter()
.position(|n| n.as_bytes() == name)
{
custom_sections[index].bytes.extend(section.get_contents());
}
}
}
let address_map = FunctionAddressMap {
instructions: vec![],
start_srcloc: SourceLoc::default(),
@ -379,8 +413,8 @@ impl FuncTranslator {
relocations,
traps: vec![],
},
vec![],
vec![],
local_relocations,
custom_sections,
))
}
}