From 57662b1575030aa09043cd7a48425abdc6e0e0a3 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Tue, 24 Sep 2019 00:48:39 +0200 Subject: [PATCH] cmd/link: add producer section to wasm binaries This change adds an optional "producer" section that reports the source language and compiler version. See https://github.com/WebAssembly/tool-conventions/blob/master/ProducersSection.md. It also removes the now redundant "go.version" section. Fixes #33295. Change-Id: Ib4c80528728caf9e524fbd3f26822cbbc8b05a75 Reviewed-on: https://go-review.googlesource.com/c/go/+/196804 Run-TryBot: Richard Musiol TryBot-Result: Gobot Gobot Reviewed-by: Agniva De Sarker Reviewed-by: Cherry Zhang --- src/cmd/link/internal/wasm/asm.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/cmd/link/internal/wasm/asm.go b/src/cmd/link/internal/wasm/asm.go index 54b265cb19f..ee0a5176acf 100644 --- a/src/cmd/link/internal/wasm/asm.go +++ b/src/cmd/link/internal/wasm/asm.go @@ -11,7 +11,6 @@ import ( "cmd/link/internal/sym" "io" "regexp" - "runtime" ) const ( @@ -177,7 +176,6 @@ func asmb2(ctxt *ld.Link) { writeBuildID(ctxt, buildid) } - writeGoVersion(ctxt) writeTypeSec(ctxt, types) writeImportSec(ctxt, hostImports) writeFunctionSec(ctxt, fns) @@ -188,6 +186,7 @@ func asmb2(ctxt *ld.Link) { writeElementSec(ctxt, uint64(len(hostImports)), uint64(len(fns))) writeCodeSec(ctxt, fns) writeDataSec(ctxt) + writeProducerSec(ctxt) if !*ld.FlagS { writeNameSec(ctxt, len(hostImports), fns) } @@ -226,13 +225,6 @@ func writeBuildID(ctxt *ld.Link, buildid []byte) { writeSecSize(ctxt, sizeOffset) } -func writeGoVersion(ctxt *ld.Link) { - sizeOffset := writeSecHeader(ctxt, sectionCustom) - writeName(ctxt.Out, "go.version") - ctxt.Out.Write([]byte(runtime.Version())) - 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) { @@ -488,6 +480,26 @@ func writeDataSec(ctxt *ld.Link) { writeSecSize(ctxt, sizeOffset) } +// writeProducerSec writes an optional section that reports the source language and compiler version. +func writeProducerSec(ctxt *ld.Link) { + sizeOffset := writeSecHeader(ctxt, sectionCustom) + writeName(ctxt.Out, "producers") + + writeUleb128(ctxt.Out, 2) // number of fields + + writeName(ctxt.Out, "language") // field name + writeUleb128(ctxt.Out, 1) // number of values + writeName(ctxt.Out, "Go") // value: name + writeName(ctxt.Out, objabi.Version) // value: version + + writeName(ctxt.Out, "processed-by") // field name + writeUleb128(ctxt.Out, 1) // number of values + writeName(ctxt.Out, "Go cmd/compile") // value: name + writeName(ctxt.Out, objabi.Version) // value: version + + writeSecSize(ctxt, sizeOffset) +} + var nameRegexp = regexp.MustCompile(`[^\w\.]`) // writeNameSec writes an optional section that assigns names to the functions declared by the "func" section.