1
2
3
4
5 package goobj
6
7 import (
8 "bufio"
9 "bytes"
10 "fmt"
11 "internal/buildcfg"
12 "internal/testenv"
13 "os"
14 "testing"
15
16 "cmd/internal/bio"
17 "cmd/internal/objabi"
18 )
19
20 func dummyWriter(buf *bytes.Buffer) *Writer {
21 wr := &bio.Writer{Writer: bufio.NewWriter(buf)}
22 return NewWriter(wr)
23 }
24
25 func TestReadWrite(t *testing.T) {
26
27
28
29 var buf bytes.Buffer
30 w := dummyWriter(&buf)
31
32 var s Sym
33 s.SetABI(1)
34 s.SetType(uint8(objabi.STEXT))
35 s.SetFlag(0x12)
36 s.SetSiz(12345)
37 s.SetAlign(8)
38 s.Write(w)
39
40 var r Reloc
41 r.SetOff(12)
42 r.SetSiz(4)
43 r.SetType(uint16(objabi.R_ADDR))
44 r.SetAdd(54321)
45 r.SetSym(SymRef{11, 22})
46 r.Write(w)
47
48 var a Aux
49 a.SetType(AuxFuncInfo)
50 a.SetSym(SymRef{33, 44})
51 a.Write(w)
52
53 w.wr.Flush()
54
55
56 b := buf.Bytes()
57 var s2 Sym
58 s2.fromBytes(b)
59 if s2.ABI() != 1 || s2.Type() != uint8(objabi.STEXT) || s2.Flag() != 0x12 || s2.Siz() != 12345 || s2.Align() != 8 {
60 t.Errorf("read Sym2 mismatch: got %v %v %v %v %v", s2.ABI(), s2.Type(), s2.Flag(), s2.Siz(), s2.Align())
61 }
62
63 b = b[SymSize:]
64 var r2 Reloc
65 r2.fromBytes(b)
66 if r2.Off() != 12 || r2.Siz() != 4 || r2.Type() != uint16(objabi.R_ADDR) || r2.Add() != 54321 || r2.Sym() != (SymRef{11, 22}) {
67 t.Errorf("read Reloc2 mismatch: got %v %v %v %v %v", r2.Off(), r2.Siz(), r2.Type(), r2.Add(), r2.Sym())
68 }
69
70 b = b[RelocSize:]
71 var a2 Aux
72 a2.fromBytes(b)
73 if a2.Type() != AuxFuncInfo || a2.Sym() != (SymRef{33, 44}) {
74 t.Errorf("read Aux2 mismatch: got %v %v", a2.Type(), a2.Sym())
75 }
76 }
77
78 var issue41621prolog = `
79 package main
80 var lines = []string{
81 `
82
83 var issue41621epilog = `
84 }
85 func getLines() []string {
86 return lines
87 }
88 func main() {
89 println(getLines())
90 }
91 `
92
93 func TestIssue41621LargeNumberOfRelocations(t *testing.T) {
94 if testing.Short() || (buildcfg.GOARCH != "amd64") {
95 t.Skipf("Skipping large number of relocations test in short mode or on %s", buildcfg.GOARCH)
96 }
97 testenv.MustHaveGoBuild(t)
98
99 tmpdir := t.TempDir()
100
101
102 var w bytes.Buffer
103 w.WriteString(issue41621prolog)
104 for i := 0; i < 1048576+13; i++ {
105 fmt.Fprintf(&w, "\t\"%d\",\n", i)
106 }
107 w.WriteString(issue41621epilog)
108 err := os.WriteFile(tmpdir+"/large.go", w.Bytes(), 0666)
109 if err != nil {
110 t.Fatalf("can't write output: %v\n", err)
111 }
112
113
114 w.Reset()
115 fmt.Fprintf(&w, "module issue41621\n\ngo 1.12\n")
116 err = os.WriteFile(tmpdir+"/go.mod", w.Bytes(), 0666)
117 if err != nil {
118 t.Fatalf("can't write output: %v\n", err)
119 }
120 w.Reset()
121
122
123 cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", "large")
124 cmd.Dir = tmpdir
125 out, err := cmd.CombinedOutput()
126 if err != nil {
127 t.Fatalf("Build failed: %v, output: %s", err, out)
128 }
129 }
130
View as plain text