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  	syms[3].R = []Reloc{{Sym: syms[0]}}
    56  	// s4 references s0
    57  	syms[4].R = []Reloc{{Sym: syms[0]}}
    58  	// s5 references s1
    59  	syms[5].R = []Reloc{{Sym: syms[1]}}
    60  	// s6 references s2
    61  	syms[6].R = []Reloc{{Sym: syms[2]}}
    62  
    63  	// compute hashes
    64  	h := make([]goobj.HashType, len(syms))
    65  	w := &writer{}
    66  	for i := range h {
    67  		h[i] = w.contentHash(syms[i])
    68  	}
    69  
    70  	tests := []struct {
    71  		a, b  int
    72  		equal bool
    73  	}{
    74  		{0, 1, true},  // same contents, no relocs
    75  		{0, 2, false}, // different contents
    76  		{3, 4, true},  // same contents, same relocs
    77  		{3, 5, true},  // recursively same contents
    78  		{3, 6, false}, // same contents, different relocs
    79  	}
    80  	for _, test := range tests {
    81  		if (h[test.a] == h[test.b]) != test.equal {
    82  			eq := "equal"
    83  			if !test.equal {
    84  				eq = "not equal"
    85  			}
    86  			t.Errorf("h%d=%x, h%d=%x, expect %s", test.a, h[test.a], test.b, h[test.b], eq)
    87  		}
    88  	}
    89  }
    90  
    91  func TestSymbolTooLarge(t *testing.T) { // Issue 42054
    92  	testenv.MustHaveGoBuild(t)
    93  	if unsafe.Sizeof(uintptr(0)) < 8 {
    94  		t.Skip("skip on 32-bit architectures")
    95  	}
    96  
    97  	tmpdir := t.TempDir()
    98  
    99  	src := filepath.Join(tmpdir, "p.go")
   100  	err := os.WriteFile(src, []byte("package p; var x [1<<32]byte"), 0666)
   101  	if err != nil {
   102  		t.Fatalf("failed to write source file: %v\n", err)
   103  	}
   104  	obj := filepath.Join(tmpdir, "p.o")
   105  	cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", obj, src)
   106  	out, err := cmd.CombinedOutput()
   107  	if err == nil {
   108  		t.Fatalf("did not fail\noutput: %s", out)
   109  	}
   110  	const want = "symbol too large"
   111  	if !bytes.Contains(out, []byte(want)) {
   112  		t.Errorf("unexpected error message: want: %q, got: %s", want, out)
   113  	}
   114  }
   115  
   116  func TestNoRefName(t *testing.T) {
   117  	// Test that the norefname flag works.
   118  	testenv.MustHaveGoBuild(t)
   119  
   120  	tmpdir := t.TempDir()
   121  
   122  	src := filepath.Join(tmpdir, "x.go")
   123  	err := os.WriteFile(src, []byte("package main; import \"fmt\"; func main() { fmt.Println(123) }\n"), 0666)
   124  	if err != nil {
   125  		t.Fatalf("failed to write source file: %v\n", err)
   126  	}
   127  	exe := filepath.Join(tmpdir, "x.exe")
   128  
   129  	// Build the fmt package with norefname. Not rebuilding all packages to save time.
   130  	// Also testing that norefname and non-norefname packages can link together.
   131  	cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags=fmt=-d=norefname", "-o", exe, src)
   132  	out, err := cmd.CombinedOutput()
   133  	if err != nil {
   134  		t.Fatalf("build failed: %v, output:\n%s", err, out)
   135  	}
   136  }
   137  

View as plain text