From 3b0b3a02231083d8946b5afa5be6dd16dae5c6bf Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 15 Jun 2018 19:13:51 +0000 Subject: [PATCH] cmd/link: add buildid to wasm modules 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 Reviewed-by: Russ Cox --- src/cmd/link/internal/wasm/asm.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/cmd/link/internal/wasm/asm.go b/src/cmd/link/internal/wasm/asm.go index 96d9cbd71e..7cc6bef6b7 100644 --- a/src/cmd/link/internal/wasm/asm.go +++ b/src/cmd/link/internal/wasm/asm.go @@ -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) { -- 2.50.0