github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/debug/dwarf/type_test.go (about)

     1  // Copyright 2009 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 dwarf_test
     6  
     7  import (
     8  	. "debug/dwarf"
     9  	"debug/elf"
    10  	"debug/macho"
    11  	"testing"
    12  )
    13  
    14  var typedefTests = map[string]string{
    15  	"t_ptr_volatile_int":                    "*volatile int",
    16  	"t_ptr_const_char":                      "*const char",
    17  	"t_long":                                "long int",
    18  	"t_ushort":                              "short unsigned int",
    19  	"t_func_int_of_float_double":            "func(float, double) int",
    20  	"t_ptr_func_int_of_float_double":        "*func(float, double) int",
    21  	"t_ptr_func_int_of_float_complex":       "*func(complex float) int",
    22  	"t_ptr_func_int_of_double_complex":      "*func(complex double) int",
    23  	"t_ptr_func_int_of_long_double_complex": "*func(complex long double) int",
    24  	"t_func_ptr_int_of_char_schar_uchar":    "func(char, signed char, unsigned char) *int",
    25  	"t_func_void_of_char":                   "func(char) void",
    26  	"t_func_void_of_void":                   "func() void",
    27  	"t_func_void_of_ptr_char_dots":          "func(*char, ...) void",
    28  	"t_my_struct":                           "struct my_struct {vi volatile int@0; x char@4 : 1@7; y int@4 : 4@27; z [0]int@8; array [40]long long int@8; zz [0]int@328}",
    29  	"t_my_struct1":                          "struct my_struct1 {zz [1]int@0}",
    30  	"t_my_union":                            "union my_union {vi volatile int@0; x char@0 : 1@7; y int@0 : 4@28; array [40]long long int@0}",
    31  	"t_my_enum":                             "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
    32  	"t_my_list":                             "struct list {val short int@0; next *t_my_list@8}",
    33  	"t_my_tree":                             "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
    34  }
    35  
    36  // As Apple converts gcc to a clang-based front end
    37  // they keep breaking the DWARF output. This map lists the
    38  // conversion from real answer to Apple answer.
    39  var machoBug = map[string]string{
    40  	"func(*char, ...) void":                                 "func(*char) void",
    41  	"enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}": "enum my_enum {e1=1; e2=2; e3=-5; e4=-1530494976}",
    42  }
    43  
    44  func elfData(t *testing.T, name string) *Data {
    45  	f, err := elf.Open(name)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	d, err := f.DWARF()
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	return d
    55  }
    56  
    57  func machoData(t *testing.T, name string) *Data {
    58  	f, err := macho.Open(name)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	d, err := f.DWARF()
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	return d
    68  }
    69  
    70  func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
    71  
    72  func TestTypedefsMachO(t *testing.T) {
    73  	testTypedefs(t, machoData(t, "testdata/typedef.macho"), "macho")
    74  }
    75  
    76  func TestTypedefsELFDwarf4(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf4"), "elf") }
    77  
    78  func testTypedefs(t *testing.T, d *Data, kind string) {
    79  	r := d.Reader()
    80  	seen := make(map[string]bool)
    81  	for {
    82  		e, err := r.Next()
    83  		if err != nil {
    84  			t.Fatal("r.Next:", err)
    85  		}
    86  		if e == nil {
    87  			break
    88  		}
    89  		if e.Tag == TagTypedef {
    90  			typ, err := d.Type(e.Offset)
    91  			if err != nil {
    92  				t.Fatal("d.Type:", err)
    93  			}
    94  			t1 := typ.(*TypedefType)
    95  			var typstr string
    96  			if ts, ok := t1.Type.(*StructType); ok {
    97  				typstr = ts.Defn()
    98  			} else {
    99  				typstr = t1.Type.String()
   100  			}
   101  
   102  			if want, ok := typedefTests[t1.Name]; ok {
   103  				if seen[t1.Name] {
   104  					t.Errorf("multiple definitions for %s", t1.Name)
   105  				}
   106  				seen[t1.Name] = true
   107  				if typstr != want && (kind != "macho" || typstr != machoBug[want]) {
   108  					t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
   109  				}
   110  			}
   111  		}
   112  		if e.Tag != TagCompileUnit {
   113  			r.SkipChildren()
   114  		}
   115  	}
   116  
   117  	for k := range typedefTests {
   118  		if !seen[k] {
   119  			t.Errorf("missing %s", k)
   120  		}
   121  	}
   122  }
   123  
   124  func TestTypedefCycle(t *testing.T) {
   125  	// See issue #13039: reading a typedef cycle starting from a
   126  	// different place than the size needed to be computed from
   127  	// used to crash.
   128  	//
   129  	// cycle.elf built with GCC 4.8.4:
   130  	//    gcc -g -c -o cycle.elf cycle.c
   131  	d := elfData(t, "testdata/cycle.elf")
   132  	r := d.Reader()
   133  	offsets := []Offset{}
   134  	for {
   135  		e, err := r.Next()
   136  		if err != nil {
   137  			t.Fatal("r.Next:", err)
   138  		}
   139  		if e == nil {
   140  			break
   141  		}
   142  		switch e.Tag {
   143  		case TagBaseType, TagTypedef, TagPointerType, TagStructType:
   144  			offsets = append(offsets, e.Offset)
   145  		}
   146  	}
   147  
   148  	// Parse each type with a fresh type cache.
   149  	for _, offset := range offsets {
   150  		d := elfData(t, "testdata/cycle.elf")
   151  		_, err := d.Type(offset)
   152  		if err != nil {
   153  			t.Fatalf("d.Type(0x%x): %s", offset, err)
   154  		}
   155  	}
   156  }