Commit 3b0b3a02 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 57568958
......@@ -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) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment