Skip to content

Commit 03e5d83

Browse files
committed
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent a7c7ec5 commit 03e5d83

File tree

6 files changed

+222
-194
lines changed

6 files changed

+222
-194
lines changed

src/cmd/internal/obj/link.go

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package obj
3232

3333
import (
3434
"bufio"
35+
"bytes"
3536
"cmd/internal/dwarf"
3637
"cmd/internal/goobj"
3738
"cmd/internal/objabi"
@@ -496,9 +497,9 @@ type FuncInfo struct {
496497
WrapInfo *LSym // for wrapper, info of wrapped function
497498
JumpTables []JumpTable
498499

499-
FuncInfoSym *LSym
500-
WasmImportSym *LSym
501-
WasmImport *WasmImport
500+
FuncInfoSym *LSym
501+
502+
WasmImport *WasmImport
502503

503504
sehUnwindInfoSym *LSym
504505
}
@@ -609,45 +610,118 @@ type WasmImport struct {
609610
// Name holds the WASM imported function name specified by the
610611
// //go:wasmimport directive.
611612
Name string
613+
614+
WasmFuncType // type of the imported function
615+
616+
// aux symbol to pass metadata to the linker, serialization of
617+
// the fields above.
618+
AuxSym *LSym
619+
}
620+
621+
func (wi *WasmImport) CreateAuxSym() {
622+
var b bytes.Buffer
623+
wi.Write(&b)
624+
p := b.Bytes()
625+
wi.AuxSym = &LSym{
626+
Type: objabi.SDATA, // doesn't really matter
627+
P: append([]byte(nil), p...),
628+
Size: int64(len(p)),
629+
}
630+
}
631+
632+
func (wi *WasmImport) Write(w *bytes.Buffer) {
633+
var b [8]byte
634+
writeUint32 := func(x uint32) {
635+
binary.LittleEndian.PutUint32(b[:], x)
636+
w.Write(b[:4])
637+
}
638+
writeString := func(s string) {
639+
writeUint32(uint32(len(s)))
640+
w.WriteString(s)
641+
}
642+
writeString(wi.Module)
643+
writeString(wi.Name)
644+
wi.WasmFuncType.Write(w)
645+
}
646+
647+
func (wi *WasmImport) Read(b []byte) {
648+
readUint32 := func() uint32 {
649+
x := binary.LittleEndian.Uint32(b)
650+
b = b[4:]
651+
return x
652+
}
653+
readString := func() string {
654+
n := readUint32()
655+
s := string(b[:n])
656+
b = b[n:]
657+
return s
658+
}
659+
wi.Module = readString()
660+
wi.Name = readString()
661+
wi.WasmFuncType.Read(b)
662+
}
663+
664+
// WasmFuncType represents a WebAssembly (WASM) function type with
665+
// parameters and results translated into WASM types based on the Go function
666+
// declaration.
667+
type WasmFuncType struct {
612668
// Params holds the imported function parameter fields.
613669
Params []WasmField
614670
// Results holds the imported function result fields.
615671
Results []WasmField
616672
}
617673

618-
func (wi *WasmImport) CreateSym(ctxt *Link) *LSym {
619-
var sym LSym
620-
674+
func (ft *WasmFuncType) Write(w *bytes.Buffer) {
621675
var b [8]byte
622676
writeByte := func(x byte) {
623-
sym.WriteBytes(ctxt, sym.Size, []byte{x})
677+
w.WriteByte(x)
624678
}
625679
writeUint32 := func(x uint32) {
626680
binary.LittleEndian.PutUint32(b[:], x)
627-
sym.WriteBytes(ctxt, sym.Size, b[:4])
681+
w.Write(b[:4])
628682
}
629683
writeInt64 := func(x int64) {
630684
binary.LittleEndian.PutUint64(b[:], uint64(x))
631-
sym.WriteBytes(ctxt, sym.Size, b[:])
632-
}
633-
writeString := func(s string) {
634-
writeUint32(uint32(len(s)))
635-
sym.WriteString(ctxt, sym.Size, len(s), s)
685+
w.Write(b[:])
636686
}
637-
writeString(wi.Module)
638-
writeString(wi.Name)
639-
writeUint32(uint32(len(wi.Params)))
640-
for _, f := range wi.Params {
687+
writeUint32(uint32(len(ft.Params)))
688+
for _, f := range ft.Params {
641689
writeByte(byte(f.Type))
642690
writeInt64(f.Offset)
643691
}
644-
writeUint32(uint32(len(wi.Results)))
645-
for _, f := range wi.Results {
692+
writeUint32(uint32(len(ft.Results)))
693+
for _, f := range ft.Results {
646694
writeByte(byte(f.Type))
647695
writeInt64(f.Offset)
648696
}
697+
}
649698

650-
return &sym
699+
func (ft *WasmFuncType) Read(b []byte) {
700+
readByte := func() byte {
701+
x := b[0]
702+
b = b[1:]
703+
return x
704+
}
705+
readUint32 := func() uint32 {
706+
x := binary.LittleEndian.Uint32(b)
707+
b = b[4:]
708+
return x
709+
}
710+
readInt64 := func() int64 {
711+
x := binary.LittleEndian.Uint64(b)
712+
b = b[8:]
713+
return int64(x)
714+
}
715+
ft.Params = make([]WasmField, readUint32())
716+
for i := range ft.Params {
717+
ft.Params[i].Type = WasmFieldType(readByte())
718+
ft.Params[i].Offset = int64(readInt64())
719+
}
720+
ft.Results = make([]WasmField, readUint32())
721+
for i := range ft.Results {
722+
ft.Results[i].Type = WasmFieldType(readByte())
723+
ft.Results[i].Offset = int64(readInt64())
724+
}
651725
}
652726

653727
type WasmField struct {

src/cmd/internal/obj/objfile.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,11 +621,11 @@ func (w *writer) Aux(s *LSym) {
621621
for _, pcSym := range fn.Pcln.Pcdata {
622622
w.aux1(goobj.AuxPcdata, pcSym)
623623
}
624-
if fn.WasmImportSym != nil {
625-
if fn.WasmImportSym.Size == 0 {
624+
if fn.WasmImport != nil {
625+
if fn.WasmImport.AuxSym.Size == 0 {
626626
panic("wasmimport aux sym must have non-zero size")
627627
}
628-
w.aux1(goobj.AuxWasmImport, fn.WasmImportSym)
628+
w.aux1(goobj.AuxWasmImport, fn.WasmImport.AuxSym)
629629
}
630630
} else if v := s.VarInfo(); v != nil {
631631
if v.dwarfInfoSym != nil && v.dwarfInfoSym.Size != 0 {
@@ -732,7 +732,7 @@ func nAuxSym(s *LSym) int {
732732
}
733733
n += len(fn.Pcln.Pcdata)
734734
if fn.WasmImport != nil {
735-
if fn.WasmImportSym == nil || fn.WasmImportSym.Size == 0 {
735+
if fn.WasmImport.AuxSym == nil || fn.WasmImport.AuxSym.Size == 0 {
736736
panic("wasmimport aux sym must exist and have non-zero size")
737737
}
738738
n++
@@ -797,7 +797,10 @@ func genFuncInfoSyms(ctxt *Link) {
797797
fn.FuncInfoSym = isym
798798
b.Reset()
799799

800-
auxsyms := []*LSym{fn.dwarfRangesSym, fn.dwarfLocSym, fn.dwarfDebugLinesSym, fn.dwarfInfoSym, fn.WasmImportSym}
800+
auxsyms := []*LSym{fn.dwarfRangesSym, fn.dwarfLocSym, fn.dwarfDebugLinesSym, fn.dwarfInfoSym}
801+
if wi := fn.WasmImport; wi != nil {
802+
auxsyms = append(auxsyms, wi.AuxSym)
803+
}
801804
for _, s := range auxsyms {
802805
if s == nil || s.Size == 0 {
803806
continue

src/cmd/internal/obj/sym.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,10 @@ func (ctxt *Link) traverseFuncAux(flag traverseFlag, fsym *LSym, fn func(parent
458458
}
459459
}
460460

461-
auxsyms := []*LSym{fninfo.dwarfRangesSym, fninfo.dwarfLocSym, fninfo.dwarfDebugLinesSym, fninfo.dwarfInfoSym, fninfo.WasmImportSym, fninfo.sehUnwindInfoSym}
461+
auxsyms := []*LSym{fninfo.dwarfRangesSym, fninfo.dwarfLocSym, fninfo.dwarfDebugLinesSym, fninfo.dwarfInfoSym, fninfo.sehUnwindInfoSym}
462+
if wi := fninfo.WasmImport; wi != nil {
463+
auxsyms = append(auxsyms, wi.AuxSym)
464+
}
462465
for _, s := range auxsyms {
463466
if s == nil || s.Size == 0 {
464467
continue

0 commit comments

Comments
 (0)