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.
17 var typedefTests = map[string]string{
18 "t_ptr_volatile_int": "*volatile int",
19 "t_ptr_const_char": "*const char",
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}",
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}",
47 func elfData(t *testing.T, name string) *Data {
48 f, err := elf.Open(name)
60 func machoData(t *testing.T, name string) *Data {
61 f, err := macho.Open(name)
73 func peData(t *testing.T, name string) *Data {
74 f, err := pe.Open(name)
86 func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf"), "elf") }
88 func TestTypedefsMachO(t *testing.T) {
89 testTypedefs(t, machoData(t, "testdata/typedef.macho"), "macho")
92 func TestTypedefsELFDwarf4(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf4"), "elf") }
94 func testTypedefs(t *testing.T, d *Data, kind string) {
96 seen := make(map[string]bool)
100 t.Fatal("r.Next:", err)
105 if e.Tag == TagTypedef {
106 typ, err := d.Type(e.Offset)
108 t.Fatal("d.Type:", err)
110 t1 := typ.(*TypedefType)
112 if ts, ok := t1.Type.(*StructType); ok {
115 typstr = t1.Type.String()
118 if want, ok := typedefTests[t1.Name]; ok {
120 t.Errorf("multiple definitions for %s", t1.Name)
123 if typstr != want && (kind != "macho" || typstr != machoBug[want]) {
124 t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
128 if e.Tag != TagCompileUnit {
133 for k := range typedefTests {
135 t.Errorf("missing %s", k)
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
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")
149 offsets := []Offset{}
153 t.Fatal("r.Next:", err)
159 case TagBaseType, TagTypedef, TagPointerType, TagStructType:
160 offsets = append(offsets, e.Offset)
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)
169 t.Fatalf("d.Type(0x%x): %s", offset, err)
174 var unsupportedTypeTests = []string{
175 // varname:typename:string:size
176 "culprit::(unsupported type ReferenceType):8",
177 "pdm::(unsupported type PtrToMemberType):-1",
180 func TestUnsupportedTypes(t *testing.T) {
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")
187 seen := make(map[string]bool)
191 t.Fatal("r.Next:", err)
196 if e.Tag == TagVariable {
197 vname, _ := e.Val(AttrName).(string)
198 tAttr := e.Val(AttrType)
199 typOff, ok := tAttr.(Offset)
201 t.Errorf("variable at offset %v has no type", e.Offset)
204 typ, err := d.Type(typOff)
206 t.Errorf("err in type decode: %v\n", err)
209 unsup, isok := typ.(*UnsupportedType)
213 tag := vname + ":" + unsup.Name + ":" + unsup.String() +
214 ":" + strconv.FormatInt(unsup.Size(), 10)
219 for _, v := range unsupportedTypeTests {
221 t.Errorf("missing %s", v)
226 for k := range seen {
227 fmt.Printf("seen: %s\n", k)