1 //===- DWARFDebugAranges.cpp ----------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
10 #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
12 #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
14 #include "llvm/DebugInfo/DWARF/DWARFObject.h"
15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
23 void DWARFDebugAranges::extract(
24 DWARFDataExtractor DebugArangesData
,
25 function_ref
<void(Error
)> RecoverableErrorHandler
,
26 function_ref
<void(Error
)> WarningHandler
) {
27 if (!DebugArangesData
.isValidOffset(0))
30 DWARFDebugArangeSet Set
;
32 while (DebugArangesData
.isValidOffset(Offset
)) {
33 if (Error E
= Set
.extract(DebugArangesData
, &Offset
, WarningHandler
)) {
34 RecoverableErrorHandler(std::move(E
));
37 uint64_t CUOffset
= Set
.getCompileUnitDIEOffset();
38 for (const auto &Desc
: Set
.descriptors()) {
39 uint64_t LowPC
= Desc
.Address
;
40 uint64_t HighPC
= Desc
.getEndAddress();
41 appendRange(CUOffset
, LowPC
, HighPC
);
43 ParsedCUOffsets
.insert(CUOffset
);
47 void DWARFDebugAranges::generate(DWARFContext
*CTX
) {
52 // Extract aranges from .debug_aranges section.
53 DWARFDataExtractor
ArangesData(CTX
->getDWARFObj().getArangesSection(),
54 CTX
->isLittleEndian(), 0);
55 extract(ArangesData
, CTX
->getRecoverableErrorHandler(),
56 CTX
->getWarningHandler());
58 // Generate aranges from DIEs: even if .debug_aranges section is present,
59 // it may describe only a small subset of compilation units, so we need to
60 // manually build aranges for the rest of them.
61 for (const auto &CU
: CTX
->compile_units()) {
62 uint64_t CUOffset
= CU
->getOffset();
63 if (ParsedCUOffsets
.insert(CUOffset
).second
) {
64 Expected
<DWARFAddressRangesVector
> CURanges
= CU
->collectAddressRanges();
66 CTX
->getRecoverableErrorHandler()(CURanges
.takeError());
68 for (const auto &R
: *CURanges
)
69 appendRange(CUOffset
, R
.LowPC
, R
.HighPC
);
76 void DWARFDebugAranges::clear() {
79 ParsedCUOffsets
.clear();
82 void DWARFDebugAranges::appendRange(uint64_t CUOffset
, uint64_t LowPC
,
86 Endpoints
.emplace_back(LowPC
, CUOffset
, true);
87 Endpoints
.emplace_back(HighPC
, CUOffset
, false);
90 void DWARFDebugAranges::construct() {
91 std::multiset
<uint64_t> ValidCUs
; // Maintain the set of CUs describing
92 // a current address range.
93 llvm::sort(Endpoints
);
94 uint64_t PrevAddress
= -1ULL;
95 for (const auto &E
: Endpoints
) {
96 if (PrevAddress
< E
.Address
&& !ValidCUs
.empty()) {
97 // If the address range between two endpoints is described by some
98 // CU, first try to extend the last range in Aranges. If we can't
99 // do it, start a new range.
100 if (!Aranges
.empty() && Aranges
.back().HighPC() == PrevAddress
&&
101 ValidCUs
.find(Aranges
.back().CUOffset
) != ValidCUs
.end()) {
102 Aranges
.back().setHighPC(E
.Address
);
104 Aranges
.emplace_back(PrevAddress
, E
.Address
, *ValidCUs
.begin());
107 // Update the set of valid CUs.
108 if (E
.IsRangeStart
) {
109 ValidCUs
.insert(E
.CUOffset
);
111 auto CUPos
= ValidCUs
.find(E
.CUOffset
);
112 assert(CUPos
!= ValidCUs
.end());
113 ValidCUs
.erase(CUPos
);
115 PrevAddress
= E
.Address
;
117 assert(ValidCUs
.empty());
119 // Endpoints are not needed now.
121 Endpoints
.shrink_to_fit();
124 uint64_t DWARFDebugAranges::findAddress(uint64_t Address
) const {
125 RangeCollIterator It
=
126 partition_point(Aranges
, [=](Range R
) { return R
.HighPC() <= Address
; });
127 if (It
!= Aranges
.end() && It
->LowPC
<= Address
)