diff options
author | Russ Cox <rsc@golang.org> | 2009-09-18 11:50:24 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-09-18 11:50:24 -0700 |
commit | 6e899e0fefad63f7e271dc2dc1c543acf563a6d2 (patch) | |
tree | c76196eb63f43e02a0e04681890f94abab56151c /src/pkg/debug/dwarf | |
parent | b442bf31218e0d2e827b908c25f9eb01efc24c54 (diff) | |
download | golang-6e899e0fefad63f7e271dc2dc1c543acf563a6d2.tar.gz |
add DWARF method to elf.File.
test both ELF and Mach-O in dwarf package.
R=r
DELTA=83 (44 added, 10 deleted, 29 changed)
OCL=34717
CL=34790
Diffstat (limited to 'src/pkg/debug/dwarf')
-rw-r--r-- | src/pkg/debug/dwarf/testdata/typedef.c | 6 | ||||
-rw-r--r-- | src/pkg/debug/dwarf/type.go | 17 | ||||
-rw-r--r-- | src/pkg/debug/dwarf/type_test.go | 51 |
3 files changed, 41 insertions, 33 deletions
diff --git a/src/pkg/debug/dwarf/testdata/typedef.c b/src/pkg/debug/dwarf/testdata/typedef.c index 9a46d42e0..2ceb00ced 100644 --- a/src/pkg/debug/dwarf/testdata/typedef.c +++ b/src/pkg/debug/dwarf/testdata/typedef.c @@ -3,7 +3,11 @@ // license that can be found in the LICENSE file. /* -gcc -gdwarf-2 -c typedef.c && gcc -gdwarf-2 -o typedef.elf typedef.o +Linux ELF: +gcc -gdwarf-2 -m64 -c typedef.c && gcc -gdwarf-2 -m64 -o typedef.elf typedef.o + +OS X Mach-O: +gcc -gdwarf-2 -m64 -c typedef.c -o typedef.macho */ typedef volatile int* t_ptr_volatile_int; diff --git a/src/pkg/debug/dwarf/type.go b/src/pkg/debug/dwarf/type.go index 335ef314a..09672f392 100644 --- a/src/pkg/debug/dwarf/type.go +++ b/src/pkg/debug/dwarf/type.go @@ -103,7 +103,7 @@ type ArrayType struct { CommonType; Type Type; StrideBitSize int64; // if > 0, number of bits to hold each element - Count int64; + Count int64; // if == -1, an incomplete array, like char x[]. } func (t *ArrayType) String() string { @@ -111,8 +111,6 @@ func (t *ArrayType) String() string { } // A VoidType represents the C void type. -// It is only used as the subtype for a pointer: -// a FuncType that returns no value has a nil ReturnType. type VoidType struct { CommonType; } @@ -306,8 +304,8 @@ func (d *Data) Type(off Offset) (Type, os.Error) { typeOf := func(e *Entry) Type { toff, ok := e.Val(AttrType).(Offset); if !ok { - err = DecodeError{"info", e.Offset, "missing type attribute"}; - return nil; + // It appears that no Type means "void". + return new(VoidType); } var t Type; if t, err = d.Type(toff); err != nil { @@ -343,8 +341,7 @@ func (d *Data) Type(off Offset) (Type, os.Error) { case TagSubrangeType: max, ok := kid.Val(AttrUpperBound).(int64); if !ok { - err = DecodeError{"info", kid.Offset, "missing upper bound"}; - goto Error; + max = -2; // Count == -1, as in x[]. } if ndim == 0 { t.Count = max+1; @@ -548,10 +545,8 @@ func (d *Data) Type(off Offset) (Type, os.Error) { t := new(FuncType); typ = t; d.typeCache[off] = t; - if e.Val(AttrType) != nil { - if t.ReturnType = typeOf(e); err != nil { - goto Error; - } + if t.ReturnType = typeOf(e); err != nil { + goto Error; } t.ParamType = make([]Type, 0, 8); for kid := next(); kid != nil; kid = next() { diff --git a/src/pkg/debug/dwarf/type_test.go b/src/pkg/debug/dwarf/type_test.go index ea7f21976..0534518cc 100644 --- a/src/pkg/debug/dwarf/type_test.go +++ b/src/pkg/debug/dwarf/type_test.go @@ -2,10 +2,12 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package dwarf +package dwarf_test import ( + . "debug/dwarf"; "debug/elf"; + "debug/macho"; "testing"; ) @@ -32,30 +34,37 @@ func elfData(t *testing.T, name string) *Data { if err != nil { t.Fatal(err); } - - dat := func(name string) []byte { - s := f.Section(".debug_" + name); - if s == nil { - return nil - } - b, err := s.Data(); - if err != nil { - t.Fatal(".debug_"+name+":", err); - } - return b; - }; - - d, err := New(dat("abbrev"), nil, nil, dat("info"), nil, nil, nil, dat("str")); + + d, err := f.DWARF(); if err != nil { - t.Fatal("New:", err); + t.Fatal(err); } - return d; } +func machoData(t *testing.T, name string) *Data { + f, err := macho.Open(name); + if err != nil { + t.Fatal(err); + } -func TestTypedefs(t *testing.T) { - d := elfData(t, "testdata/typedef.elf"); + d, err := f.DWARF(); + if err != nil { + t.Fatal(err); + } + return d; +} + + +func TestTypedefsELF(t *testing.T) { + testTypedefs(t, elfData(t, "testdata/typedef.elf")); +} + +func TestTypedefsMachO(t *testing.T) { + testTypedefs(t, machoData(t, "testdata/typedef.macho")); +} + +func testTypedefs(t *testing.T, d *Data) { r := d.Reader(); seen := make(map[string]bool); for { @@ -78,7 +87,7 @@ func TestTypedefs(t *testing.T) { } else { typstr = t1.Type.String(); } - + if want, ok := typedefTests[t1.Name]; ok { if _, ok := seen[t1.Name]; ok { t.Errorf("multiple definitions for %s", t1.Name); @@ -93,7 +102,7 @@ func TestTypedefs(t *testing.T) { r.SkipChildren(); } } - + for k := range typedefTests { if _, ok := seen[k]; !ok { t.Errorf("missing %s", k); |