wasmtime_environ/
module_artifacts.rs

1//! Definitions of runtime structures and metadata which are serialized into ELF
2//! with `bincode` as part of a module's compilation process.
3
4use crate::prelude::*;
5use crate::{
6    DefinedFuncIndex, FilePos, FuncIndex, Module, ModuleInternedTypeIndex, PrimaryMap, StackMap,
7};
8use core::fmt;
9use core::ops::Range;
10use core::str;
11use serde_derive::{Deserialize, Serialize};
12
13/// Secondary in-memory results of function compilation.
14#[derive(Serialize, Deserialize)]
15pub struct CompiledFunctionInfo {
16    /// The [`WasmFunctionInfo`] for this function.
17    pub wasm_func_info: WasmFunctionInfo,
18    /// The [`FunctionLoc`] indicating the location of this function in the text
19    /// section of the competition artifact.
20    pub wasm_func_loc: FunctionLoc,
21    /// A trampoline for array callers (e.g. `Func::new`) calling into this function (if needed).
22    pub array_to_wasm_trampoline: Option<FunctionLoc>,
23}
24
25/// Information about a function, such as trap information, address map,
26/// and stack maps.
27#[derive(Serialize, Deserialize, Default)]
28#[allow(missing_docs, reason = "self-describing fields")]
29pub struct WasmFunctionInfo {
30    pub start_srcloc: FilePos,
31    pub stack_maps: Box<[StackMapInformation]>,
32}
33
34/// Description of where a function is located in the text section of a
35/// compiled image.
36#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
37pub struct FunctionLoc {
38    /// The byte offset from the start of the text section where this
39    /// function starts.
40    pub start: u32,
41    /// The byte length of this function's function body.
42    pub length: u32,
43}
44
45/// The offset within a function of a GC safepoint, and its associated stack
46/// map.
47#[derive(Serialize, Deserialize, Debug)]
48pub struct StackMapInformation {
49    /// The offset of the GC safepoint within the function's native code. It is
50    /// relative to the beginning of the function.
51    pub code_offset: u32,
52
53    /// The stack map for identifying live GC refs at the GC safepoint.
54    pub stack_map: StackMap,
55}
56
57/// Secondary in-memory results of module compilation.
58///
59/// This opaque structure can be optionally passed back to
60/// `CompiledModule::from_artifacts` to avoid decoding extra information there.
61#[derive(Serialize, Deserialize)]
62pub struct CompiledModuleInfo {
63    /// Type information about the compiled WebAssembly module.
64    pub module: Module,
65
66    /// Metadata about each compiled function.
67    pub funcs: PrimaryMap<DefinedFuncIndex, CompiledFunctionInfo>,
68
69    /// Sorted list, by function index, of names we have for this module.
70    pub func_names: Vec<FunctionName>,
71
72    /// Metadata about wasm-to-array trampolines. Used when exposing a native
73    /// callee (e.g. `Func::wrap`) to a Wasm caller. Sorted by signature index.
74    pub wasm_to_array_trampolines: Vec<(ModuleInternedTypeIndex, FunctionLoc)>,
75
76    /// General compilation metadata.
77    pub meta: Metadata,
78}
79
80/// The name of a function stored in the
81/// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
82#[derive(Serialize, Deserialize)]
83pub struct FunctionName {
84    /// The Wasm function index of this function.
85    pub idx: FuncIndex,
86    /// The offset of the name in the
87    /// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
88    pub offset: u32,
89    /// The length of the name in bytes.
90    pub len: u32,
91}
92
93/// Metadata associated with a compiled ELF artifact.
94#[derive(Serialize, Deserialize)]
95pub struct Metadata {
96    /// Whether or not the original wasm module contained debug information that
97    /// we skipped and did not parse.
98    pub has_unparsed_debuginfo: bool,
99
100    /// Offset in the original wasm file to the code section.
101    pub code_section_offset: u64,
102
103    /// Whether or not custom wasm-specific dwarf sections were inserted into
104    /// the ELF image.
105    ///
106    /// Note that even if this flag is `true` sections may be missing if they
107    /// weren't found in the original wasm module itself.
108    pub has_wasm_debuginfo: bool,
109
110    /// Dwarf sections and the offsets at which they're stored in the
111    /// ELF_WASMTIME_DWARF
112    pub dwarf: Vec<(u8, Range<u64>)>,
113}
114
115/// Value of a configured setting for a [`Compiler`](crate::Compiler)
116#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
117pub enum FlagValue<'a> {
118    /// Name of the value that has been configured for this setting.
119    Enum(&'a str),
120    /// The numerical value of the configured settings.
121    Num(u8),
122    /// Whether the setting is on or off.
123    Bool(bool),
124}
125
126impl fmt::Display for FlagValue<'_> {
127    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128        match self {
129            Self::Enum(v) => v.fmt(f),
130            Self::Num(v) => v.fmt(f),
131            Self::Bool(v) => v.fmt(f),
132        }
133    }
134}
135
136/// Types of objects that can be created by `Compiler::object`
137pub enum ObjectKind {
138    /// A core wasm compilation artifact
139    Module,
140    /// A component compilation artifact
141    Component,
142}