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/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"
23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
24 if (!DebugArangesData.isValidOffset(0))
27 DWARFDebugArangeSet Set;
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);
36 ParsedCUOffsets.insert(CUOffset);
40 void DWARFDebugAranges::generate(DWARFContext *CTX) {
45 // Extract aranges from .debug_aranges section.
46 DataExtractor ArangesData(CTX->getDWARFObj().getARangeSection(),
47 CTX->isLittleEndian(), 0);
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();
58 WithColor::error() << toString(CURanges.takeError()) << '\n';
60 for (const auto &R : *CURanges)
61 appendRange(CUOffset, R.LowPC, R.HighPC);
68 void DWARFDebugAranges::clear() {
71 ParsedCUOffsets.clear();
74 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
78 Endpoints.emplace_back(LowPC, CUOffset, true);
79 Endpoints.emplace_back(HighPC, CUOffset, false);
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);
96 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
99 // Update the set of valid CUs.
100 if (E.IsRangeStart) {
101 ValidCUs.insert(E.CUOffset);
103 auto CUPos = ValidCUs.find(E.CUOffset);
104 assert(CUPos != ValidCUs.end());
105 ValidCUs.erase(CUPos);
107 PrevAddress = E.Address;
109 assert(ValidCUs.empty());
111 // Endpoints are not needed now.
113 Endpoints.shrink_to_fit();
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)