Record the .eh_frame sections from each .o file.

This commit is contained in:
Nick Lewycky
2020-06-18 13:17:58 -07:00
parent 69ccd86b4a
commit 52eae576fc
4 changed files with 23 additions and 6 deletions

View File

@@ -83,7 +83,7 @@ impl Compiler for LLVMCompiler {
) )
.collect::<Result<Vec<_>, CompileError>>()? .collect::<Result<Vec<_>, CompileError>>()?
.into_iter() .into_iter()
.map(|(mut compiled_function, function_custom_sections)| { .map(|(mut compiled_function, function_custom_sections, _eh_frame_section_indices)| {
let first_section = module_custom_sections.len() as u32; let first_section = module_custom_sections.len() as u32;
for (_, custom_section) in function_custom_sections.iter() { for (_, custom_section) in function_custom_sections.iter() {
// TODO: remove this call to clone() // TODO: remove this call to clone()

View File

@@ -43,7 +43,7 @@ pub fn load_object_file<F>(
root_section: &str, root_section: &str,
self_referential_relocation_target: Option<RelocationTarget>, self_referential_relocation_target: Option<RelocationTarget>,
mut symbol_name_to_relocation_target: F, mut symbol_name_to_relocation_target: F,
) -> Result<(CompiledFunction, CustomSections), CompileError> ) -> Result<(CompiledFunction, CustomSections, Vec<SectionIndex>), CompileError>
where where
F: FnMut(&String) -> Result<Option<RelocationTarget>, CompileError>, F: FnMut(&String) -> Result<Option<RelocationTarget>, CompileError>,
{ {
@@ -142,6 +142,20 @@ where
// the sections we want to include. // the sections we want to include.
worklist.push(root_section_index); worklist.push(root_section_index);
visited.insert(root_section_index); visited.insert(root_section_index);
// Also add any .eh_frame sections.
let mut eh_frame_section_indices = vec![];
// TODO: this constant has been added to goblin, now waiting for release
const SHT_X86_64_UNWIND: u64 = 0x7000_0001;
for (index, shdr) in elf.section_headers.iter().enumerate() {
if shdr.sh_flags & SHT_X86_64_UNWIND != 0 {
let index = ElfSectionIndex::from_usize(index)?;
worklist.push(index);
visited.insert(index);
eh_frame_section_indices.push(index);
}
}
while let Some(section_index) = worklist.pop() { while let Some(section_index) = worklist.pop() {
for reloc in reloc_sections for reloc in reloc_sections
.get(&section_index) .get(&section_index)
@@ -241,6 +255,8 @@ where
.map(|(_, v)| v) .map(|(_, v)| v)
.collect::<PrimaryMap<SectionIndex, _>>(); .collect::<PrimaryMap<SectionIndex, _>>();
let eh_frame_section_indices = eh_frame_section_indices.iter().map(|index| section_to_custom_section.get(index).map_or_else(|| Err(CompileError::Codegen(format!(".eh_frame section with index={:?} was never loaded", index))), |idx| Ok(*idx))).collect::<Result<Vec<SectionIndex>, _>>()?;
let function_body = FunctionBody { let function_body = FunctionBody {
body: section_bytes(root_section_index), body: section_bytes(root_section_index),
unwind_info: None, unwind_info: None,
@@ -271,5 +287,6 @@ where
}, },
}, },
custom_sections, custom_sections,
eh_frame_section_indices,
)) ))
} }

View File

@@ -94,7 +94,7 @@ impl FuncTrampoline {
} }
let mem_buf_slice = memory_buffer.as_slice(); let mem_buf_slice = memory_buffer.as_slice();
let (function, sections) = let (function, sections, _eh_frame_section_indices) =
load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| { load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| {
Err(CompileError::Codegen(format!( Err(CompileError::Codegen(format!(
"trampoline generation produced reference to unknown function {}", "trampoline generation produced reference to unknown function {}",
@@ -175,7 +175,7 @@ impl FuncTrampoline {
} }
let mem_buf_slice = memory_buffer.as_slice(); let mem_buf_slice = memory_buffer.as_slice();
let (function, sections) = let (function, sections, _eh_frame_section_indices) =
load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| { load_object_file(mem_buf_slice, FUNCTION_SECTION, None, |name: &String| {
Err(CompileError::Codegen(format!( Err(CompileError::Codegen(format!(
"trampoline generation produced reference to unknown function {}", "trampoline generation produced reference to unknown function {}",

View File

@@ -33,7 +33,7 @@ use wasmer_compiler::wasmparser::{MemoryImmediate, Operator};
use wasmer_compiler::{ use wasmer_compiler::{
to_wasm_error, wptype_to_type, CompileError, CompiledFunction, CustomSections, to_wasm_error, wptype_to_type, CompileError, CompiledFunction, CustomSections,
FunctionBodyData, GenerateMiddlewareChain, MiddlewareBinaryReader, ModuleTranslationState, FunctionBodyData, GenerateMiddlewareChain, MiddlewareBinaryReader, ModuleTranslationState,
RelocationTarget, RelocationTarget, SectionIndex,
}; };
use wasmer_runtime::{MemoryPlan, ModuleInfo, TablePlan}; use wasmer_runtime::{MemoryPlan, ModuleInfo, TablePlan};
@@ -76,7 +76,7 @@ impl FuncTranslator {
memory_plans: &PrimaryMap<MemoryIndex, MemoryPlan>, memory_plans: &PrimaryMap<MemoryIndex, MemoryPlan>,
_table_plans: &PrimaryMap<TableIndex, TablePlan>, _table_plans: &PrimaryMap<TableIndex, TablePlan>,
func_names: &SecondaryMap<FunctionIndex, String>, func_names: &SecondaryMap<FunctionIndex, String>,
) -> Result<(CompiledFunction, CustomSections), CompileError> { ) -> Result<(CompiledFunction, CustomSections, Vec<SectionIndex>), CompileError> {
// The function type, used for the callbacks. // The function type, used for the callbacks.
let function = CompiledFunctionKind::Local(*local_func_index); let function = CompiledFunctionKind::Local(*local_func_index);
let func_index = wasm_module.func_index(*local_func_index); let func_index = wasm_module.func_index(*local_func_index);