ziglang/zig

wasm automatically links and re-exports all of libc

fengb opened this issue · 6 comments

fengb commented
$ zig build-lib empty.zig -OReleaseSmall -target wasm32-freestanding

$ ls -l empty.*
-rwxr-xr-x  1 fengb  staff  48776 Nov 16 12:06 empty.wasm
-rw-r--r--  1 fengb  staff      0 Nov 16 12:06 empty.zig

$ twiggy top empty.wasm 
 Shallow Bytes │ Shallow % │ Item
───────────────┼───────────┼────────────────────────────────────────────────
          4255 ┊     8.72% ┊ data[0]
          2851 ┊     5.85% ┊ "function names" subsection
          1884 ┊     3.86% ┊ __divtf3
          1477 ┊     3.03% ┊ compiler_rt.addXf3.addXf3.27
          1384 ┊     2.84% ┊ __multf3
           941 ┊     1.93% ┊ __divdf3
           879 ┊     1.80% ┊ compiler_rt.udivmod.udivmod.64
           864 ┊     1.77% ┊ fma
           803 ┊     1.65% ┊ __muldf3
           778 ┊     1.60% ┊ compiler_rt.addXf3.addXf3.26
           767 ┊     1.57% ┊ __udivmoddi4
           675 ┊     1.38% ┊ __divsf3
           639 ┊     1.31% ┊ compiler_rt.addXf3.addXf3
           592 ┊     1.21% ┊ __mulsf3
           551 ┊     1.13% ┊ sqrt
           519 ┊     1.06% ┊ fmod
           512 ┊     1.05% ┊ expf
[...]
fengb commented

I think we need to apply this to the new stage2 code: #2369

fengb commented

Alternatively, would it make sense to enable build-exe for wasm32-freestanding? It doesn't work right now because it expects main.

Alternatively, would it make sense to enable build-exe for wasm32-freestanding? It doesn't work right now because it expects main.

Let's explore this- without diving back into wasm stuff I recall that I was trying to avoid adding the logic from #2369 because it would require maintaining more in-memory data with regards to incremental compilation.

There is an easier way. From LLD documentation:

When building an executable, only the entry point (_start) and symbols with the WASM_SYMBOL_EXPORTED flag are exported by default. In LLVM the WASM_SYMBOL_EXPORTED flag is set by the wasm-export-name attribute which in turn can be set using __attribute__((export_name)) clang attribute.

In practice, we would need to add this attribute to all exported functions, except when compiling libc and other internal libraries.

Can someone provide a breakdown of what needs to be done to fix this issue? I'd possibly like to contribute here. Context is I'm writing a compiler, currently in C but would prefer it be with Zig, and wasm is my core target. This is a blocker because file size is extremely important in the browser.