When using Clang/LLC/WASM-LD to compile C to WASM, how can I export an integer constant directly?

Viewed 347

Given the following C, fed into Clang, LLC and then WASM-LD to generate a WASM binary:

__attribute__((visibility("default"))) const int an_export_name = 1234;

WASM equivalent to the following WAT is generated:

(module
  (memory $memory (export "memory") 2)
  (global $an_export_name (export "an_export_name") i32 (i32.const 1024))
  (data (i32.const 1024) "\d2\04"))

This appears to have exported:

  • A blob of binary data containing the value 1234.
  • An i32 constant which is an offset within the binary blob to that value (1024).

Is it possible to instead just export the value? For example:

(module
  (memory $memory (export "memory") 2)
  (global $an_export_name (export "an_export_name") i32 (i32.const 1234)))
1 Answers

Currently the only things you can export from C/C++ are functions, and data addresses.

There are plans to expose things like wasm globals, tables, and other constructs to clang/llvm but non of them are ready for production use yet.

So for now you need to export the address of your constant along with the memory in which it lives and then the embeddeder needs to read the constant out of the memory.

Related