...

Source file src/cmd/link/internal/ld/data_test.go

Documentation: cmd/link/internal/ld

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     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  	ctxt.HeadType = ht
    18  	er := loader.ErrorReporter{}
    19  	ctxt.loader = loader.NewLoader(0, &er)
    20  	ctxt.BuildMode.Set(bm)
    21  	ctxt.LinkMode.Set(lm)
    22  	ctxt.IsELF = iself
    23  	ctxt.mustSetHeadType()
    24  	ctxt.setArchSyms()
    25  	return ctxt
    26  }
    27  
    28  // Make sure the addgotsym properly increases the symbols.
    29  func TestAddGotSym(t *testing.T) {
    30  	tests := []struct {
    31  		arch    *sys.Arch
    32  		ht      objabi.HeadType
    33  		bm, lm  string
    34  		rel     string
    35  		relsize int
    36  		gotsize int
    37  	}{
    38  		{
    39  			arch:    sys.Arch386,
    40  			ht:      objabi.Hlinux,
    41  			bm:      "pie",
    42  			lm:      "internal",
    43  			rel:     ".rel",
    44  			relsize: 2 * sys.Arch386.PtrSize,
    45  			gotsize: sys.Arch386.PtrSize,
    46  		},
    47  		{
    48  			arch:    sys.ArchAMD64,
    49  			ht:      objabi.Hlinux,
    50  			bm:      "pie",
    51  			lm:      "internal",
    52  			rel:     ".rela",
    53  			relsize: 3 * sys.ArchAMD64.PtrSize,
    54  			gotsize: sys.ArchAMD64.PtrSize,
    55  		},
    56  		{
    57  			arch:    sys.ArchAMD64,
    58  			ht:      objabi.Hdarwin,
    59  			bm:      "pie",
    60  			lm:      "external",
    61  			gotsize: sys.ArchAMD64.PtrSize,
    62  		},
    63  	}
    64  
    65  	// Save the architecture as we're going to set it on each test run.
    66  	origArch := buildcfg.GOARCH
    67  	defer func() {
    68  		buildcfg.GOARCH = origArch
    69  	}()
    70  
    71  	for i, test := range tests {
    72  		iself := len(test.rel) != 0
    73  		buildcfg.GOARCH = test.arch.Name
    74  		ctxt := setUpContext(test.arch, iself, test.ht, test.bm, test.lm)
    75  		foo := ctxt.loader.CreateSymForUpdate("foo", 0)
    76  		ctxt.loader.CreateExtSym("bar", 0)
    77  		AddGotSym(&ctxt.Target, ctxt.loader, &ctxt.ArchSyms, foo.Sym(), 0)
    78  
    79  		if iself {
    80  			rel := ctxt.loader.Lookup(test.rel, 0)
    81  			if rel == 0 {
    82  				t.Fatalf("[%d] could not find symbol: %q", i, test.rel)
    83  			}
    84  			if s := ctxt.loader.SymSize(rel); s != int64(test.relsize) {
    85  				t.Fatalf("[%d] expected ldr.Size(%q) == %v, got %v", i, test.rel, test.relsize, s)
    86  			}
    87  		}
    88  		if s := ctxt.loader.SymSize(ctxt.loader.Lookup(".got", 0)); s != int64(test.gotsize) {
    89  			t.Fatalf(`[%d] expected ldr.Size(".got") == %v, got %v`, i, test.gotsize, s)
    90  		}
    91  	}
    92  }
    93  

View as plain text