mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-08 21:58:20 +00:00
Initial commit for support of custom sections.
This commit is contained in:
@@ -9,14 +9,16 @@ use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
|
||||
use wasm_common::entity::{EntityRef, PrimaryMap, SecondaryMap};
|
||||
use wasm_common::Features;
|
||||
use wasm_common::{FuncIndex, FuncType, LocalFuncIndex, MemoryIndex, TableIndex};
|
||||
use wasmer_compiler::FunctionBodyData;
|
||||
use wasmer_compiler::TrapInformation;
|
||||
use wasmer_compiler::{Compilation, CompileError, CompiledFunction, Compiler};
|
||||
use wasmer_compiler::{CompilerConfig, ModuleTranslationState, Target};
|
||||
use wasmer_compiler::{
|
||||
Compilation, CompileError, CompiledFunction, Compiler, CompilerConfig, CustomSection,
|
||||
CustomSectionProtection, FunctionBodyData, ModuleTranslationState, Relocation,
|
||||
RelocationTarget, SectionIndex, Target, TrapInformation,
|
||||
};
|
||||
use wasmer_runtime::{MemoryPlan, Module, TablePlan, TrapCode};
|
||||
|
||||
use inkwell::targets::{InitializationConfig, Target as InkwellTarget};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex}; // TODO: remove
|
||||
|
||||
/// A compiler that compiles a WebAssembly module with LLVM, translating the Wasm to LLVM IR,
|
||||
@@ -62,7 +64,6 @@ impl Compiler for LLVMCompiler {
|
||||
) -> Result<Compilation, CompileError> {
|
||||
//let data = Arc::new(Mutex::new(0));
|
||||
let mut func_names = SecondaryMap::new();
|
||||
let custom_sections = PrimaryMap::new();
|
||||
|
||||
for (func_index, _) in &module.functions {
|
||||
func_names[func_index] = module
|
||||
@@ -71,7 +72,7 @@ impl Compiler for LLVMCompiler {
|
||||
.cloned()
|
||||
.unwrap_or_else(|| format!("fn{}", func_index.index()));
|
||||
}
|
||||
let functions = function_body_inputs
|
||||
let mut outputs = function_body_inputs
|
||||
.into_iter()
|
||||
.collect::<Vec<(LocalFuncIndex, &FunctionBodyData<'_>)>>()
|
||||
.par_iter()
|
||||
@@ -92,6 +93,51 @@ impl Compiler for LLVMCompiler {
|
||||
.into_iter()
|
||||
.collect::<PrimaryMap<LocalFuncIndex, _>>();
|
||||
|
||||
// We're going to "link" the sections by simply appending all compatible
|
||||
// sections, then building the new relocations.
|
||||
// TODO: merge constants.
|
||||
let mut readonly_section = CustomSection {
|
||||
protection: CustomSectionProtection::Read,
|
||||
bytes: Vec::new(),
|
||||
};
|
||||
/*
|
||||
let mut readexecute_section = CustomSection {
|
||||
protection: CustomSectionProtection::Read,
|
||||
bytes: Vec::new(),
|
||||
};
|
||||
*/
|
||||
for (ref mut function, ref local_relocations, ref custom_sections) in outputs.values_mut() {
|
||||
for (local_idx, custom_section) in custom_sections.iter().enumerate() {
|
||||
let local_idx = local_idx as u32;
|
||||
// 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 {
|
||||
Read => (&mut readonly_section, SectionIndex::from_u32(0)),
|
||||
//ReadExecute => (&mut readexecute_section, 1),
|
||||
};
|
||||
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 {
|
||||
function.relocations.push(Relocation {
|
||||
kind: local_relocation.kind,
|
||||
reloc_target: RelocationTarget::CustomSection(section_num),
|
||||
offset: local_relocation.offset,
|
||||
addend: local_relocation.addend + offset,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let functions = outputs
|
||||
.iter_mut()
|
||||
.map(|(i, (f, _, _))| *f)
|
||||
.collect::<PrimaryMap<LocalFuncIndex, _>>();
|
||||
|
||||
let mut custom_sections = PrimaryMap::new();
|
||||
custom_sections.push(readonly_section);
|
||||
Ok(Compilation::new(functions, custom_sections))
|
||||
}
|
||||
|
||||
|
||||
@@ -39,14 +39,13 @@ use wasm_common::{
|
||||
};
|
||||
use wasmer_compiler::wasmparser::{self, BinaryReader, MemoryImmediate, Operator};
|
||||
use wasmer_compiler::{
|
||||
to_wasm_error, wasm_unsupported, CompileError, CompiledFunction, WasmResult,
|
||||
};
|
||||
use wasmer_compiler::{
|
||||
CompiledFunctionUnwindInfo, FunctionAddressMap, FunctionBodyData, Relocation, RelocationKind,
|
||||
RelocationTarget, SourceLoc,
|
||||
to_wasm_error, wasm_unsupported, Addend, CodeOffset, CompileError, CompiledFunction,
|
||||
CompiledFunctionUnwindInfo, CustomSection, FunctionAddressMap, FunctionBodyData, Relocation,
|
||||
RelocationKind, RelocationTarget, SourceLoc, WasmResult,
|
||||
};
|
||||
use wasmer_runtime::libcalls::LibCall;
|
||||
use wasmer_runtime::Module as WasmerCompilerModule;
|
||||
use wasmer_runtime::{libcalls::LibCall, MemoryPlan, MemoryStyle, TablePlan};
|
||||
use wasmer_runtime::{MemoryPlan, MemoryStyle, TablePlan};
|
||||
|
||||
// TODO: debugging
|
||||
use std::fs;
|
||||
@@ -84,6 +83,14 @@ fn const_zero<'ctx>(ty: BasicTypeEnum<'ctx>) -> BasicValueEnum<'ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
// Relocation against a per-function section.
|
||||
pub struct LocalRelocation {
|
||||
pub kind: RelocationKind,
|
||||
pub local_section_index: u32,
|
||||
pub offset: CodeOffset,
|
||||
pub addend: Addend,
|
||||
}
|
||||
|
||||
impl FuncTranslator {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
@@ -100,7 +107,7 @@ impl FuncTranslator {
|
||||
memory_plans: &PrimaryMap<MemoryIndex, MemoryPlan>,
|
||||
table_plans: &PrimaryMap<TableIndex, TablePlan>,
|
||||
func_names: &SecondaryMap<FuncIndex, String>,
|
||||
) -> Result<CompiledFunction, CompileError> {
|
||||
) -> Result<(CompiledFunction, Vec<LocalRelocation>, Vec<CustomSection>), CompileError> {
|
||||
let func_index = wasm_module.func_index(*func_index);
|
||||
let func_name = func_names.get(func_index).unwrap();
|
||||
let module_name = match wasm_module.name.as_ref() {
|
||||
@@ -363,14 +370,18 @@ impl FuncTranslator {
|
||||
body_len: 0, // TODO
|
||||
};
|
||||
|
||||
Ok(CompiledFunction {
|
||||
address_map,
|
||||
body: bytes,
|
||||
jt_offsets: SecondaryMap::new(),
|
||||
unwind_info: CompiledFunctionUnwindInfo::None,
|
||||
relocations,
|
||||
traps: vec![],
|
||||
})
|
||||
Ok((
|
||||
CompiledFunction {
|
||||
address_map,
|
||||
body: bytes,
|
||||
jt_offsets: SecondaryMap::new(),
|
||||
unwind_info: CompiledFunctionUnwindInfo::None,
|
||||
relocations,
|
||||
traps: vec![],
|
||||
},
|
||||
vec![],
|
||||
vec![],
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user