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