LLVM  9.0.0svn
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 "llvm/Support/WithColor.h"
15 #include <algorithm>
16 #include <cassert>
17 #include <cstdint>
18 #include <set>
19 #include <vector>
20 
21 using namespace llvm;
22 
23 void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
24  if (!DebugArangesData.isValidOffset(0))
25  return;
26  uint32_t Offset = 0;
28 
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);
35  }
36  ParsedCUOffsets.insert(CUOffset);
37  }
38 }
39 
41  clear();
42  if (!CTX)
43  return;
44 
45  // Extract aranges from .debug_aranges section.
46  DataExtractor ArangesData(CTX->getDWARFObj().getARangeSection(),
47  CTX->isLittleEndian(), 0);
48  extract(ArangesData);
49 
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();
57  if (!CURanges)
58  WithColor::error() << toString(CURanges.takeError()) << '\n';
59  else
60  for (const auto &R : *CURanges)
61  appendRange(CUOffset, R.LowPC, R.HighPC);
62  }
63  }
64 
65  construct();
66 }
67 
68 void DWARFDebugAranges::clear() {
69  Endpoints.clear();
70  Aranges.clear();
71  ParsedCUOffsets.clear();
72 }
73 
74 void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
75  uint64_t HighPC) {
76  if (LowPC >= HighPC)
77  return;
78  Endpoints.emplace_back(LowPC, CUOffset, true);
79  Endpoints.emplace_back(HighPC, CUOffset, false);
80 }
81 
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);
95  } else {
96  Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
97  }
98  }
99  // Update the set of valid CUs.
100  if (E.IsRangeStart) {
101  ValidCUs.insert(E.CUOffset);
102  } else {
103  auto CUPos = ValidCUs.find(E.CUOffset);
104  assert(CUPos != ValidCUs.end());
105  ValidCUs.erase(CUPos);
106  }
107  PrevAddress = E.Address;
108  }
109  assert(ValidCUs.empty());
110 
111  // Endpoints are not needed now.
112  Endpoints.clear();
113  Endpoints.shrink_to_fit();
114 }
115 
117  RangeCollIterator It =
118  llvm::bsearch(Aranges, [=](Range RHS) { return Address < RHS.HighPC(); });
119  if (It != Aranges.end() && It->LowPC <= Address)
120  return It->CUOffset;
121  return -1U;
122 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
desc_iterator_range descriptors() const
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:60
uint32_t findAddress(uint64_t Address) const
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
void generate(DWARFContext *CTX)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:966
unit_iterator_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:152
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
virtual StringRef getARangeSection() const
Definition: DWARFObject.h:42
size_t bsearch(size_t Lo, size_t Hi, Predicate P)
Binary search for the first index where a predicate is true.
Definition: STLExtras.h:1329
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:187
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1115
bool extract(DataExtractor data, uint32_t *offset_ptr)
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:58
bool isLittleEndian() const
Definition: DWARFContext.h:334
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:116
bool isValidOffset(uint32_t offset) const
Test the validity of offset.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint32_t getCompileUnitDIEOffset() const