]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: add buildid to wasm modules
authorBrad Fitzpatrick <bradfitz@golang.org>
Fri, 15 Jun 2018 19:13:51 +0000 (19:13 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 15 Jun 2018 19:25:02 +0000 (19:25 +0000)
Add Go buildids into a custom wasm section ("go.buildid", arbitrarily)
early in the wasm module, right after the magic & version.

Fixes #25910

Change-Id: If3f7cb267bf8c7beb6fa8d8b7a4829419720bbd8
Reviewed-on: https://go-review.googlesource.com/119175
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/link/internal/wasm/asm.go

index 96d9cbd71efb1b47d6f2e932f32d27a58c601717..7cc6bef6b78daa888c45f1ab26b19208efb280e1 100644 (file)
@@ -122,6 +122,7 @@ func asmb(ctxt *ld.Link) {
        }
 
        // collect functions with WebAssembly body
+       var buildid []byte
        fns := make([]*wasmFunc, len(ctxt.Textp))
        for i, fn := range ctxt.Textp {
                wfn := new(bytes.Buffer)
@@ -129,7 +130,7 @@ func asmb(ctxt *ld.Link) {
                        writeUleb128(wfn, 0) // number of sets of locals
                        writeI32Const(wfn, 0)
                        wfn.WriteByte(0x0b) // end
-
+                       buildid = fn.P
                } else {
                        // Relocations have variable length, handle them here.
                        off := int32(0)
@@ -166,6 +167,11 @@ func asmb(ctxt *ld.Link) {
        ctxt.Out.Write([]byte{0x00, 0x61, 0x73, 0x6d}) // magic
        ctxt.Out.Write([]byte{0x01, 0x00, 0x00, 0x00}) // version
 
+       // Add any buildid early in the binary:
+       if len(buildid) != 0 {
+               writeBuildID(ctxt, buildid)
+       }
+
        writeTypeSec(ctxt, types)
        writeImportSec(ctxt, hostImports)
        writeFunctionSec(ctxt, fns)
@@ -207,6 +213,13 @@ func writeSecSize(ctxt *ld.Link, sizeOffset int64) {
        ctxt.Out.SeekSet(endOffset)
 }
 
+func writeBuildID(ctxt *ld.Link, buildid []byte) {
+       sizeOffset := writeSecHeader(ctxt, sectionCustom)
+       writeName(ctxt.Out, "go.buildid")
+       ctxt.Out.Write(buildid)
+       writeSecSize(ctxt, sizeOffset)
+}
+
 // writeTypeSec writes the section that declares all function types
 // so they can be referenced by index.
 func writeTypeSec(ctxt *ld.Link, types []*wasmFuncType) {