Index: include/llvm/DebugInfo/DWARF/DWARFObject.h =================================================================== --- include/llvm/DebugInfo/DWARF/DWARFObject.h +++ include/llvm/DebugInfo/DWARF/DWARFObject.h @@ -27,6 +27,7 @@ public: virtual ~DWARFObject() = default; virtual StringRef getFileName() const { llvm_unreachable("unimplemented"); } + virtual StringRef getSectionName(uint64_t Index) const { return ""; } virtual bool isLittleEndian() const = 0; virtual uint8_t getAddressSize() const { llvm_unreachable("unimplemented"); } virtual const DWARFSection &getInfoSection() const { return Dummy; } Index: lib/DebugInfo/DWARF/DWARFContext.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFContext.cpp +++ lib/DebugInfo/DWARF/DWARFContext.cpp @@ -933,6 +933,7 @@ bool IsLittleEndian; uint8_t AddressSize; StringRef FileName; + const object::ObjectFile *Obj = nullptr; using TypeSectionMap = MapVector>; @@ -1051,7 +1052,8 @@ DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L, function_ref HandleError) : IsLittleEndian(Obj.isLittleEndian()), - AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()) { + AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()), + Obj(&Obj) { for (const SectionRef &Section : Obj.sections()) { StringRef Name; @@ -1188,6 +1190,16 @@ return AI->second; } + StringRef getSectionName(uint64_t Index) const override { + StringRef Ret; + if (Obj) { + auto SecI = Obj->section_begin(); + std::advance(SecI, Index); + (*SecI).getName(Ret); + } + return Ret; + } + bool isLittleEndian() const override { return IsLittleEndian; } StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; } const DWARFSection &getLineDWOSection() const override { Index: lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp +++ lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp @@ -33,20 +33,22 @@ return false; Offset = *offset_ptr; while (true) { - RangeListEntry entry; + RangeListEntry Entry; + Entry.SectionIndex = -1ULL; + uint32_t prev_offset = *offset_ptr; - entry.StartAddress = - data.getRelocatedAddress(offset_ptr, &entry.SectionIndex); - entry.EndAddress = data.getRelocatedAddress(offset_ptr); + Entry.StartAddress = + data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex); + Entry.EndAddress = data.getRelocatedAddress(offset_ptr); // Check that both values were extracted correctly. if (*offset_ptr != prev_offset + 2 * AddressSize) { clear(); return false; } - if (entry.isEndOfListEntry()) + if (Entry.isEndOfListEntry()) break; - Entries.push_back(entry); + Entries.push_back(Entry); } return true; } Index: lib/DebugInfo/DWARF/DWARFDie.cpp =================================================================== --- lib/DebugInfo/DWARF/DWARFDie.cpp +++ lib/DebugInfo/DWARF/DWARFDie.cpp @@ -51,17 +51,23 @@ OS << ")"; } -static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges, +static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS, + const DWARFAddressRangesVector &Ranges, unsigned AddressSize, unsigned Indent) { if (Ranges.empty()) return; - for (const auto &Range: Ranges) { + for (const auto &Range : Ranges) { OS << '\n'; OS.indent(Indent); - OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", - AddressSize*2, Range.LowPC, - AddressSize*2, Range.HighPC); + + std::string NameIndex = "N/A"; + if (Range.SectionIndex != -1ULL) + NameIndex = Obj.getSectionName(Range.SectionIndex).str() + "(" + + std::to_string(Range.SectionIndex) + ")"; + + OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ") %s", AddressSize * 2, + Range.LowPC, AddressSize * 2, Range.HighPC, NameIndex.c_str()); } } @@ -126,10 +132,11 @@ if (Optional OptVal = formValue.getAsUnsignedConstant()) dumpApplePropertyAttribute(OS, *OptVal); } else if (Attr == DW_AT_ranges) { - dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(), - sizeof(BaseIndent)+Indent+4); + const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj(); + dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(), + sizeof(BaseIndent) + Indent + 4); } - + OS << ")\n"; } Index: test/DebugInfo/X86/dwarfdump-ranges-unrelocated.s =================================================================== --- test/DebugInfo/X86/dwarfdump-ranges-unrelocated.s +++ test/DebugInfo/X86/dwarfdump-ranges-unrelocated.s @@ -1,6 +1,12 @@ # RUN: llvm-mc -triple x86_64-pc-linux -filetype=obj %s -o %t # RUN: llvm-dwarfdump %t | FileCheck %s +# CHECK: .debug_info contents: +# CHECK: DW_TAG_compile_unit +# CHECK: DW_AT_ranges [DW_FORM_sec_offset] (0x00000000 +# CHECK-NEXT: [0x0000000000000000 - 0x0000000000000001) .text.foo1(3) +# CHECK-NEXT: [0x0000000000000000 - 0x0000000000000002) .text.foo2(4)) + # CHECK: .debug_ranges contents: # CHECK: 00000000 0000000000000000 0000000000000001 # CHECK: 00000000 0000000000000000 0000000000000002 Index: test/DebugInfo/dwarfdump-ranges.test =================================================================== --- test/DebugInfo/dwarfdump-ranges.test +++ test/DebugInfo/dwarfdump-ranges.test @@ -4,14 +4,14 @@ CHECK: DW_TAG_compile_unit CHECK-NOT: TAG CHECK: DW_AT_ranges [DW_FORM_data4] (0x00000000 -CHECK-NEXT: [0x000000000000062c - 0x0000000000000637) -CHECK-NEXT: [0x0000000000000637 - 0x000000000000063d)) +CHECK-NEXT: [0x000000000000062c - 0x0000000000000637) N/A +CHECK-NEXT: [0x0000000000000637 - 0x000000000000063d) N/A) CHECK: DW_TAG_compile_unit CHECK-NOT: TAG CHECK: DW_AT_ranges [DW_FORM_data4] (0x00000030 -CHECK-NEXT: [0x0000000000000640 - 0x000000000000064b) -CHECK-NEXT: [0x0000000000000637 - 0x000000000000063d)) +CHECK-NEXT: [0x0000000000000640 - 0x000000000000064b) N/A +CHECK-NEXT: [0x0000000000000637 - 0x000000000000063d) N/A) CHECK: .debug_ranges contents: