1
0
mirror of https://github.com/golang/go synced 2024-11-18 05:54:49 -07:00

[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>
This commit is contained in:
Cherry Zhang 2020-06-01 18:53:58 -04:00
parent 4cc043d883
commit 7795523910
2 changed files with 7 additions and 13 deletions

View File

@ -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[:])
}

View File

@ -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