diff options
| author | Cherry Mui <cherryyz@google.com> | 2024-08-05 13:40:18 -0400 |
|---|---|---|
| committer | Cherry Mui <cherryyz@google.com> | 2024-08-06 15:56:16 +0000 |
| commit | 03e5d83ca7323e354dfff6ba50720302ed835b7c (patch) | |
| tree | 1c6e1d4c01f98fe6cee93bd03648e5dadcde49c0 /src/cmd/link | |
| parent | a7c7ec5995b3901145f847c01f3e100b2b7e3421 (diff) | |
| download | go-03e5d83ca7323e354dfff6ba50720302ed835b7c.tar.xz | |
cmd/internal/obj: minor refactor of wasmimport code
This CL does some minor refactoring of the code handling
wasmimport.
- Put the WasmImport aux reading and writing code together for
symmetry.
- Define WasmFuncType, embedded in WasmImport. WasmFuncType could
also be used (later) for wasmexport.
- Move code generation code to a separate function. The containing
function is already pretty large.
- Simplify linker code a little bit. The loader convention is to
return the 0 Sym for nonexistent symbol, instead of a separate
boolean.
No change in generated code. Passes toolstash -cmp
(GOARCH=wasm GOOS=wasip1 go build -toolexec "toolstash -cmp" -a std cmd).
Change-Id: Idc2514f84a08621333841ae4034b81130e0ce411
Reviewed-on: https://go-review.googlesource.com/c/go/+/603135
Reviewed-by: Than McIntosh <thanm@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'src/cmd/link')
| -rw-r--r-- | src/cmd/link/internal/loader/loader.go | 14 | ||||
| -rw-r--r-- | src/cmd/link/internal/wasm/asm.go | 54 |
2 files changed, 5 insertions, 63 deletions
diff --git a/src/cmd/link/internal/loader/loader.go b/src/cmd/link/internal/loader/loader.go index f448a3ee7c..98bff775fb 100644 --- a/src/cmd/link/internal/loader/loader.go +++ b/src/cmd/link/internal/loader/loader.go @@ -1620,21 +1620,11 @@ func (l *Loader) Aux(i Sym, j int) Aux { // contains the information necessary for the linker to add a WebAssembly // import statement. // (https://webassembly.github.io/spec/core/syntax/modules.html#imports) -func (l *Loader) WasmImportSym(fnSymIdx Sym) (Sym, bool) { +func (l *Loader) WasmImportSym(fnSymIdx Sym) Sym { if l.SymType(fnSymIdx) != sym.STEXT { log.Fatalf("error: non-function sym %d/%s t=%s passed to WasmImportSym", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String()) } - r, li := l.toLocal(fnSymIdx) - auxs := r.Auxs(li) - for i := range auxs { - a := &auxs[i] - switch a.Type() { - case goobj.AuxWasmImport: - return l.resolve(r, a.Sym()), true - } - } - - return 0, false + return l.aux1(fnSymIdx, goobj.AuxWasmImport) } // SEHUnwindSym returns the auxiliary SEH unwind symbol associated with diff --git a/src/cmd/link/internal/wasm/asm.go b/src/cmd/link/internal/wasm/asm.go index 2f511b97c7..09c54c1392 100644 --- a/src/cmd/link/internal/wasm/asm.go +++ b/src/cmd/link/internal/wasm/asm.go @@ -12,7 +12,6 @@ import ( "cmd/link/internal/ld" "cmd/link/internal/loader" "cmd/link/internal/sym" - "encoding/binary" "fmt" "internal/abi" "internal/buildcfg" @@ -61,55 +60,8 @@ type wasmFuncType struct { } func readWasmImport(ldr *loader.Loader, s loader.Sym) obj.WasmImport { - reportError := func(err error) { panic(fmt.Sprintf("failed to read WASM import in sym %v: %v", s, err)) } - - data := ldr.Data(s) - - readUint32 := func() (v uint32) { - v = binary.LittleEndian.Uint32(data) - data = data[4:] - return - } - - readUint64 := func() (v uint64) { - v = binary.LittleEndian.Uint64(data) - data = data[8:] - return - } - - readByte := func() byte { - if len(data) == 0 { - reportError(io.EOF) - } - - b := data[0] - data = data[1:] - return b - } - - readString := func() string { - n := readUint32() - - s := string(data[:n]) - - data = data[n:] - - return s - } - var wi obj.WasmImport - wi.Module = readString() - wi.Name = readString() - wi.Params = make([]obj.WasmField, readUint32()) - for i := range wi.Params { - wi.Params[i].Type = obj.WasmFieldType(readByte()) - wi.Params[i].Offset = int64(readUint64()) - } - wi.Results = make([]obj.WasmField, readUint32()) - for i := range wi.Results { - wi.Results[i].Type = obj.WasmFieldType(readByte()) - wi.Results[i].Offset = int64(readUint64()) - } + wi.Read(ldr.Data(s)) return wi } @@ -207,8 +159,8 @@ func asmb2(ctxt *ld.Link, ldr *loader.Loader) { for ri := 0; ri < relocs.Count(); ri++ { r := relocs.At(ri) if r.Type() == objabi.R_WASMIMPORT { - if lsym, ok := ldr.WasmImportSym(fn); ok { - wi := readWasmImport(ldr, lsym) + if wsym := ldr.WasmImportSym(fn); wsym != 0 { + wi := readWasmImport(ldr, wsym) hostImportMap[fn] = int64(len(hostImports)) hostImports = append(hostImports, &wasmFunc{ Module: wi.Module, |
