[llvm] r369378 - [DWARF] Fix reading 64-bit DWARF type units.

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 05:52:33 PDT 2019


Author: ikudrin
Date: Tue Aug 20 05:52:32 2019
New Revision: 369378

URL: http://llvm.org/viewvc/llvm-project?rev=369378&view=rev
Log:
[DWARF] Fix reading 64-bit DWARF type units.

The type_offset field is 8 bytes long in DWARF64. The patch extends
TypeOffset to uint64_t and fixes its reading. The patch also fixes
checking of TypeOffset bounds as it was inaccurate in DWARF64 case.

Differential Revision: https://reviews.llvm.org/D66465

Added:
    llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v4-dwarf64.s
    llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v5-dwarf64.s
Modified:
    llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFTypeUnit.h
    llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFUnit.h
    llvm/trunk/lib/DebugInfo/DWARF/DWARFTypeUnit.cpp
    llvm/trunk/lib/DebugInfo/DWARF/DWARFUnit.cpp

Modified: llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFTypeUnit.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFTypeUnit.h?rev=369378&r1=369377&r2=369378&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFTypeUnit.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFTypeUnit.h Tue Aug 20 05:52:32 2019
@@ -34,7 +34,7 @@ public:
                   LS, LE, IsDWO, UnitVector) {}
 
   uint64_t getTypeHash() const { return getHeader().getTypeHash(); }
-  uint32_t getTypeOffset() const { return getHeader().getTypeOffset(); }
+  uint64_t getTypeOffset() const { return getHeader().getTypeOffset(); }
 
   void dump(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override;
   // Enable LLVM-style RTTI.

Modified: llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFUnit.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFUnit.h?rev=369378&r1=369377&r2=369378&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFUnit.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/DWARF/DWARFUnit.h Tue Aug 20 05:52:32 2019
@@ -56,7 +56,7 @@ class DWARFUnitHeader {
 
   // For type units only.
   uint64_t TypeHash = 0;
-  uint32_t TypeOffset = 0;
+  uint64_t TypeOffset = 0;
 
   // For v5 split or skeleton compile units only.
   Optional<uint64_t> DWOId;
@@ -91,7 +91,7 @@ public:
   }
   const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; }
   uint64_t getTypeHash() const { return TypeHash; }
