Skip to content

Commit

Permalink
Transform ranges and simple expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
yurydelendik committed Jul 3, 2019
1 parent 210e959 commit a8c771f
Show file tree
Hide file tree
Showing 21 changed files with 2,709 additions and 987 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ path = "src/wasm2obj.rs"
[dependencies]
cranelift-codegen = "0.32.0"
cranelift-native = "0.32.0"
cranelift-entity = "0.32.0"
cranelift-wasm = "0.32.0"
wasmtime-debug = { path = "wasmtime-debug" }
wasmtime-environ = { path = "wasmtime-environ" }
wasmtime-runtime = { path = "wasmtime-runtime" }
Expand All @@ -49,3 +51,6 @@ errno = "0.2.4"
[features]
lightbeam = ["wasmtime-environ/lightbeam", "wasmtime-jit/lightbeam"]
wasi-c = ["wasmtime-wasi-c"]

[patch.crates-io]
gimli = { git = "https://github.com/gimli-rs/gimli", rev="423431f6747167af8e64cc8c8378f9a34a18ff8f" }
42 changes: 31 additions & 11 deletions src/wasm2obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ extern crate serde_derive;

use cranelift_codegen::isa;
use cranelift_codegen::settings;
use cranelift_entity::EntityRef;
use cranelift_native;
use cranelift_wasm::DefinedMemoryIndex;
use docopt::Docopt;
use faerie::Artifact;
use std::error::Error;
Expand All @@ -48,8 +50,8 @@ use std::process;
use std::str;
use std::str::FromStr;
use target_lexicon::Triple;
use wasmtime_debug::{emit_debugsections, read_debuginfo};
use wasmtime_environ::{Compiler, Cranelift, ModuleEnvironment, Tunables};
use wasmtime_debug::{emit_debugsections, read_debuginfo, ModuleVmctxInfo};
use wasmtime_environ::{Compiler, Cranelift, ModuleEnvironment, Tunables, VMOffsets};
use wasmtime_obj::emit_module;

const USAGE: &str = "
Expand Down Expand Up @@ -159,13 +161,24 @@ fn handle_module(
)
};

let (compilation, relocations, address_transform) = Cranelift::compile_module(
&module,
lazy_function_body_inputs,
&*isa,
generate_debug_info,
)
.map_err(|e| e.to_string())?;
let (compilation, relocations, address_transform, value_ranges, stack_slots) =
Cranelift::compile_module(
&module,
lazy_function_body_inputs,
&*isa,
generate_debug_info,
)
.map_err(|e| e.to_string())?;

let module_vmctx_info = {
let ofs = VMOffsets::new(target_config.pointer_bytes(), &module);
let memory_offset = ofs.vmctx_vmmemory_definition(DefinedMemoryIndex::new(0)) as i64
+ ofs.vmmemory_definition_base() as i64;
ModuleVmctxInfo {
memory_offset,
stack_slots,
}
};

