Repository: go
Issue: cmd/link: on wasm, number of functions limited to 2^16
Labels: NeedsInvestigation,arch-wasm,compiler/runtime

# Overview

It seems to me that the size of a WASM function is limited to 2^16 = 65536 bytes?

https://github.com/golang/go/blob/649671b08fbd49bcf65578b18d188d55779e6eca/src/cmd/link/internal/wasm/asm.go#L90

This leads to a bunch of errors whenever using more than a few libraries in our WASM app
The code for our app is available in this branch https://github.com/konveyor/move2kube/blob/wasm/go.mod
The code is able to build using the official Go compiler WASIP1 support
https://github.com/konveyor/move2kube/blob/dec9d8da6e5f8882aacb46a8e5f784b96b42a3c6/Makefile#L45C2-L45C100

# Issue

When we try to add the https://pkg.go.dev/go.starlark.net/starlark library then we get a bunch of compile errors
```
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).ModTime is too big: 0x100c40000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).ModTime is too big: 0x100c40000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).Mode is too big: 0x100c60000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).Mode is too big: 0x100c60000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).Name is too big: 0x100c70000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).Name is too big: 0x100c70000
type:*github.com/mholt/archiver/v3.FileInfo: non-pc-relative relocation address for github.com/mholt/archiver/v3.(*FileInfo).Size is too big: 0x100c90000
/usr/local/go/pkg/tool/linux_amd64/link: too many errors
make: *** [Makefile:45: build] Error 1
```

# Fix

Please remove this arbitrary limit on function size or at least provide a way to work around it. This is a severe limit on any real world app that will use many different libraries to implement various features.

The WASM page size is 65536 bytes https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory/Memory which is 2^16 and can be addressed with 16 bits. That might explain where this particular limit came from but it doesn't explain why a single function has to fit all inside of one WASM page. Especially since WASM functions live is a completely separate address space than the WASM linear memory.

Also see these comments:
- https://github.com/WebAssembly/design/issues/1138#issuecomment-337766850
- https://github.com/WebAssembly/design/issues/1138#issuecomment-341808035
- https://github.com/WebAssembly/design/issues/1138#issuecomment-338921461

# Related

https://stackoverflow.com/questions/67294859/why-golang-limit-the-symbol-number-to-65535-while-compile-wasm-target
https://github.com/golang/go/issues/7769
https://github.com/golang/go/issues/7980
https://github.com/WebAssembly/design/issues/1138