Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/DebugInfo/DWARF/DWARFGdbIndex.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DWARFGdbIndex.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/DWARFGdbIndex.h"
10
#include "llvm/ADT/SmallVector.h"
11
#include "llvm/ADT/StringRef.h"
12
#include "llvm/Support/Format.h"
13
#include "llvm/Support/FormatVariadic.h"
14
#include "llvm/Support/raw_ostream.h"
15
#include <algorithm>
16
#include <cassert>
17
#include <cinttypes>
18
#include <cstdint>
19
#include <utility>
20
21
using namespace llvm;
22
23
// .gdb_index section format reference:
24
// https://sourceware.org/gdb/onlinedocs/gdb/Index-Section-Format.html
25
26
12
void DWARFGdbIndex::dumpCUList(raw_ostream &OS) const {
27
12
  OS << format("\n  CU list offset = 0x%x, has %" PRId64 " entries:",
28
12
               CuListOffset, (uint64_t)CuList.size())
29
12
     << '\n';
30
12
  uint32_t I = 0;
31
12
  for (const CompUnitEntry &CU : CuList)
32
18
    OS << format("    %d: Offset = 0x%llx, Length = 0x%llx\n", I++, CU.Offset,
33
18
                 CU.Length);
34
12
}
35
36
12
void DWARFGdbIndex::dumpTUList(raw_ostream &OS) const {
37
12
  OS << formatv("\n  Types CU list offset = {0:x}, has {1} entries:\n",
38
12
                TuListOffset, TuList.size());
39
12
  uint32_t I = 0;
40
12
  for (const TypeUnitEntry &TU : TuList)
41
0
    OS << formatv("    {0}: offset = {1:x8}, type_offset = {2:x8}, "
42
0
                  "type_signature = {3:x16}\n",
43
0
                  I++, TU.Offset, TU.TypeOffset, TU.TypeSignature);
44
12
}
45
46
12
void DWARFGdbIndex::dumpAddressArea(raw_ostream &OS) const {
47
12
  OS << format("\n  Address area offset = 0x%x, has %" PRId64 " entries:",
48
12
               AddressAreaOffset, (uint64_t)AddressArea.size())
49
12
     << '\n';
50
12
  for (const AddressEntry &Addr : AddressArea)
51
15
    OS << format(
52
15
        "    Low/High address = [0x%llx, 0x%llx) (Size: 0x%llx), CU id = %d\n",
53
15
        Addr.LowAddress, Addr.HighAddress, Addr.HighAddress - Addr.LowAddress,
54
15
        Addr.CuIndex);
55
12
}
56
57
12
void DWARFGdbIndex::dumpSymbolTable(raw_ostream &OS) const {
58
12
  OS << format("\n  Symbol table offset = 0x%x, size = %" PRId64
59
12
               ", filled slots:",
60
12
               SymbolTableOffset, (uint64_t)SymbolTable.size())
61
12
     << '\n';
62
12
  uint32_t I = -1;
63
12.2k
  for (const SymTableEntry &E : SymbolTable) {
64
12.2k
    ++I;
65
12.2k
    if (!E.NameOffset && 
!E.VecOffset12.2k
)
66
12.2k
      continue;
67
12
68
12
    OS << format("    %d: Name offset = 0x%x, CU vector offset = 0x%x\n", I,
69
12
                 E.NameOffset, E.VecOffset);
70
12
71
12
    StringRef Name = ConstantPoolStrings.substr(
72
12
        ConstantPoolOffset - StringPoolOffset + E.NameOffset);
73
12
74
12
    auto CuVector = std::find_if(
75
12
        ConstantPoolVectors.begin(), ConstantPoolVectors.end(),
76
22
        [&](const std::pair<uint32_t, SmallVector<uint32_t, 0>> &V) {
77
22
          return V.first == E.VecOffset;
78
22
        });
79
12
    assert(CuVector != ConstantPoolVectors.end() && "Invalid symbol table");
80
12
    uint32_t CuVectorId = CuVector - ConstantPoolVectors.begin();
81
12
    OS << format("      String name: %s, CU vector index: %d\n", Name.data(),
82
12
                 CuVectorId);
83
12
  }
84
12
}
85
86
12
void DWARFGdbIndex::dumpConstantPool(raw_ostream &OS) const {
87
12
  OS << format("\n  Constant pool offset = 0x%x, has %" PRId64 " CU vectors:",
88
12
               ConstantPoolOffset, (uint64_t)ConstantPoolVectors.size());
89
12
  uint32_t I = 0;
90
12
  for (const auto &V : ConstantPoolVectors) {
91
12
    OS << format("\n    %d(0x%x): ", I++, V.first);
92
12
    for (uint32_t Val : V.second)
93
15
      OS << format("0x%x ", Val);
94
12
  }
95
12
  OS << '\n';
96
12
}
97
98
12
void DWARFGdbIndex::dump(raw_ostream &OS) {
99
12
  if (HasError) {
100
0
    OS << "\n<error parsing>\n";
101
0
    return;
102
0
  }
103
12
104
12
  if (HasContent) {
105
12
    OS << "  Version = " << Version << '\n';
106
12
    dumpCUList(OS);
107
12
    dumpTUList(OS);
108
12
    dumpAddressArea(OS);
109
12
    dumpSymbolTable(OS);
110
12
    dumpConstantPool(OS);
111
12
  }
112
12
}
113
114
12
bool DWARFGdbIndex::parseImpl(DataExtractor Data) {
115
12
  uint32_t Offset = 0;
116
12
117
12
  // Only version 7 is supported at this moment.
118
12
  Version = Data.getU32(&Offset);
119
12
  if (Version != 7)
120
0
    return false;
121
12
122
12
  CuListOffset = Data.getU32(&Offset);
123
12
  TuListOffset = Data.getU32(&Offset);
124
12
  AddressAreaOffset = Data.getU32(&Offset);
125
12
  SymbolTableOffset = Data.getU32(&Offset);
126
12
  ConstantPoolOffset = Data.getU32(&Offset);
127
12
128
12
  if (Offset != CuListOffset)
129
0
    return false;
130
12
131
12
  uint32_t CuListSize = (TuListOffset - CuListOffset) / 16;
132
12
  CuList.reserve(CuListSize);
133
30
  for (uint32_t i = 0; i < CuListSize; 
++i18
) {
134
18
    uint64_t CuOffset = Data.getU64(&Offset);
135
18
    uint64_t CuLength = Data.getU64(&Offset);
136
18
    CuList.push_back({CuOffset, CuLength});
137
18
  }
138
12
139
12
  // CU Types are no longer needed as DWARF skeleton type units never made it
140
12
  // into the standard.
141
12
  uint32_t TuListSize = (AddressAreaOffset - TuListOffset) / 24;
142
12
  TuList.resize(TuListSize);
143
12
  for (uint32_t I = 0; I < TuListSize; 
++I0
) {
144
0
    uint64_t CuOffset = Data.getU64(&Offset);
145
0
    uint64_t TypeOffset = Data.getU64(&Offset);
146
0
    uint64_t Signature = Data.getU64(&Offset);
147
0
    TuList[I] = {CuOffset, TypeOffset, Signature};
148
0
  }
149
12
150
12
  uint32_t AddressAreaSize = (SymbolTableOffset - AddressAreaOffset) / 20;
151
12
  AddressArea.reserve(AddressAreaSize);
152
27
  for (uint32_t i = 0; i < AddressAreaSize; 
++i15
) {
153
15
    uint64_t LowAddress = Data.getU64(&Offset);
154
15
    uint64_t HighAddress = Data.getU64(&Offset);
155
15
    uint32_t CuIndex = Data.getU32(&Offset);
156
15
    AddressArea.push_back({LowAddress, HighAddress, CuIndex});
157
15
  }
158
12
159
12
  // The symbol table. This is an open addressed hash table. The size of the
160
12
  // hash table is always a power of 2.
161
12
  // Each slot in the hash table consists of a pair of offset_type values. The
162
12
  // first value is the offset of the symbol's name in the constant pool. The
163
12
  // second value is the offset of the CU vector in the constant pool.
164
12
  // If both values are 0, then this slot in the hash table is empty. This is ok
165
12
  // because while 0 is a valid constant pool index, it cannot be a valid index
166
12
  // for both a string and a CU vector.
167
12
  uint32_t SymTableSize = (ConstantPoolOffset - SymbolTableOffset) / 8;
168
12
  SymbolTable.reserve(SymTableSize);
169
12
  uint32_t CuVectorsTotal = 0;
170
12.3k
  for (uint32_t i = 0; i < SymTableSize; 
++i12.2k
) {
171
12.2k
    uint32_t NameOffset = Data.getU32(&Offset);
172
12.2k
    uint32_t CuVecOffset = Data.getU32(&Offset);
173
12.2k
    SymbolTable.push_back({NameOffset, CuVecOffset});
174
12.2k
    if (NameOffset || 
CuVecOffset12.2k
)
175
12
      ++CuVectorsTotal;
176
12.2k
  }
177
12
178
12
  // The constant pool. CU vectors are stored first, followed by strings.
179
12
  // The first value is the number of CU indices in the vector. Each subsequent
180
12
  // value is the index and symbol attributes of a CU in the CU list.
181
24
  for (uint32_t i = 0; i < CuVectorsTotal; 
++i12
) {
182
12
    ConstantPoolVectors.emplace_back(0, SmallVector<uint32_t, 0>());
183
12
    auto &Vec = ConstantPoolVectors.back();
184
12
    Vec.first = Offset - ConstantPoolOffset;
185
12
186
12
    uint32_t Num = Data.getU32(&Offset);
187
27
    for (uint32_t j = 0; j < Num; 
++j15
)
188
15
      Vec.second.push_back(Data.getU32(&Offset));
189
12
  }
190
12
191
12
  ConstantPoolStrings = Data.getData().drop_front(Offset);
192
12
  StringPoolOffset = Offset;
193
12
  return true;
194
12
}
195
196
12
void DWARFGdbIndex::parse(DataExtractor Data) {
197
12
  HasContent = !Data.getData().empty();
198
12
  HasError = HasContent && !parseImpl(Data);
199
12
}