Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DWARFDebugLoc.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/DWARFDebugLoc.h"
10
#include "llvm/ADT/StringRef.h"
11
#include "llvm/BinaryFormat/Dwarf.h"
12
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
13
#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
14
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
15
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16
#include "llvm/Support/Compiler.h"
17
#include "llvm/Support/Format.h"
18
#include "llvm/Support/WithColor.h"
19
#include "llvm/Support/raw_ostream.h"
20
#include <algorithm>
21
#include <cinttypes>
22
#include <cstdint>
23
24
using namespace llvm;
25
26
// When directly dumping the .debug_loc without a compile unit, we have to guess
27
// at the DWARF version. This only affects DW_OP_call_ref, which is a rare
28
// expression that LLVM doesn't produce. Guessing the wrong version means we
29
// won't be able to pretty print expressions in DWARF2 binaries produced by
30
// non-LLVM tools.
31
static void dumpExpression(raw_ostream &OS, ArrayRef<char> Data,
32
                           bool IsLittleEndian, unsigned AddressSize,
33
487
                           const MCRegisterInfo *MRI, DWARFUnit *U) {
34
487
  DWARFDataExtractor Extractor(StringRef(Data.data(), Data.size()),
35
487
                               IsLittleEndian, AddressSize);
36
487
  DWARFExpression(Extractor, dwarf::DWARF_VERSION, AddressSize).print(OS, MRI, U);
37
487
}
38
39
void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian,
40
                                       unsigned AddressSize,
41
                                       const MCRegisterInfo *MRI,
42
                                       DWARFUnit *U,
43
                                       uint64_t BaseAddress,
44
274
                                       unsigned Indent) const {
45
429
  for (const Entry &E : Entries) {
46
429
    OS << '\n';
47
429
    OS.indent(Indent);
48
429
    OS << format("[0x%*.*" PRIx64 ", ", AddressSize * 2, AddressSize * 2,
49
429
                 BaseAddress + E.Begin);
50
429
    OS << format(" 0x%*.*" PRIx64 ")", AddressSize * 2, AddressSize * 2,
51
429
                 BaseAddress + E.End);
52
429
    OS << ": ";
53
429
54
429
    dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U);
55
429
  }
56
274
}
57
58
DWARFDebugLoc::LocationList const *
59
44
DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
60
44
  auto It = partition_point(
61
87
      Locations, [=](const LocationList &L) { return L.Offset < Offset; });
62
44
  if (It != Locations.end() && 
It->Offset == Offset31
)
63
31
    return &(*It);
64
13
  return nullptr;
