Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 9ed561f

Browse files
committed
Debug info: Support DWARF4 bitfields via DW_AT_data_bit_offset.
The DWARF2 specification of DW_AT_bit_offset was written from the perspective of a big-endian machine with unclear semantics for other systems. DWARF4 deprecated DW_AT_bit_offset and introduced a new attribute DW_AT_data_bit_offset that simply counts the number of bits from the beginning of the containing entity regardless of endianness. After this patch LLVM emits DW_AT_bit_offset for DWARF 2 or 3 and DW_AT_data_bit_offset when DWARF 4 or later is requested. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267895 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a7480ba commit 9ed561f

File tree

3 files changed

+154
-28
lines changed

3 files changed

+154
-28
lines changed

lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,41 +1395,43 @@ void DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
13951395
// Handle bitfield, assume bytes are 8 bits.
13961396
addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
13971397
addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1398-
//
1399-
// The DWARF 2 DW_AT_bit_offset is counting the bits between the most
1400-
// significant bit of the aligned storage unit containing the bit field to
1401-
// the most significan bit of the bit field.
1402-
//
1403-
// FIXME: DWARF 4 states that DW_AT_data_bit_offset (which
1404-
// counts from the beginning, regardless of endianness) should
1405-
// be used instead.
1406-
//
1407-
//
1408-
// Struct Align Align Align
1409-
// v v v v
1410-
// +-----------+-----*-----+-----*-----+--
1411-
// | ... |b1|b2|b3|b4|
1412-
// +-----------+-----*-----+-----*-----+--
1413-
// | | |<-- Size ->| |
1414-
// |<---- Offset --->| |<--->|
1415-
// | | | \_ DW_AT_bit_offset (little endian)
1416-
// | |<--->|
1417-
// |<--------->| \_ StartBitOffset = DW_AT_bit_offset (big endian)
1418-
// \ = DW_AT_data_bit_offset (biendian)
1419-
// \_ OffsetInBytes
1398+
14201399
uint64_t Offset = DT->getOffsetInBits();
14211400
uint64_t Align = DT->getAlignInBits() ? DT->getAlignInBits() : FieldSize;
14221401
uint64_t AlignMask = ~(Align - 1);
14231402
// The bits from the start of the storage unit to the start of the field.
14241403
uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1425-
// The endian-dependent DWARF 2 offset.
1426-
uint64_t DwarfBitOffset = Asm->getDataLayout().isLittleEndian()
1427-
? OffsetToAlignment(Offset + Size, Align)
1428-
: StartBitOffset;
1429-
14301404
// The byte offset of the field's aligned storage unit inside the struct.
14311405
OffsetInBytes = (Offset - StartBitOffset) / 8;
1432-
addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, DwarfBitOffset);
1406+
1407+
if (DD->getDwarfVersion() >= 4)
1408+
addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1409+
else {
1410+
//
1411+
// The DWARF 2 DW_AT_bit_offset is counting the bits between the most
1412+
// significant bit of the aligned storage unit containing the bit field
1413+
// to
1414+
// the most significan bit of the bit field.
1415+
//
1416+
// Struct Align Align Align
1417+
// v v v v
1418+
// +-----------+-----*-----+-----*-----+--
1419+
// | ... |b1|b2|b3|b4|
1420+
// +-----------+-----*-----+-----*-----+--
1421+
// | | |<-- Size ->| |
1422+
// |<---- Offset --->| |<--->|
1423+
// | | | \_ DW_AT_bit_offset (little endian)
1424+
// | |<--->|
1425+
// |<--------->| \_ StartBitOffset = DW_AT_bit_offset (big endian)
1426+
// \ = DW_AT_data_bit_offset (biendian)
1427+
// \_ OffsetInBytes
1428+
// The endian-dependent DWARF 2 offset.
1429+
uint64_t DwarfBitOffset = Asm->getDataLayout().isLittleEndian()
1430+
? OffsetToAlignment(Offset + Size, Align)
1431+
: StartBitOffset;
1432+
1433+
addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, DwarfBitOffset);
1434+
}
14331435
} else
14341436
// This is not a bitfield.
14351437
OffsetInBytes = DT->getOffsetInBits() / 8;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; RUN: llc -O0 -filetype=obj -mtriple=armeb-none-linux %s -o - \
2+
; RUN: | llvm-dwarfdump --debug-dump=info - | FileCheck %s
3+
; Generated from:
4+
; struct S {
5+
; int j:5;
6+
; int k:6;
7+
; int m:5;
8+
; int n:8;
9+
; } s;
10+
11+
target datalayout = "E-m:e-p:32:32-i64:64-v128:64:128-n32-S64"
12+
13+
%struct.S = type { i24 }
14+
15+
@s = common global %struct.S zeroinitializer, align 4
16+
17+
!llvm.dbg.cu = !{!0}
18+
!llvm.module.flags = !{!12, !13, !14}
19+
!llvm.ident = !{!15}
20+
21+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 (trunk 267633)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
22+
!1 = !DIFile(filename: "bitfield.c", directory: "/Volumes/Data/llvm")
23+
!2 = !{}
24+
!3 = !{!4}
25+
!4 = distinct !DIGlobalVariable(name: "s", scope: !0, file: !1, line: 6, type: !5, isLocal: false, isDefinition: true, variable: %struct.S* @s)
26+
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !1, line: 1, size: 32, align: 32, elements: !6)
27+
!6 = !{!7, !9, !10, !11}
28+
; CHECK: DW_TAG_member
29+
; CHECK-NEXT: DW_AT_name{{.*}}"j"
30+
; CHECK-NOT: DW_TAG
31+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x00)
32+
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)
33+
!7 = !DIDerivedType(tag: DW_TAG_member, name: "j", scope: !5, file: !1, line: 2, baseType: !8, size: 5, align: 32)
34+
!8 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
35+
; CHECK: DW_TAG_member
36+
; CHECK-NEXT: DW_AT_name{{.*}}"k"
37+
; CHECK-NOT: DW_TAG
38+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x05)
39+
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)
40+
!9 = !DIDerivedType(tag: DW_TAG_member, name: "k", scope: !5, file: !1, line: 3, baseType: !8, size: 6, align: 32, offset: 5)
41+
; CHECK: DW_TAG_member
42+
; CHECK-NEXT: DW_AT_name{{.*}}"m"
43+
; CHECK-NOT: DW_TAG
44+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x0b)
45+
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)
46+
!10 = !DIDerivedType(tag: DW_TAG_member, name: "m", scope: !5, file: !1, line: 4, baseType: !8, size: 5, align: 32, offset: 11)
47+
; CHECK: DW_TAG_member
48+
; CHECK-NEXT: DW_AT_name{{.*}}"n"
49+
; CHECK-NOT: DW_TAG
50+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x10)
51+
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)
52+
!11 = !DIDerivedType(tag: DW_TAG_member, name: "n", scope: !5, file: !1, line: 5, baseType: !8, size: 8, align: 32, offset: 16)
53+
!12 = !{i32 2, !"Dwarf Version", i32 4}
54+
!13 = !{i32 2, !"Debug Info Version", i32 3}
55+
!14 = !{i32 1, !"PIC Level", i32 2}
56+
!15 = !{!"clang version 3.9.0 (trunk 267633)"}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; RUN: llc -mtriple x86_64-apple-macosx -O0 -filetype=obj -o - %s \
2+
; RUN: | llvm-dwarfdump -debug-dump=info - | FileCheck %s
3+
;
4+
; Generated from:
5+
; #include <stdint.h>
6+
; #pragma pack(1)
7+
; struct PackedBits
8+
; {
9+
; char a;
10+
; uint32_t b : 5,
11+
; c : 27
12+
; } s;
13+
; #pragma pack()
14+
15+
source_filename = "bitfield.c"
16+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
17+
target triple = "x86_64-apple-macosx"
18+
19+
%struct.PackedBits = type <{ i8, i32 }>
20+
21+
@s = common global %struct.PackedBits zeroinitializer, align 1
22+
23+
!llvm.dbg.cu = !{!0}
24+
!llvm.module.flags = !{!14, !15, !16}
25+
!llvm.ident = !{!17}
26+
27+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 (trunk 267633)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
28+
!1 = !DIFile(filename: "bitfield.c", directory: "/Volumes/Data/llvm")
29+
!2 = !{}
30+
!3 = !{!4}
31+
!4 = distinct !DIGlobalVariable(name: "s", scope: !0, file: !1, line: 8, type: !5, isLocal: false, isDefinition: true, variable: %struct.PackedBits* @s)
32+
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "PackedBits", file: !1, line: 3, size: 40, align: 8, elements: !6)
33+
!6 = !{!7, !9, !13}
34+
35+
; CHECK: DW_TAG_member
36+
; CHECK-NEXT: DW_AT_name{{.*}}"a"
37+
; CHECK-NOT: DW_TAG
38+
; CHECK-NOT: DW_AT_bit_offset
39+
; CHECK-NOT: DW_AT_data_bit_offset
40+
; CHECK: DW_AT_data_member_location [DW_FORM_data1] (0x00)
41+
!7 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !5, file: !1, line: 5, baseType: !8, size: 8, align: 8)
42+
43+
!8 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
44+
45+
; CHECK: DW_TAG_member
46+
; CHECK-NEXT: DW_AT_name{{.*}}"b"
47+
; CHECK-NOT: DW_TAG
48+
; CHECK-NOT: DW_AT_bit_offset
49+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x08)
50+
; CHECK-NEXT: DW_AT_data_member_location [DW_FORM_data1] (0x00)
51+
!9 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !5, file: !1, line: 6, baseType: !10, size: 5, align: 32, offset: 8)
52+
53+
!10 = !DIDerivedType(tag: DW_TAG_typedef, name: "uint32_t", file: !11, line: 183, baseType: !12)
54+
!11 = !DIFile(filename: "/Volumes/Data/llvm/_build.ninja.release/bin/../lib/clang/3.9.0/include/stdint.h", directory: "/Volumes/Data/llvm")
55+
!12 = !DIBasicType(name: "unsigned int", size: 32, align: 32, encoding: DW_ATE_unsigned)
56+
57+
; CHECK: DW_TAG_member
58+
; CHECK-NEXT: DW_AT_name{{.*}}"c"
59+
; CHECK-NOT: DW_TAG
60+
; CHECK-NOT: DW_AT_bit_offset
61+
; CHECK: DW_AT_data_bit_offset [DW_FORM_data1] (0x0d)
62+
; CHECK-NEXT: DW_AT_data_member_location [DW_FORM_data1] (0x00)
63+
!13 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !5, file: !1, line: 7, baseType: !10, size: 27, align: 32, offset: 13)
64+
65+
!14 = !{i32 2, !"Dwarf Version", i32 4}
66+
!15 = !{i32 2, !"Debug Info Version", i32 3}
67+
!16 = !{i32 1, !"PIC Level", i32 2}
68+
!17 = !{!"clang version 3.9.0 (trunk 267633)"}

0 commit comments

Comments
 (0)