//! Utilities for working with object files that operate as Wasmtime's1//! serialization and intermediate format for compiled modules.23use core::fmt;45/// Filler for the `os_abi` field of the ELF header.6///7/// This is just a constant that seems reasonable in the sense it's unlikely to8/// clash with others.9pub const ELFOSABI_WASMTIME: u8 = 200;1011/// Flag for the `e_flags` field in the ELF header indicating a compiled12/// module.13pub const EF_WASMTIME_MODULE: u32 = 1 << 0;1415/// Flag for the `e_flags` field in the ELF header indicating a compiled16/// component.17pub const EF_WASMTIME_COMPONENT: u32 = 1 << 1;1819/// Flag for the `e_flags` field in the ELF header indicating compiled code for20/// pulley3221pub const EF_WASMTIME_PULLEY32: u32 = 1 << 2;2223/// Flag for the `e_flags` field in the ELF header indicating compiled code for24/// pulley6425pub const EF_WASMTIME_PULLEY64: u32 = 1 << 3;2627/// Flag for the `sh_flags` field in the ELF text section that indicates that28/// the text section does not itself need to be executable. This is used for the29/// Pulley target, for example, to indicate that it does not need to be made30/// natively executable as it does not contain actual native code.31pub const SH_WASMTIME_NOT_EXECUTED: u64 = 1 << 0;3233/// A custom Wasmtime-specific section of our compilation image which stores34/// mapping data from offsets in the image to offset in the original wasm35/// binary.36///37/// This section has a custom binary encoding. Currently its encoding is:38///39/// * The section starts with a 32-bit little-endian integer. This integer is40/// how many entries are in the following two arrays.41/// * Next is an array with the previous count number of 32-bit little-endian42/// integers. This array is a sorted list of relative offsets within the text43/// section. This is intended to be a lookup array to perform a binary search44/// on an offset within the text section on this array.45/// * Finally there is another array, with the same count as before, also of46/// 32-bit little-endian integers. These integers map 1:1 with the previous47/// array of offsets, and correspond to what the original offset was in the48/// wasm file.49///50/// Decoding this section is intentionally simple, it only requires loading a51/// 32-bit little-endian integer plus some bounds checks. Reading this section52/// is done with the `lookup_file_pos` function below. Reading involves53/// performing a binary search on the first array using the index found for the54/// native code offset to index into the second array and find the wasm code55/// offset.56///57/// At this time this section has an alignment of 1, which means all reads of it58/// are unaligned. Additionally at this time the 32-bit encodings chosen here59/// mean that >=4gb text sections are not supported.60pub const ELF_WASMTIME_ADDRMAP: &str = ".wasmtime.addrmap";6162/// A custom Wasmtime-specific section of compilation which store information63/// about live gc references at various locations in the text section (stack64/// maps).65///66/// This section has a custom binary encoding described in `stack_maps.rs` which67/// is used to implement the single query we want to satisfy of: where are the68/// live GC references at this pc? Like the addrmap section this has an69/// alignment of 1 with unaligned reads, and it additionally doesn't support70/// >=4gb text sections.71pub const ELF_WASMTIME_STACK_MAP: &str = ".wasmtime.stackmap";7273/// A custom binary-encoded section of wasmtime compilation artifacts which74/// encodes the ability to map an offset in the text section to the trap code75/// that it corresponds to.76///77/// This section is used at runtime to determine what flavor of trap happened to78/// ensure that embedders and debuggers know the reason for the wasm trap. The79/// encoding of this section is custom to Wasmtime and managed with helpers in80/// the `object` crate:81///82/// * First the section has a 32-bit little endian integer indicating how many83/// trap entries are in the section.84/// * Next is an array, of the same length as read before, of 32-bit85/// little-endian integers. These integers are offsets into the text section86/// of the compilation image.87/// * Finally is the same count number of bytes. Each of these bytes corresponds88/// to a trap code.89///90/// This section is decoded by `lookup_trap_code` below which will read the91/// section count, slice some bytes to get the various arrays, and then perform92/// a binary search on the offsets array to find the index corresponding to93/// the pc being looked up. If found the same index in the trap array (the array94/// of bytes) is the trap code for that offset.95///96/// Note that at this time this section has an alignment of 1. Additionally due97/// to the 32-bit encodings for offsets this doesn't support images >=4gb.98pub const ELF_WASMTIME_TRAPS: &str = ".wasmtime.traps";99100/// A custom binary-encoded section of the wasmtime compilation101/// artifacts which encodes exception tables.102///103/// This section is used at runtime to allow the unwinder to find104/// exception handler blocks active at particular callsites.105///106/// This section's format is defined by the107/// [`wasmtime_unwinder::ExceptionTableBuilder`] data structure. Its108/// code offsets are relative to the start of the text segment.109pub const ELF_WASMTIME_EXCEPTIONS: &str = ".wasmtime.exceptions";110111/// A custom section which consists of just 1 byte which is either 0 or 1 as to112/// whether BTI is enabled.113pub const ELF_WASM_BTI: &str = ".wasmtime.bti";114115/// A bincode-encoded section containing engine-specific metadata used to116/// double-check that an artifact can be loaded into the current host.117pub const ELF_WASM_ENGINE: &str = ".wasmtime.engine";118119/// This is the name of the section in the final ELF image which contains120/// concatenated data segments from the original wasm module.121///122/// This section is simply a list of bytes and ranges into this section are123/// stored within a `Module` for each data segment. Memory initialization and124/// passive segment management all index data directly located in this section.125///126/// Note that this implementation does not afford any method of leveraging the127/// `data.drop` instruction to actually release the data back to the OS. The128/// data section is simply always present in the ELF image. If we wanted to129/// release the data it's probably best to figure out what the best130/// implementation is for it at the time given a particular set of constraints.131pub const ELF_WASM_DATA: &'static str = ".rodata.wasm";132133/// This is the name of the section in the final ELF image which contains a134/// `bincode`-encoded `CompiledModuleInfo`.135///136/// This section is optionally decoded in `CompiledModule::from_artifacts`137/// depending on whether or not a `CompiledModuleInfo` is already available. In138/// cases like `Module::new` where compilation directly leads into consumption,139/// it's available. In cases like `Module::deserialize` this section must be140/// decoded to get all the relevant information.141pub const ELF_WASMTIME_INFO: &'static str = ".wasmtime.info";142143/// This is the name of the section in the final ELF image which contains a144/// concatenated list of all function names.145///146/// This section is optionally included in the final artifact depending on147/// whether the wasm module has any name data at all (or in the future if we add148/// an option to not preserve name data). This section is a concatenated list of149/// strings where `CompiledModuleInfo::func_names` stores offsets/lengths into150/// this section.151///152/// Note that the goal of this section is to avoid having to decode names at153/// module-load time if we can. Names are typically only used for debugging or154/// things like backtraces so there's no need to eagerly load all of them. By155/// storing the data in a separate section the hope is that the data, which is156/// sometimes quite large (3MB seen for spidermonkey-compiled-to-wasm), can be157/// paged in lazily from an mmap and is never paged in if we never reference it.158pub const ELF_NAME_DATA: &'static str = ".name.wasm";159160/// This is the name of the section in the final ELF image that contains the161/// concatenation of all the native DWARF information found in the original wasm162/// files.163///164/// This concatenation is not intended to be read by external tools at this time165/// and is instead indexed directly by relative indices stored in compilation166/// metadata.167pub const ELF_WASMTIME_DWARF: &str = ".wasmtime.dwarf";168169/// Workaround to implement `core::error::Error` until170/// gimli-rs/object#747 is settled.171pub struct ObjectCrateErrorWrapper(pub object::Error);172173impl fmt::Debug for ObjectCrateErrorWrapper {174fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {175self.0.fmt(f)176}177}178179impl fmt::Display for ObjectCrateErrorWrapper {180fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {181self.0.fmt(f)182}183}184185impl core::error::Error for ObjectCrateErrorWrapper {}186187188