Delete unused code in wasmtime-obj (#3179)

I believe this was likely used at some point historically, but nowadays
this code isn't used so let's delete it.
This commit is contained in:
Alex Crichton
2021-08-12 13:28:00 -05:00
committed by GitHub
parent e0c8961333
commit 2da1b9d375
5 changed files with 0 additions and 258 deletions

View File

@@ -1,95 +0,0 @@
#![allow(clippy::cast_ptr_alignment)]
use more_asserts::assert_le;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::ptr;
use wasmtime_environ::entity::EntityRef;
use wasmtime_environ::isa::TargetFrontendConfig;
use wasmtime_environ::wasm::GlobalInit;
use wasmtime_environ::{Module, ModuleType, TargetSharedSignatureIndex, VMOffsets};
pub struct TableRelocation {
pub index: usize,
pub offset: usize,
}
pub fn layout_vmcontext(
module: &Module,
target_config: &TargetFrontendConfig,
) -> (Box<[u8]>, Box<[TableRelocation]>) {
let ofs = VMOffsets::new(target_config.pointer_bytes(), &module);
let out_len = ofs.size_of_vmctx() as usize;
let mut out = vec![0; out_len];
// Assign unique indices to unique signatures.
let mut signature_registry = HashMap::new();
let mut signature_registry_len = signature_registry.len();
for (index, sig) in module.types.iter() {
let offset = ofs.vmctx_vmshared_signature_id(index) as usize;
let target_index = match sig {
ModuleType::Function(sig) => match signature_registry.entry(sig) {
Entry::Occupied(o) => *o.get(),
Entry::Vacant(v) => {
assert_le!(signature_registry_len, std::u32::MAX as usize);
let id = TargetSharedSignatureIndex::new(signature_registry_len as u32);
signature_registry_len += 1;
*v.insert(id)
}
},
_ => TargetSharedSignatureIndex::new(u32::max_value()),
};
unsafe {
let to = out.as_mut_ptr().add(offset) as *mut TargetSharedSignatureIndex;
ptr::write(to, target_index);
}
}
let num_tables_imports = module.num_imported_tables;
let mut table_relocs = Vec::with_capacity(module.table_plans.len() - num_tables_imports);
for (index, table) in module.table_plans.iter().skip(num_tables_imports) {
let def_index = module.defined_table_index(index).unwrap();
let offset = ofs.vmctx_vmtable_definition(def_index) as usize;
let current_elements = table.table.minimum;
unsafe {
assert_eq!(
::std::mem::size_of::<u32>() as u8,
ofs.size_of_vmtable_definition_current_elements(),
"vmtable_definition_current_elements expected to be u32"
);
let to = out
.as_mut_ptr()
.add(offset)
.add(ofs.vmtable_definition_current_elements() as usize);
ptr::write(to as *mut u32, current_elements);
}
table_relocs.push(TableRelocation {
index: def_index.index(),
offset,
});
}
let num_globals_imports = module.num_imported_globals;
for (index, global) in module.globals.iter().skip(num_globals_imports) {
let def_index = module.defined_global_index(index).unwrap();
let offset = ofs.vmctx_vmglobal_definition(def_index) as usize;
let to = unsafe { out.as_mut_ptr().add(offset) };
match global.initializer {
GlobalInit::I32Const(x) => unsafe {
ptr::write(to as *mut i32, x);
},
GlobalInit::I64Const(x) => unsafe {
ptr::write(to as *mut i64, x);
},
GlobalInit::F32Const(x) => unsafe {
ptr::write(to as *mut u32, x);
},
GlobalInit::F64Const(x) => unsafe {
ptr::write(to as *mut u64, x);
},
_ => panic!("unsupported global type"),
}
}
(out.into_boxed_slice(), table_relocs.into_boxed_slice())
}

View File

@@ -1,37 +0,0 @@
use anyhow::Result;
use object::write::{Object, StandardSection, Symbol, SymbolSection};
use object::{SymbolFlags, SymbolKind, SymbolScope};
use wasmtime_environ::MemoryInitializer;
/// Declares data segment symbol
pub fn declare_data_segment(
obj: &mut Object,
_memory_initializer: &MemoryInitializer,
index: usize,
) -> Result<()> {
let name = format!("_memory_{}", index);
let _symbol_id = obj.add_symbol(Symbol {
name: name.as_bytes().to_vec(),
value: 0,
size: 0,
kind: SymbolKind::Data,
scope: SymbolScope::Linkage,
weak: false,
section: SymbolSection::Undefined,
flags: SymbolFlags::None,
});
Ok(())
}
/// Emit segment data and initialization location
pub fn emit_data_segment(
obj: &mut Object,
memory_initializer: &MemoryInitializer,
index: usize,
) -> Result<()> {
let name = format!("_memory_{}", index);
let symbol_id = obj.symbol_id(name.as_bytes()).unwrap();
let section_id = obj.section_id(StandardSection::Data);
obj.add_symbol_data(symbol_id, section_id, &memory_initializer.data, 1);
Ok(())
}

View File

@@ -23,13 +23,8 @@
)] )]
mod builder; mod builder;
mod context;
mod data_segment;
mod module;
mod table;
pub use crate::builder::{utils, ObjectBuilder, ObjectBuilderTarget}; pub use crate::builder::{utils, ObjectBuilder, ObjectBuilderTarget};
pub use crate::module::emit_module;
/// Version number of this crate. /// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");

