I am currently toying around with WebAssembly compiled through LLVM but I haven't yet managed to understand the stack / stack pointer and how it relates to the overall memory layout.
I learned that I have to use s2wasm with --allocate-stack N to make my program run and I figured that this is basically adding (data (i32.const 4) "8\00\00\00") (with N=8) to my generated wast, with the binary part obviously being a pointer to a memory offset and the i32 constant being its offset in linear memory.
What I do not quite understand, though, is why the pointer's value is 56 (again with N=8) and how this value relates to the exact region of the stack in memory, which, in my case, currently looks like:
0-3: zero
4-7: 56
7-35: other data sections
36-55: zeroes
56-59: zero
I know that I am probably more a candidate for "just use emscripten", but I'd also like to understand this.
- Is the stack pointer always stored at offset 4 in linear memory?
- How is its initial value calculated? (aligned to next offset%16==0 + N after data?)
- What's stored before, and what's after the offset it points at?