From: Cherry Zhang Date: Wed, 29 Jul 2020 22:10:15 +0000 (-0400) Subject: [dev.link] cmd/compile, cmd/link: make itab symbols content-addressable X-Git-Tag: go1.16beta1~1378^2~18 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cecb7a1cf31e8d8991669208e584269db32066c9;p=gostls13.git [dev.link] cmd/compile, cmd/link: make itab symbols content-addressable Extend the content-addressable symbol mechanism to itab symbols. Itab symbols require global uniqueness (as at run time we compare pointers), so it needs to be reliably deduplicated. Currently the content hash depends on symbol name expansion, so we can only do this when all Go packages are built with know package paths. Fall back to checking names if any Go package is built with unknown package path. Change-Id: Icf5e8873755050c20e5fc6549f6de1c883254c89 Reviewed-on: https://go-review.googlesource.com/c/go/+/245719 Reviewed-by: Jeremy Faller --- diff --git a/src/cmd/compile/internal/gc/reflect.go b/src/cmd/compile/internal/gc/reflect.go index 7758097db8..ce4838ce80 100644 --- a/src/cmd/compile/internal/gc/reflect.go +++ b/src/cmd/compile/internal/gc/reflect.go @@ -1559,6 +1559,7 @@ func dumptabs() { } // Nothing writes static itabs, so they are read only. ggloblsym(i.lsym, int32(o), int16(obj.DUPOK|obj.RODATA)) + i.lsym.Set(obj.AttrContentAddressable, true) ilink := itablinkpkg.Lookup(i.t.ShortString() + "," + i.itype.ShortString()).Linksym() dsymptr(ilink, 0, i.lsym, 0) ggloblsym(ilink, int32(Widthptr), int16(obj.DUPOK|obj.RODATA)) diff --git a/src/cmd/internal/obj/objfile2.go b/src/cmd/internal/obj/objfile2.go index 6740f42220..74f4fc63ba 100644 --- a/src/cmd/internal/obj/objfile2.go +++ b/src/cmd/internal/obj/objfile2.go @@ -367,7 +367,8 @@ func contentHash64(s *LSym) goobj2.Hash64Type { // consistent. // - For referenced content-addressable symbol, its content hash // is globally consistent. -// - For package symbol, its local index is globally consistent. +// - For package symbol and builtin symbol, its local index is +// globally consistent. // - For non-package symbol, its fully-expanded name is globally // consistent. For now, we require we know the current package // path so we can always expand symbol names. (Otherwise, @@ -398,11 +399,13 @@ func (w *writer) contentHash(s *LSym) goobj2.HashType { h.Write([]byte{1}) t := w.contentHash(rs) h.Write(t[:]) - case goobj2.PkgIdxBuiltin: - panic("unsupported") case goobj2.PkgIdxNone: h.Write([]byte{2}) io.WriteString(h, rs.Name) // name is already expanded at this point + case goobj2.PkgIdxBuiltin: + h.Write([]byte{3}) + binary.LittleEndian.PutUint32(tmp[:4], uint32(rs.SymIdx)) + h.Write(tmp[:4]) case goobj2.PkgIdxSelf: io.WriteString(h, w.pkgpath) binary.LittleEndian.PutUint32(tmp[:4], uint32(rs.SymIdx)) diff --git a/src/cmd/link/internal/loader/loader.go b/src/cmd/link/internal/loader/loader.go index d34e6fdf6b..2f4a0efbf4 100644 --- a/src/cmd/link/internal/loader/loader.go +++ b/src/cmd/link/internal/loader/loader.go @@ -277,6 +277,8 @@ type Loader struct { flags uint32 + hasUnknownPkgPath bool // if any Go object has unknown package path + strictDupMsgs int // number of strict-dup warning/errors, when FlagStrictDups is enabled elfsetstring elfsetstringFunc @@ -378,6 +380,9 @@ func (l *Loader) addObj(pkg string, r *oReader) Sym { i := Sym(len(l.objSyms)) l.start[r] = i l.objs = append(l.objs, objIdx{r, i}) + if r.NeedNameExpansion() && !r.FromAssembly() { + l.hasUnknownPkgPath = true + } return i } @@ -2086,6 +2091,16 @@ func (l *Loader) preloadSyms(r *oReader, kind int) { case hashedDef: start = uint32(r.ndef + r.nhashed64def) end = uint32(r.ndef + r.nhashed64def + r.nhasheddef) + if l.hasUnknownPkgPath { + // The content hash depends on symbol name expansion. If any package is + // built without fully expanded names, the content hash is unreliable. + // Treat them as named symbols. + // This is rare. + // (We don't need to do this for hashed64Def case, as there the hash + // function is simply the identity function, which doesn't depend on + // name expansion.) + kind = nonPkgDef + } case nonPkgDef: start = uint32(r.ndef + r.nhashed64def + r.nhasheddef) end = uint32(r.ndef + r.nhashed64def + r.nhasheddef + r.NNonpkgdef())