-  uint32_t getTypeOffset() const { return TypeOffset; }
+  uint64_t getTypeOffset() const { return TypeOffset; }
   uint8_t getUnitType() const { return UnitType; }
   bool isTypeUnit() const {
     return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type;

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFTypeUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFTypeUnit.cpp?rev=369378&r1=369377&r2=369378&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFTypeUnit.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFTypeUnit.cpp Tue Aug 20 05:52:32 2019
@@ -38,7 +38,7 @@ void DWARFTypeUnit::dump(raw_ostream &OS
      << " addr_size = " << format("0x%02x", getAddressByteSize())
      << " name = '" << Name << "'"
      << " type_signature = " << format("0x%016" PRIx64, getTypeHash())
-     << " type_offset = " << format("0x%04x", getTypeOffset())
+     << " type_offset = " << format("0x%04" PRIx64, getTypeOffset())
      << " (next unit at " << format("0x%08" PRIx64, getNextUnitOffset())
      << ")\n";
 

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFUnit.cpp?rev=369378&r1=369377&r2=369378&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFUnit.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFUnit.cpp Tue Aug 20 05:52:32 2019
@@ -243,11 +243,9 @@ bool DWARFUnitHeader::extract(DWARFConte
     IndexEntry = Index->getFromOffset(*offset_ptr);
   Length = debug_info.getRelocatedValue(4, offset_ptr);
   FormParams.Format = DWARF32;
-  unsigned SizeOfLength = 4;
   if (Length == dwarf::DW_LENGTH_DWARF64) {
     Length = debug_info.getU64(offset_ptr);
     FormParams.Format = DWARF64;
-    SizeOfLength = 8;
   }
   FormParams.Version = debug_info.getU16(offset_ptr);
   if (FormParams.Version >= 5) {
@@ -277,7 +275,8 @@ bool DWARFUnitHeader::extract(DWARFConte
   }
   if (isTypeUnit()) {
     TypeHash = debug_info.getU64(offset_ptr);
-    TypeOffset = debug_info.getU32(offset_ptr);
+    TypeOffset =
+        debug_info.getUnsigned(offset_ptr, FormParams.getDwarfOffsetByteSize());
   } else if (UnitType == DW_UT_split_compile || UnitType == DW_UT_skeleton)
     DWOId = debug_info.getU64(offset_ptr);
 
@@ -290,7 +289,8 @@ bool DWARFUnitHeader::extract(DWARFConte
   bool TypeOffsetOK =
       !isTypeUnit()
           ? true
-          : TypeOffset >= Size && TypeOffset < getLength() + SizeOfLength;
+          : TypeOffset >= Size &&
+                TypeOffset < getLength() + getUnitLengthFieldByteSize();
   bool LengthOK = debug_info.isValidOffset(getNextUnitOffset() - 1);
   bool VersionOK = DWARFContext::isSupportedVersion(getVersion());
   bool AddrSizeOK = getAddressByteSize() == 4 || getAddressByteSize() == 8;

Added: llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v4-dwarf64.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v4-dwarf64.s?rev=369378&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v4-dwarf64.s (added)
+++ llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v4-dwarf64.s Tue Aug 20 05:52:32 2019
@@ -0,0 +1,57 @@
+# RUN: llvm-mc %s -filetype obj -triple x86_64-unknown-elf -o - | \
+# RUN:   llvm-dwarfdump -debug-types - | \
+# RUN:   FileCheck %s
+
+        .section .debug_abbrev,"", at progbits
+        .byte 0x01  # Abbrev code
+        .byte 0x41  # DW_TAG_type_unit
+        .byte 0x01  # DW_CHILDREN_yes
+        .byte 0x17  # DW_AT_visibility
+        .byte 0x0b  # DW_FORM_data1
+        .byte 0x00  # EOM(1)
+        .byte 0x00  # EOM(2)
+        .byte 0x02  # Abbrev code
+        .byte 0x13  # DW_TAG_structure_type
+        .byte 0x00  # DW_CHILDREN_no (no members)
+        .byte 0x17  # DW_AT_visibility
+        .byte 0x0b  # DW_FORM_data1
+        .byte 0x00  # EOM(1)
+        .byte 0x00  # EOM(2)
+        .byte 0x00  # EOM(3)
+
+        .section .debug_types,"", at progbits
+# CHECK: .debug_types contents:
+# CHECK-NEXT: 0x00000000: Type Unit:
+TU_4_64_start:
+        .long 0xffffffff            # DWARF64 mark
+        .quad TU_4_64_end-TU_4_64_version  # Length of Unit
+# CHECK-SAME: length = 0x00000021
+TU_4_64_version:
+        .short 4                    # DWARF version number
+# CHECK-SAME: version = 0x0004
+        .quad .debug_abbrev         # Offset Into Abbrev. Section
+# CHECK-SAME: abbr_offset = 0x0000
+        .byte 8                     # Address Size (in bytes)
+# CHECK-SAME: addr_size = 0x08
+# CHECK-SAME: name = ''
+        .quad 0x0011223344556677    # Type Signature
+# CHECK-SAME: type_signature = 0x0011223344556677
+        .quad TU_4_64_type-TU_4_64_start # Type offset
+# CHECK-SAME: type_offset = 0x0029
+# CHECK-SAME: (next unit at 0x0000002d)
+
+        .byte 1                     # Abbreviation code
+# CHECK: 0x00000027: DW_TAG_type_unit
+        .byte 1                     # DW_VIS_local
+# CHECK-NEXT: DW_AT_visibility (DW_VIS_local)
+
+TU_4_64_type:
+        .byte 2                     # Abbreviation code
+# CHECK: 0x00000029: DW_TAG_structure_type
+        .byte 1                     # DW_VIS_local
+# CHECK-NEXT: DW_AT_visibility (DW_VIS_local)
+
+        .byte 0 # NULL
+# CHECK: 0x0000002b: NULL
+        .byte 0 # NULL
+TU_4_64_end:

Added: llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v5-dwarf64.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v5-dwarf64.s?rev=369378&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v5-dwarf64.s (added)
+++ llvm/trunk/test/tools/llvm-dwarfdump/X86/typeunit-v5-dwarf64.s Tue Aug 20 05:52:32 2019
@@ -0,0 +1,58 @@
+# RUN: llvm-mc %s -filetype obj -triple x86_64-unknown-elf -o - | \
+# RUN:   llvm-dwarfdump -debug-info - | \
+# RUN:   FileCheck %s
+
+        .section .debug_abbrev,"", at progbits
+        .byte 0x01  # Abbrev code
+        .byte 0x41  # DW_TAG_type_unit
+        .byte 0x01  # DW_CHILDREN_yes
+        .byte 0x17  # DW_AT_visibility
+        .byte 0x0b  # DW_FORM_data1
+        .byte 0x00  # EOM(1)
+        .byte 0x00  # EOM(2)
+        .byte 0x02  # Abbrev code
+        .byte 0x13  # DW_TAG_structure_type
+        .byte 0x00  # DW_CHILDREN_no (no members)
+        .byte 0x17  # DW_AT_visibility
+        .byte 0x0b  # DW_FORM_data1
+        .byte 0x00  # EOM(1)
+        .byte 0x00  # EOM(2)
+        .byte 0x00  # EOM(3)
+
+        .section .debug_info,"", at progbits
+# CHECK: .debug_info contents:
+# CHECK-NEXT: 0x00000000: Type Unit:
+TU_5_64_start:
+        .long 0xffffffff            # DWARF64 mark
+        .quad TU_5_64_end-TU_5_64_version  # Length of Unit
+# CHECK-SAME: length = 0x00000022
+TU_5_64_version:
+        .short 5                    # DWARF version number
+# CHECK-SAME: version = 0x0005
+        .byte 2                     # DW_UT_type
+        .byte 8                     # Address Size (in bytes)
+        .quad .debug_abbrev         # Offset Into Abbrev. Section
+# CHECK-SAME: abbr_offset = 0x0000
+# CHECK-SAME: addr_size = 0x08
+# CHECK-SAME: name = ''
+        .quad 0x0011223344556677    # Type Signature
+# CHECK-SAME: type_signature = 0x0011223344556677
+        .quad TU_5_64_type-TU_5_64_start # Type offset
+# CHECK-SAME: type_offset = 0x002a
+# CHECK-SAME: (next unit at 0x0000002e)
+
+        .byte 1                     # Abbreviation code
+# CHECK: 0x00000028: DW_TAG_type_unit
+        .byte 1                     # DW_VIS_local
+# CHECK-NEXT: DW_AT_visibility (DW_VIS_local)
+
+TU_5_64_type:
+        .byte 2                     # Abbreviation code
+# CHECK: 0x0000002a: DW_TAG_structure_type
+        .byte 1                     # DW_VIS_local
+# CHECK-NEXT: DW_AT_visibility (DW_VIS_local)
+
+        .byte 0 # NULL
+# CHECK: 0x0000002c: NULL
+        .byte 0 # NULL
+TU_5_64_end:




More information about the llvm-commits mailing list