In trampoline generation, ensure all custom sections are eh frames.

.eh_frames may refer to the trampoline, but it is impossible to encode such a relocation. The optional relocation "self-referential" target is replaced with a better named mandatory argument. The value will be discarded in trampoline generation with the rest of the .eh_frame.
This commit is contained in:
Nick Lewycky
2020-06-18 15:32:50 -07:00
parent 310ac6b44c
commit 54c7b6f876
3 changed files with 52 additions and 28 deletions

View File

@@ -41,7 +41,7 @@ fn map_goblin_err(error: goblin::error::Error) -> CompileError {
pub fn load_object_file<F>(
contents: &[u8],
root_section: &str,
self_referential_relocation_target: Option<RelocationTarget>,
root_section_reloc_target: RelocationTarget,
mut symbol_name_to_relocation_target: F,
) -> Result<(CompiledFunction, CustomSections, Vec<SectionIndex>), CompileError>
where
@@ -102,9 +102,7 @@ where
let mut section_to_custom_section = HashMap::new();
if let Some(reloc_target) = self_referential_relocation_target {
section_targets.insert(root_section_index, reloc_target);
};
section_targets.insert(root_section_index, root_section_reloc_target);
let mut next_custom_section: u32 = 0;
let mut elf_section_to_target = |elf_section_index: ElfSectionIndex| {
@@ -197,17 +195,13 @@ where
))
})?;
let elf_target_section = ElfSectionIndex::from_usize(elf_target.st_shndx)?;
let reloc_target = if elf_target.st_type() == goblin::elf::sym::STT_SECTION {
let reloc_target = if elf_target_section == root_section_index {
root_section_reloc_target
} else if elf_target.st_type() == goblin::elf::sym::STT_SECTION {
if visited.insert(elf_target_section) {
worklist.push(elf_target_section);
}
elf_section_to_target(elf_target_section)
} else if elf_target.st_type() == goblin::elf::sym::STT_FUNC
&& elf_target_section == root_section_index
&& self_referential_relocation_target.is_some()
{
// This is a function referencing its own byte stream.
self_referential_relocation_target.unwrap()
} else if elf_target.st_type() == goblin::elf::sym::STT_NOTYPE
&& elf_target_section.is_undef()
{

View File

@@ -17,8 +17,8 @@ use inkwell::{
};
use std::cmp;
use std::convert::TryInto;
use wasm_common::{FunctionType, Type};
use wasmer_compiler::{CompileError, FunctionBody};
use wasm_common::{FunctionType, LocalFunctionIndex, Type};
use wasmer_compiler::{CompileError, FunctionBody, RelocationTarget};
pub struct FuncTrampoline {
ctx: Context,
@@ -94,20 +94,35 @@ impl FuncTrampoline {
}
let mem_buf_slice = memory_buffer.as_slice();
let (function, _sections, _eh_frame_section_indices) =
load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| {
let (function, sections, eh_frame_section_indices) = load_object_file(
mem_buf_slice,
FUNCTION_SECTION,
RelocationTarget::LocalFunc(LocalFunctionIndex::from_u32(0)),
|name: &String| {
Err(CompileError::Codegen(format!(
"trampoline generation produced reference to unknown function {}",
name
)))
})?;
/*
if !sections.is_empty() {
},
)?;
let mut all_sections_are_eh_sections = true;
if eh_frame_section_indices.len() != sections.len() {
all_sections_are_eh_sections = false;
} else {
let mut eh_frame_section_indices = eh_frame_section_indices.clone();
eh_frame_section_indices.sort_unstable();
for (idx, section_idx) in eh_frame_section_indices.iter().enumerate() {
if idx as u32 != section_idx.as_u32() {
all_sections_are_eh_sections = false;
break;
}
}
}
if !all_sections_are_eh_sections {
return Err(CompileError::Codegen(
"trampoline generation produced custom sections".into(),
"trampoline generation produced non-eh custom sections".into(),
));
}
*/
if !function.relocations.is_empty() {
return Err(CompileError::Codegen(
"trampoline generation produced relocations".into(),
@@ -177,20 +192,35 @@ impl FuncTrampoline {
}
let mem_buf_slice = memory_buffer.as_slice();
let (function, _sections, _eh_frame_section_indices) =
load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| {
let (function, sections, eh_frame_section_indices) = load_object_file(
mem_buf_slice,
FUNCTION_SECTION,
RelocationTarget::LocalFunc(LocalFunctionIndex::from_u32(0)),
|name: &String| {
Err(CompileError::Codegen(format!(
"trampoline generation produced reference to unknown function {}",
name
)))
})?;
/*
if !sections.is_empty() {
},
)?;
let mut all_sections_are_eh_sections = true;
if eh_frame_section_indices.len() != sections.len() {
all_sections_are_eh_sections = false;
} else {
let mut eh_frame_section_indices = eh_frame_section_indices.clone();
eh_frame_section_indices.sort_unstable();
for (idx, section_idx) in eh_frame_section_indices.iter().enumerate() {
if idx as u32 != section_idx.as_u32() {
all_sections_are_eh_sections = false;
break;
}
}
}
if !all_sections_are_eh_sections {
return Err(CompileError::Codegen(
"trampoline generation produced custom sections".into(),
"trampoline generation produced non-eh custom sections".into(),
));
}
*/
if !function.relocations.is_empty() {
return Err(CompileError::Codegen(
"trampoline generation produced relocations".into(),

View File

@@ -274,7 +274,7 @@ impl FuncTranslator {
load_object_file(
mem_buf_slice,
".wasmer_function",
Some(RelocationTarget::LocalFunc(*local_func_index)),
RelocationTarget::LocalFunc(*local_func_index),
|name: &String| {
if let Some((index, _)) = func_names
.iter()