[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 <cherryyz@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/src/cmd/compile/internal/gc/iexport.go b/src/cmd/compile/internal/gc/iexport.go
index 32cc50f..328260f 100644
--- a/src/cmd/compile/internal/gc/iexport.go
+++ b/src/cmd/compile/internal/gc/iexport.go
@@ -207,6 +207,7 @@
 	"cmd/compile/internal/types"
 	"cmd/internal/goobj2"
 	"cmd/internal/src"
+	"crypto/md5"
 	"encoding/binary"
 	"fmt"
 	"io"
@@ -295,12 +296,15 @@
 	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 4cbcb87..72a314a 100644
--- a/src/cmd/internal/obj/sym.go
+++ b/src/cmd/internal/obj/sym.go
@@ -34,7 +34,6 @@
 import (
 	"cmd/internal/goobj2"
 	"cmd/internal/objabi"
-	"crypto/md5"
 	"fmt"
 	"log"
 	"math"
@@ -238,15 +237,6 @@
 		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