Source file src/cmd/internal/obj/objfile_test.go

     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 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) // little endian
    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")},  // 0
    43  		&LSym{P: []byte("TestSymbol")},  // 1
    44  		&LSym{P: []byte("TestSymbol2")}, // 2
    45  		&LSym{P: []byte("")},            // 3
    46  		&LSym{P: []byte("")},            // 4
    47  		&LSym{P: []byte("")},            // 5
    48  		&LSym{P: []byte("")},            // 6
    49  	}
    50  	for _, s := range syms {
    51  		s.Set(AttrContentAddressable, true)
    52  		s.PkgIdx = goobj.PkgIdxHashed
    53  	}
    54  	// s3 references s0
    55  	r := Addrel(syms[3])
    56  	r.Sym = syms[0]
    57  	// s4 references s0
    58  	r = Addrel(syms[4])
    59  	r.Sym = syms[0]
    60  	// s5 references s1
    61  	r = Addrel(syms[5])
    62  	r.Sym = syms[1]
    63  	// s6 references s2
    64  	r = Addrel(syms[6])
    65  	r.Sym = syms[2]
    66  
    67  	// compute hashes
    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},  // same contents, no relocs
    79  		{0, 2, false}, // different contents
    80  		{3, 4, true},  // same contents, same relocs
    81  		{3, 5, true},  // recursively same contents
    82  		{3, 6, false}, // same contents, different relocs
    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) { // Issue 42054
    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  	// Test that the norefname flag works.
   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  	// Build the fmt package with norefname. Not rebuilding all packages to save time.
   138  	// Also testing that norefname and non-norefname packages can link together.
   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