Skip to content

Commit 03937ca

Browse files
author
George Rimar
committed
[llvm-dwarfdump] - Print section name and index when dumping .debug_info ranges
Teaches llvm-dwarfdump to print section index and name of range when it dumps .debug_info. Differential revision: https://reviews.llvm.org/D36313 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310915 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6d6a041 commit 03937ca

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

include/llvm/DebugInfo/DWARF/DWARFObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DWARFObject {
2727
public:
2828
virtual ~DWARFObject() = default;
2929
virtual StringRef getFileName() const { llvm_unreachable("unimplemented"); }
30+
virtual const object::ObjectFile *getFile() const { return nullptr; }
3031
virtual bool isLittleEndian() const = 0;
3132
virtual uint8_t getAddressSize() const { llvm_unreachable("unimplemented"); }
3233
virtual const DWARFSection &getInfoSection() const { return Dummy; }

lib/DebugInfo/DWARF/DWARFContext.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ class DWARFObjInMemory final : public DWARFObject {
933933
bool IsLittleEndian;
934934
uint8_t AddressSize;
935935
StringRef FileName;
936+
const object::ObjectFile *Obj = nullptr;
936937

937938
using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
938939
std::map<object::SectionRef, unsigned>>;
@@ -1051,7 +1052,8 @@ class DWARFObjInMemory final : public DWARFObject {
10511052
DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
10521053
function_ref<ErrorPolicy(Error)> HandleError)
10531054
: IsLittleEndian(Obj.isLittleEndian()),
1054-
AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()) {
1055+
AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1056+
Obj(&Obj) {
10551057

10561058
for (const SectionRef &Section : Obj.sections()) {
10571059
StringRef Name;
@@ -1188,6 +1190,8 @@ class DWARFObjInMemory final : public DWARFObject {
11881190
return AI->second;
11891191
}
11901192

1193+
const object::ObjectFile *getFile() const override { return Obj; }
1194+
11911195
bool isLittleEndian() const override { return IsLittleEndian; }
11921196
StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
11931197
const DWARFSection &getLineDWOSection() const override {

lib/DebugInfo/DWARF/DWARFDebugRangeList.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ bool DWARFDebugRangeList::extract(const DWARFDataExtractor &data,
3333
return false;
3434
Offset = *offset_ptr;
3535
while (true) {
36-
RangeListEntry entry;
36+
RangeListEntry Entry;
37+
Entry.SectionIndex = -1ULL;
38+
3739
uint32_t prev_offset = *offset_ptr;
38-
entry.StartAddress =
39-
data.getRelocatedAddress(offset_ptr, &entry.SectionIndex);
40-
entry.EndAddress = data.getRelocatedAddress(offset_ptr);
40+
Entry.StartAddress = data.getRelocatedAddress(offset_ptr);
41+
Entry.EndAddress =
42+
data.getRelocatedAddress(offset_ptr, &Entry.SectionIndex);
4143

4244
// Check that both values were extracted correctly.
4345
if (*offset_ptr != prev_offset + 2 * AddressSize) {
4446
clear();
4547
return false;
4648
}
47-
if (entry.isEndOfListEntry())
49+
if (Entry.isEndOfListEntry())
4850
break;
49-
Entries.push_back(entry);
51+
Entries.push_back(Entry);
5052
}
5153
return true;
5254
}

lib/DebugInfo/DWARF/DWARFDie.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1919
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
2020
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
21+
#include "llvm/Object/ObjectFile.h"
2122
#include "llvm/Support/DataExtractor.h"
2223
#include "llvm/Support/Format.h"
2324
#include "llvm/Support/MathExtras.h"
@@ -31,6 +32,7 @@
3132

3233
using namespace llvm;
3334
using namespace dwarf;
35+
using namespace object;
3436
using namespace syntax;
3537

3638
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
@@ -51,17 +53,42 @@ static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
5153
OS << ")";
5254
}
5355

54-
static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
55-
unsigned AddressSize, unsigned Indent) {
56-
if (Ranges.empty())
57-
return;
58-
59-
for (const auto &Range: Ranges) {
56+
static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
57+
const DWARFAddressRangesVector &Ranges,
58+
unsigned AddressSize, unsigned Indent,
59+
const DIDumpOptions &DumpOpts) {
60+
StringMap<unsigned> SectionAmountMap;
61+
std::vector<StringRef> SectionNames;
62+
if (Obj.getFile() && !DumpOpts.Brief) {
63+
for (const SectionRef &Section : Obj.getFile()->sections()) {
64+
StringRef Name;
65+
if (Section.getName(Name))
66+
Name = "<error>";
67+
68+
++SectionAmountMap[Name];
69+
SectionNames.push_back(Name);
70+
}
71+
}
72+
73+
for (size_t I = 0; I < Ranges.size(); ++I) {
74+
const DWARFAddressRange &R = Ranges[I];
75+
6076
OS << '\n';
6177
OS.indent(Indent);
62-
OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
63-
AddressSize*2, Range.LowPC,
64-
AddressSize*2, Range.HighPC);
78+
OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", AddressSize * 2,
79+
R.LowPC, AddressSize * 2, R.HighPC);
80+
81+
if (SectionNames.empty() || R.SectionIndex == -1ULL)
82+
continue;
83+
84+
StringRef Name = R.SectionIndex < SectionNames.size()
85+
? SectionNames[R.SectionIndex]
86+
: "<error>";
87+
OS << format(" \"%s\"", Name.str().c_str());
88+
89+
// Print section index if there is more than one section with this name.
90+
if (SectionAmountMap[Name] > 1)
91+
OS << format(" [%u]", R.SectionIndex);
6592
}
6693
}
6794

@@ -126,10 +153,11 @@ static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
126153
if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
127154
dumpApplePropertyAttribute(OS, *OptVal);
128155
} else if (Attr == DW_AT_ranges) {
129-
dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
130-
sizeof(BaseIndent)+Indent+4);
156+
const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
157+
dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
158+
sizeof(BaseIndent) + Indent + 4, DumpOpts);
131159
}
132-
160+
133161
OS << ")\n";
134162
}
135163

