LLVM  14.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 
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;
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 
46  clear();
47  if (!CTX)
48  return;
49 
50  // Extract aranges from .debug_aranges section.
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 
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 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
DWARFCompileUnit.h
llvm::DWARFDebugAranges::findAddress
uint64_t findAddress(uint64_t Address) const
Definition: DWARFDebugAranges.cpp:121
llvm::DWARFObject::getArangesSection
virtual StringRef getArangesSection() const
Definition: DWARFObject.h:42
DWARFContext.h
llvm::DWARFContext
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:53
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
DWARFDebugArangeSet.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
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:45
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::DWARFDebugArangeSet::descriptors
desc_iterator_range descriptors() const
Definition: DWARFDebugArangeSet.h:71
llvm::DWARFContext::compile_units
compile_unit_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:172
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
DWARFDebugAranges.h
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
llvm::HighlightColor::Address
@ Address
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:22
llvm::detail::DenseSetImpl::clear
void clear()
Definition: DenseSet.h:92
llvm::DWARFContext::isLittleEndian
bool isLittleEndian() const
Definition: DWARFContext.h:372
llvm::DWARFDebugArangeSet::getCompileUnitDIEOffset
uint64_t getCompileUnitDIEOffset() const
Definition: DWARFDebugArangeSet.h:67
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:1748
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
DataExtractor.h
llvm::DWARFContext::getRecoverableErrorHandler
function_ref< void(Error)> getRecoverableErrorHandler()
Definition: DWARFContext.h:406
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1541
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:599
llvm::DWARFContext::getDWARFObj
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:130
CU
Definition: AArch64AsmBackend.cpp:504