]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: convert asmb2 pass to new style on Wasm
authorCherry Zhang <cherryyz@google.com>
Fri, 8 May 2020 18:09:57 +0000 (14:09 -0400)
committerCherry Zhang <cherryyz@google.com>
Mon, 11 May 2020 14:43:50 +0000 (14:43 +0000)
And no longer do loadlibfull there.

Change-Id: I3dd41d25f5f7db4ef1d112559299e322acb32641
Reviewed-on: https://go-review.googlesource.com/c/go/+/232987
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
12 files changed:
src/cmd/link/internal/amd64/asm.go
src/cmd/link/internal/arm/asm.go
src/cmd/link/internal/arm64/asm.go
src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/main.go
src/cmd/link/internal/mips/asm.go
src/cmd/link/internal/mips64/asm.go
src/cmd/link/internal/ppc64/asm.go
src/cmd/link/internal/riscv64/asm.go
src/cmd/link/internal/s390x/asm.go
src/cmd/link/internal/wasm/asm.go
src/cmd/link/internal/x86/asm.go

index a0eaf64c8e8c8461f952f7efe742af8f51fe3273..c05767022ca8446cc3091594981c77555db27c19 100644 (file)
@@ -722,7 +722,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        machlink := int64(0)
        if ctxt.HeadType == objabi.Hdarwin {
                machlink = ld.Domacholink(ctxt)
index 275b5fe34fe2ba2d36aeae51c0de86f9cf445729..5990179fe74c09f5eb652e304b1f3916a249fff9 100644 (file)
@@ -696,7 +696,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        /* output symbol table */
        ld.Symsize = 0
 
index 8e0c75621ca2559d09a60a811ef1498c1cc2d531..306eb7d46cf8ee3de7a4f3f4030be79e31409591 100644 (file)
@@ -825,7 +825,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        machlink := uint32(0)
        if ctxt.HeadType == objabi.Hdarwin {
                machlink = uint32(ld.Domacholink(ctxt))
index 7f25e0c18d742ebb0f615ad55ed5f770248ff7dc..20c483ea4468adddda4dadea3e591187bb174546 100644 (file)
@@ -266,7 +266,7 @@ type Arch struct {
        // segments), for which we have computed the size and offset. Asmb2
        // writes the rest.
        Asmb  func(*Link, *loader.Loader)
-       Asmb2 func(*Link)
+       Asmb2 func(*Link, *loader.Loader)
 
        Elfreloc1   func(*Link, *sym.Reloc, int64) bool
        Elfreloc2   func(*Link, *loader.Loader, loader.Sym, loader.ExtRelocView, int64) bool
index 5b16314955c38e988289a60a207f0be960bde1f3..3c28a8b6da8c547dae39aafbd0e53884d082ce84 100644 (file)
@@ -320,19 +320,18 @@ func Main(arch *sys.Arch, theArch Arch) {
        thearch.Asmb(ctxt, ctxt.loader)
        bench.Start("reloc")
        ctxt.reloc()
-       newasmb2 := ctxt.IsDarwin() || ctxt.IsWindows()
+       newasmb2 := ctxt.IsDarwin() || ctxt.IsWindows() || ctxt.IsWasm()
        if !newasmb2 {
                bench.Start("loadlibfull")
                // We don't need relocations at this point.
-               // Wasm is an exception, where it applies text relocations in Asmb2.
-               needReloc := ctxt.IsWasm()
+               needReloc := false
                // On AMD64 ELF, we directly use the loader's ExtRelocs, so we don't
                // need conversion. Otherwise we do.
                needExtReloc := ctxt.IsExternal() && !(ctxt.IsAMD64() && ctxt.IsELF)
                ctxt.loadlibfull(symGroupType, needReloc, needExtReloc) // XXX do it here for now
        }
        bench.Start("Asmb2")
-       thearch.Asmb2(ctxt)
+       thearch.Asmb2(ctxt, ctxt.loader)
 
        bench.Start("Munmap")
        ctxt.Out.Close() // Close handles Munmapping if necessary.
index e372a5a7ec086484031623623d7df65b4b9cbd4d..67b3726271cf17c58ceaf2467fcf657aceb9b75d 100644 (file)
@@ -184,7 +184,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        /* output symbol table */
        ld.Symsize = 0
 
index 317ede70a769fb90758ce145978e93ff5bf272a6..c67753c290e30da9f44c781f0b248783bcb0151e 100644 (file)
@@ -193,7 +193,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        /* output symbol table */
        ld.Symsize = 0
 
index b0adf27f9bab46ccdcbc6ace803a595f204e72c5..c6ee3d6033a9b59d6b5729b7bc14a2aa0a6afdb2 100644 (file)
@@ -1106,7 +1106,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        /* output symbol table */
        ld.Symsize = 0
 
index b82f98fda8ed57a31777cf19285ae26cebd148cf..f76a42b1c0542b28b2e12107b412696bf934188a 100644 (file)
@@ -129,7 +129,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        ld.Symsize = 0
        ld.Lcsize = 0
        symo := uint32(0)
index a39ddcf787c2f46b5b220c7d48436e977e316da3..3e7b1ffb1498ef369f907fea0f9a0f10444f6637 100644 (file)
@@ -503,7 +503,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        /* output symbol table */
        ld.Symsize = 0
 
index 1eb3291db6667bd15a3129181383abba8726d938..e52f44b94a2d9ca2ad8aeebdb88e63cf299f538f 100644 (file)
@@ -119,7 +119,7 @@ func asmb(ctxt *ld.Link, ldr *loader.Loader) {
 
 // asmb writes the final WebAssembly module binary.
 // Spec: https://webassembly.github.io/spec/core/binary/modules.html
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, ldr *loader.Loader) {
        types := []*wasmFuncType{
                // For normal Go functions, the single parameter is PC_B,
                // the return value is
@@ -135,13 +135,15 @@ func asmb2(ctxt *ld.Link) {
                        Type: lookupType(&wasmFuncType{Params: []byte{I32}}, &types),
                },
        }
-       hostImportMap := make(map[*sym.Symbol]int64)
-       for _, fn := range ctxt.Textp {
-               for _, r := range fn.R {
-                       if r.Type == objabi.R_WASMIMPORT {
-                               hostImportMap[r.Sym] = int64(len(hostImports))
+       hostImportMap := make(map[loader.Sym]int64)
+       for _, fn := range ctxt.Textp2 {
+               relocs := ldr.Relocs(fn)
+               for ri := 0; ri < relocs.Count(); ri++ {
+                       r := relocs.At2(ri)
+                       if r.Type() == objabi.R_WASMIMPORT {
+                               hostImportMap[r.Sym()] = int64(len(hostImports))
                                hostImports = append(hostImports, &wasmFunc{
-                                       Name: r.Sym.Name,
+                                       Name: ldr.SymName(r.Sym()),
                                        Type: lookupType(&wasmFuncType{Params: []byte{I32}}, &types),
                                })
                        }
@@ -150,41 +152,45 @@ func asmb2(ctxt *ld.Link) {
 
        // collect functions with WebAssembly body
        var buildid []byte
-       fns := make([]*wasmFunc, len(ctxt.Textp))
-       for i, fn := range ctxt.Textp {
+       fns := make([]*wasmFunc, len(ctxt.Textp2))
+       for i, fn := range ctxt.Textp2 {
                wfn := new(bytes.Buffer)
-               if fn.Name == "go.buildid" {
+               if ldr.SymName(fn) == "go.buildid" {
                        writeUleb128(wfn, 0) // number of sets of locals
                        writeI32Const(wfn, 0)
                        wfn.WriteByte(0x0b) // end
-                       buildid = fn.P
+                       buildid = ldr.Data(fn)
                } else {
                        // Relocations have variable length, handle them here.
+                       relocs := ldr.Relocs(fn)
+                       P := ldr.Data(fn)
                        off := int32(0)
-                       for _, r := range fn.R {
-                               wfn.Write(fn.P[off:r.Off])
-                               off = r.Off
-                               switch r.Type {
+                       for ri := 0; ri < relocs.Count(); ri++ {
+                               r := relocs.At2(ri)
+                               wfn.Write(P[off:r.Off()])
+                               off = r.Off()
+                               rs := ldr.ResolveABIAlias(r.Sym())
+                               switch r.Type() {
                                case objabi.R_ADDR:
-                                       writeSleb128(wfn, r.Sym.Value+r.Add)
+                                       writeSleb128(wfn, ldr.SymValue(rs)+r.Add())
                                case objabi.R_CALL:
-                                       writeSleb128(wfn, int64(len(hostImports))+r.Sym.Value>>16-funcValueOffset)
+                                       writeSleb128(wfn, int64(len(hostImports))+ldr.SymValue(rs)>>16-funcValueOffset)
                                case objabi.R_WASMIMPORT:
-                                       writeSleb128(wfn, hostImportMap[r.Sym])
+                                       writeSleb128(wfn, hostImportMap[rs])
                                default:
-                                       ld.Errorf(fn, "bad reloc type %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type))
+                                       ldr.Errorf(fn, "bad reloc type %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
                                        continue
                                }
                        }
-                       wfn.Write(fn.P[off:])
+                       wfn.Write(P[off:])
                }
 
                typ := uint32(0)
-               if sig, ok := wasmFuncTypes[fn.Name]; ok {
+               if sig, ok := wasmFuncTypes[ldr.SymName(fn)]; ok {
                        typ = lookupType(sig, &types)
                }
 
-               name := nameRegexp.ReplaceAllString(fn.Name, "_")
+               name := nameRegexp.ReplaceAllString(ldr.SymName(fn), "_")
                fns[i] = &wasmFunc{Name: name, Type: typ, Code: wfn.Bytes()}
        }
 
@@ -200,9 +206,9 @@ func asmb2(ctxt *ld.Link) {
        writeImportSec(ctxt, hostImports)
        writeFunctionSec(ctxt, fns)
        writeTableSec(ctxt, fns)
-       writeMemorySec(ctxt)
+       writeMemorySec(ctxt, ldr)
        writeGlobalSec(ctxt)
-       writeExportSec(ctxt, len(hostImports))
+       writeExportSec(ctxt, ldr, len(hostImports))
        writeElementSec(ctxt, uint64(len(hostImports)), uint64(len(fns)))
        writeCodeSec(ctxt, fns)
        writeDataSec(ctxt)
@@ -311,10 +317,10 @@ func writeTableSec(ctxt *ld.Link, fns []*wasmFunc) {
 
 // writeMemorySec writes the section that declares linear memories. Currently one linear memory is being used.
 // Linear memory always starts at address zero. More memory can be requested with the GrowMemory instruction.
-func writeMemorySec(ctxt *ld.Link) {
+func writeMemorySec(ctxt *ld.Link, ldr *loader.Loader) {
        sizeOffset := writeSecHeader(ctxt, sectionMemory)
 
-       dataSection := ctxt.Syms.Lookup("runtime.data", 0).Sect
+       dataSection := ldr.SymSect(ldr.Lookup("runtime.data", 0))
        dataEnd := dataSection.Vaddr + dataSection.Length
        var initialSize = dataEnd + 16<<20 // 16MB, enough for runtime init without growing
 
@@ -362,13 +368,14 @@ func writeGlobalSec(ctxt *ld.Link) {
 // writeExportSec writes the section that declares exports.
 // Exports can be accessed by the WebAssembly host, usually JavaScript.
 // The wasm_export_* functions and the linear memory get exported.
-func writeExportSec(ctxt *ld.Link, lenHostImports int) {
+func writeExportSec(ctxt *ld.Link, ldr *loader.Loader, lenHostImports int) {
        sizeOffset := writeSecHeader(ctxt, sectionExport)
 
        writeUleb128(ctxt.Out, 4) // number of exports
 
        for _, name := range []string{"run", "resume", "getsp"} {
-               idx := uint32(lenHostImports) + uint32(ctxt.Syms.ROLookup("wasm_export_"+name, 0).Value>>16) - funcValueOffset
+               s := ldr.Lookup("wasm_export_"+name, 0)
+               idx := uint32(lenHostImports) + uint32(ldr.SymValue(s)>>16) - funcValueOffset
                writeName(ctxt.Out, name)           // inst.exports.run/resume/getsp in wasm_exec.js
                ctxt.Out.WriteByte(0x00)            // func export
                writeUleb128(ctxt.Out, uint64(idx)) // funcidx
index ddf04083afb8c5e32ca38b1937bf6c152c49ba76..824e20b828eebc04f1ca9523baae5caef192be53 100644 (file)
@@ -587,7 +587,7 @@ func asmb(ctxt *ld.Link, _ *loader.Loader) {
        wg.Wait()
 }
 
-func asmb2(ctxt *ld.Link) {
+func asmb2(ctxt *ld.Link, _ *loader.Loader) {
        machlink := uint32(0)
        if ctxt.HeadType == objabi.Hdarwin {
                machlink = uint32(ld.Domacholink(ctxt))