Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/cli-flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ wasmtime_option_group! {
pub inherit_stderr: Option<bool>,
/// Maximum number of frames to capture in backtraces.
pub max_backtrace: Option<usize>,
/// Whether or not `*.cwasm` files have symbols in them.
pub symbols: Option<bool>,
}

enum Debug {
Expand Down Expand Up @@ -974,6 +976,9 @@ impl CommonOptions {
}
}
}
if let Some(enable) = self.debug.symbols {
config.debug_symbols(enable);
}
if let Some(enable) = self.opts.memory_init_cow {
config.memory_init_cow(enable);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/cranelift/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ impl wasmtime_environ::Compiler for Compiler {
obj: &mut Object<'static>,
funcs: &[(String, FuncKey, Box<dyn Any + Send + Sync>)],
resolve_reloc: &dyn Fn(usize, FuncKey) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>> {
) -> Result<Vec<(Option<SymbolId>, FunctionLoc)>> {
log::trace!(
"appending functions to object file: {:#?}",
funcs.iter().map(|(sym, _, _)| sym).collect::<Vec<_>>()
Expand Down Expand Up @@ -878,7 +878,7 @@ impl wasmtime_environ::Compiler for Compiler {
get_func: &'a dyn Fn(
StaticModuleIndex,
DefinedFuncIndex,
) -> (SymbolId, &'a (dyn Any + Send + Sync)),
) -> (Option<SymbolId>, &'a (dyn Any + Send + Sync)),
dwarf_package_bytes: Option<&'a [u8]>,
tunables: &'a Tunables,
) -> Result<()> {
Expand Down Expand Up @@ -922,7 +922,7 @@ impl wasmtime_environ::Compiler for Compiler {
let section_id = *dwarf_sections_ids.get(name).unwrap();
for reloc in relocs {
let target_symbol = match reloc.target {
DwarfSectionRelocTarget::Func(id) => compilation.symbol_id(id),
DwarfSectionRelocTarget::Func(id) => compilation.symbol_id(id).unwrap(),
DwarfSectionRelocTarget::Section(name) => {
obj.section_symbol(dwarf_sections_ids[name])
}
Expand Down
28 changes: 19 additions & 9 deletions crates/cranelift/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ pub struct Compilation<'a> {
///
/// This returns the `object`-based-symbol for the function as well as the
/// `&CompiledFunction`.
get_func:
&'a dyn Fn(StaticModuleIndex, DefinedFuncIndex) -> (SymbolId, &'a CompiledFunctionMetadata),
get_func: &'a dyn Fn(
StaticModuleIndex,
DefinedFuncIndex,
) -> (Option<SymbolId>, &'a CompiledFunctionMetadata),

/// Optionally-specified `*.dwp` file, currently only supported for core
/// wasm modules.
Expand All @@ -68,7 +70,7 @@ pub struct Compilation<'a> {

/// Translation between `SymbolId` and a `usize`-based symbol which gimli
/// uses.
symbol_index_to_id: Vec<SymbolId>,
symbol_index_to_id: Vec<Option<SymbolId>>,
symbol_id_to_index: HashMap<SymbolId, (usize, StaticModuleIndex, DefinedFuncIndex)>,

/// The `ModuleMemoryOffset` for each module within `translations`.
Expand All @@ -84,7 +86,7 @@ impl<'a> Compilation<'a> {
get_func: &'a dyn Fn(
StaticModuleIndex,
DefinedFuncIndex,
) -> (SymbolId, &'a CompiledFunctionMetadata),
) -> (Option<SymbolId>, &'a CompiledFunctionMetadata),
dwarf_package_bytes: Option<&'a [u8]>,
tunables: &'a Tunables,
) -> Compilation<'a> {
Expand Down Expand Up @@ -127,7 +129,9 @@ impl<'a> Compilation<'a> {
for (module, translation) in translations {
for func in translation.module.defined_func_indices() {
let (sym, _func) = get_func(module, func);
symbol_id_to_index.insert(sym, (symbol_index_to_id.len(), module, func));
if let Some(sym) = sym {
symbol_id_to_index.insert(sym, (symbol_index_to_id.len(), module, func));
}
symbol_index_to_id.push(sym);
}
}
Expand Down Expand Up @@ -157,7 +161,13 @@ impl<'a> Compilation<'a> {
/// function metadata that were produced during compilation.
fn functions(
&self,
) -> impl Iterator<Item = (StaticModuleIndex, usize, &'a CompiledFunctionMetadata)> + '_ {
) -> impl Iterator<
Item = (
StaticModuleIndex,
Option<usize>,
&'a CompiledFunctionMetadata,
),
> + '_ {
self.indexes().map(move |(module, func)| {
let (sym, func) = self.function(module, func);
(module, sym, func)
Expand All @@ -169,14 +179,14 @@ impl<'a> Compilation<'a> {
&self,
module: StaticModuleIndex,
func: DefinedFuncIndex,
) -> (usize, &'a CompiledFunctionMetadata) {
) -> (Option<usize>, &'a CompiledFunctionMetadata) {
let (sym, func) = (self.get_func)(module, func);
(self.symbol_id_to_index[&sym].0, func)
(sym.map(|sym| self.symbol_id_to_index[&sym].0), func)
}

/// Maps a `usize`-based symbol used by gimli to the object-based
/// `SymbolId`.
pub fn symbol_id(&self, sym: usize) -> SymbolId {
pub fn symbol_id(&self, sym: usize) -> Option<SymbolId> {
self.symbol_index_to_id[sym]
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/cranelift/src/debug/transform/address_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct AddressMap {
/// length, and instructions addresses.
#[derive(Debug)]
pub struct FunctionMap {
pub symbol: usize,
pub symbol: Option<usize>,
pub offset: GeneratedAddress,
pub len: GeneratedAddress,
pub wasm_start: WasmAddress,
Expand Down Expand Up @@ -514,7 +514,7 @@ impl AddressTransform {
object::Endianness::Little,
);
let dummy_symbol = dummy_obj.add_file_symbol(Vec::new());
let func_lookup = move |_, f| (dummy_symbol, module_map[f]);
let func_lookup = move |_, f| (Some(dummy_symbol), module_map[f]);
let tunables = wasmtime_environ::Tunables::default_host();
let compile = Compilation::new(
&*cranelift_codegen::isa::lookup(target_lexicon::Triple::host())
Expand Down Expand Up @@ -565,10 +565,10 @@ impl AddressTransform {
if addr == func.end {
// Clamp last address to the end to extend translation to the end
// of the function.
return Some((map.symbol, map.len));
return Some((map.symbol?, map.len));
}
let first_result = TransformRangeStartIter::new(func, addr).next();
first_result.map(|(address, _)| (map.symbol, address))
first_result.and_then(|(address, _)| Some((map.symbol?, address)))
} else {
// Address was not found: function was not compiled?
None
Expand Down Expand Up @@ -598,7 +598,7 @@ impl AddressTransform {
}
if let Some(func) = self.find_func(start) {
let result = TransformRangeIter::new(func, start, end);
let symbol = self.map[func.index].symbol;
let symbol = self.map[func.index].symbol?;
return Some((symbol, result));
}
// Address was not found: function was not compiled?
Expand Down
4 changes: 3 additions & 1 deletion crates/cranelift/src/debug/transform/line_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub(crate) fn clone_line_program(
let Some(map) = addr_tr.map().get(index) else {
continue; // no code generated
};
let symbol = map.symbol;
let Some(symbol) = map.symbol else {
continue;
};
let base_addr = map.offset;
transform.begin_sequence(Some(write::Address::Symbol { symbol, addend: 0 }));
// TODO track and place function declaration line here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ impl RangeInfoBuilder {
}
}
RangeInfoBuilder::Function(index) => {
let symbol = addr_tr.map()[*index].symbol;
let Some(symbol) = addr_tr.map()[*index].symbol else {
return;
};
let range = addr_tr.func_range(*index);
let addr = write::Address::Symbol {
symbol,
Expand Down
9 changes: 6 additions & 3 deletions crates/cranelift/src/debug/transform/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ fn generate_line_info(

let maps = addr_tr.iter().flat_map(|(_, transform)| {
transform.map().iter().filter_map(|(_, map)| {
if translated.contains(&map.symbol) {
let sym = map.symbol?;
if translated.contains(&sym) {
None
} else {
Some((map.symbol, map))
Some((sym, map))
}
})
});
Expand Down Expand Up @@ -357,7 +358,9 @@ pub fn generate_simulated_dwarf(
let wasm_types = add_wasm_types(unit, root_id, out_strings);
let mut unit_ranges = vec![];
for (module, index) in compilation.indexes().collect::<Vec<_>>() {
let (symbol, _) = compilation.function(module, index);
let (Some(symbol), _) = compilation.function(module, index) else {
continue;
};
if translated.contains(&symbol) {
continue;
}
Expand Down
8 changes: 5 additions & 3 deletions crates/cranelift/src/debug/transform/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,11 @@ pub(crate) fn clone_unit<'a>(
let frame_info = compilation.function_frame_info(module, func);
current_value_range.push(new_stack_len, frame_info);
let (symbol, _) = compilation.function(module, func);
translated.insert(symbol);
current_scope_ranges.push(new_stack_len, range_builder.get_ranges(addr_tr));
Some(range_builder)
symbol.map(|symbol| {
translated.insert(symbol);
current_scope_ranges.push(new_stack_len, range_builder.get_ranges(addr_tr));
range_builder
})
} else {
// FIXME current_scope_ranges.push()
None
Expand Down
3 changes: 3 additions & 0 deletions crates/cranelift/src/debug/write_debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ fn create_frame_table(
let cie_id = table.add_cie(isa.create_systemv_cie()?);

for (_, symbol, metadata) in compilation.functions() {
let Some(symbol) = symbol else {
continue;
};
// The CFA-based unwind info will either be natively present, or we
// have generated it and placed into the "cfa_unwind_info" auxiliary
// field. We shouldn't emit both, though, it'd be wasteful.
Expand Down
46 changes: 22 additions & 24 deletions crates/cranelift/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! function body, the imported wasm function do not. The trampolines symbol
//! names have format "_trampoline_N", where N is `SignatureIndex`.

use crate::CompiledFunction;
use crate::{CompiledFunction, Compiler};
use cranelift_codegen::TextSectionBuilder;
use cranelift_codegen::isa::unwind::{UnwindInfo, systemv};
use cranelift_control::ControlPlane;
Expand All @@ -23,12 +23,11 @@ use object::write::{Object, SectionId, StandardSegment, Symbol, SymbolId, Symbol
use object::{Architecture, SectionFlags, SectionKind, SymbolFlags, SymbolKind, SymbolScope};
use std::ops::Range;
use wasmtime_environ::error::Result;
use wasmtime_environ::{Compiler, TripleExt};
use wasmtime_environ::{FuncKey, obj};
use wasmtime_environ::{Compiler as _, FuncKey, TripleExt, obj};

const TEXT_SECTION_NAME: &[u8] = b".text";

fn text_align(compiler: &dyn Compiler) -> u64 {
fn text_align(compiler: &Compiler) -> u64 {
// text pages will not be made executable with pulley, so the section
// doesn't need to be padded out to page alignment boundaries.
if compiler.triple().is_pulley() {
Expand All @@ -47,7 +46,7 @@ fn text_align(compiler: &dyn Compiler) -> u64 {
pub struct ModuleTextBuilder<'a> {
/// The target that we're compiling for, used to query target-specific
/// information as necessary.
compiler: &'a dyn Compiler,
compiler: &'a Compiler,

/// The object file that we're generating code into.
obj: &'a mut Object<'static>,
Expand All @@ -73,7 +72,7 @@ impl<'a> ModuleTextBuilder<'a> {
/// be called. The `finish` function will panic if this contract is not met.
pub fn new(
obj: &'a mut Object<'static>,
compiler: &'a dyn Compiler,
compiler: &'a Compiler,
text: Box<dyn TextSectionBuilder>,
) -> Self {
// Entire code (functions and trampolines) will be placed
Expand Down Expand Up @@ -121,24 +120,28 @@ impl<'a> ModuleTextBuilder<'a> {
name: &str,
compiled_func: &'a CompiledFunction,
resolve_reloc_target: impl Fn(wasmtime_environ::FuncKey) -> usize,
) -> (SymbolId, Range<u64>) {
) -> (Option<SymbolId>, Range<u64>) {
let body = compiled_func.buffer.data();
let alignment = compiled_func.alignment;
let body_len = body.len() as u64;
let off = self
.text
.append(true, &body, alignment, &mut self.ctrl_plane);

let symbol_id = self.obj.add_symbol(Symbol {
name: name.as_bytes().to_vec(),
value: off,
size: body_len,
kind: SymbolKind::Text,
scope: SymbolScope::Compilation,
weak: false,
section: SymbolSection::Section(self.text_section),
flags: SymbolFlags::None,
});
let symbol_id = if self.compiler.tunables().debug_symbols {
Some(self.obj.add_symbol(Symbol {
name: name.as_bytes().to_vec(),
value: off,
size: body_len,
kind: SymbolKind::Text,
scope: SymbolScope::Compilation,
weak: false,
section: SymbolSection::Section(self.text_section),
flags: SymbolFlags::None,
}))
} else {
None
};

if let Some(info) = compiled_func.unwind_info() {
self.unwind_info.push(off, body_len, info);
Expand Down Expand Up @@ -375,12 +378,7 @@ impl<'a> UnwindInfoBuilder<'a> {
/// section immediately.
///
/// The `text_section`'s section identifier is passed into this function.
fn append_section(
&self,
compiler: &dyn Compiler,
obj: &mut Object<'_>,
text_section: SectionId,
) {
fn append_section(&self, compiler: &Compiler, obj: &mut Object<'_>, text_section: SectionId) {
// This write will align the text section to a page boundary and then
// return the offset at that point. This gives us the full size of the
// text section at that point, after alignment.
Expand Down Expand Up @@ -513,7 +511,7 @@ impl<'a> UnwindInfoBuilder<'a> {
/// bits.
fn write_systemv_unwind_info(
&self,
compiler: &dyn Compiler,
compiler: &Compiler,
obj: &mut Object<'_>,
section_id: SectionId,
text_section_size: u64,
Expand Down
4 changes: 2 additions & 2 deletions crates/environ/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub trait Compiler: Send + Sync {
obj: &mut Object<'static>,
funcs: &[(String, FuncKey, Box<dyn Any + Send + Sync>)],
resolve_reloc: &dyn Fn(usize, FuncKey) -> usize,
) -> Result<Vec<(SymbolId, FunctionLoc)>>;
) -> Result<Vec<(Option<SymbolId>, FunctionLoc)>>;

/// Creates a new `Object` file which is used to build the results of a
/// compilation into.
Expand Down Expand Up @@ -417,7 +417,7 @@ pub trait Compiler: Send + Sync {
get_func: &'a dyn Fn(
StaticModuleIndex,
DefinedFuncIndex,
) -> (SymbolId, &'a (dyn Any + Send + Sync)),
) -> (Option<SymbolId>, &'a (dyn Any + Send + Sync)),
dwarf_package_bytes: Option<&'a [u8]>,
tunables: &'a Tunables,
) -> Result<()>;
Expand Down
5 changes: 5 additions & 0 deletions crates/environ/src/tunables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ define_tunables! {
/// the guest.
pub debug_guest: bool,

/// Whether we are enabling native symbols to get inserted into the
/// final `*.cwasm`.
pub debug_symbols: bool,

/// Whether or not to retain DWARF sections in compiled modules.
pub parse_wasm_debuginfo: bool,

Expand Down Expand Up @@ -278,6 +282,7 @@ impl Tunables {
metadata_for_internal_asserts: false,
metadata_for_gc_heap_corruption: true,
branch_hinting: false,
debug_symbols: true,
}
}

Expand Down
Loading
Loading