LLVM  15.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 
21 using namespace llvm;
22 
23 void 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 
76 void DWARFDebugAranges::clear() {
77  Endpoints.clear();
78  Aranges.clear();
79  ParsedCUOffsets.clear();
80 }
81 
82 void 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 
90 void 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 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::DWARFDebugAranges::findAddress
uint64_t findAddress(uint64_t Address) const
Definition: DWARFDebugAranges.cpp:124
llvm::DWARFObject::getArangesSection
virtual StringRef getArangesSection() const
Definition: DWARFObject.h:42
DWARFObject.h
DWARFContext.h
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:47
DWARFAddressRange.h
DWARFDebugArangeSet.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::DWARFDataExtractor
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
Definition: DWARFDataExtractor.h:21
llvm::detail::DenseSetImpl::insert
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
llvm::DWARFDebugAranges::generate
void generate(DWARFContext *CTX)
Definition: DWARFDebugAranges.cpp:47
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DWARFDebugArangeSet::descriptors
desc_iterator_range descriptors() const
Definition: DWARFDebugArangeSet.h:72
llvm::DWARFContext::compile_units
compile_unit_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:168
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
DWARFDebugAranges.h
llvm::AMDGPU::Hwreg::Offset
Offset
Definition: SIDefines.h:412
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
DWARFUnit.h
llvm::DWARFContext::getWarningHandler
function_ref< void(Error)> getWarningHandler()
Definition: DWARFContext.h:417
llvm::DataExtractor::isValidOffset
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
Definition: DataExtractor.h:665
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::DWARFDebugArangeSet
Definition: DWARFDebugArangeSet.h:23
llvm::detail::DenseSetImpl::clear
void clear()
Definition: DenseSet.h:92
llvm::DWARFContext::isLittleEndian
bool isLittleEndian() const
Definition: DWARFContext.h:379
llvm::DWARFDebugArangeSet::getCompileUnitDIEOffset
uint64_t getCompileUnitDIEOffset() const
Definition: DWARFDebugArangeSet.h:68
llvm::DWARFDebugArangeSet::extract
Error extract(DWARFDataExtractor data, uint64_t *offset_ptr, function_ref< void(Error)> WarningHandler)
Definition: DWARFDebugArangeSet.cpp:38
llvm::partition_point
auto partition_point(R &&Range, Predicate P)
Binary search for the first iterator in a range where a predicate is false.
Definition: STLExtras.h:1764
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::DWARFContext::getRecoverableErrorHandler
function_ref< void(Error)> getRecoverableErrorHandler()
Definition: DWARFContext.h:413
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1552
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::DWARFContext::getDWARFObj
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:126
CU
Definition: AArch64AsmBackend.cpp:504
DWARFDataExtractor.h