test/DebugInfo/X86/dwarfdump-ranges-unrelocated.s

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# RUN: llvm-mc -triple x86_64-pc-linux -filetype=obj %s -o %t
22
# RUN: llvm-dwarfdump %t | FileCheck %s
33

4+
# CHECK: .debug_info contents:
5+
# CHECK: DW_TAG_compile_unit
6+
# CHECK: DW_AT_ranges [DW_FORM_sec_offset] (0x00000000
7+
# CHECK-NEXT: [0x0000000000000000 - 0x0000000000000001) ".text.foo1"
8+
# CHECK-NEXT: [0x0000000000000000 - 0x0000000000000002) ".text.foo2" [4]
9+
# CHECK-NEXT: [0x0000000000000000 - 0x0000000000000003) ".text.foo2" [5])
10+
411
# CHECK: .debug_ranges contents:
512
# CHECK: 00000000 0000000000000000 0000000000000001
613
# CHECK: 00000000 0000000000000000 0000000000000002
14+
# CHECK: 00000000 0000000000000000 0000000000000003
715
# CHECK: 00000000 <End of list>
816

9-
## Asm code for testcase is a reduced output from next invocation and source:
17+
# RUN: llvm-dwarfdump -brief=true %t | FileCheck %s --check-prefix=BRIEF
18+
# BRIEF: DW_TAG_compile_unit
19+
# BRIEF: DW_AT_ranges (0x00000000
20+
# BRIEF-NEXT: [0x0000000000000000 - 0x0000000000000001)
21+
# BRIEF-NEXT: [0x0000000000000000 - 0x0000000000000002)
22+
# BRIEF-NEXT: [0x0000000000000000 - 0x0000000000000003))
23+
24+
## Asm code for testcase is a reduced and modified output from next
25+
## invocation and source:
1026
# clang test.cpp -S -o test.s -gmlt -ffunction-sections
1127
# test.cpp:
1228
# void foo1() { }
@@ -17,12 +33,19 @@
1733
nop
1834
.Lfunc_end0:
1935

20-
.section .text.foo2,"ax",@progbits
36+
.section .text.foo2,"ax",@progbits, unique, 1
2137
.Lfunc_begin1:
2238
nop
2339
nop
2440
.Lfunc_end1:
2541

42+
.section .text.foo2,"ax",@progbits, unique, 2
43+
.Lfunc_begin2:
44+
nop
45+
nop
46+
nop
47+
.Lfunc_end2:
48+
2649
.section .debug_abbrev,"",@progbits
2750
.byte 1 # Abbreviation Code
2851
.byte 17 # DW_TAG_compile_unit
@@ -66,5 +89,7 @@
6689
.quad .Lfunc_end0
6790
.quad .Lfunc_begin1
6891
.quad .Lfunc_end1
92+
.quad .Lfunc_begin2
93+
.quad .Lfunc_end2
6994
.quad 0
7095
.quad 0

0 commit comments

Comments
 (0)