Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage2: wasm-linker - Place stack at the beginning of the linear memory #10572

Merged
merged 2 commits into from
Jan 12, 2022

Conversation

Luukdegram
Copy link
Contributor

By placing the stack at the start of the memory section, we prevent the runtime from silently overwriting the static memory and instead trap on a stack overflow.

We do, however, allow users to overwrite this behavior by setting the global-base flag, which puts the stack at the end of the memory section and the static data at the base that was specified.
The reason a user would want to do this is when they need to put the static data at a specific address. Another benefit of doing so it has a side effect of lower binary size.
(Having the stack in front, means that accessing the memory after the stack has a bigger offset when loading/storing from memory. Bigger offsets mean it takes more bytes to represent the instruction).
Note: While we used to append this flag by default to wasm-ld, we only did this for build-exe and had no way to overwrite such behavior.

This PR implements it for both the self-hosted linker and the linker frontend for stage1.

Closes #10567

By placing the stack at the start of the memory section, we prevent the runtime
from silently overwriting the global declarations and instead trap.

We do however, allow users to overwrite this behavior by setting the global-base,
which puts the stack at the end of the memory section and the static data at the base that was specified.
The reason a user would want to do this, is when they are sure the stack will not overflow and they want
to decrease the binary size as the offsets to the static memory are generally smaller.
(Having the stack in front, means that accessing the memory after the stack has a bigger offset when loading/storing from memory).
@andrewrk andrewrk merged commit 1a7c47d into ziglang:master Jan 12, 2022
andrewrk added a commit that referenced this pull request Jan 12, 2022
Stage2: wasm-linker - Place stack at the beginning of the linear memory
scorphus pushed a commit to scorphus/zig that referenced this pull request Jan 15, 2022
Stage2: wasm-linker - Place stack at the beginning of the linear memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wasm targets should append lld --stack-first (again)
2 participants