1
2
3
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; array [40]long long int@8}",
29 "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}",
30 "t_my_enum": "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
31 "t_my_list": "struct list {val short int@0; next *t_my_list@8}",
32 "t_my_tree": "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
33 }
34
35 func elfData(t *testing.T, name string) *Data {
36 f, err := elf.Open(name)
37 if err != nil {
38 t.Fatal(err)
39 }
40
41 d, err := f.DWARF()
42 if err != nil {
43 t.Fatal(err)
44 }
45 return d
46 }
47
48 func machoData(t *testing.T, name string) *Data {
49 f, err := macho.Open(name)
50 if err != nil {
51 t.Fatal(err)
52 }
53
54 d, err := f.DWARF()
55 if err != nil {
56 t.Fatal(err)
57 }
58 return d
59 }
60
61 func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf")) }
62
63 func TestTypedefsMachO(t *testing.T) {
64 testTypedefs(t, machoData(t, "testdata/typedef.macho"))
65 }
66
67 func testTypedefs(t *testing.T, d *Data) {
68 r := d.Reader()
69 seen := make(map[string]bool)
70 for {
71 e, err := r.Next()
72 if err != nil {
73 t.Fatal("r.Next:", err)
74 }
75 if e == nil {
76 break
77 }
78 if e.Tag == TagTypedef {
79 typ, err := d.Type(e.Offset)
80 if err != nil {
81 t.Fatal("d.Type:", err)
82 }
83 t1 := typ.(*TypedefType)
84 var typstr string
85 if ts, ok := t1.Type.(*StructType); ok {
86 typstr = ts.Defn()
87 } else {
88 typstr = t1.Type.String()
89 }
90
91 if want, ok := typedefTests[t1.Name]; ok {
92 if seen[t1.Name] {
93 t.Errorf("multiple definitions for %s", t1.Name)
94 }
95 seen[t1.Name] = true
96 if typstr != want {
97 t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
98 }
99 }
100 }
101 if e.Tag != TagCompileUnit {
102 r.SkipChildren()
103 }
104 }
105
106 for k := range typedefTests {
107 if !seen[k] {
108 t.Errorf("missing %s", k)
109 }
110 }
111 }