Skip to content

Compilation fails when --stack-first and -Oz are used together. #21215

Closed

Description

@netogallo

Reproduction steps:

  1. Use any c program. Take for example the following main.c
int main(int argc, char** argv) { return 0; }
  1. Compile with /home/ernesto/code/emsdk/upstream/emscripten/emcc -Oz -Wl,--stack-first main.c
  2. Compilation will fail with: "wasm-ld: error: --global-base cannot be less than stack size when --stack-first is used"
  3. Now compile with /home/ernesto/code/emsdk/upstream/emscripten/emcc -Wl,--stack-first main.c compilation succeeds.

Workarounds:

  1. Avoid "-Oz" when compiling with "--stack-first"
  2. Edit "settings.js" and set "var GLOBAL_BASE = 1024;" to the same value as "var STACK_SIZE = 64*1024;"

Potential resolutions:

  1. The "emcc.py" and "em++.py" programs can check if the "-Oz" flag is used together with "--stack-first". If so (Option 1) set the value of GLOBAL_BASE to the same value as STACK_SIZE. (Option 2) Exit with an error informing the user they are using an incompatible combination of flags.

Happy to issue a pull request with the fix above if any of those solutions would be satisfactory.

Activity

  1. sbc100 commented on Jan 29, 2024

    @sbc100
    Collaborator

    The problem is that emscripten itself wants to control the --stack-first linker flag.

    See

    emscripten/tools/link.py

    Lines 882 to 888 in 0e5b3a8

    if 'GLOBAL_BASE' not in user_settings and not settings.SHRINK_LEVEL and not settings.OPT_LEVEL:
    # When optimizing for size it helps to put static data first before
    # the stack (since this makes instructions for accessing this data
    # use a smaller LEB encoding).
    # However, for debugability is better to have the stack come first
    # (because stack overflows will trap rather than corrupting data).
    settings.STACK_FIRST = True

    And:

    if settings.STACK_FIRST:
    cmd.append('--stack-first')

    The idea is that we use stack-first in debug builds, but not in optimizing builds. Is there some reason you want the stack to be first, even in optimized builds?

  2. netogallo commented on Jan 30, 2024

    @netogallo
    Author

    Thank you for letting me know. What you say is sensible. In case you are curious, the reason why I am attempting that is because I am trying to build GHC using the upstream emsdk (rather than using the wasm-sdk fork). The instructions use both flags: https://gitlab.haskell.org/ghc/ghc-wasm-meta#set-environment-variables

    I don't know why they use "--stack-first" in all honesty, it might not be necessary as you say. This issue does not affect me as I can get around it. I was just reporting what I encountered. Feel free to close.

  3. sbc100 commented on Jan 30, 2024

    @sbc100
    Collaborator

    Thanks for the report. We should certainly fix this (either by disallowing explicit -Wl,--stack-first or honoring it).

    The reason that folks generally prefer --stack-first, and the reason emscripten uses it in debug builds, is that you get better stack overflow reporting. i.e. stack overflows will trap right away.

  4. added 2 commits that reference this issue on Jan 30, 2024
    6ab2858
    1917a52
  5. added a commit that references this issue on Jan 30, 2024
    d660cca
  6. added a commit that references this issue on Apr 12, 2024
    baa07c9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions