Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp
Line
Count
Source (jump to first uncovered line)
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
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"
15
#include <algorithm>
16
#include <cassert>
17
#include <cstdint>
18
#include <set>
19
#include <vector>
20
21
using namespace llvm;
22
23
208
void DWARFDebugAranges::extract(DataExtractor DebugArangesData) {
24
208
  if (!DebugArangesData.isValidOffset(0))
25
164
    return;
26
44
  uint32_t Offset = 0;
27
44
  DWARFDebugArangeSet Set;
28
44
29
93
  while (Set.extract(DebugArangesData, &Offset)) {
30
49
    uint32_t CUOffset = Set.getCompileUnitDIEOffset();
31
51
    for (const auto &Desc : Set.descriptors()) {
32
51
      uint64_t LowPC = Desc.Address;
33
51
      uint64_t HighPC = Desc.getEndAddress();
34
51
      appendRange(CUOffset, LowPC, HighPC);
35
51
    }
36
49
    ParsedCUOffsets.insert(CUOffset);
37
49
  }
38
44
}
39
40
208
void DWARFDebugAranges::generate(DWARFContext *CTX) {
41
208
  clear();
42
208
  if (!CTX)
43
0
    return;
44
208
45
208
  // Extract aranges from .debug_aranges section.
46
208
  DataExtractor ArangesData(CTX->getDWARFObj().getARangeSection(),
47
208
                            CTX->isLittleEndian(), 0);
48
208
  extract(ArangesData);
49
208
50
208
  // Generate aranges from DIEs: even if .debug_aranges section is present,
51
208
  // it may describe only a small subset of compilation units, so we need to
52
208
  // manually build aranges for the rest of them.
53
341
  for (const auto &CU : CTX->compile_units()) {
54
341
    uint32_t CUOffset = CU->getOffset();
55
341
    if (ParsedCUOffsets.insert(CUOffset).second) {
56
292
      Expected<DWARFAddressRangesVector> CURanges = CU->collectAddressRanges();
57
292
      if (!CURanges)
58
2
        WithColor::error() << toString(CURanges.takeError()) << '\n';
59
290
      else
60
290
        for (const auto &R : *CURanges)
61
617
          appendRange(CUOffset, R.LowPC, R.HighPC);
62
292
    }
63
341
  }
64
208
65
208
  construct();
66
208
}
67
68
208
void DWARFDebugAranges::clear() {
69
208
  Endpoints.clear();
70
208
  Aranges.clear();
71
208
  ParsedCUOffsets.clear();
72
208
}
73
74
void DWARFDebugAranges::appendRange(uint32_t CUOffset, uint64_t LowPC,
75
668
                                    uint64_t HighPC) {
76
668
  if (LowPC >= HighPC)
77
0
    return;
78
668
  Endpoints.emplace_back(LowPC, CUOffset, true);
79
668
  Endpoints.emplace_back(HighPC, CUOffset, false);
80
668
}
81
82
208
void DWARFDebugAranges::construct() {
83
208
  std::multiset<uint32_t> ValidCUs;  // Maintain the set of CUs describing
84
208
                                     // a current address range.
85
208
  llvm::sort(Endpoints);
86
208
  uint64_t PrevAddress = -1ULL;
87
1.33k
  for (const auto &E : Endpoints) {
88
1.33k
    if (PrevAddress < E.Address && 
!ValidCUs.empty()1.00k
) {
89
616
      // If the address range between two endpoints is described by some
90
616
      // CU, first try to extend the last range in Aranges. If we can't
91
616
      // do it, start a new range.
92
616
      if (!Aranges.empty() && 
Aranges.back().HighPC() == PrevAddress427
&&
93
616
          
ValidCUs.find(Aranges.back().CUOffset) != ValidCUs.end()41
) {
94
30
        Aranges.back().setHighPC(E.Address);
95
586
      } else {
96
586
        Aranges.emplace_back(PrevAddress, E.Address, *ValidCUs.begin());
97
586
      }
98
616
    }
99
1.33k
    // Update the set of valid CUs.
100
1.33k
    if (E.IsRangeStart) {
101
668
      ValidCUs.insert(E.CUOffset);
102
668
    } else {
103
668
      auto CUPos = ValidCUs.find(E.CUOffset);
104
668
      assert(CUPos != ValidCUs.end());
105
668
      ValidCUs.erase(CUPos);
106
668
    }
107
1.33k
    PrevAddress = E.Address;
108
1.33k
  }
109
208
  assert(ValidCUs.empty());
110
208
111
208
  // Endpoints are not needed now.
112
208
  Endpoints.clear();
113
208
  Endpoints.shrink_to_fit();
114
208
}
115
116
1.15k
uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
117
1.15k
  RangeCollIterator It =
118
1.18k
      partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
119
1.15k
  if (It != Aranges.end() && 
It->LowPC <= Address1.01k
)
120
885
    return It->CUOffset;
121
266
  return -1U;
122
266
}