#Export custom section in WASM

1 messages · Page 1 of 1 (latest)

zenith ferry
#

I want to embed a file into a custom section in a WASM module. The following line does not work:

export var manifest linksection("+tag doc:manifest") = @embedFile("manifest.json").*;

Variations I have tried include export const, var, removing the .*. That is the section name I need, but I have also checked that it does not work with a more 'conventional' name like .foo.

silk meadow
#

I'm not 100% on what "does not work" means, but try the @export builtin:

const foo = @embedFile("foo.txt");
comptime {
    @export(&foo, .{ .name = "bar", .section = ".baz" });
}
#

then compile:
zig build-exe whatevs.zig -target wasm32-freestanding -fno-entry --export=bar

#

you should get .wasm that has the file exported in the named section; note that the exported symbol is named bar in this example , because it's more confusing like that

zenith ferry
#

By 'does not work' I mean that wasm-objdump -h shows no such section in the final artifact. And the same goes for that @export invocation.

neon harness
#

it seems to export the section as a symbol if you look at llvm-nm

zenith ferry
#

Yeah, I know it's different because there shouldn't actually be a symbol per se when it works

#

but that's how you do it in Rust:

#[link_section = "+tag doc:manifest"]
static MANIFEST: [u8; 153] = *include_bytes!("manifest.json");
ebon palm
#

This is currently not supported yet. Eventually there will be a builtin for this such as @WasmCustonSection(name, value)

#

Linksection will put it in the data section

sleek roost
#

Wasm's spec isn't that complicated. Custom sections can be between any other sections in the Wasm file. Provided that the software using the custom section doesn't require a specific location (it probably doesn't), you could literally just concatenate the section to the end of the file. See the spec for what the bytes need to look like: https://webassembly.github.io/spec/core/binary/modules.html