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