1
2
3
4
5 package ld
6
7 import (
8 "cmd/internal/objabi"
9 "cmd/internal/sys"
10 "cmd/link/internal/loader"
11 "internal/buildcfg"
12 "testing"
13 )
14
15 func setUpContext(arch *sys.Arch, iself bool, ht objabi.HeadType, bm, lm string) *Link {
16 ctxt := linknew(arch)
17 edummy := func(str string, off int) {}
18 ctxt.HeadType = ht
19 er := loader.ErrorReporter{}
20 ctxt.loader = loader.NewLoader(0, edummy, &er)
21 ctxt.BuildMode.Set(bm)
22 ctxt.LinkMode.Set(lm)
23 ctxt.IsELF = iself
24 ctxt.mustSetHeadType()
25 ctxt.setArchSyms()
26 return ctxt
27 }
28
29
30 func TestAddGotSym(t *testing.T) {
31 tests := []struct {
32 arch *sys.Arch
33 ht objabi.HeadType
34 bm, lm string
35 rel string
36 relsize int
37 gotsize int
38 }{
39 {
40 arch: sys.Arch386,
41 ht: objabi.Hlinux,
42 bm: "pie",
43 lm: "internal",
44 rel: ".rel",
45 relsize: 2 * sys.Arch386.PtrSize,
46 gotsize: sys.Arch386.PtrSize,
47 },
48 {
49 arch: sys.ArchAMD64,
50 ht: objabi.Hlinux,
51 bm: "pie",
52 lm: "internal",
53 rel: ".rela",
54 relsize: 3 * sys.ArchAMD64.PtrSize,
55 gotsize: sys.ArchAMD64.PtrSize,
56 },
57 {
58 arch: sys.ArchAMD64,
59 ht: objabi.Hdarwin,
60 bm: "pie",
61 lm: "external",
62 gotsize: sys.ArchAMD64.PtrSize,
63 },
64 }
65
66
67 origArch := buildcfg.GOARCH
68 defer func() {
69 buildcfg.GOARCH = origArch
70 }()
71
72 for i, test := range tests {
73 iself := len(test.rel) != 0
74 buildcfg.GOARCH = test.arch.Name
75 ctxt := setUpContext(test.arch, iself, test.ht, test.bm, test.lm)
76 foo := ctxt.loader.CreateSymForUpdate("foo", 0)
77 ctxt.loader.CreateExtSym("bar", 0)
78 AddGotSym(&ctxt.Target, ctxt.loader, &ctxt.ArchSyms, foo.Sym(), 0)
79
80 if iself {
81 rel := ctxt.loader.Lookup(test.rel, 0)
82 if rel == 0 {
83 t.Fatalf("[%d] could not find symbol: %q", i, test.rel)
84 }
85 if s := ctxt.loader.SymSize(rel); s != int64(test.relsize) {
86 t.Fatalf("[%d] expected ldr.Size(%q) == %v, got %v", i, test.rel, test.relsize, s)
87 }
88 }
89 if s := ctxt.loader.SymSize(ctxt.loader.Lookup(".got", 0)); s != int64(test.gotsize) {
90 t.Fatalf(`[%d] expected ldr.Size(".got") == %v, got %v`, i, test.gotsize, s)
91 }
92 }
93 }
94
View as plain text