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