Skip to content

Commit 065f380

Browse files
jtsylvethanm
authored andcommitted
debug/dwarf: check for DWARFv4 AttrDataBitOffset value
AttrBitOffset is deprecated (but reserved) in DWARFv4. This fix adds logic to check the new AttrDataBitOffset attribute if AttrBitOffset attribute is not present. Fixes #46784 Change-Id: I7406dcaa4c98e95df72361fd4462c39e6be8879d GitHub-Last-Rev: 5aa10d0 GitHub-Pull-Request: #46790 Reviewed-on: https://go-review.googlesource.com/c/go/+/328709 Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Michael Knyszek <[email protected]>
1 parent a8aa6cf commit 065f380

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

src/debug/dwarf/testdata/typedef.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ gcc -gdwarf-2 -m64 -c typedef.c && gcc -gdwarf-2 -m64 -o typedef.elf typedef.o
88
99
OS X Mach-O:
1010
gcc -gdwarf-2 -m64 -c typedef.c -o typedef.macho
11+
gcc -gdwarf-4 -m64 -c typedef.c -o typedef.macho4
1112
*/
1213
#include <complex.h>
1314

6.07 KB
Binary file not shown.

src/debug/dwarf/type.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Off
516516
}).Basic()
517517
t.Name = name
518518
t.BitSize, _ = e.Val(AttrBitSize).(int64)
519-
t.BitOffset, _ = e.Val(AttrBitOffset).(int64)
519+
haveBitOffset := false
520+
if t.BitOffset, haveBitOffset = e.Val(AttrBitOffset).(int64); !haveBitOffset {
521+
t.BitOffset, _ = e.Val(AttrDataBitOffset).(int64)
522+
}
520523

521524
case TagClassType, TagStructType, TagUnionType:
522525
// Structure, union, or class type. (DWARF v2 §5.5)
@@ -578,7 +581,9 @@ func (d *Data) readType(name string, r typeReader, off Offset, typeCache map[Off
578581
haveBitOffset := false
579582
f.Name, _ = kid.Val(AttrName).(string)
580583
f.ByteSize, _ = kid.Val(AttrByteSize).(int64)
581-
f.BitOffset, haveBitOffset = kid.Val(AttrBitOffset).(int64)
584+
if f.BitOffset, haveBitOffset = kid.Val(AttrBitOffset).(int64); !haveBitOffset {
585+
f.BitOffset, haveBitOffset = kid.Val(AttrDataBitOffset).(int64)
586+
}
582587
f.BitSize, _ = kid.Val(AttrBitSize).(int64)
583588
t.Field = append(t.Field, f)
584589

src/debug/dwarf/type_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,54 @@ func TestUnsupportedTypes(t *testing.T) {
228228
}
229229
}
230230
}
231+
232+
func TestBitOffsetsELF(t *testing.T) { testBitOffsets(t, elfData(t, "testdata/typedef.elf")) }
233+
234+
func TestBitOffsetsMachO(t *testing.T) {
235+
testBitOffsets(t, machoData(t, "testdata/typedef.macho"))
236+
}
237+
238+
func TestBitOffsetsMachO4(t *testing.T) {
239+
testBitOffsets(t, machoData(t, "testdata/typedef.macho4"))
240+
}
241+
242+
func TestBitOffsetsELFDwarf4(t *testing.T) {
243+
testBitOffsets(t, elfData(t, "testdata/typedef.elf4"))
244+
}
245+
246+
func testBitOffsets(t *testing.T, d *Data) {
247+
r := d.Reader()
248+
for {
249+
e, err := r.Next()
250+
if err != nil {
251+
t.Fatal("r.Next:", err)
252+
}
253+
if e == nil {
254+
break
255+
}
256+
257+
if e.Tag == TagStructType {
258+
typ, err := d.Type(e.Offset)
259+
if err != nil {
260+
t.Fatal("d.Type:", err)
261+
}
262+
263+
t1 := typ.(*StructType)
264+
265+
for _, field := range t1.Field {
266+
// We're only testing for bitfields
267+
if field.BitSize == 0 {
268+
continue
269+
}
270+
271+
// Ensure BitOffset is not zero
272+
if field.BitOffset == 0 {
273+
t.Errorf("bit offset of field %s in %s %s is not set", field.Name, t1.Kind, t1.StructName)
274+
}
275+
}
276+
}
277+
if e.Tag != TagCompileUnit {
278+
r.SkipChildren()
279+
}
280+
}
281+
}

0 commit comments

Comments
 (0)