github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/link/auto_test.go (about)

     1  // Copyright 2014 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  // Test for auto-generated symbols.
     6  
     7  // There is no test for $f64. and $f32. symbols, because those are
     8  // not possible to write in the assembler syntax. Instead of changing
     9  // the assembler to allow that, we plan to change the compilers
    10  // not to generate such symbols (plain dupok data is sufficient).
    11  
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  	"cmd/internal/goobj"
    17  	"testing"
    18  )
    19  
    20  // Each test case is an object file, generated from a corresponding .s file.
    21  // The image of the autotab symbol should be a sequence of pairs of
    22  // identical 8-byte sequences.
    23  var autoTests = []string{
    24  	"testdata/autosection.6",
    25  	"testdata/autoweak.6",
    26  }
    27  
    28  func TestAuto(t *testing.T) {
    29  	for _, obj := range autoTests {
    30  		p := Prog{GOOS: "darwin", GOARCH: "amd64", StartSym: "start"}
    31  		p.omitRuntime = true
    32  		p.Error = func(s string) { t.Error(s) }
    33  		var buf bytes.Buffer
    34  		p.link(&buf, obj)
    35  		if p.NumError > 0 {
    36  			continue // already reported
    37  		}
    38  
    39  		const name = "autotab"
    40  		sym := p.Syms[goobj.SymID{Name: name}]
    41  		if sym == nil {
    42  			t.Errorf("%s is missing %s symbol", obj, name)
    43  			return
    44  		}
    45  		if sym.Size == 0 {
    46  			return
    47  		}
    48  
    49  		seg := sym.Section.Segment
    50  		off := sym.Addr - seg.VirtAddr
    51  		data := seg.Data[off : off+Addr(sym.Size)]
    52  		if len(data)%16 != 0 {
    53  			t.Errorf("%s: %s.Size = %d, want multiple of 16", obj, name, len(data))
    54  			return
    55  		}
    56  	Data:
    57  		for i := 0; i < len(data); i += 16 {
    58  			have := p.byteorder.Uint64(data[i : i+8])
    59  			want := p.byteorder.Uint64(data[i+8 : i+16])
    60  			if have != want {
    61  				// Look for relocation so we can explain what went wrong.
    62  				for _, r := range sym.Reloc {
    63  					if r.Offset == i {
    64  						t.Errorf("%s: %s+%#x: %s: have %#x want %#x", obj, name, i, r.Sym, have, want)
    65  						continue Data
    66  					}
    67  				}
    68  				t.Errorf("%s: %s+%#x: have %#x want %#x", obj, name, i, have, want)
    69  			}
    70  		}
    71  	}
    72  }