From 7795523910227cb6df3c91ebdb317037fc9ceb86 Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Mon, 1 Jun 2020 18:53:58 -0400 Subject: [PATCH] [dev.link] cmd/compile: use hash of export data as fingerprint Currently, the compiler generates a fingerprint for each package, which is used by the linker for index consistency check. When building plugin or shared object, currently the linker also generates a hash, by hashing the export data. At run time, when a package is referenced by multiple DSOs, this hash is compared to ensure consistency. It would be good if we can unify this two hashes. This way, the linker doesn't need to read the export data (which is intended for the compiler only, and is not always available for the linker). The export data hash is sufficient for both purposes. It is consistent with the current hash geneated by the linker. And the export data includes indices for exported symbols, so its hash can be used to catch index mismatches. Updates #33820. Change-Id: I2bc0d74930746f54c683a10dfd695d50ea3f5a38 Reviewed-on: https://go-review.googlesource.com/c/go/+/236118 Run-TryBot: Cherry Zhang Reviewed-by: Jeremy Faller Reviewed-by: Than McIntosh --- src/cmd/compile/internal/gc/iexport.go | 10 +++++++--- src/cmd/internal/obj/sym.go | 10 ---------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/cmd/compile/internal/gc/iexport.go b/src/cmd/compile/internal/gc/iexport.go index 32cc50fda1..328260fc59 100644 --- a/src/cmd/compile/internal/gc/iexport.go +++ b/src/cmd/compile/internal/gc/iexport.go @@ -207,6 +207,7 @@ import ( "cmd/compile/internal/types" "cmd/internal/goobj2" "cmd/internal/src" + "crypto/md5" "encoding/binary" "fmt" "io" @@ -295,12 +296,15 @@ func iexport(out *bufio.Writer) { hdr.uint64(dataLen) // Flush output. - io.Copy(out, &hdr) - io.Copy(out, &p.strings) - io.Copy(out, &p.data0) + h := md5.New() + wr := io.MultiWriter(out, h) + io.Copy(wr, &hdr) + io.Copy(wr, &p.strings) + io.Copy(wr, &p.data0) // Add fingerprint (used by linker object file). // Attach this to the end, so tools (e.g. gcimporter) don't care. + copy(Ctxt.Fingerprint[:], h.Sum(nil)[:]) out.Write(Ctxt.Fingerprint[:]) } diff --git a/src/cmd/internal/obj/sym.go b/src/cmd/internal/obj/sym.go index 4cbcb87b48..72a314aae0 100644 --- a/src/cmd/internal/obj/sym.go +++ b/src/cmd/internal/obj/sym.go @@ -34,7 +34,6 @@ package obj import ( "cmd/internal/goobj2" "cmd/internal/objabi" - "crypto/md5" "fmt" "log" "math" @@ -238,15 +237,6 @@ func (ctxt *Link) NumberSyms() { ctxt.pkgIdx[pkg] = ipkg ipkg++ }) - - // Compute a fingerprint of the indices, for exporting. - if !ctxt.IsAsm { - h := md5.New() - for _, s := range ctxt.defs { - h.Write([]byte(s.Name)) - } - copy(ctxt.Fingerprint[:], h.Sum(nil)[:]) - } } // Returns whether s is a non-package symbol, which needs to be referenced -- 2.48.1