View File

@@ -1,92 +0,0 @@
use crate::builder::{ObjectBuilder, ObjectBuilderTarget};
use crate::context::layout_vmcontext;
use crate::data_segment::{declare_data_segment, emit_data_segment};
use crate::table::{declare_table, emit_table};
use anyhow::Result;
use object::write::{Object, Relocation, StandardSection, Symbol, SymbolSection};
use object::{RelocationEncoding, RelocationKind, SymbolFlags, SymbolKind, SymbolScope};
use wasmtime_debug::DwarfSection;
use wasmtime_environ::isa::TargetFrontendConfig;
use wasmtime_environ::{CompiledFunctions, MemoryInitialization, Module};
fn emit_vmcontext_init(
obj: &mut Object,
module: &Module,
target_config: &TargetFrontendConfig,
) -> Result<()> {
let (data, table_relocs) = layout_vmcontext(module, target_config);
let symbol_id = obj.add_symbol(Symbol {
name: "_vmcontext_init".as_bytes().to_vec(),
value: 0,
size: 0,
kind: SymbolKind::Data,
scope: SymbolScope::Linkage,
weak: false,
section: SymbolSection::Undefined,
flags: SymbolFlags::None,
});
let section_id = obj.section_id(StandardSection::Data);
let section_offset = obj.add_symbol_data(symbol_id, section_id, &data, 1);
for reloc in table_relocs.iter() {
let target_name = format!("_table_{}", reloc.index);
let target_symbol = obj.symbol_id(target_name.as_bytes()).unwrap();
obj.add_relocation(
section_id,
Relocation {
offset: section_offset + reloc.offset as u64,
size: 64, // FIXME for all targets
kind: RelocationKind::Absolute,
encoding: RelocationEncoding::Generic,
symbol: target_symbol,
addend: 0,
},
)?;
}
Ok(())
}
/// Emits a module that has been emitted with the `wasmtime-environ` environment
/// implementation to a native object file.
pub fn emit_module(
target: ObjectBuilderTarget,
module: &Module,
target_config: &TargetFrontendConfig,
compilation: CompiledFunctions,
dwarf_sections: Vec<DwarfSection>,
) -> Result<Object> {
let mut builder = ObjectBuilder::new(target, module, &compilation);
builder.set_dwarf_sections(dwarf_sections);
let mut obj = builder.build()?;
// Append data, table and vmcontext_init code to the object file.
match &module.memory_initialization {
MemoryInitialization::Segmented(initializers) => {
for (i, initializer) in initializers.iter().enumerate() {
declare_data_segment(&mut obj, initializer, i)?;
}
}
_ => unimplemented!(),
}
for i in 0..module.table_plans.len() {
declare_table(&mut obj, i)?;
}
match &module.memory_initialization {
MemoryInitialization::Segmented(initializers) => {
for (i, initializer) in initializers.iter().enumerate() {
emit_data_segment(&mut obj, initializer, i)?;
}
}
_ => unimplemented!(),
}
for i in 0..module.table_plans.len() {
emit_table(&mut obj, i)?;
}
emit_vmcontext_init(&mut obj, module, target_config)?;
Ok(obj)
}

View File

@@ -1,29 +0,0 @@
use anyhow::Result;
use object::write::{Object, StandardSection, Symbol, SymbolSection};
use object::{SymbolFlags, SymbolKind, SymbolScope};
/// Declares data segment symbol
pub fn declare_table(obj: &mut Object, index: usize) -> Result<()> {
let name = format!("_table_{}", index);
let _symbol_id = obj.add_symbol(Symbol {
name: name.as_bytes().to_vec(),
value: 0,
size: 0,
kind: SymbolKind::Data,
scope: SymbolScope::Linkage,
weak: false,
section: SymbolSection::Undefined,
flags: SymbolFlags::None,
});
Ok(())
}
/// Emit segment data and initialization location
pub fn emit_table(obj: &mut Object, index: usize) -> Result<()> {
let name = format!("_table_{}", index);
let symbol_id = obj.symbol_id(name.as_bytes()).unwrap();
let section_id = obj.section_id(StandardSection::Data);
// FIXME: We need to initialize table using function symbols
obj.add_symbol_data(symbol_id, section_id, &[], 1);
Ok(())
}