121
|
1 //===- DWARFDebugAranges.cpp ----------------------------------------------===//
|
83
|
2 //
|
147
|
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
|
83
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
|
9 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
|
|
10 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
|
|
11 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
|
12 #include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
|
121
|
13 #include "llvm/Support/DataExtractor.h"
|
147
|
14 #include "llvm/Support/WithColor.h"
|
83
|
15 #include <algorithm>
|
|
16 #include <cassert>
|
121
|
17 #include <cstdint>
|
83
|
18 #include <set>
|
121
|
19 #include <vector>
|
|
20
|
83
|
21 using namespace llvm;
|
|
22
|
|
23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
|
|
24 if (!DebugArangesData.isValidOffset(0))
|
|
25 return;
|
147
|
26 uint64_t Offset = 0;
|
83
|
27 DWARFDebugArangeSet Set;
|
|
28
|
|
29 while (Set.extract(DebugArangesData, &Offset)) {
|
147
|
30 uint64_t CUOffset = Set.getCompileUnitDIEOffset();
|
83
|
31 for (const auto &Desc : Set.descriptors()) {
|
|
32 uint64_t LowPC = Desc.Address;
|
|
33 uint64_t HighPC = Desc.getEndAddress();
|
|
34 appendRange(CUOffset, LowPC, HighPC);
|
|
35 }
|
|
36 ParsedCUOffsets.insert(CUOffset);
|
|
37 }
|
|
38 }
|
|
39
|
|
40 void DWARFDebugAranges::generate(DWARFContext *CTX) {
|
|
41 clear();
|
|
42 if (!CTX)
|
|
43 return;
|
|
44
|
|
45 // Extract aranges from .debug_aranges section.
|
147
|
46 DataExtractor ArangesData(CTX->getDWARFObj().getArangesSection(),
|
121
|
47 CTX->isLittleEndian(), 0);
|
83
|
48 extract(ArangesData);
|
|
49
|
|
50 // Generate aranges from DIEs: even if .debug_aranges section is present,
|
|
51 // it may describe only a small subset of compilation units, so we need to
|
|
52 // manually build aranges for the rest of them.
|
|
53 for (const auto &CU : CTX->compile_units()) {
|
147
|
54 uint64_t CUOffset = CU->getOffset();
|
83
|
55 if (ParsedCUOffsets.insert(CUOffset).second) {
|
147
|
56 Expected<DWARFAddressRangesVector> CURanges = CU->collectAddressRanges();
|
|
57 if (!CURanges)
|
|
58 WithColor::error() << toString(CURanges.takeError()) << '\n';
|
|
59 else
|
|
60 for (const auto &R : *CURanges)
|
|
61 appendRange(CUOffset, R.LowPC, R.HighPC);
|
83
|
62 }
|
|
63 }
|
|
64
|
|
65 construct();
|
|
66 }
|
|
67
|
|
68 void DWARFDebugAranges::clear() {
|
|
69 Endpoints.clear();
|
|
70 Aranges.clear();
|
|
71 ParsedCUOffsets.clear();
|
|
72 }
|
|
73
|
147
|
74 void DWARFDebugAranges::appendRange(uint64_t CUOffset, uint64_t LowPC,
|
83
|
75 uint64_t HighPC) {
|
|
76 if (LowPC >= HighPC)
|
|
77 return;
|
|
78 Endpoints.emplace_back(LowPC, CUOffset, true);
|
|
79 Endpoints.emplace_back(HighPC, CUOffset, false);
|
|
80 }
|
|
81
|
|
82 void DWARFDebugAranges::construct() {
|
147
|
83 std::multiset<uint64_t> ValidCUs; // Maintain the set of CUs describing
|
83
|
84 // a current address range.
|
147
|
85 llvm::sort(Endpoints);
|
83
|
86 uint64_t PrevAddress = -1ULL;
|
|
87 for (const auto &E : Endpoints) {
|
121
|
88 if (PrevAddress < E.Address && !ValidCUs.empty()) {
|
83
|
89 // If the address range between two endpoints is described by some
|
|
90 // CU, first try to extend the last range in Aranges. If we can't
|
|
91 // do it, start a new range.
|
|
92 if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress &&
|
|
93 ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) {
|
|
94 Aranges.back().setHighPC(E.Address);
|
|
95 } else {
|
|
96 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
|
|
97 }
|
|
98 }
|
|
99 // Update the set of valid CUs.
|
|
100 if (E.IsRangeStart) {
|
|
101 ValidCUs.insert(E.CUOffset);
|
|
102 } else {
|
|
103 auto CUPos = ValidCUs.find(E.CUOffset);
|
|
104 assert(CUPos != ValidCUs.end());
|
|
105 ValidCUs.erase(CUPos);
|
|
106 }
|
|
107 PrevAddress = E.Address;
|
|
108 }
|
|
109 assert(ValidCUs.empty());
|
|
110
|
|
111 // Endpoints are not needed now.
|
121
|
112 Endpoints.clear();
|
|
113 Endpoints.shrink_to_fit();
|
83
|
114 }
|
|
115
|
|
116 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
|
147
|
117 RangeCollIterator It =
|
|
118 partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
|
|
119 if (It != Aranges.end() && It->LowPC <= Address)
|
|
120 return It->CUOffset;
|
83
|
121 return -1U;
|
|
122 }
|