Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/environ/src/module_artifacts.rs
1691 views
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, ModuleInternedTypeIndex, PrimaryMap};
6
use core::fmt;
7
use core::ops::Range;
8
use core::str;
9
use serde_derive::{Deserialize, Serialize};
10
11
/// Secondary in-memory results of function compilation.
12
#[derive(Clone, Serialize, Deserialize)]
13
pub struct CompiledFunctionInfo {
14
/// Where this function was found in the original wasm file.
15
pub start_srcloc: FilePos,
16
/// The [`FunctionLoc`] indicating the location of this function in the text
17
/// section of the competition artifact.
18
pub wasm_func_loc: FunctionLoc,
19
/// A trampoline for array callers (e.g. `Func::new`) calling into this function (if needed).
20
pub array_to_wasm_trampoline: Option<FunctionLoc>,
21
}
22
23
/// Description of where a function is located in the text section of a
24
/// compiled image.
25
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
26
pub struct FunctionLoc {
27
/// The byte offset from the start of the text section where this
28
/// function starts.
29
pub start: u32,
30
/// The byte length of this function's function body.
31
pub length: u32,
32
}
33
34
/// Secondary in-memory results of module compilation.
35
///
36
/// This opaque structure can be optionally passed back to
37
/// `CompiledModule::from_artifacts` to avoid decoding extra information there.
38
#[derive(Serialize, Deserialize)]
39
pub struct CompiledModuleInfo {
40
/// Type information about the compiled WebAssembly module.
41
pub module: Module,
42
43
/// Metadata about each compiled function.
44
pub funcs: PrimaryMap<DefinedFuncIndex, CompiledFunctionInfo>,
45
46
/// Sorted list, by function index, of names we have for this module.
47
pub func_names: Vec<FunctionName>,
48
49
/// Metadata about wasm-to-array trampolines. Used when exposing a native
50
/// callee (e.g. `Func::wrap`) to a Wasm caller. Sorted by signature index.
51
pub wasm_to_array_trampolines: Vec<(ModuleInternedTypeIndex, FunctionLoc)>,
52
53
/// General compilation metadata.
54
pub meta: Metadata,
55
}
56
57
/// The name of a function stored in the
58
/// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
59
#[derive(Serialize, Deserialize)]
60
pub struct FunctionName {
61
/// The Wasm function index of this function.
62
pub idx: FuncIndex,
63
/// The offset of the name in the
64
/// [`ELF_NAME_DATA`](crate::obj::ELF_NAME_DATA) section.
65
pub offset: u32,
66
/// The length of the name in bytes.
67
pub len: u32,
68
}
69
70
/// Metadata associated with a compiled ELF artifact.
71
#[derive(Serialize, Deserialize)]
72
pub struct Metadata {
73
/// Whether or not the original wasm module contained debug information that
74
/// we skipped and did not parse.
75
pub has_unparsed_debuginfo: bool,
76
77
/// Offset in the original wasm file to the code section.
78
pub code_section_offset: u64,
79
80
/// Whether or not custom wasm-specific dwarf sections were inserted into
81
/// the ELF image.
82
///
83
/// Note that even if this flag is `true` sections may be missing if they
84
/// weren't found in the original wasm module itself.
85
pub has_wasm_debuginfo: bool,
86
87
/// Dwarf sections and the offsets at which they're stored in the
88
/// ELF_WASMTIME_DWARF
89
pub dwarf: Vec<(u8, Range<u64>)>,
90
}
91
92
/// Value of a configured setting for a [`Compiler`](crate::Compiler)
93
#[derive(Serialize, Deserialize, Hash, Eq, PartialEq, Debug)]
94
pub enum FlagValue<'a> {
95
/// Name of the value that has been configured for this setting.
96
Enum(&'a str),
97
/// The numerical value of the configured settings.
98
Num(u8),
99
/// Whether the setting is on or off.
100
Bool(bool),
101
}
102
103
impl fmt::Display for FlagValue<'_> {
104
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105
match self {
106
Self::Enum(v) => v.fmt(f),
107
Self::Num(v) => v.fmt(f),
108
Self::Bool(v) => v.fmt(f),
109
}
110
}
111
}
112
113
/// Types of objects that can be created by `Compiler::object`
114
pub enum ObjectKind {
115
/// A core wasm compilation artifact
116
Module,
117
/// A component compilation artifact
118
Component,
119
}
120
121