]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/debug/dwarf/type_test.go
libgo: update to Go1.14beta1
[thirdparty/gcc.git] / libgo / go / debug / dwarf / type_test.go
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 "debug/pe"
12 "fmt"
13 "strconv"
14 "testing"
15 )
16
17 var typedefTests = map[string]string{
18 "t_ptr_volatile_int": "*volatile int",
19 "t_ptr_const_char": "*const char",
20 "t_long": "long int",
21 "t_ushort": "short unsigned int",
22 "t_func_int_of_float_double": "func(float, double) int",
23 "t_ptr_func_int_of_float_double": "*func(float, double) int",
24 "t_ptr_func_int_of_float_complex": "*func(complex float) int",
25 "t_ptr_func_int_of_double_complex": "*func(complex double) int",
26 "t_ptr_func_int_of_long_double_complex": "*func(complex long double) int",
27 "t_func_ptr_int_of_char_schar_uchar": "func(char, signed char, unsigned char) *int",
28 "t_func_void_of_char": "func(char) void",
29 "t_func_void_of_void": "func() void",
30 "t_func_void_of_ptr_char_dots": "func(*char, ...) void",
31 "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}",
32 "t_my_struct1": "struct my_struct1 {zz [1]int@0}",
33 "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}",
34 "t_my_enum": "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
35 "t_my_list": "struct list {val short int@0; next *t_my_list@8}",
36 "t_my_tree": "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
37 }
38
39 // As Apple converts gcc to a clang-based front end
40 // they keep breaking the DWARF output. This map lists the
41 // conversion from real answer to Apple answer.
42 var machoBug = map[string]string{
43 "func(*char, ...) void": "func(*char) void",
44 "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}": "enum my_enum {e1=1; e2=2; e3=-5; e4=-1530494976}",
45 }
46
47 func elfData(t *testing.T, name string) *Data {
48 f, err := elf.Open(name)
49 if err != nil {
50 t.Fatal(err)
51 }
52
53 d, err := f.DWARF()
54 if err != nil {
55 t.Fatal(err)
56 }
57 return d
58 }
59
60 func machoData(t *testing.T, name string) *Data {
61 f, err := macho.Open(name)
62 if err != nil {
63 t.Fatal(err)
64 }
65
66 d, err := f.DWARF()
67 if err != nil {
68 t.Fatal(err)
69 }
70 return d
71 }
72
73 func peData(t *testing.T, name string) *Data {
74 f, err := pe.Open(name)
75 if err != nil {
76 t.Fatal(err)
77 }
78
79 d, err := f.DWARF()
80 if err != nil {
81 t.Fatal(err)
82 }
83 return d
84 }
85
86 func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
87
88 func TestTypedefsMachO(t *testing.T) {
89 testTypedefs(t, machoData(t, "testdata/typedef.macho"), "macho")
90 }
91
92 func TestTypedefsELFDwarf4(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf4"), "elf") }
93
94 func testTypedefs(t *testing.T, d *Data, kind string) {
95 r := d.Reader()
96 seen := make(map[string]bool)
97 for {
98 e, err := r.Next()
99 if err != nil {
100 t.Fatal("r.Next:", err)
101 }
102 if e == nil {
103 break
104 }
105 if e.Tag == TagTypedef {
106 typ, err := d.Type(e.Offset)
107 if err != nil {
108 t.Fatal("d.Type:", err)
109 }
110 t1 := typ.(*TypedefType)
111 var typstr string
112 if ts, ok := t1.Type.(*StructType); ok {
113 typstr = ts.Defn()
114 } else {
115 typstr = t1.Type.String()
116 }
117
118 if want, ok := typedefTests[t1.Name]; ok {
119 if seen[t1.Name] {
120 t.Errorf("multiple definitions for %s", t1.Name)
121 }
122 seen[t1.Name] = true
123 if typstr != want && (kind != "macho" || typstr != machoBug[want]) {
124 t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
125 }
126 }
127 }
128 if e.Tag != TagCompileUnit {
129 r.SkipChildren()
130 }
131 }
132
133 for k := range typedefTests {
134 if !seen[k] {
135 t.Errorf("missing %s", k)
136 }
137 }
138 }
139
140 func TestTypedefCycle(t *testing.T) {
141 // See issue #13039: reading a typedef cycle starting from a
142 // different place than the size needed to be computed from
143 // used to crash.
144 //
145 // cycle.elf built with GCC 4.8.4:
146 // gcc -g -c -o cycle.elf cycle.c
147 d := elfData(t, "testdata/cycle.elf")
148 r := d.Reader()
149 offsets := []Offset{}
150 for {
151 e, err := r.Next()
152 if err != nil {
153 t.Fatal("r.Next:", err)
154 }
155 if e == nil {
156 break
157 }
158 switch e.Tag {
159 case TagBaseType, TagTypedef, TagPointerType, TagStructType:
160 offsets = append(offsets, e.Offset)
161 }
162 }
163
164 // Parse each type with a fresh type cache.
165 for _, offset := range offsets {
166 d := elfData(t, "testdata/cycle.elf")
167 _, err := d.Type(offset)
168 if err != nil {
169 t.Fatalf("d.Type(0x%x): %s", offset, err)
170 }
171 }
172 }
173
174 var unsupportedTypeTests = []string{
175 // varname:typename:string:size
176 "culprit::(unsupported type ReferenceType):8",
177 "pdm::(unsupported type PtrToMemberType):-1",
178 }
179
180 func TestUnsupportedTypes(t *testing.T) {
181 // Issue 29601:
182 // When reading DWARF from C++ load modules, we can encounter
183 // oddball type DIEs. These will be returned as "UnsupportedType"
184 // objects; check to make sure this works properly.
185 d := elfData(t, "testdata/cppunsuptypes.elf")
186 r := d.Reader()
187 seen := make(map[string]bool)
188 for {
189 e, err := r.Next()
190 if err != nil {
191 t.Fatal("r.Next:", err)
192 }
193 if e == nil {
194 break
195 }
196 if e.Tag == TagVariable {
197 vname, _ := e.Val(AttrName).(string)
198 tAttr := e.Val(AttrType)
199 typOff, ok := tAttr.(Offset)
200 if !ok {
201 t.Errorf("variable at offset %v has no type", e.Offset)
202 continue
203 }
204 typ, err := d.Type(typOff)
205 if err != nil {
206 t.Errorf("err in type decode: %v\n", err)
207 continue
208 }
209 unsup, isok := typ.(*UnsupportedType)
210 if !isok {
211 continue
212 }
213 tag := vname + ":" + unsup.Name + ":" + unsup.String() +
214 ":" + strconv.FormatInt(unsup.Size(), 10)
215 seen[tag] = true
216 }
217 }
218 dumpseen := false
219 for _, v := range unsupportedTypeTests {
220 if !seen[v] {
221 t.Errorf("missing %s", v)
222 dumpseen = true
223 }
224 }
225 if dumpseen {
226 for k := range seen {
227 fmt.Printf("seen: %s\n", k)
228 }
229 }
230 }