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

@@ -68,6 +68,7 @@ impl Compiler for LLVMCompiler {
// We're going to "link" the sections by simply appending all compatible
// sections, then building the new relocations.
// TODO: merge constants.
let mut used_readonly_section = false;
let mut readonly_section = CustomSection {
protection: CustomSectionProtection::Read,
bytes: Vec::new(),
@@ -106,14 +107,16 @@ impl Compiler for LLVMCompiler {
// TODO: these section numbers are potentially wrong, if there's
// no Read and only a ReadExecute then ReadExecute is 0.
let (ref mut section, section_num) = match &custom_section.protection {
CustomSectionProtection::Read => (&mut readonly_section, SectionIndex::from_u32(0)),
//ReadExecute => (&mut readexecute_section, 1),
CustomSectionProtection::Read => {
(&mut readonly_section, SectionIndex::from_u32(0))
}
};
let offset = section.bytes.len() as i64;
section.bytes.extend(&custom_section.bytes);
// TODO: we're needlessly rescanning the whole list.
for local_relocation in &local_relocations {
if local_relocation.local_section_index == local_idx {
used_readonly_section = true;
function.relocations.push(Relocation {
kind: local_relocation.kind,
reloc_target: RelocationTarget::CustomSection(section_num),
@@ -130,7 +133,9 @@ impl Compiler for LLVMCompiler {
.collect::<PrimaryMap<LocalFuncIndex, _>>();
let mut custom_sections = PrimaryMap::new();
custom_sections.push(readonly_section);
if used_readonly_section {
custom_sections.push(readonly_section);
}
Ok(Compilation::new(functions, custom_sections))
}

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,
))
}
}