]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp
MFC r355940:
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / DebugInfo / DWARF / DWARFDebugAranges.cpp
1 //===- DWARFDebugAranges.cpp ----------------------------------------------===//
2 //
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
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"
13 #include "llvm/Support/DataExtractor.h"
14 #include "llvm/Support/WithColor.h"
15 #include <algorithm>
16 #include <cassert>
17 #include <cstdint>
18 #include <set>
19 #include <vector>
20
21 using namespace llvm;
22
23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
24   if (!DebugArangesData.isValidOffset(0))
25     return;
26   uint32_t Offset = 0;
27   DWARFDebugArangeSet Set;
28
29   while (Set.extract(DebugArangesData, &Offset)) {
30     uint32_t CUOffset = Set.getCompileUnitDIEOffset();
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.
46   DataExtractor ArangesData(CTX->getDWARFObj().getARangeSection(),
47                             CTX->isLittleEndian(), 0);
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()) {
54     uint32_t CUOffset = CU->getOffset();
55     if (ParsedCUOffsets.insert(CUOffset).second) {
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);
62     }
63   }
64
65   construct();
66 }
67
68 void DWARFDebugAranges::clear() {
69   Endpoints.clear();
70   Aranges.clear();
71   ParsedCUOffsets.clear();
72 }
73
74 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
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() {
83   std::multiset<uint32_t> ValidCUs;  // Maintain the set of CUs describing
84                                      // a current address range.
85   llvm::sort(Endpoints);
86   uint64_t PrevAddress = -1ULL;
87   for (const auto &E : Endpoints) {
88     if (PrevAddress < E.Address && !ValidCUs.empty()) {
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.
112   Endpoints.clear();
113   Endpoints.shrink_to_fit();
114 }
115
116 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
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;
121   return -1U;
122 }