65
13
}
66
67
void DWARFDebugLoc::dump(raw_ostream &OS, const MCRegisterInfo *MRI,
68
48
                         Optional<uint64_t> Offset) const {
69
84
  auto DumpLocationList = [&](const LocationList &L) {
70
84
    OS << format("0x%8.8x: ", L.Offset);
71
84
    L.dump(OS, IsLittleEndian, AddressSize, MRI, nullptr, 0, 12);
72
84
    OS << "\n\n";
73
84
  };
74
48
75
48
  if (Offset) {
76
2
    if (auto *L = getLocationListAtOffset(*Offset))
77
2
      DumpLocationList(*L);
78
2
    return;
79
2
  }
80
46
81
82
  
for (const LocationList &L : Locations)46
{
82
82
    DumpLocationList(L);
83
82
  }
84
46
}
85
86
Optional<DWARFDebugLoc::LocationList>
87
304
DWARFDebugLoc::parseOneLocationList(DWARFDataExtractor Data, unsigned *Offset) {
88
304
  LocationList LL;
89
304
  LL.Offset = *Offset;
90
304
91
304
  // 2.6.2 Location Lists
92
304
  // A location list entry consists of:
93
781
  while (true) {
94
781
    Entry E;
95
781
    if (!Data.isValidOffsetForDataOfSize(*Offset, 2 * Data.getAddressSize())) {
96
0
      WithColor::error() << "location list overflows the debug_loc section.\n";
97
0
      return None;
98
0
    }
99
781
100
781
    // 1. A beginning address offset. ...
101
781
    E.Begin = Data.getRelocatedAddress(Offset);
102
781
103
781
    // 2. An ending address offset. ...
104
781
    E.End = Data.getRelocatedAddress(Offset);
105
781
106
781
    // The end of any given location list is marked by an end of list entry,
107
781
    // which consists of a 0 for the beginning address offset and a 0 for the
108
781
    // ending address offset.
109
781
    if (E.Begin == 0 && 
E.End == 0419
)
110
304
      return LL;
111
477
112
477
    if (!Data.isValidOffsetForDataOfSize(*Offset, 2)) {
113
0
      WithColor::error() << "location list overflows the debug_loc section.\n";
114
0
      return None;
115
0
    }
116
477
117
477
    unsigned Bytes = Data.getU16(Offset);
118
477
    if (!Data.isValidOffsetForDataOfSize(*Offset, Bytes)) {
119
0
      WithColor::error() << "location list overflows the debug_loc section.\n";
120
0
      return None;
121
0
    }
122
477
    // A single location description describing the location of the object...
123
477
    StringRef str = Data.getData().substr(*Offset, Bytes);
124
477
    *Offset += Bytes;
125
477
    E.Loc.reserve(str.size());
126
477
    llvm::copy(str, std::back_inserter(E.Loc));
127
477
    LL.Entries.push_back(std::move(E));
128
477
  }
129
304
}
130
131
56
void DWARFDebugLoc::parse(const DWARFDataExtractor &data) {
132
56
  IsLittleEndian = data.isLittleEndian();
133
56
  AddressSize = data.getAddressSize();
134
56
135
56
  uint32_t Offset = 0;
136
170
  while (data.isValidOffset(Offset + data.getAddressSize() - 1)) {
137
114
    if (auto LL = parseOneLocationList(data, &Offset))
138
114
      Locations.push_back(std::move(*LL));
139
0
    else
140
0
      break;
141
114
  }
142
56
  if (data.isValidOffset(Offset))
143
0
    WithColor::error() << "failed to consume entire .debug_loc section\n";
144
56
}
145
146
Optional<DWARFDebugLoclists::LocationList>
147
DWARFDebugLoclists::parseOneLocationList(DataExtractor Data, unsigned *Offset,
148
28
                                         unsigned Version) {
149
28
  LocationList LL;
150
28
  LL.Offset = *Offset;
151
28
152
28
  // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list.
153
86
  while (auto Kind =
154
58
             static_cast<dwarf::LocationListEntry>(Data.getU8(Offset))) {
155
58
156
58
    Entry E;
157
58
    E.Kind = Kind;
158
58
    switch (Kind) {
159
58
    case dwarf::DW_LLE_startx_length:
160
28
      E.Value0 = Data.getULEB128(Offset);
161
28
      // Pre-DWARF 5 has different interpretation of the length field. We have
162
28
      // to support both pre- and standartized styles for the compatibility.
163
28
      if (Version < 5)
164
27
        E.Value1 = Data.getU32(Offset);
165
1
      else
166
1
        E.Value1 = Data.getULEB128(Offset);
167
28
      break;
168
58
    case dwarf::DW_LLE_start_length:
169
2
      E.Value0 = Data.getAddress(Offset);
170
2
      E.Value1 = Data.getULEB128(Offset);
171
2
      break;
172
58
    case dwarf::DW_LLE_offset_pair:
173
26
      E.Value0 = Data.getULEB128(Offset);
174
26
      E.Value1 = Data.getULEB128(Offset);
175
26
      break;
176
58
    case dwarf::DW_LLE_base_address:
177
2
      E.Value0 = Data.getAddress(Offset);
178
2
      break;
179
58
    default:
180
0
      WithColor::error() << "dumping support for LLE of kind " << (int)Kind
181
0
                         << " not implemented\n";
182
0
      return None;
183
58
    }
184
58
185
58
    if (Kind != dwarf::DW_LLE_base_address) {
186
56
      unsigned Bytes =
187
56
          Version >= 5 ? 
Data.getULEB128(Offset)29
:
Data.getU16(Offset)27
;
188
56
      // A single location description describing the location of the object...
189
56
      StringRef str = Data.getData().substr(*Offset, Bytes);
190
56
      *Offset += Bytes;
191
56
      E.Loc.resize(str.size());
192
56
      llvm::copy(str, E.Loc.begin());
193
56
    }
194
58
195
58
    LL.Entries.push_back(std::move(E));
196
58
  }
197
28
  return LL;
198
28
}
199
200
8
void DWARFDebugLoclists::parse(DataExtractor data, unsigned Version) {
201
8
  IsLittleEndian = data.isLittleEndian();
202
8
  AddressSize = data.getAddressSize();
203
8
204
8
  uint32_t Offset = 0;
205
23
  while (data.isValidOffset(Offset)) {
206
15
    if (auto LL = parseOneLocationList(data, &Offset, Version))
207
15
      Locations.push_back(std::move(*LL));
208
0
    else
209
0
      return;
210
15
  }
211
8
}
212
213
DWARFDebugLoclists::LocationList const *
214
0
DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
215
0
  auto It = partition_point(
216
0
      Locations, [=](const LocationList &L) { return L.Offset < Offset; });
