aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/link/internal/loader/loader_test.go
diff options
context:
space:
mode:
authorThan McIntosh <thanm@google.com>2019-12-11 14:36:17 -0500
committerThan McIntosh <thanm@google.com>2019-12-27 15:05:39 +0000
commitb720014743d7de2e706bb6b54ca914653d86f76c (patch)
treed59d348cb15c6fea327d7bbdc25e9d7b94f095f8 /src/cmd/link/internal/loader/loader_test.go
parentc6fea80b9582d7c4f86bf88b404ac6ebb33359fd (diff)
downloadgo-b720014743d7de2e706bb6b54ca914653d86f76c.tar.xz
[dev.link] cmd/link: add SymbolBuilder helper
Add SymbolBuilder helper type -- this type provides a set of methods intended to make it easy to manipulate the content of a symbol (type, relocations, data, etc). Change-Id: I579bf8d04650e66d33a9780a6c2347a576c94c6f Reviewed-on: https://go-review.googlesource.com/c/go/+/210178 Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Jeremy Faller <jeremy@golang.org>
Diffstat (limited to 'src/cmd/link/internal/loader/loader_test.go')
-rw-r--r--src/cmd/link/internal/loader/loader_test.go223
1 files changed, 219 insertions, 4 deletions
diff --git a/src/cmd/link/internal/loader/loader_test.go b/src/cmd/link/internal/loader/loader_test.go
index 92ade70b8f..9ed84ccc5e 100644
--- a/src/cmd/link/internal/loader/loader_test.go
+++ b/src/cmd/link/internal/loader/loader_test.go
@@ -5,6 +5,9 @@
package loader
import (
+ "bytes"
+ "cmd/internal/objabi"
+ "cmd/internal/sys"
"cmd/link/internal/sym"
"fmt"
"testing"
@@ -26,14 +29,15 @@ func addDummyObjSym(t *testing.T, ldr *Loader, or *oReader, name string) Sym {
}
func TestAddMaterializedSymbol(t *testing.T) {
- ldr := NewLoader(0)
+ edummy := func(s *sym.Symbol, str string, off int) {}
+ ldr := NewLoader(0, edummy)
dummyOreader := oReader{version: -1}
or := &dummyOreader
// Create some syms from a dummy object file symbol to get things going.
- addDummyObjSym(t, ldr, or, "type.uint8")
+ ts1 := addDummyObjSym(t, ldr, or, "type.uint8")
ts2 := addDummyObjSym(t, ldr, or, "mumble")
- addDummyObjSym(t, ldr, or, "type.string")
+ ts3 := addDummyObjSym(t, ldr, or, "type.string")
// Create some external symbols.
es1 := ldr.AddExtSym("extnew1", 0)
@@ -54,6 +58,22 @@ func TestAddMaterializedSymbol(t *testing.T) {
t.Fatalf("CreateExtSym failed for nameless sym")
}
+ // Grab symbol builder pointers
+ sb1 := ldr.MakeSymbolUpdater(es1)
+ sb2 := ldr.MakeSymbolUpdater(es2)
+ sb3 := ldr.MakeSymbolUpdater(es3)
+
+ // Check get/set symbol type
+ es3typ := sb3.Type()
+ if es3typ != sym.Sxxx {
+ t.Errorf("SymType(es3): expected %d, got %d", sym.Sxxx, es3typ)
+ }
+ sb2.SetType(sym.SRODATA)
+ es3typ = sb2.Type()
+ if es3typ != sym.SRODATA {
+ t.Errorf("SymType(es3): expected %d, got %d", sym.SRODATA, es3typ)
+ }
+
// New symbols should not initially be reachable.
if ldr.AttrReachable(es1) || ldr.AttrReachable(es2) || ldr.AttrReachable(es3) {
t.Errorf("newly materialized symbols should not be reachable")
@@ -88,6 +108,9 @@ func TestAddMaterializedSymbol(t *testing.T) {
}
}
+ sb1 = ldr.MakeSymbolUpdater(es1)
+ sb2 = ldr.MakeSymbolUpdater(es2)
+
// Get/set a few other attributes
if ldr.AttrVisibilityHidden(es3) {
t.Errorf("expected initially not hidden")
@@ -120,10 +143,202 @@ func TestAddMaterializedSymbol(t *testing.T) {
if es3al != 128 {
t.Errorf("SymAlign(es3): expected 128, got %d", es3al)
}
+
+ // Add some relocations to the new symbols.
+ r1 := Reloc{0, 1, objabi.R_ADDR, 0, ts1}
+ r2 := Reloc{3, 8, objabi.R_CALL, 0, ts2}
+ r3 := Reloc{7, 1, objabi.R_USETYPE, 0, ts3}
+ sb1.AddReloc(r1)
+ sb1.AddReloc(r2)
+ sb2.AddReloc(r3)
+
+ // Add some data to the symbols.
+ d1 := []byte{1, 2, 3}
+ d2 := []byte{4, 5, 6, 7}
+ sb1.AddBytes(d1)
+ sb2.AddBytes(d2)
+
+ // Now invoke the usual loader interfaces to make sure
+ // we're getting the right things back for these symbols.
+ // First relocations...
+ expRel := [][]Reloc{[]Reloc{r1, r2}, []Reloc{r3}}
+ for k, sb := range []*SymbolBuilder{sb1, sb2} {
+ rsl := sb.Relocs()
+ exp := expRel[k]
+ if !sameRelocSlice(rsl, exp) {
+ t.Errorf("expected relocs %v, got %v", exp, rsl)
+ }
+ relocs := ldr.Relocs(sb.Sym())
+ r0 := relocs.At(0)
+ if r0 != exp[0] {
+ t.Errorf("expected reloc %v, got %v", exp[0], r0)
+ }
+ }
+
+ // ... then data.
+ dat := sb2.Data()
+ if bytes.Compare(dat, d2) != 0 {
+ t.Errorf("expected es2 data %v, got %v", d2, dat)
+ }
+
+ // Nameless symbol should still be nameless.
+ es3name := ldr.RawSymName(es3)
+ if "" != es3name {
+ t.Errorf("expected es3 name of '', got '%s'", es3name)
+ }
+
+ // Read value of materialized symbol.
+ es1val := sb1.Value()
+ if 0 != es1val {
+ t.Errorf("expected es1 value of 0, got %v", es1val)
+ }
+
+ // Test other misc methods
+ irm := ldr.IsReflectMethod(es1)
+ if 0 != es1val {
+ t.Errorf("expected IsReflectMethod(es1) value of 0, got %v", irm)
+ }
+
+ // Writing data to a materialized symbol should mark it reachable.
+ if !sb1.Reachable() || !sb2.Reachable() {
+ t.Fatalf("written-to materialized symbols should be reachable")
+ }
+}
+
+func sameRelocSlice(s1 []Reloc, s2 []Reloc) bool {
+ if len(s1) != len(s2) {
+ return false
+ }
+ for i := 0; i < len(s1); i++ {
+ if s1[i] != s2[i] {
+ return false
+ }
+ }
+ return true
+}
+
+type addFunc func(l *Loader, s Sym, s2 Sym)
+
+func TestAddDataMethods(t *testing.T) {
+ edummy := func(s *sym.Symbol, str string, off int) {}
+ ldr := NewLoader(0, edummy)
+ dummyOreader := oReader{version: -1}
+ or := &dummyOreader
+
+ // Populate loader with some symbols.
+ addDummyObjSym(t, ldr, or, "type.uint8")
+ ldr.AddExtSym("hello", 0)
+
+ arch := sys.ArchAMD64
+ var testpoints = []struct {
+ which string
+ addDataFunc addFunc
+ expData []byte
+ expKind sym.SymKind
+ expRel []Reloc
+ }{
+ {
+ which: "AddUint8",
+ addDataFunc: func(l *Loader, s Sym, _ Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddUint8('a')
+ },
+ expData: []byte{'a'},
+ expKind: sym.SDATA,
+ },
+ {
+ which: "AddUintXX",
+ addDataFunc: func(l *Loader, s Sym, _ Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddUintXX(arch, 25185, 2)
+ },
+ expData: []byte{'a', 'b'},
+ expKind: sym.SDATA,
+ },
+ {
+ which: "SetUint8",
+ addDataFunc: func(l *Loader, s Sym, _ Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddUint8('a')
+ sb.AddUint8('b')
+ sb.SetUint8(arch, 1, 'c')
+ },
+ expData: []byte{'a', 'c'},
+ expKind: sym.SDATA,
+ },
+ {
+ which: "AddString",
+ addDataFunc: func(l *Loader, s Sym, _ Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.Addstring("hello")
+ },
+ expData: []byte{'h', 'e', 'l', 'l', 'o', 0},
+ expKind: sym.SNOPTRDATA,
+ },
+ {
+ which: "AddAddrPlus",
+ addDataFunc: func(l *Loader, s Sym, s2 Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddAddrPlus(arch, s2, 3)
+ },
+ expData: []byte{0, 0, 0, 0, 0, 0, 0, 0},
+ expKind: sym.SDATA,
+ expRel: []Reloc{Reloc{Type: objabi.R_ADDR, Size: 8, Add: 3, Sym: 6}},
+ },
+ {
+ which: "AddAddrPlus4",
+ addDataFunc: func(l *Loader, s Sym, s2 Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddAddrPlus4(arch, s2, 3)
+ },
+ expData: []byte{0, 0, 0, 0},
+ expKind: sym.SDATA,
+ expRel: []Reloc{Reloc{Type: objabi.R_ADDR, Size: 4, Add: 3, Sym: 7}},
+ },
+ {
+ which: "AddCURelativeAddrPlus",
+ addDataFunc: func(l *Loader, s Sym, s2 Sym) {
+ sb := l.MakeSymbolUpdater(s)
+ sb.AddCURelativeAddrPlus(arch, s2, 7)
+ },
+ expData: []byte{0, 0, 0, 0, 0, 0, 0, 0},
+ expKind: sym.SDATA,
+ expRel: []Reloc{Reloc{Type: objabi.R_ADDRCUOFF, Size: 8, Add: 7, Sym: 8}},
+ },
+ }
+
+ var pmi Sym
+ for k, tp := range testpoints {
+ name := fmt.Sprintf("new%d", k+1)
+ mi := ldr.AddExtSym(name, 0)
+ if mi == 0 {
+ t.Fatalf("AddExtSym failed for '" + name + "'")
+ }
+ tp.addDataFunc(ldr, mi, pmi)
+ if ldr.SymType(mi) != tp.expKind {
+ t.Errorf("testing Loader.%s: expected kind %s got %s",
+ tp.which, tp.expKind, ldr.SymType(mi))
+ }
+ if bytes.Compare(ldr.Data(mi), tp.expData) != 0 {
+ t.Errorf("testing Loader.%s: expected data %v got %v",
+ tp.which, tp.expData, ldr.Data(mi))
+ }
+ if !ldr.AttrReachable(mi) {
+ t.Fatalf("testing Loader.%s: sym updated should be reachable", tp.which)
+ }
+ relocs := ldr.Relocs(mi)
+ rsl := relocs.ReadAll(nil)
+ if !sameRelocSlice(rsl, tp.expRel) {
+ t.Fatalf("testing Loader.%s: got relocslice %+v wanted %+v",
+ tp.which, rsl, tp.expRel)
+ }
+ pmi = mi
+ }
}
func TestOuterSub(t *testing.T) {
- ldr := NewLoader(0)
+ edummy := func(s *sym.Symbol, str string, off int) {}
+ ldr := NewLoader(0, edummy)
dummyOreader := oReader{version: -1}
or := &dummyOreader