1
2
3
4
5 package obj
6
7 import (
8 "bytes"
9 "internal/testenv"
10 "os"
11 "path/filepath"
12 "testing"
13 "unsafe"
14
15 "cmd/internal/goobj"
16 "cmd/internal/sys"
17 )
18
19 var dummyArch = LinkArch{Arch: sys.ArchAMD64}
20
21 func TestContentHash64(t *testing.T) {
22 s1 := &LSym{P: []byte("A")}
23 s2 := &LSym{P: []byte("A\x00\x00\x00")}
24 s1.Set(AttrContentAddressable, true)
25 s2.Set(AttrContentAddressable, true)
26 h1 := contentHash64(s1)
27 h2 := contentHash64(s2)
28 if h1 != h2 {
29 t.Errorf("contentHash64(s1)=%x, contentHash64(s2)=%x, expect equal", h1, h2)
30 }
31
32 ctxt := Linknew(&dummyArch)
33 s3 := ctxt.Int64Sym(int64('A'))
34 h3 := contentHash64(s3)
35 if h1 != h3 {
36 t.Errorf("contentHash64(s1)=%x, contentHash64(s3)=%x, expect equal", h1, h3)
37 }
38 }
39
40 func TestContentHash(t *testing.T) {
41 syms := []*LSym{
42 &LSym{P: []byte("TestSymbol")},
43 &LSym{P: []byte("TestSymbol")},
44 &LSym{P: []byte("TestSymbol2")},
45 &LSym{P: []byte("")},
46 &LSym{P: []byte("")},
47 &LSym{P: []byte("")},
48 &LSym{P: []byte("")},
49 }
50 for _, s := range syms {
51 s.Set(AttrContentAddressable, true)
52 s.PkgIdx = goobj.PkgIdxHashed
53 }
54
55 r := Addrel(syms[3])
56 r.Sym = syms[0]
57
58 r = Addrel(syms[4])
59 r.Sym = syms[0]
60
61 r = Addrel(syms[5])
62 r.Sym = syms[1]
63
64 r = Addrel(syms[6])
65 r.Sym = syms[2]
66
67
68 h := make([]goobj.HashType, len(syms))
69 w := &writer{}
70 for i := range h {
71 h[i] = w.contentHash(syms[i])
72 }
73
74 tests := []struct {
75 a, b int
76 equal bool
77 }{
78 {0, 1, true},
79 {0, 2, false},
80 {3, 4, true},
81 {3, 5, true},
82 {3, 6, false},
83 }
84 for _, test := range tests {
85 if (h[test.a] == h[test.b]) != test.equal {
86 eq := "equal"
87 if !test.equal {
88 eq = "not equal"
89 }
90 t.Errorf("h%d=%x, h%d=%x, expect %s", test.a, h[test.a], test.b, h[test.b], eq)
91 }
92 }
93 }
94
95 func TestSymbolTooLarge(t *testing.T) {
96 testenv.MustHaveGoBuild(t)
97 if unsafe.Sizeof(uintptr(0)) < 8 {
98 t.Skip("skip on 32-bit architectures")
99 }
100
101 tmpdir, err := os.MkdirTemp("", "TestSymbolTooLarge")
102 if err != nil {
103 t.Fatal(err)
104 }
105 defer os.RemoveAll(tmpdir)
106
107 src := filepath.Join(tmpdir, "p.go")
108 err = os.WriteFile(src, []byte("package p; var x [1<<32]byte"), 0666)
109 if err != nil {
110 t.Fatalf("failed to write source file: %v\n", err)
111 }
112 obj := filepath.Join(tmpdir, "p.o")
113 cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", obj, src)
114 out, err := cmd.CombinedOutput()
115 if err == nil {
116 t.Fatalf("did not fail\noutput: %s", out)
117 }
118 const want = "symbol too large"
119 if !bytes.Contains(out, []byte(want)) {
120 t.Errorf("unexpected error message: want: %q, got: %s", want, out)
121 }
122 }
123
124 func TestNoRefName(t *testing.T) {
125
126 testenv.MustHaveGoBuild(t)
127
128 tmpdir := t.TempDir()
129
130 src := filepath.Join(tmpdir, "x.go")
131 err := os.WriteFile(src, []byte("package main; import \"fmt\"; func main() { fmt.Println(123) }\n"), 0666)
132 if err != nil {
133 t.Fatalf("failed to write source file: %v\n", err)
134 }
135 exe := filepath.Join(tmpdir, "x.exe")
136
137
138
139 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags=fmt=-d=norefname", "-o", exe, src)
140 out, err := cmd.CombinedOutput()
141 if err != nil {
142 t.Fatalf("build failed: %v, output:\n%s", err, out)
143 }
144 }
145
View as plain text