diff options
| author | Cherry Zhang <cherryyz@google.com> | 2020-07-15 23:14:50 -0400 |
|---|---|---|
| committer | Cherry Zhang <cherryyz@google.com> | 2020-07-20 17:26:32 +0000 |
| commit | 526d99a49ae67bfde15134b96159680988615d2d (patch) | |
| tree | 35f2e189bdb7fedec71ae11933b8b1edd2642a23 /src/cmd/internal/obj/objfile_test.go | |
| parent | 289c238a339c59737b7f271b19bc9217e7d69851 (diff) | |
| download | go-526d99a49ae67bfde15134b96159680988615d2d.tar.xz | |
[dev.link] cmd/internal/obj: handle content-addressable symbols with relocations
For content-addressable symbols with relocations, we build a
content hash based on its content and relocations. Depending on
the category of the referenced symbol, we choose different hash
algorithms such that the hash is globally consistent.
For now, we only support content-addressable symbols with
relocations when the current package's import path is known, so
that the symbol names are fully expanded. Otherwise, if the
referenced symbol is a named symbol whose name is not fully
expanded, the hash won't be globally consistent, and can cause
erroneous collisions. This is fine for now, as the deduplication
is just an optimization, not a requirement for correctness (until
we get to type descriptors).
Change-Id: I639e4e03dd749b5d71f0a55c2525926575b1ac30
Reviewed-on: https://go-review.googlesource.com/c/go/+/243142
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
Diffstat (limited to 'src/cmd/internal/obj/objfile_test.go')
| -rw-r--r-- | src/cmd/internal/obj/objfile_test.go | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/cmd/internal/obj/objfile_test.go b/src/cmd/internal/obj/objfile_test.go new file mode 100644 index 0000000000..ed3be20760 --- /dev/null +++ b/src/cmd/internal/obj/objfile_test.go @@ -0,0 +1,87 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package obj + +import ( + "cmd/internal/goobj2" + "cmd/internal/sys" + "testing" +) + +var dummyArch = LinkArch{Arch: sys.ArchAMD64} + +func TestContentHash64(t *testing.T) { + s1 := &LSym{P: []byte("A")} + s2 := &LSym{P: []byte("A\x00\x00\x00")} + s1.Set(AttrContentAddressable, true) + s2.Set(AttrContentAddressable, true) + h1 := contentHash64(s1) + h2 := contentHash64(s2) + if h1 != h2 { + t.Errorf("contentHash64(s1)=%x, contentHash64(s2)=%x, expect equal", h1, h2) + } + + ctxt := Linknew(&dummyArch) // little endian + s3 := ctxt.Int64Sym(int64('A')) + h3 := contentHash64(s3) + if h1 != h3 { + t.Errorf("contentHash64(s1)=%x, contentHash64(s3)=%x, expect equal", h1, h3) + } +} + +func TestContentHash(t *testing.T) { + syms := []*LSym{ + &LSym{P: []byte("TestSymbol")}, // 0 + &LSym{P: []byte("TestSymbol")}, // 1 + &LSym{P: []byte("TestSymbol2")}, // 2 + &LSym{P: []byte("")}, // 3 + &LSym{P: []byte("")}, // 4 + &LSym{P: []byte("")}, // 5 + &LSym{P: []byte("")}, // 6 + } + for _, s := range syms { + s.Set(AttrContentAddressable, true) + s.PkgIdx = goobj2.PkgIdxHashed + } + // s3 references s0 + r := Addrel(syms[3]) + r.Sym = syms[0] + // s4 references s0 + r = Addrel(syms[4]) + r.Sym = syms[0] + // s5 references s1 + r = Addrel(syms[5]) + r.Sym = syms[1] + // s6 references s2 + r = Addrel(syms[6]) + r.Sym = syms[2] + + // compute hashes + h := make([]goobj2.HashType, len(syms)) + w := &writer{} + for i := range h { + h[i] = w.contentHash(syms[i]) + } + + tests := []struct { + a, b int + equal bool + }{ + {0, 1, true}, // same contents, no relocs + {0, 2, false}, // different contents + {3, 4, true}, // same contents, same relocs + {3, 5, true}, // recursively same contents + {3, 6, false}, // same contents, different relocs + } + for _, test := range tests { + if (h[test.a] == h[test.b]) != test.equal { + eq := "equal" + if !test.equal { + eq = "not equal" + } + t.Errorf("h%d=%x, h%d=%x, expect %s", test.a, h[test.a], test.b, h[test.b], eq) + } + } +} |
