emscripten-core / emscripten-core/emscripten
-g3 .wasm size after strip-debug does not equal size with -g0
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
Related to some discussion in https://github.com/emscripten-core/emscripten/issues/13084
Let's say I build a wasm binary with `-g0`, and measure the size. Then I build the binary with the same flags, but with `-g3`, and then take that binary, and strip out debug information, I expect the resulting size to be the same as the first binary I built. But it doesn't seem to be. I am not sure if this is because the debug mode turns off certain compiler optimizations, or if it means that extra data gets generated in the binary in sections other than the debug one.
I am using the [bazel SDK](https://github.com/emscripten-core/emsdk/tree/main/bazel) for this, but it is unclear if that is part of the problem or not. There is already a small test app [here](https://github.com/emscripten-core/emsdk/tree/main/bazel/test_external) that can reproduce the problem, although the size difference is much bigger when using real projects with more code.
Steps:
Build and link with -Oz
```
cd bazel/test_external
% bazel build --copt=-Oz --linkopt=-Oz //:hello-world-wasm
% ls -al bazel-bin/hello-world-wasm/hello-world.wasm
120959 8 Oct 16:11 bazel-bin/hello-world-wasm/hello-world.wasm
```
Build and link with -Oz -g3
```
% bazel build --copt=-g3 --linkopt=-g3 --copt=-Oz --linkopt=-Oz //:hello-world-wasm
% ls -al bazel-bin/hello-world-wasm/hello-world.wasm
2217363 8 Oct 16:13 bazel-bin/hello-world-wasm/hello-world.wasm
```
Strip using `objcopy`, in the same way as used [here](https://github.com/emscripten-core/emscripten/blob/main/tools/building.py#L1140), as called from [here](https://github.com/emscripten-core/emscripten/blob/main/tools/building.py#L1164)
```
% cp bazel-bin/hello-world-wasm/hello-world.wasm debug.wasm
% bazel run @emscripten_bin_mac//:bin/llvm-objcopy -- --remove-section='.debug*' debug.wasm
% ls -al debug.wasm
% ls -al debug.wasm
309399 8 Oct 16:24 debug.wasm
```
We went from 120KB at `-g0` to 309KB after stripping `-g3` on this empty test app.
Is this a bug or expected behaviour? How come this happens? Are there additional sections that can be stripped after that fact to get back down to the original size?
CC @sbc100
Contributor guide
Assessment
This issue has not been assessed yet.