Generate debug info for LLDB/GDB (#50)

* Transform DWARF sections into native format for wasm2obj and wasmtime.

Generate DWARF sections based on WASM DWARF.
Ignore some of debug_info/debug_line for dead code.

* Fix test
This commit is contained in:
Yury Delendik
2019-03-06 18:03:32 -06:00
committed by Dan Gohman
parent 6eb09d9edd
commit ddbc00752e
36 changed files with 2252 additions and 85 deletions

View File

@@ -14,5 +14,6 @@ edition = "2018"
[dependencies]
cranelift-codegen = "0.28.0"
cranelift-entity = "0.28.0"
cranelift-wasm = "0.28.0"
wasmtime-environ = { path = "../environ" }
faerie = "0.7.1"

90
lib/obj/src/context.rs Normal file
View File

@@ -0,0 +1,90 @@
use cranelift_codegen::isa::TargetFrontendConfig;
use cranelift_entity::EntityRef;
use cranelift_wasm::GlobalInit;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::ptr;
use wasmtime_environ::{Module, 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::with_capacity(out_len);
out.resize(out_len, 0);
// Assign unique indicies to unique signatures.
let mut signature_registry = HashMap::new();
let mut signature_registry_len = signature_registry.len();
for (index, sig) in module.signatures.iter() {
let offset = ofs.vmctx_vmshared_signature_id(index) as usize;
let target_index = match signature_registry.entry(sig) {
Entry::Occupied(o) => *o.get(),
Entry::Vacant(v) => {
assert!(signature_registry_len <= ::std::u32::MAX as usize);
let id = TargetSharedSignatureIndex::new(signature_registry_len as u32);
signature_registry_len += 1;
*v.insert(id)
}
};
unsafe {
let to = out.as_mut_ptr().add(offset) as *mut TargetSharedSignatureIndex;
ptr::write(to, target_index);
}
}
let num_tables_imports = module.imported_tables.len();
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 as usize;
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 as u32);
}
table_relocs.push(TableRelocation {
index: def_index.index(),
offset,
});
}
let num_globals_imports = module.imported_globals.len();
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

@@ -0,0 +1,32 @@
use faerie::{Artifact, Decl};
use wasmtime_environ::DataInitializer;
/// Declares data segment symbol
pub fn declare_data_segment(
obj: &mut Artifact,
_data_initaliazer: &DataInitializer,
index: usize,
) -> Result<(), String> {
let name = format!("_memory_{}", index);
obj.declare(
name,
Decl::Data {
writable: false,
global: false,
},
)
.map_err(|err| format!("{}", err))?;
Ok(())
}
/// Emit segment data and initialization location
pub fn emit_data_segment(
obj: &mut Artifact,
data_initaliazer: &DataInitializer,
index: usize,
) -> Result<(), String> {
let name = format!("_memory_{}", index);
obj.define(name, Vec::from(data_initaliazer.data))
.map_err(|err| format!("{}", err))?;
Ok(())
}

View File

@@ -1,37 +0,0 @@
use cranelift_codegen::settings;
use cranelift_codegen::settings::Configurable;
use cranelift_entity::EntityRef;
use faerie::Artifact;
use wasmtime_environ::{Compilation, Module, Relocations};
/// Emits a module that has been emitted with the `wasmtime-environ` environment
/// implementation to a native object file.
pub fn emit_module(
obj: &mut Artifact,
module: &Module,
compilation: &Compilation,
relocations: &Relocations,
) -> Result<(), String> {
debug_assert!(
module.start_func.is_none()
|| module.start_func.unwrap().index() >= module.imported_funcs.len(),
"imported start functions not supported yet"
);
let mut shared_builder = settings::builder();
shared_builder
.enable("enable_verifier")
.expect("Missing enable_verifier setting");
for (i, function_relocs) in relocations.iter() {
assert!(function_relocs.is_empty(), "relocations not supported yet");
let body = &compilation.functions[i];
let func_index = module.func_index(i);
let string_name = format!("wasm_function[{}]", func_index.index());
obj.define(string_name, body.clone())
.map_err(|err| format!("{}", err))?;
}
Ok(())
}

70
lib/obj/src/function.rs Normal file
View File

@@ -0,0 +1,70 @@
use cranelift_codegen::settings;
use cranelift_codegen::settings::Configurable;
use cranelift_entity::EntityRef;
use faerie::{Artifact, Decl, Link};
use wasmtime_environ::{Compilation, Module, RelocationTarget, Relocations};
/// Defines module functions
pub fn declare_functions(
obj: &mut Artifact,
module: &Module,
relocations: &Relocations,
) -> Result<(), String> {
for (i, _function_relocs) in relocations.iter().rev() {
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
obj.declare(string_name, Decl::Function { global: true })
.map_err(|err| format!("{}", err))?;
}
Ok(())
}
/// Emits module functions
pub fn emit_functions(
obj: &mut Artifact,
module: &Module,
compilation: &Compilation,
relocations: &Relocations,
) -> Result<(), String> {
debug_assert!(
module.start_func.is_none()
|| module.start_func.unwrap().index() >= module.imported_funcs.len(),
"imported start functions not supported yet"
);
let mut shared_builder = settings::builder();
shared_builder
.enable("enable_verifier")
.expect("Missing enable_verifier setting");
for (i, _function_relocs) in relocations.iter() {
let body = &compilation.functions[i];
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
obj.define(string_name, body.clone())
.map_err(|err| format!("{}", err))?;
}
for (i, function_relocs) in relocations.iter() {
let func_index = module.func_index(i);
let string_name = format!("_wasm_function_{}", func_index.index());
for r in function_relocs {
debug_assert_eq!(r.addend, 0);
match r.reloc_target {
RelocationTarget::UserFunc(target_index) => {
let target_name = format!("_wasm_function_{}", target_index.index());
obj.link(Link {
from: &string_name,
to: &target_name,
at: r.offset as u64,
})
.map_err(|err| format!("{}", err))?;
}
_ => panic!("relocations target not supported yet"),
};
}
}
Ok(())
}

View File

@@ -26,9 +26,13 @@
)
)]
mod emit_module;
mod context;
mod data_segment;
mod function;
mod module;
mod table;
pub use crate::emit_module::emit_module;
pub use crate::module::emit_module;
/// Version number of this crate.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

69
lib/obj/src/module.rs Normal file
View File

@@ -0,0 +1,69 @@
use crate::context::layout_vmcontext;
use crate::data_segment::{declare_data_segment, emit_data_segment};
use crate::function::{declare_functions, emit_functions};
use crate::table::{declare_table, emit_table};
use cranelift_codegen::isa::TargetFrontendConfig;
use faerie::{Artifact, Decl, Link};
use wasmtime_environ::{Compilation, DataInitializer, Module, Relocations};
fn emit_vmcontext_init(
obj: &mut Artifact,
module: &Module,
target_config: &TargetFrontendConfig,
) -> Result<(), String> {
let (data, table_relocs) = layout_vmcontext(module, target_config);
obj.declare_with(
"_vmcontext_init",
Decl::Data {
writable: false,
global: true,
},
data.to_vec(),
)
.map_err(|err| format!("{}", err))?;
for reloc in table_relocs.iter() {
let target_name = format!("_table_{}", reloc.index);
obj.link(Link {
from: "_vmcontext_init",
to: &target_name,
at: reloc.offset as u64,
})
.map_err(|err| format!("{}", err))?;
}
Ok(())
}
/// Emits a module that has been emitted with the `wasmtime-environ` environment
/// implementation to a native object file.
pub fn emit_module(
obj: &mut Artifact,
module: &Module,
compilation: &Compilation,
relocations: &Relocations,
data_initializers: &[DataInitializer],
target_config: &TargetFrontendConfig,
) -> Result<(), String> {
declare_functions(obj, module, relocations)?;
for i in 0..data_initializers.len() {
declare_data_segment(obj, &data_initializers[i], i)?;
}
for i in 0..module.table_plans.len() {
declare_table(obj, i)?;
}
emit_functions(obj, module, compilation, relocations)?;
for i in 0..data_initializers.len() {
emit_data_segment(obj, &data_initializers[i], i)?;
}
for i in 0..module.table_plans.len() {
emit_table(obj, i)?;
}
emit_vmcontext_init(obj, module, target_config)?;
Ok(())
}

24
lib/obj/src/table.rs Normal file
View File

@@ -0,0 +1,24 @@
use faerie::{Artifact, Decl};
/// Declares data segment symbol
pub fn declare_table(obj: &mut Artifact, index: usize) -> Result<(), String> {
let name = format!("_table_{}", index);
obj.declare(
name,
Decl::Data {
writable: false,
global: false,
},
)
.map_err(|err| format!("{}", err))?;
Ok(())
}
/// Emit segment data and initialization location
pub fn emit_table(obj: &mut Artifact, index: usize) -> Result<(), String> {
let name = format!("_table_{}", index);
// FIXME: We need to initialize table using function symbols
obj.define(name, Vec::new())
.map_err(|err| format!("{}", err))?;
Ok(())
}