usecrate::{CustomSection, Encode, Section, SectionId, encode_section}; use alloc::borrow::Cow; use alloc::vec; use alloc::vec::Vec;
const VERSION: u32 = 2;
/// An encoder for the [linking custom /// section](https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md#linking-metadata-section). /// /// This section is a non-standard convention that is supported by the LLVM /// toolchain. It, along with associated "reloc.*" custom sections, allows you /// to treat a Wasm module as a low-level object file that can be linked with /// other Wasm object files to produce a final, complete Wasm module. /// /// The linking section must come before the reloc sections. /// /// # Example /// /// ``` /// use wasm_encoder::{LinkingSection, Module, SymbolTable}; /// /// // Create a new linking section. /// let mut linking = LinkingSection::new(); /// /// // Define a symbol table. /// let mut sym_tab = SymbolTable::new(); /// /// // Define a function symbol in the symbol table. /// let flags = SymbolTable::WASM_SYM_BINDING_LOCAL | SymbolTable::WASM_SYM_EXPORTED; /// let func_index = 42; /// let sym_name = "my_exported_func"; /// sym_tab.function(flags, func_index, Some(sym_name)); /// /// // Add the symbol table to our linking section. /// linking.symbol_table(&sym_tab); /// /// // Add the linking section to a new Wasm module and get the encoded bytes. /// let mut module = Module::new(); /// module.section(&linking); /// let wasm_bytes = module.finish(); /// ``` #[derive(Clone, Debug)] pubstruct LinkingSection {
bytes: Vec<u8>,
}
impl LinkingSection { /// Construct a new encoder for the linking custom section. pubfn new() -> Self { Self::default()
}
// TODO: `fn segment_info` for the `WASM_SEGMENT_INFO` linking subsection.
// TODO: `fn init_funcs` for the `WASM_INIT_FUNCS` linking subsection.
// TODO: `fn comdat_info` for the `WASM_COMDAT_INFO` linking subsection.
/// Add a symbol table subsection. pubfn symbol_table(&mutself, symbol_table: &SymbolTable) -> &mutSelf {
symbol_table.encode(&mutself.bytes); self
}
}
/// A subsection of the [linking custom section][crate::LinkingSection] that /// provides extra information about the symbols present in this Wasm object /// file. #[derive(Clone, Debug, Default)] pubstruct SymbolTable {
bytes: Vec<u8>,
num_added: u32,
}
impl SymbolTable { /// Construct a new symbol table subsection encoder. pubfn new() -> Self {
SymbolTable {
bytes: vec![],
num_added: 0,
}
}
/// Define a function symbol in this symbol table. /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. pubfn function(&mutself, flags: u32, index: u32, name: Option<&str>) -> &mutSelf {
SYMTAB_FUNCTION.encode(&mutself.bytes);
flags.encode(&mutself.bytes);
index.encode(&mutself.bytes); iflet Some(name) = name {
name.encode(&mutself.bytes);
} self.num_added += 1; self
}
/// Define a global symbol in this symbol table. /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. pubfn global(&mutself, flags: u32, index: u32, name: Option<&str>) -> &pan style='color:red'>mut Self {
SYMTAB_GLOBAL.encode(&mutself.bytes);
flags.encode(&mutself.bytes);
index.encode(&mutself.bytes); iflet Some(name) = name {
name.encode(&mutself.bytes);
} self.num_added += 1; self
}
// TODO: tags
/// Define a table symbol in this symbol table. /// /// The `name` must be omitted if `index` references an imported table and /// the `WASM_SYM_EXPLICIT_NAME` flag is not set. pubfn table(&mutself, flags: u32, index: u32, name: Option<&str>) -> &an style='color:red'>mut Self {
SYMTAB_TABLE.encode(&mutself.bytes);
flags.encode(&mutself.bytes);
index.encode(&mutself.bytes); iflet Some(name) = name {
name.encode(&mutself.bytes);
} self.num_added += 1; self
}
/// Add a data symbol to this symbol table. pubfn data(
&mutself,
flags: u32,
name: &str,
definition: Option<DataSymbolDefinition>,
) -> &mutSelf {
SYMTAB_DATA.encode(&mutself.bytes);
flags.encode(&mutself.bytes);
name.encode(&mutself.bytes); iflet Some(def) = definition {
def.index.encode(&mutself.bytes);
def.offset.encode(&mutself.bytes);
def.size.encode(&mutself.bytes);
} self.num_added += 1; self
}
// TODO: sections
/// This is a weak symbol. /// /// This flag is mutually exclusive with `WASM_SYM_BINDING_LOCAL`. /// /// When linking multiple modules defining the same symbol, all weak /// definitions are discarded if any strong definitions exist; then if /// multiple weak definitions exist all but one (unspecified) are discarded; /// and finally it is an error if more than one definition remains. pubconst WASM_SYM_BINDING_WEAK: u32 = 0x1;
/// This is a local symbol. /// /// This flag is mutually exclusive with `WASM_SYM_BINDING_WEAK`. /// /// Local symbols are not to be exported, or linked to other /// modules/sections. The names of all non-local symbols must be unique, but /// the names of local symbols are not considered for uniqueness. A local /// function or global symbol cannot reference an import. pubconst WASM_SYM_BINDING_LOCAL: u32 = 0x02;
/// This is a hidden symbol. /// /// Hidden symbols are not to be exported when performing the final link, /// but may be linked to other modules. pubconst WASM_SYM_VISIBILITY_HIDDEN: u32 = 0x04;
/// This symbol is not defined. /// /// For non-data symbols, this must match whether the symbol is an import or /// is defined; for data symbols, determines whether a segment is specified. pubconst WASM_SYM_UNDEFINED: u32 = 0x10;
/// This symbol is intended to be exported from the wasm module to the host /// environment. /// /// This differs from the visibility flags in that it effects the static /// linker. pubconst WASM_SYM_EXPORTED: u32 = 0x20;
/// This symbol uses an explicit symbol name, rather than reusing the name /// from a wasm import. /// /// This allows it to remap imports from foreign WebAssembly modules into /// local symbols with different names. pubconst WASM_SYM_EXPLICIT_NAME: u32 = 0x40;
/// This symbol is intended to be included in the linker output, regardless /// of whether it is used by the program. pubconst WASM_SYM_NO_STRIP: u32 = 0x80;
}
/// The definition of a data symbol within a symbol table. #[derive(Clone, Debug)] pubstruct DataSymbolDefinition { /// The index of the data segment that this symbol is in. pub index: u32, /// The offset of this symbol within its segment. pub offset: u32, /// The byte size (which can be zero) of this data symbol. /// /// Note that `offset + size` must be less than or equal to the segment's /// size. pub size: u32,
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.