pub fn create_metadata_file_for_wasm(
    data: &[u8],
    section_name: &[u8]
) -> Vec<u8>
Expand description

Creates a simple WebAssembly object file, which is itself a wasm module, that contains a custom section of the name section_name with contents data.

NB: the object crate does not yet have support for writing the wasm object file format. The format is simple enough that for now an extra crate from crates.io (such as wasm-encoder). The file format is:

  • 4-byte header “\0asm”
  • 4-byte version number - 1u32 in little-endian format
  • concatenated sections, which for this object is always “custom sections”

Custom sections are then defined by:

  • 1-byte section identifier - 0 for a custom section
  • leb-encoded section length (size of the contents beneath this bullet)
    • leb-encoded custom section name length
    • custom section name
    • section contents

One custom section, linking, is added here in accordance with https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md which is required to inform LLD that this is an object file but it should otherwise basically ignore it if it otherwise looks at it. The linking section currently is defined by a single version byte (2) and then further sections, but we have no more sections, so it’s just the byte “2”.

The next custom section is the one we’re interested in.