emit_module(
&mut obj,
Expand All @@ -178,8 +191,15 @@ fn handle_module(

if generate_debug_info {
let debug_data = read_debuginfo(&data);
emit_debugsections(&mut obj, &target_config, &debug_data, &address_transform)
.map_err(|e| e.to_string())?;
emit_debugsections(
&mut obj,
&module_vmctx_info,
&target_config,
&debug_data,
&address_transform,
&value_ranges,
)
.map_err(|e| e.to_string())?;
}

// FIXME: Make the format a parameter.
Expand Down
4 changes: 2 additions & 2 deletions wasmtime-debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ readme = "README.md"
edition = "2018"

[dependencies]
gimli = "0.17.0"
gimli = "0.18.0"
wasmparser = { version = "0.32.1" }
cranelift-codegen = "0.32.0"
cranelift-entity = "0.32.0"
cranelift-wasm = "0.32.0"
faerie = "0.10.1"
wasmtime-environ = { path = "../wasmtime-environ", default-features = false }
#wasmtime-environ = { path = "../wasmtime-environ", default-features = false }
target-lexicon = { version = "0.4.0", default-features = false }
failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false }
Expand Down
139 changes: 0 additions & 139 deletions wasmtime-debug/src/address_transform.rs

This file was deleted.

26 changes: 15 additions & 11 deletions wasmtime-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use faerie::{Artifact, Decl};
use failure::Error;
use target_lexicon::{BinaryFormat, Triple};

pub use crate::read_debuginfo::{read_debuginfo, DebugInfoData};
pub use crate::transform::transform_dwarf;
pub use crate::read_debuginfo::{read_debuginfo, DebugInfoData, WasmFileInfo};
pub use crate::transform::{
transform_dwarf, FunctionAddressMap, InstructionAddressMap, ModuleAddressMap, ModuleVmctxInfo,
ValueLabelsRanges,
};
pub use crate::write_debuginfo::{emit_dwarf, ResolvedSymbol, SymbolResolver};

use wasmtime_environ::AddressTransforms;

mod address_transform;
mod read_debuginfo;
mod transform;
mod write_debuginfo;
Expand All @@ -28,13 +28,15 @@ impl SymbolResolver for FunctionRelocResolver {

pub fn emit_debugsections(
obj: &mut Artifact,
vmctx_info: &ModuleVmctxInfo,
target_config: &TargetFrontendConfig,
debuginfo_data: &DebugInfoData,
at: &AddressTransforms,
at: &ModuleAddressMap,
ranges: &ValueLabelsRanges,
) -> Result<(), Error> {
let dwarf = transform_dwarf(target_config, debuginfo_data, at)?;
let resolver = FunctionRelocResolver {};
emit_dwarf(obj, dwarf, &resolver);
let dwarf = transform_dwarf(target_config, debuginfo_data, at, vmctx_info, ranges)?;
emit_dwarf(obj, dwarf, &resolver)?;
Ok(())
}

Expand All @@ -53,16 +55,18 @@ pub fn emit_debugsections_image(
triple: Triple,
target_config: &TargetFrontendConfig,
debuginfo_data: &DebugInfoData,
at: &AddressTransforms,
vmctx_info: &ModuleVmctxInfo,
at: &ModuleAddressMap,
ranges: &ValueLabelsRanges,
funcs: &Vec<(*const u8, usize)>,
) -> Result<Vec<u8>, Error> {
let ref func_offsets = funcs
.iter()
.map(|(ptr, _)| *ptr as u64)
.collect::<Vec<u64>>();
let mut obj = Artifact::new(triple, String::from("module"));
let dwarf = transform_dwarf(target_config, debuginfo_data, at)?;
let resolver = ImageRelocResolver { func_offsets };
let dwarf = transform_dwarf(target_config, debuginfo_data, at, vmctx_info, ranges)?;

// Assuming all functions in the same code block, looking min/max of its range.
assert!(funcs.len() > 0);
Expand All @@ -76,7 +80,7 @@ pub fn emit_debugsections_image(
let body = unsafe { ::std::slice::from_raw_parts(segment_body.0, segment_body.1) };
obj.declare_with("all", Decl::function(), body.to_vec())?;

emit_dwarf(&mut obj, dwarf, &resolver);
emit_dwarf(&mut obj, dwarf, &resolver)?;

// LLDB is too "magical" about mach-o, generating elf
let mut bytes = obj.emit_as(BinaryFormat::Elf)?;
Expand Down
13 changes: 0 additions & 13 deletions wasmtime-debug/src/read_debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub type Dwarf<'input> = gimli::Dwarf<gimli::EndianSlice<'input, LittleEndian>>;
#[derive(Debug)]
pub struct WasmFileInfo {
pub code_section_offset: u64,
pub function_offsets_and_sizes: Box<[(u64, u32)]>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -100,7 +99,6 @@ pub fn read_debuginfo(data: &[u8]) -> DebugInfoData {
let mut reader = ModuleReader::new(data).expect("reader");
let mut sections = HashMap::new();
let mut code_section_offset = 0;
let mut function_offsets_and_sizes = Vec::new();
while !reader.eof() {
let section = reader.read().expect("section");
if let SectionCode::Custom { name, .. } = section.code {
Expand All @@ -112,23 +110,12 @@ pub fn read_debuginfo(data: &[u8]) -> DebugInfoData {
}
if let SectionCode::Code = section.code {
code_section_offset = section.range().start as u64;
// TODO remove me later
let mut reader = section.get_code_section_reader().expect("code reader");
for _ in 0..reader.get_count() {
let body = reader.read().expect("function body read");
let range = body.range();
let fn_body_size = range.end - range.start;
let fn_body_offset = range.start;
function_offsets_and_sizes.push((fn_body_offset as u64, fn_body_size as u32));
}
}
}
let function_offsets_and_sizes = function_offsets_and_sizes.into_boxed_slice();
DebugInfoData {
dwarf: convert_sections(sections),
wasm_file: WasmFileInfo {
code_section_offset,
function_offsets_and_sizes,
},
}
}
Loading

0 comments on commit a8c771f

Please sign in to comment.