llvm.org GIT mirror llvm / 60bd365
Debug Info: Fix a bug in the DW_AT_bit_offset calculation that would result in negative offsets and attempt a better job at documenting the algorithm. rdar://21082998 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240853 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 5 years ago
2 changed file(s) with 73 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
13441344 // Handle bitfield, assume bytes are 8 bits.
13451345 addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
13461346 addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1347
1347 //
13481348 // The DWARF 2 DW_AT_bit_offset is counting the bits between
13491349 // the high end of the aligned storage unit containing the bit
13501350 // field to the high end of the bit field.
13521352 // FIXME: DWARF 4 states that DW_AT_data_bit_offset (which
13531353 // counts from the beginning, regardless of endianness) should
13541354 // be used instead.
1355 //
1356 //
1357 // Struct Align Align Align
1358 // v v v v
1359 // +-----------+-----*-----+-----*-----+--
1360 // | ... |b1|b2|b3|b4|
1361 // +-----------+-----*-----+-----*-----+--
1362 // | | |<-- Size ->| |
1363 // |<---- Offset --->| |<--->|
1364 // | | | \_ DW_AT_bit_offset (little endian)
1365 // | |<--->|
1366 // |<--------->| \_ StartBitOffset = DW_AT_bit_offset (big endian)
1367 // \ = DW_AT_data_bit_offset (biendian)
1368 // \_ OffsetInBytes
13551369 uint64_t Offset = DT->getOffsetInBits();
1356 uint64_t AlignMask = ~(DT->getAlignInBits() - 1);
1357 uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1358 uint64_t FieldOffset = (HiMark - FieldSize);
1359 Offset -= FieldOffset;
1360
1361 // Maybe we need to work from the other end.
1362 if (Asm->getDataLayout().isLittleEndian())
1363 Offset = FieldSize - (Offset + Size);
1364 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1365
1366 OffsetInBytes = FieldOffset >> 3;
1370 uint64_t Align = DT->getAlignInBits() ? DT->getAlignInBits() : FieldSize;
1371 uint64_t AlignMask = ~(Align - 1);
1372 // The bits from the start of the storage unit to the start of the field.
1373 uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1374 // The endian-dependent DWARF 2 offset.
1375 uint64_t DwarfBitOffset = Asm->getDataLayout().isLittleEndian()
1376 ? OffsetToAlignment(Offset + Size, Align)
1377 : StartBitOffset;
1378
1379 // The byte offset of the field's aligned storage unit inside the struct.
1380 OffsetInBytes = (Offset - StartBitOffset) / 8;
1381 addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, DwarfBitOffset);
13671382 } else
13681383 // This is not a bitfield.
1369 OffsetInBytes = DT->getOffsetInBits() >> 3;
1384 OffsetInBytes = DT->getOffsetInBits() / 8;
13701385
13711386 if (DD->getDwarfVersion() <= 2) {
13721387 DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
0 ; RUN: %llc_dwarf -O0 -filetype=obj -o %t.o %s
1 ; RUN: llvm-dwarfdump -debug-dump=info %t.o | FileCheck %s
2 ; REQUIRES: object-emission
3 ;
4 ; Generated from:
5 ; struct {
6 ; char c;
7 ; int : 4;
8 ; int reserved : 28;
9 ; } a;
10 ;
11 ; CHECK: DW_TAG_member
12 ; CHECK: DW_AT_name {{.*}} "reserved"
13 ; CHECK: DW_AT_byte_size {{.*}} (0x04)
14 ; CHECK: DW_AT_bit_size {{.*}} (0x1c)
15 ; CHECK: DW_AT_bit_offset {{.*}} (0x18)
16 ; CHECK: DW_AT_data_member_location {{.*}}00
17 target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
18 target triple = "thumbv7-apple-ios"
19
20 %struct.anon = type { i8, [5 x i8] }
21
22 @a = common global %struct.anon zeroinitializer, align 1
23
24 !llvm.dbg.cu = !{!0}
25 !llvm.module.flags = !{!11, !12, !13, !14, !15}
26 !llvm.ident = !{!16}
27
28 !0 = !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.7.0 (trunk 240548) (llvm/trunk 240554)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, retainedTypes: !2, subprograms: !2, globals: !3, imports: !2)
29 !1 = !DIFile(filename: "test.i", directory: "/")
30 !2 = !{}
31 !3 = !{!4}
32 !4 = !DIGlobalVariable(name: "a", scope: !0, file: !1, line: 5, type: !5, isLocal: false, isDefinition: true, variable: %struct.anon* @a)
33 !5 = !DICompositeType(tag: DW_TAG_structure_type, file: !1, line: 1, size: 48, align: 8, elements: !6)
34 !6 = !{!7, !9}
35 !7 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !5, file: !1, line: 2, baseType: !8, size: 8, align: 8)
36 !8 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char)
37 !9 = !DIDerivedType(tag: DW_TAG_member, name: "reserved", scope: !5, file: !1, line: 4, baseType: !10, size: 28, align: 32, offset: 12)
38 !10 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed)
39 !11 = !{i32 2, !"Dwarf Version", i32 2}
40 !12 = !{i32 2, !"Debug Info Version", i32 3}
41 !13 = !{i32 1, !"wchar_size", i32 4}
42 !14 = !{i32 1, !"min_enum_size", i32 4}
43 !15 = !{i32 1, !"PIC Level", i32 2}
44 !16 = !{!"clang version 3.7.0 (trunk 240548) (llvm/trunk 240554)"}