217
0
  if (It != Locations.end() && It->Offset == Offset)
218
0
    return &(*It);
219
0
  return nullptr;
220
0
}
221
222
void DWARFDebugLoclists::LocationList::dump(raw_ostream &OS, uint64_t BaseAddr,
223
                                            bool IsLittleEndian,
224
                                            unsigned AddressSize,
225
                                            const MCRegisterInfo *MRI,
226
                                            DWARFUnit *U,
227
28
                                            unsigned Indent) const {
228
58
  for (const Entry &E : Entries) {
229
58
    switch (E.Kind) {
230
58
    case dwarf::DW_LLE_startx_length:
231
28
      OS << '\n';
232
28
      OS.indent(Indent);
233
28
      OS << "Addr idx " << E.Value0 << " (w/ length " << E.Value1 << "): ";
234
28
      break;
235
58
    case dwarf::DW_LLE_start_length:
236
2
      OS << '\n';
237
2
      OS.indent(Indent);
238
2
      OS << format("[0x%*.*" PRIx64 ", 0x%*.*" PRIx64 "): ", AddressSize * 2,
239
2
                   AddressSize * 2, E.Value0, AddressSize * 2, AddressSize * 2,
240
2
                   E.Value0 + E.Value1);
241
2
      break;
242
58
    case dwarf::DW_LLE_offset_pair:
243
26
      OS << '\n';
244
26
      OS.indent(Indent);
245
26
      OS << format("[0x%*.*" PRIx64 ", 0x%*.*" PRIx64 "): ", AddressSize * 2,
246
26
                   AddressSize * 2, BaseAddr + E.Value0, AddressSize * 2,
247
26
                   AddressSize * 2, BaseAddr + E.Value1);
248
26
      break;
249
58
    case dwarf::DW_LLE_base_address:
250
2
      BaseAddr = E.Value0;
251
2
      break;
252
58
    default:
253
0
      llvm_unreachable("unreachable locations list kind");
254
58
    }
255
58
256
58
    dumpExpression(OS, E.Loc, IsLittleEndian, AddressSize, MRI, U);
257
58
  }
258
28
}
259
260
void DWARFDebugLoclists::dump(raw_ostream &OS, uint64_t BaseAddr,
261
                              const MCRegisterInfo *MRI,
262
8
                              Optional<uint64_t> Offset) const {
263
15
  auto DumpLocationList = [&](const LocationList &L) {
264
15
    OS << format("0x%8.8x: ", L.Offset);
265
15
    L.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, nullptr, /*Indent=*/12);
266
15
    OS << "\n\n";
267
15
  };
268
8
269
8
  if (Offset) {
270
0
    if (auto *L = getLocationListAtOffset(*Offset))
271
0
      DumpLocationList(*L);
272
0
    return;
273
0
  }
274
8
275
15
  
for (const LocationList &L : Locations)8
{
276
15
    DumpLocationList(L);
277
15
  }
278
8
}