It seems that a module I test with imports env::__stack_pointer
. How do I obtain this value? It seems that none of the modules I have export it or define the value in custom sections.
@Risto env::__stack_pointer
is created by the linker. It only exists in the final linked wasm module.
It seems that if I define a stack for a program it will be compiled with a stack.
However, the stack size isn't exported by llvm/clang even if one is defined.
The command I used to compile the module that should define the stack is
$CC example.c -o example.wasm -Wl,-no-entry,--allow-undefined,-zstack-size=1600,--import-table,--import-memory,--stack-first
Using wasi-sdk, so the compile target should be set to wasm-wasi automatically.
Even if --export-all
is passed to the linker, there is no exported value that signals the position or size of the stack. Many other variables are exported though, for example __memory_base
. Unsure if I am missing something from the linking step. Hmm.
does any of the compiled "example.wasm" functions needs __stack_pointer ?
/me tried locally -- the global is not exported indeed
@Yury Delendik No, it is not used I would say as far as I see.
I tried --export,__stack_pointer,--export,__stack_base
, but they do nothing - no error or export. The export switch works for all other values, such as __memory_base
or unused functions.
There is also no way to calculate/get the initial value for it either as far as I see, as there is no information on the location or size of the stack even if it is created to the module as stack-size and stack-first options dictate.
@Risto I suspect that lld cannot do mutable globals by looking at https://github.com/llvm/llvm-project/blob/master/lld/wasm/Writer.cpp#L566
/me suggests to add function/accessors and export these
For now I will build the codes with emscripten, which doesn't seem to have the same issue.
I also found some posts around the web that claimed the stack pointer being at position 4 in memory as a convention or something, but I will rather try emscripten at this time than try guess if that is a valid assumption at all.
Making the getter for the stack position seems like a potential workaround, I think I found a stackoverflow post mentioning the same.
"position 4" true for old emscripten, I wonder if it is still true, since new emscripten is more inline with what new clang/llvm produces
Last updated: Dec 23 2024 at 12:05 UTC