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

Commit 418b24f

Browse files
George Rimaradrian-prantl
authored andcommitted
[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 (cherry picked from commit 03937ca)
1 parent c4bf463 commit 418b24f

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
@@ -941,6 +941,7 @@ class DWARFObjInMemory final : public DWARFObject {
941941
bool IsLittleEndian;
942942
uint8_t AddressSize;
943943
StringRef FileName;
944+
const object::ObjectFile *Obj = nullptr;
944945

945946
using TypeSectionMap = MapVector<object::SectionRef, DWARFSectionMap,
946947
std::map<object::SectionRef, unsigned>>;
@@ -1059,7 +1060,8 @@ class DWARFObjInMemory final : public DWARFObject {
10591060
DWARFObjInMemory(const object::ObjectFile &Obj, const LoadedObjectInfo *L,
10601061
function_ref<ErrorPolicy(Error)> HandleError)
10611062
: IsLittleEndian(Obj.isLittleEndian()),
1062-
AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()) {
1063+
AddressSize(Obj.getBytesInAddress()), FileName(Obj.getFileName()),
1064+
Obj(&Obj) {
10631065

10641066
for (const SectionRef &Section : Obj.sections()) {
10651067
StringRef Name;
@@ -1196,6 +1198,8 @@ class DWARFObjInMemory final : public DWARFObject {
11961198
return AI->second;
11971199
}
11981200

1201+
const object::ObjectFile *getFile() const override { return Obj; }
1202+
11991203
bool isLittleEndian() const override { return IsLittleEndian; }
12001204
StringRef getAbbrevDWOSection() const override { return AbbrevDWOSection; }
12011205
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
@@ -19,6 +19,7 @@
1919
#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
2020
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
2121
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
22+
#include "llvm/Object/ObjectFile.h"
2223
#include "llvm/Support/DataExtractor.h"
2324
#include "llvm/Support/Format.h"
2425
#include "llvm/Support/MathExtras.h"
@@ -32,6 +33,7 @@
3233

3334
using namespace llvm;
3435
using namespace dwarf;
36+
using namespace object;
3537
using namespace syntax;
3638

3739
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
@@ -52,17 +54,42 @@ static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
5254
OS << ")";
5355
}
5456

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

@@ -171,10 +198,11 @@ static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
171198
if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
172199
dumpApplePropertyAttribute(OS, *OptVal);
173200
} else if (Attr == DW_AT_ranges) {
174-
dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
175-
sizeof(BaseIndent)+Indent+4);
201+
const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
202+
dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
203+
sizeof(BaseIndent) + Indent + 4, DumpOpts);
176204
}
177-
205+
178206
OS << ")\n";
179207
}
180208

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)