LLVM 19.0.0git
DWARFDebugAranges.cpp
Go to the documentation of this file.
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
16#include <algorithm>
17#include <cassert>
18#include <cstdint>
19#include <set>
20
21using namespace llvm;
22
23void DWARFDebugAranges::extract(
24 DWARFDataExtractor DebugArangesData,
25 function_ref<void(Error)> RecoverableErrorHandler,
26 function_ref<void(Error)> WarningHandler) {
27 if (!DebugArangesData.isValidOffset(0))
28 return;
29 uint64_t Offset = 0;
31
32 while (DebugArangesData.isValidOffset(Offset)) {
33 if (Error E = Set.extract(DebugArangesData, &Offset, WarningHandler)) {
34 RecoverableErrorHandler(std::move(E));
35 return;
36 }
37 uint64_t CUOffset = Set.getCompileUnitDIEOffset();
38 for (const auto &Desc : Set.descriptors()) {
39 uint64_t LowPC = Desc.Address;
40 uint64_t HighPC = Desc.getEndAddress();
41 appendRange(CUOffset, LowPC, HighPC);
42 }
43 ParsedCUOffsets.insert(CUOffset);
44 }
45}
46
48 clear();
49 if (!CTX)
50 return;
51
52 // Extract aranges from .debug_aranges section.
54 CTX->isLittleEndian(), 0);
55 extract(ArangesData, CTX->getRecoverableErrorHandler(),
56 CTX->getWarningHandler());
57
58 // Generate aranges from DIEs: even if .debug_aranges section is present,
59 // it may describe only a small subset of compilation units, so we need to
60 // manually build aranges for the rest of them.
61 for (const auto &CU : CTX->compile_units()) {
62 uint64_t CUOffset = CU->getOffset();
63 if (ParsedCUOffsets.insert(CUOffset).second) {
64 Expected<DWARFAddressRangesVector> CURanges = CU->collectAddressRanges();
65 if (!CURanges)
66 CTX->getRecoverableErrorHandler()(CURanges.takeError());
67 else
68 for (const auto &R : *CURanges)
69 appendRange(CUOffset, R.LowPC, R.HighPC);
70 }
71 }
72
73 construct();
74}
75
76void DWARFDebugAranges::clear() {
77 Endpoints.clear();
78 Aranges.clear();
79 ParsedCUOffsets.clear();
80}
81
82void DWARFDebugAranges::appendRange(uint64_t CUOffset, uint64_t LowPC,
83 uint64_t HighPC) {
84 if (LowPC >= HighPC)
85 return;
86 Endpoints.emplace_back(LowPC, CUOffset, true);
87 Endpoints.emplace_back(HighPC, CUOffset, false);
88}
89
90void DWARFDebugAranges::construct() {
91 std::multiset<uint64_t> ValidCUs; // Maintain the set of CUs describing
92 // a current address range.
93 llvm::sort(Endpoints);
94 uint64_t PrevAddress = -1ULL;
95 for (const auto &E : Endpoints) {
96 if (PrevAddress < E.Address && !ValidCUs.empty()) {
97 // If the address range between two endpoints is described by some
98 // CU, first try to extend the last range in Aranges. If we can't
99 // do it, start a new range.
100 if (!Aranges.empty() && Aranges.back().HighPC() == PrevAddress &&
101 ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()) {
102 Aranges.back().setHighPC(E.Address);
103 } else {
104 Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
105 }
106 }
107 // Update the set of valid CUs.
108 if (E.IsRangeStart) {
109 ValidCUs.insert(E.CUOffset);
110 } else {
111 auto CUPos = ValidCUs.find(E.CUOffset);
112 assert(CUPos != ValidCUs.end());
113 ValidCUs.erase(CUPos);
114 }
115 PrevAddress = E.Address;
116 }
117 assert(ValidCUs.empty());
118
119 // Endpoints are not needed now.
120 Endpoints.clear();
121 Endpoints.shrink_to_fit();
122}
123
125 RangeCollIterator It =
126 partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
127 if (It != Aranges.end() && It->LowPC <= Address)
128 return It->CUOffset;
129 return -1ULL;
130}
loop extract
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:48
function_ref< void(Error)> getRecoverableErrorHandler()
Definition: DWARFContext.h:430
compile_unit_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:188
function_ref< void(Error)> getWarningHandler()
Definition: DWARFContext.h:434
bool isLittleEndian() const
Definition: DWARFContext.h:398
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:147
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint64_t getCompileUnitDIEOffset() const
Error extract(DWARFDataExtractor data, uint64_t *offset_ptr, function_ref< void(Error)> WarningHandler)
desc_iterator_range descriptors() const
void generate(DWARFContext *CTX)
uint64_t findAddress(uint64_t Address) const
virtual StringRef getArangesSection() const
Definition: DWARFObject.h:43
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:2008
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
Description of the encoding of one expression Op.