Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- DWARFUnit.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 "DWARFUnit.h"
10
11
#include "lldb/Core/Module.h"
12
#include "lldb/Symbol/ObjectFile.h"
13
#include "lldb/Utility/LLDBAssert.h"
14
#include "lldb/Utility/StreamString.h"
15
#include "lldb/Utility/Timer.h"
16
#include "llvm/Object/Error.h"
17
18
#include "DWARFCompileUnit.h"
19
#include "DWARFDebugAranges.h"
20
#include "DWARFDebugInfo.h"
21
#include "DWARFTypeUnit.h"
22
#include "LogChannelDWARF.h"
23
#include "SymbolFileDWARFDwo.h"
24
25
using namespace lldb;
26
using namespace lldb_private;
27
using namespace std;
28
29
extern int g_verbose;
30
31
DWARFUnit::DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
32
                     const DWARFUnitHeader &header,
33
                     const DWARFAbbreviationDeclarationSet &abbrevs,
34
                     DIERef::Section section, bool is_dwo)
35
    : UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs),
36
      m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
37
22.4k
      m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.GetDWOId()) {}
38
39
22.0k
DWARFUnit::~DWARFUnit() = default;
40
41
// Parses first DIE of a compile unit, excluding DWO.
42
938k
void DWARFUnit::ExtractUnitDIENoDwoIfNeeded() {
43
938k
  {
44
938k
    llvm::sys::ScopedReader lock(m_first_die_mutex);
45
938k
    if (m_first_die)
46
916k
      return; // Already parsed
47
938k
  }
48
21.4k
  llvm::sys::ScopedWriter lock(m_first_die_mutex);
49
21.4k
  if (m_first_die)
50
0
    return; // Already parsed
51
52
21.4k
  ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef());
53
21.4k
  LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractUnitDIENoDwoIfNeeded()",
54
21.4k
                     GetOffset());
55
56
  // Set the offset to that of the first DIE and calculate the start of the
57
  // next compilation unit header.
58
21.4k
  lldb::offset_t offset = GetFirstDIEOffset();
59
60
  // We are in our compile unit, parse starting at the offset we were told to
61
  // parse
62
21.4k
  const DWARFDataExtractor &data = GetData();
63
21.4k
  if (offset < GetNextUnitOffset() &&
64
21.4k
      m_first_die.Extract(data, this, &offset)) {
65
21.4k
    AddUnitDIE(m_first_die);
66
21.4k
    return;
67
21.4k
  }
68
21.4k
}
69
70
// Parses first DIE of a compile unit including DWO.
71
874k
void DWARFUnit::ExtractUnitDIEIfNeeded() {
72
874k
  ExtractUnitDIENoDwoIfNeeded();
73
74
874k
  if (m_has_parsed_non_skeleton_unit)
75
863k
    return;
76
77
10.9k
  m_has_parsed_non_skeleton_unit = true;
78
79
10.9k
  std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
80
10.9k
      m_dwarf.GetDwoSymbolFileForCompileUnit(*this, m_first_die);
81
10.9k
  if (!dwo_symbol_file)
82
10.7k
    return;
83
84
150
  DWARFUnit *dwo_cu = dwo_symbol_file->GetDWOCompileUnitForHash(m_dwo_id);
85
86
150
  if (!dwo_cu)
87
127
    return; // Can't fetch the compile unit from the dwo file.
88
23
  dwo_cu->SetUserData(this);
89
90
23
  DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
91
23
  if (!dwo_cu_die.IsValid())
92
0
    return; // Can't fetch the compile unit DIE from the dwo file.
93
94
  // Here for DWO CU we want to use the address base set in the skeleton unit
95
  // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base
96
  // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_*
97
  // attributes which were applicable to the DWO units. The corresponding
98
  // DW_AT_* attributes standardized in DWARF v5 are also applicable to the
99
  // main unit in contrast.
100
23
  if (m_addr_base)
101
11
    dwo_cu->SetAddrBase(*m_addr_base);
102
12
  else if (m_gnu_addr_base)
103
14
    dwo_cu->SetAddrBase(*m_gnu_addr_base);
104
105
23
  if (GetVersion() <= 4 && 
m_gnu_ranges_base13
)
106
0
    dwo_cu->SetRangesBase(*m_gnu_ranges_base);
107
23
  else if (dwo_symbol_file->GetDWARFContext()
108
23
               .getOrLoadRngListsData()
109
23
               .GetByteSize() > 0)
110
4
    dwo_cu->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
111
112
23
  if (GetVersion() >= 5 &&
113
23
      dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
114
12
          0)
115
2
    dwo_cu->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
116
117
23
  dwo_cu->SetBaseAddress(GetBaseAddress());
118
119
23
  m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
120
23
}
121
122
// Parses a compile unit and indexes its DIEs if it hasn't already been done.
123
// It will leave this compile unit extracted forever.
124
1.25M
void DWARFUnit::ExtractDIEsIfNeeded() {
125
1.25M
  m_cancel_scopes = true;
126
127
1.25M
  {
128
1.25M
    llvm::sys::ScopedReader lock(m_die_array_mutex);
129
1.25M
    if (!m_die_array.empty())
130
1.24M
      return; // Already parsed
131
1.25M
  }
132
3.56k
  llvm::sys::ScopedWriter lock(m_die_array_mutex);
133
3.56k
  if (!m_die_array.empty())
134
0
    return; // Already parsed
135
136
3.56k
  ExtractDIEsRWLocked();
137
3.56k
}
138
139
// Parses a compile unit and indexes its DIEs if it hasn't already been done.
140
// It will clear this compile unit after returned instance gets out of scope,
141
// no other ScopedExtractDIEs instance is running for this compile unit
142
// and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs
143
// lifetime.
144
5.68k
DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() {
145
5.68k
  ScopedExtractDIEs scoped(*this);
146
147
5.68k
  {
148
5.68k
    llvm::sys::ScopedReader lock(m_die_array_mutex);
149
5.68k
    if (!m_die_array.empty())
150
5
      return scoped; // Already parsed
151
5.68k
  }
152
5.68k
  llvm::sys::ScopedWriter lock(m_die_array_mutex);
153
5.68k
  if (!m_die_array.empty())
154
0
    return scoped; // Already parsed
155
156
  // Otherwise m_die_array would be already populated.
157
5.68k
  lldbassert(!m_cancel_scopes);
158
159
5.68k
  ExtractDIEsRWLocked();
160
5.68k
  scoped.m_clear_dies = true;
161
5.68k
  return scoped;
162
5.68k
}
163
164
5.68k
DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) {
165
5.68k
  m_cu->m_die_array_scoped_mutex.lock_shared();
166
5.68k
}
167
168
11.3k
DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
169
11.3k
  if (!m_cu)
170
5.68k
    return;
171
5.68k
  m_cu->m_die_array_scoped_mutex.unlock_shared();
172
5.68k
  if (!m_clear_dies || 
m_cu->m_cancel_scopes5.68k
)
173
5.68k
    return;
174
  // Be sure no other ScopedExtractDIEs is running anymore.
175
0
  llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
176
0
  llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
177
0
  if (m_cu->m_cancel_scopes)
178
0
    return;
179
0
  m_cu->ClearDIEsRWLocked();
180
0
}
181
182
DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs)
183
5.68k
    : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
184
5.68k
  rhs.m_cu = nullptr;
185
5.68k
}
186
187
DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=(
188
0
    DWARFUnit::ScopedExtractDIEs &&rhs) {
189
0
  m_cu = rhs.m_cu;
190
0
  rhs.m_cu = nullptr;
191
0
  m_clear_dies = rhs.m_clear_dies;
192
0
  return *this;
193
0
}
194
195
// Parses a compile unit and indexes its DIEs, m_die_array_mutex must be
196
// held R/W and m_die_array must be empty.
197
9.24k
void DWARFUnit::ExtractDIEsRWLocked() {
198
9.24k
  llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex);
199
200
9.24k
  ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef());
201
9.24k
  LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractDIEsIfNeeded()", GetOffset());
202
203
  // Set the offset to that of the first DIE and calculate the start of the
204
  // next compilation unit header.
205
9.24k
  lldb::offset_t offset = GetFirstDIEOffset();
206
9.24k
  lldb::offset_t next_cu_offset = GetNextUnitOffset();
207
208
9.24k
  DWARFDebugInfoEntry die;
209
210
9.24k
  uint32_t depth = 0;
211
  // We are in our compile unit, parse starting at the offset we were told to
212
  // parse
213
9.24k
  const DWARFDataExtractor &data = GetData();
214
9.24k
  std::vector<uint32_t> die_index_stack;
215
9.24k
  die_index_stack.reserve(32);
216
9.24k
  die_index_stack.push_back(0);
217
9.24k
  bool prev_die_had_children = false;
218
6.31M
  while (offset < next_cu_offset && 
die.Extract(data, this, &offset)6.31M
) {
219
6.31M
    const bool null_die = die.IsNULL();
220
6.31M
    if (depth == 0) {
221
9.25k
      assert(m_die_array.empty() && "Compile unit DIE already added");
222
223
      // The average bytes per DIE entry has been seen to be around 14-20 so
224
      // lets pre-reserve half of that since we are now stripping the NULL
225
      // tags.
226
227
      // Only reserve the memory if we are adding children of the main
228
      // compile unit DIE. The compile unit DIE is always the first entry, so
229
      // if our size is 1, then we are adding the first compile unit child
230
      // DIE and should reserve the memory.
231
0
      m_die_array.reserve(GetDebugInfoSize() / 24);
232
9.25k
      m_die_array.push_back(die);
233
234
9.25k
      if (!m_first_die)
235
759
        AddUnitDIE(m_die_array.front());
236
237
      // With -fsplit-dwarf-inlining, clang will emit non-empty skeleton compile
238
      // units. We are not able to access these DIE *and* the dwo file
239
      // simultaneously. We also don't need to do that as the dwo file will
240
      // contain a superset of information. So, we don't even attempt to parse
241
      // any remaining DIEs.
242
9.25k
      if (m_dwo) {
243
2
        m_die_array.front().SetHasChildren(false);
244
2
        break;
245
2
      }
246
247
6.31M
    } else {
248
6.31M
      if (null_die) {
249
1.21M
        if (prev_die_had_children) {
250
          // This will only happen if a DIE says is has children but all it
251
          // contains is a NULL tag. Since we are removing the NULL DIEs from
252
          // the list (saves up to 25% in C++ code), we need a way to let the
253
          // DIE know that it actually doesn't have children.
254
2
          if (!m_die_array.empty())
255
2
            m_die_array.back().SetHasChildren(false);
256
2
        }
257
5.09M
      } else {
258
5.09M
        die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
259
260
5.09M
        if (die_index_stack.back())
261
3.87M
          m_die_array[die_index_stack.back()].SetSiblingIndex(
262
3.87M
              m_die_array.size() - die_index_stack.back());
263
264
        // Only push the DIE if it isn't a NULL DIE
265
5.09M
        m_die_array.push_back(die);
266
5.09M
      }
267
6.31M
    }
268
269
6.31M
    if (null_die) {
270
      // NULL DIE.
271
1.21M
      if (!die_index_stack.empty())
272
1.21M
        die_index_stack.pop_back();
273
274
1.21M
      if (depth > 0)
275
1.21M
        --depth;
276
1.21M
      prev_die_had_children = false;
277
5.10M
    } else {
278
5.10M
      die_index_stack.back() = m_die_array.size() - 1;
279
      // Normal DIE
280
5.10M
      const bool die_has_children = die.HasChildren();
281
5.10M
      if (die_has_children) {
282
1.21M
        die_index_stack.push_back(0);
283
1.21M
        ++depth;
284
1.21M
      }
285
5.10M
      prev_die_had_children = die_has_children;
286
5.10M
    }
287
288
6.31M
    if (depth == 0)
289
9.24k
      break; // We are done with this compile unit!
290
6.31M
  }
291
292
9.24k
  if (!m_die_array.empty()) {
293
    // The last die cannot have children (if it did, it wouldn't be the last one).
294
    // This only makes a difference for malformed dwarf that does not have a
295
    // terminating null die.
296
9.24k
    m_die_array.back().SetHasChildren(false);
297
298
9.24k
    if (m_first_die) {
299
      // Only needed for the assertion.
300
8.48k
      m_first_die.SetHasChildren(m_die_array.front().HasChildren());
301
8.48k
      lldbassert(m_first_die == m_die_array.front());
302
8.48k
    }
303
9.24k
    m_first_die = m_die_array.front();
304
9.24k
  }
305
306
9.24k
  m_die_array.shrink_to_fit();
307
308
9.24k
  if (m_dwo)
309
2
    m_dwo->ExtractDIEsIfNeeded();
310
9.24k
}
311
312
// This is used when a split dwarf is enabled.
313
// A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute
314
// that points to the first string offset of the CU contribution to the
315
// .debug_str_offsets. At the same time, the corresponding split debug unit also
316
// may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and
317
// for that case, we should find the offset (skip the section header).
318
30
void DWARFUnit::SetDwoStrOffsetsBase() {
319
30
  lldb::offset_t baseOffset = 0;
320
321
30
  if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
322
21
    if (const auto *contribution =
323
21
            entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
324
11
      baseOffset = contribution->Offset;
325
10
    else
326
10
      return;
327
21
  }
328
329
20
  if (GetVersion() >= 5) {
330
11
    const DWARFDataExtractor &strOffsets =
331
11
        GetSymbolFileDWARF().GetDWARFContext().getOrLoadStrOffsetsData();
332
11
    uint64_t length = strOffsets.GetU32(&baseOffset);
333
11
    if (length == 0xffffffff)
334
0
      length = strOffsets.GetU64(&baseOffset);
335
336
    // Check version.
337
11
    if (strOffsets.GetU16(&baseOffset) < 5)
338
2
      return;
339
340
    // Skip padding.
341
9
    baseOffset += 2;
342
9
  }
343
344
18
  SetStrOffsetsBase(baseOffset);
345
18
}
346
347
9
uint64_t DWARFUnit::GetDWOId() {
348
9
  ExtractUnitDIENoDwoIfNeeded();
349
9
  return m_dwo_id;
350
9
}
351
352
// m_die_array_mutex must be already held as read/write.
353
22.2k
void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) {
354
22.2k
  DWARFAttributes attributes;
355
22.2k
  size_t num_attributes = cu_die.GetAttributes(this, attributes);
356
357
  // Extract DW_AT_addr_base first, as other attributes may need it.
358
194k
  for (size_t i = 0; i < num_attributes; 
++i171k
) {
359
171k
    if (attributes.AttributeAtIndex(i) != DW_AT_addr_base)
360
171k
      continue;
361
22
    DWARFFormValue form_value;
362
24
    if (
attributes.ExtractFormValueAtIndex(i, form_value)22
) {
363
24
      SetAddrBase(form_value.Unsigned());
364
24
      break;
365
24
    }
366
22
  }
367
368
194k
  for (size_t i = 0; i < num_attributes; 
++i171k
) {
369
171k
    dw_attr_t attr = attributes.AttributeAtIndex(i);
370
171k
    DWARFFormValue form_value;
371
171k
    if (!attributes.ExtractFormValueAtIndex(i, form_value))
372
0
      continue;
373
171k
    switch (attr) {
374
2
    case DW_AT_loclists_base:
375
2
      SetLoclistsBase(form_value.Unsigned());
376
2
      break;
377
15
    case DW_AT_rnglists_base:
378
15
      SetRangesBase(form_value.Unsigned());
379
15
      break;
380
20
    case DW_AT_str_offsets_base:
381
20
      SetStrOffsetsBase(form_value.Unsigned());
382
20
      break;
383
8.79k
    case DW_AT_low_pc:
384
8.79k
      SetBaseAddress(form_value.Address());
385
8.79k
      break;
386
0
    case DW_AT_entry_pc:
387
      // If the value was already set by DW_AT_low_pc, don't update it.
388
0
      if (m_base_addr == LLDB_INVALID_ADDRESS)
389
0
        SetBaseAddress(form_value.Address());
390
0
      break;
391
21.8k
    case DW_AT_stmt_list:
392
21.8k
      m_line_table_offset = form_value.Unsigned();
393
21.8k
      break;
394
18
    case DW_AT_GNU_addr_base:
395
18
      m_gnu_addr_base = form_value.Unsigned();
396
18
      break;
397
0
    case DW_AT_GNU_ranges_base:
398
0
      m_gnu_ranges_base = form_value.Unsigned();
399
0
      break;
400
13.0k
    case DW_AT_GNU_dwo_id:
401
13.0k
      m_dwo_id = form_value.Unsigned();
402
13.0k
      break;
403
171k
    }
404
171k
  }
405
406
22.2k
  if (m_is_dwo) {
407
30
    m_has_parsed_non_skeleton_unit = true;
408
30
    SetDwoStrOffsetsBase();
409
30
    return;
410
30
  }
411
22.2k
}
412
413
9.25k
size_t DWARFUnit::GetDebugInfoSize() const {
414
9.25k
  return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
415
9.25k
}
416
417
7.82M
const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
418
7.82M
  return m_abbrevs;
419
7.82M
}
420
421
0
dw_offset_t DWARFUnit::GetAbbrevOffset() const {
422
0
  return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
423
0
}
424
425
8.36k
dw_offset_t DWARFUnit::GetLineTableOffset() {
426
8.36k
  ExtractUnitDIENoDwoIfNeeded();
427
8.36k
  return m_line_table_offset;
428
8.36k
}
429
430
49
void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; }
431
432
// Parse the rangelist table header, including the optional array of offsets
433
// following it (DWARF v5 and later).
434
template <typename ListTableType>
435
static llvm::Expected<ListTableType>
436
ParseListTableHeader(const llvm::DWARFDataExtractor &data, uint64_t offset,
437
19
                     DwarfFormat format) {
438
  // We are expected to be called with Offset 0 or pointing just past the table
439
  // header. Correct Offset in the latter case so that it points to the start
440
  // of the header.
441
19
  if (offset == 0) {
442
    // This means DW_AT_rnglists_base is missing and therefore DW_FORM_rnglistx
443
    // cannot be handled. Returning a default-constructed ListTableType allows
444
    // DW_FORM_sec_offset to be supported.
445
2
    return ListTableType();
446
2
  }
447
448
17
  uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
449
17
  if (offset < HeaderSize)
450
0
    return llvm::createStringError(errc::invalid_argument,
451
0
                                   "did not detect a valid"
452
0
                                   " list table with base = 0x%" PRIx64 "\n",
453
0
                                   offset);
454
17
  offset -= HeaderSize;
455
17
  ListTableType Table;
456
17
  if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
457
0
    return std::move(E);
458
17
  return Table;
459
17
}
460
461
4
void DWARFUnit::SetLoclistsBase(dw_addr_t loclists_base) {
462
4
  uint64_t offset = 0;
463
4
  if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
464
1
    const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
465
1
    if (!contribution) {
466
0
      GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
467
0
          "Failed to find location list contribution for CU with DWO Id "
468
0
          "0x%" PRIx64,
469
0
          this->GetDWOId());
470
0
      return;
471
0
    }
472
1
    offset += contribution->Offset;
473
1
  }
474
4
  m_loclists_base = loclists_base;
475
476
4
  uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
477
4
  if (loclists_base < header_size)
478
0
    return;
479
480
4
  m_loclist_table_header.emplace(".debug_loclists", "locations");
481
4
  offset += loclists_base - header_size;
482
4
  if (llvm::Error E = m_loclist_table_header->extract(
483
4
          m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(),
484
4
          &offset)) {
485
0
    GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
486
0
        "Failed to extract location list table at offset 0x%" PRIx64
487
0
        " (location list base: 0x%" PRIx64 "): %s",
488
0
        offset, loclists_base, toString(std::move(E)).c_str());
489
0
  }
490
4
}
491
492
std::unique_ptr<llvm::DWARFLocationTable>
493
574
DWARFUnit::GetLocationTable(const DataExtractor &data) const {
494
574
  llvm::DWARFDataExtractor llvm_data(
495
574
      data.GetData(), data.GetByteOrder() == lldb::eByteOrderLittle,
496
574
      data.GetAddressByteSize());
497
498
574
  if (m_is_dwo || 
GetVersion() >= 5560
)
499
34
    return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data, GetVersion());
500
540
  return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
501
574
}
502
503
216
DWARFDataExtractor DWARFUnit::GetLocationData() const {
504
216
  DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
505
216
  const DWARFDataExtractor &data =
506
216
      GetVersion() >= 5 ? 
Ctx.getOrLoadLocListsData()12
:
Ctx.getOrLoadLocData()204
;
507
216
  if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
508
7
    if (const auto *contribution = entry->getContribution(
509
7
            GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
510
7
      return DWARFDataExtractor(data, contribution->Offset,
511
7
                                contribution->Length);
512
0
    return DWARFDataExtractor();
513
7
  }
514
209
  return data;
515
216
}
516
517
75
DWARFDataExtractor DWARFUnit::GetRnglistData() const {
518
75
  DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
519
75
  const DWARFDataExtractor &data = Ctx.getOrLoadRngListsData();
520
75
  if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
521
7
    if (const auto *contribution =
522
7
            entry->getContribution(llvm::DW_SECT_RNGLISTS))
523
7
      return DWARFDataExtractor(data, contribution->Offset,
524
7
                                contribution->Length);
525
0
    GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
526
0
        "Failed to find range list contribution for CU with signature "
527
0
        "0x%" PRIx64,
528
0
        entry->getSignature());
529
530
0
    return DWARFDataExtractor();
531
7
  }
532
68
  return data;
533
75
}
534
535
19
void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) {
536
19
  lldbassert(!m_rnglist_table_done);
537
538
19
  m_ranges_base = ranges_base;
539
19
}
540
541
const llvm::Optional<llvm::DWARFDebugRnglistTable> &
542
118
DWARFUnit::GetRnglistTable() {
543
118
  if (GetVersion() >= 5 && !m_rnglist_table_done) {
544
19
    m_rnglist_table_done = true;
545
19
    if (auto table_or_error =
546
19
            ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
547
19
                GetRnglistData().GetAsLLVM(), m_ranges_base, DWARF32))
548
19
      m_rnglist_table = std::move(table_or_error.get());
549
0
    else
550
0
      GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
551
0
          "Failed to extract range list table at offset 0x%" PRIx64 ": %s",
552
0
          m_ranges_base, toString(table_or_error.takeError()).c_str());
553
19
  }
554
118
  return m_rnglist_table;
555
118
}
556
557
// This function is called only for DW_FORM_rnglistx.
558
28
llvm::Expected<uint64_t> DWARFUnit::GetRnglistOffset(uint32_t Index) {
559
28
  if (!GetRnglistTable())
560
0
    return llvm::createStringError(errc::invalid_argument,
561
0
                                   "missing or invalid range list table");
562
28
  if (!m_ranges_base)
563
3
    return llvm::createStringError(errc::invalid_argument,
564
3
                                   "DW_FORM_rnglistx cannot be used without "
565
3
                                   "DW_AT_rnglists_base for CU at 0x%8.8x",
566
3
                                   GetOffset());
567
25
  if (llvm::Optional<uint64_t> off = GetRnglistTable()->getOffsetEntry(
568
25
          GetRnglistData().GetAsLLVM(), Index))
569
22
    return *off + m_ranges_base;
570
3
  return llvm::createStringError(
571
3
      errc::invalid_argument,
572
3
      "invalid range list table index %u; OffsetEntryCount is %u, "
573
3
      "DW_AT_rnglists_base is %" PRIu64,
574
3
      Index, GetRnglistTable()->getOffsetEntryCount(), m_ranges_base);
575
25
}
576
577
38
void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) {
578
38
  m_str_offsets_base = str_offsets_base;
579
38
}
580
581
// It may be called only with m_die_array_mutex held R/W.
582
0
void DWARFUnit::ClearDIEsRWLocked() {
583
0
  m_die_array.clear();
584
0
  m_die_array.shrink_to_fit();
585
586
0
  if (m_dwo)
587
0
    m_dwo->ClearDIEsRWLocked();
588
0
}
589
590
2
lldb::ByteOrder DWARFUnit::GetByteOrder() const {
591
2
  return m_dwarf.GetObjectFile()->GetByteOrder();
592
2
}
593
594
8.82k
void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
595
596
// Compare function DWARFDebugAranges::Range structures
597
static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
598
11.8M
                             const dw_offset_t die_offset) {
599
11.8M
  return die.GetOffset() < die_offset;
600
11.8M
}
601
602
// GetDIE()
603
//
604
// Get the DIE (Debug Information Entry) with the specified offset by first
605
// checking if the DIE is contained within this compile unit and grabbing the
606
// DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
607
DWARFDIE
608
1.18M
DWARFUnit::GetDIE(dw_offset_t die_offset) {
609
1.18M
  if (die_offset == DW_INVALID_OFFSET)
610
121
    return DWARFDIE(); // Not found
611
612
1.18M
  if (!ContainsDIEOffset(die_offset)) {
613
1
    GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
614
1
        "GetDIE for DIE 0x%" PRIx32 " is outside of its CU 0x%" PRIx32,
615
1
        die_offset, GetOffset());
616
1
    return DWARFDIE(); // Not found
617
1
  }
618
619
1.18M
  ExtractDIEsIfNeeded();
620
1.18M
  DWARFDebugInfoEntry::const_iterator end = m_die_array.cend();
621
1.18M
  DWARFDebugInfoEntry::const_iterator pos =
622
1.18M
      lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset);
623
624
1.18M
  if (pos != end && die_offset == (*pos).GetOffset())
625
1.18M
    return DWARFDIE(this, &(*pos));
626
1
  return DWARFDIE(); // Not found
627
1.18M
}
628
629
869k
DWARFUnit &DWARFUnit::GetNonSkeletonUnit() {
630
869k
  ExtractUnitDIEIfNeeded();
631
869k
  if (m_dwo)
632
93
    return *m_dwo;
633
869k
  return *this;
634
869k
}
635
636
273k
uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
637
273k
  if (cu)
638
273k
    return cu->GetAddressByteSize();
639
0
  return DWARFUnit::GetDefaultAddressSize();
640
273k
}
641
642
0
uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
643
644
767k
void *DWARFUnit::GetUserData() const { return m_user_data; }
645
646
10.6k
void DWARFUnit::SetUserData(void *d) { m_user_data = d; }
647
648
448
bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
649
448
  return GetProducer() != eProducerLLVMGCC;
650
448
}
651
652
0
bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
653
  // llvm-gcc makes completely invalid decl file attributes and won't ever be
654
  // fixed, so we need to know to ignore these.
655
0
  return GetProducer() == eProducerLLVMGCC;
656
0
}
657
658
21
bool DWARFUnit::Supports_unnamed_objc_bitfields() {
659
21
  if (GetProducer() == eProducerClang)
660
21
    return GetProducerVersion() >= llvm::VersionTuple(425, 0, 13);
661
  // Assume all other compilers didn't have incorrect ObjC bitfield info.
662
0
  return true;
663
21
}
664
665
474
void DWARFUnit::ParseProducerInfo() {
666
474
  m_producer = eProducerOther;
667
474
  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
668
474
  if (!die)
669
0
    return;
670
671
474
  llvm::StringRef producer(
672
474
      die->GetAttributeValueAsString(this, DW_AT_producer, nullptr));
673
474
  if (producer.empty())
674
0
    return;
675
676
474
  static const RegularExpression g_swiftlang_version_regex(
677
474
      llvm::StringRef(R"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
678
474
  static const RegularExpression g_clang_version_regex(
679
474
      llvm::StringRef(R"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
680
474
  static const RegularExpression g_llvm_gcc_regex(
681
474
      llvm::StringRef(R"(4\.[012]\.[01] )"
682
474
                      R"(\(Based on Apple Inc\. build [0-9]+\) )"
683
474
                      R"(\(LLVM build [\.0-9]+\)$)"));
684
685
474
  llvm::SmallVector<llvm::StringRef, 3> matches;
686
474
  if (g_swiftlang_version_regex.Execute(producer, &matches)) {
687
1
      m_producer_version.tryParse(matches[1]);
688
1
    m_producer = eProducerSwift;
689
473
  } else if (producer.contains("clang")) {
690
472
    if (g_clang_version_regex.Execute(producer, &matches))
691
44
      m_producer_version.tryParse(matches[1]);
692
472
    m_producer = eProducerClang;
693
472
  } else 
if (1
producer.contains("GNU")1
) {
694
0
    m_producer = eProducerGCC;
695
1
  } else if (g_llvm_gcc_regex.Execute(producer)) {
696
1
    m_producer = eProducerLLVMGCC;
697
1
  }
698
474
}
699
700
472
DWARFProducer DWARFUnit::GetProducer() {
701
472
  if (m_producer == eProducerInvalid)
702
453
    ParseProducerInfo();
703
472
  return m_producer;
704
472
}
705
706
23
llvm::VersionTuple DWARFUnit::GetProducerVersion() {
707
23
  if (m_producer_version.empty())
708
21
    ParseProducerInfo();
709
23
  return m_producer_version;
710
23
}
711
712
877k
uint64_t DWARFUnit::GetDWARFLanguageType() {
713
877k
  if (m_language_type)
714
866k
    return *m_language_type;
715
716
10.9k
  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
717
10.9k
  if (!die)
718
0
    m_language_type = 0;
719
10.9k
  else
720
10.9k
    m_language_type = die->GetAttributeValueAsUnsigned(this, DW_AT_language, 0);
721
10.9k
  return *m_language_type;
722
877k
}
723
724
2.04k
bool DWARFUnit::GetIsOptimized() {
725
2.04k
  if (m_is_optimized == eLazyBoolCalculate) {
726
2.04k
    const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
727
2.04k
    if (die) {
728
2.04k
      m_is_optimized = eLazyBoolNo;
729
2.04k
      if (die->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized, 0) ==
730
2.04k
          1) {
731
48
        m_is_optimized = eLazyBoolYes;
732
48
      }
733
2.04k
    }
734
2.04k
  }
735
2.04k
  return m_is_optimized == eLazyBoolYes;
736
2.04k
}
737
738
15.2k
FileSpec::Style DWARFUnit::GetPathStyle() {
739
15.2k
  if (!m_comp_dir)
740
10.6k
    ComputeCompDirAndGuessPathStyle();
741
15.2k
  return m_comp_dir->GetPathStyle();
742
15.2k
}
743
744
14.9k
const FileSpec &DWARFUnit::GetCompilationDirectory() {
745
14.9k
  if (!m_comp_dir)
746
0
    ComputeCompDirAndGuessPathStyle();
747
14.9k
  return *m_comp_dir;
748
14.9k
}
749
750
0
const FileSpec &DWARFUnit::GetAbsolutePath() {
751
0
  if (!m_file_spec)
752
0
    ComputeAbsolutePath();
753
0
  return *m_file_spec;
754
0
}
755
756
129k
FileSpec DWARFUnit::GetFile(size_t file_idx) {
757
129k
  return m_dwarf.GetFile(*this, file_idx);
758
129k
}
759
760
// DWARF2/3 suggests the form hostname:pathname for compilation directory.
761
// Remove the host part if present.
762
static llvm::StringRef
763
10.6k
removeHostnameFromPathname(llvm::StringRef path_from_dwarf) {
764
10.6k
  if (!path_from_dwarf.contains(':'))
765
10.6k
    return path_from_dwarf;
766
2
  llvm::StringRef host, path;
767
2
  std::tie(host, path) = path_from_dwarf.split(':');
768
769
2
  if (host.contains('/'))
770
0
    return path_from_dwarf;
771
772
  // check whether we have a windows path, and so the first character is a
773
  // drive-letter not a hostname.
774
2
  if (host.size() == 1 && 
llvm::isAlpha(host[0])0
&&
path.startswith("\\")0
)
775
0
    return path_from_dwarf;
776
777
2
  return path;
778
2
}
779
780
10.6k
void DWARFUnit::ComputeCompDirAndGuessPathStyle() {
781
10.6k
  m_comp_dir = FileSpec();
782
10.6k
  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
783
10.6k
  if (!die)
784
0
    return;
785
786
10.6k
  llvm::StringRef comp_dir = removeHostnameFromPathname(
787
10.6k
      die->GetAttributeValueAsString(this, DW_AT_comp_dir, nullptr));
788
10.6k
  if (!comp_dir.empty()) {
789
10.2k
    FileSpec::Style comp_dir_style =
790
10.2k
        FileSpec::GuessPathStyle(comp_dir).getValueOr(FileSpec::Style::native);
791
10.2k
    m_comp_dir = FileSpec(comp_dir, comp_dir_style);
792
10.2k
  } else {
793
    // Try to detect the style based on the DW_AT_name attribute, but just store
794
    // the detected style in the m_comp_dir field.
795
313
    const char *name =
796
313
        die->GetAttributeValueAsString(this, DW_AT_name, nullptr);
797
313
    m_comp_dir = FileSpec(
798
313
        "", FileSpec::GuessPathStyle(name).getValueOr(FileSpec::Style::native));
799
313
  }
800
10.6k
}
801
802
0
void DWARFUnit::ComputeAbsolutePath() {
803
0
  m_file_spec = FileSpec();
804
0
  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
805
0
  if (!die)
806
0
    return;
807
808
0
  m_file_spec =
809
0
      FileSpec(die->GetAttributeValueAsString(this, DW_AT_name, nullptr),
810
0
               GetPathStyle());
811
812
0
  if (m_file_spec->IsRelative())
813
0
    m_file_spec->MakeAbsolute(GetCompilationDirectory());
814
0
}
815
816
5.73k
SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() {
817
5.73k
  ExtractUnitDIEIfNeeded();
818
5.73k
  if (m_dwo)
819
40
    return &llvm::cast<SymbolFileDWARFDwo>(m_dwo->GetSymbolFileDWARF());
820
5.69k
  return nullptr;
821
5.73k
}
822
823
58.5k
const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
824
58.5k
  if (m_func_aranges_up == nullptr) {
825
2.55k
    m_func_aranges_up = std::make_unique<DWARFDebugAranges>();
826
2.55k
    const DWARFDebugInfoEntry *die = DIEPtr();
827
2.55k
    if (die)
828
2.55k
      die->BuildFunctionAddressRangeTable(this, m_func_aranges_up.get());
829
830
2.55k
    if (m_dwo) {
831
0
      const DWARFDebugInfoEntry *dwo_die = m_dwo->DIEPtr();
832
0
      if (dwo_die)
833
0
        dwo_die->BuildFunctionAddressRangeTable(m_dwo.get(),
834
0
                                                m_func_aranges_up.get());
835
0
    }
836
837
2.55k
    const bool minimize = false;
838
2.55k
    m_func_aranges_up->Sort(minimize);
839
2.55k
  }
840
58.5k
  return *m_func_aranges_up;
841
58.5k
}
842
843
llvm::Expected<DWARFUnitHeader>
844
DWARFUnitHeader::extract(const DWARFDataExtractor &data,
845
                         DIERef::Section section,
846
                         lldb_private::DWARFContext &context,
847
28.3k
                         lldb::offset_t *offset_ptr) {
848
28.3k
  DWARFUnitHeader header;
849
28.3k
  header.m_offset = *offset_ptr;
850
28.3k
  header.m_length = data.GetDWARFInitialLength(offset_ptr);
851
28.3k
  header.m_version = data.GetU16(offset_ptr);
852
28.3k
  if (header.m_version == 5) {
853
54
    header.m_unit_type = data.GetU8(offset_ptr);
854
54
    header.m_addr_size = data.GetU8(offset_ptr);
855
54
    header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
856
54
    if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton ||
857
54
        
header.m_unit_type == llvm::dwarf::DW_UT_split_compile42
)
858
24
      header.m_dwo_id = data.GetU64(offset_ptr);
859
28.2k
  } else {
860
28.2k
    header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
861
28.2k
    header.m_addr_size = data.GetU8(offset_ptr);
862
28.2k
    header.m_unit_type =
863
28.2k
        section == DIERef::Section::DebugTypes ? 
DW_UT_type9
:
DW_UT_compile28.2k
;
864
28.2k
  }
865
866
28.3k
  if (context.isDwo()) {
867
34
    if (header.IsTypeUnit()) {
868
5
      header.m_index_entry =
869
5
          context.GetAsLLVM().getTUIndex().getFromOffset(header.m_offset);
870
29
    } else {
871
29
      header.m_index_entry =
872
29
          context.GetAsLLVM().getCUIndex().getFromOffset(header.m_offset);
873
29
    }
874
34
  }
875
876
28.3k
  if (header.m_index_entry) {
877
21
    if (header.m_abbr_offset) {
878
0
      return llvm::createStringError(
879
0
          llvm::inconvertibleErrorCode(),
880
0
          "Package unit with a non-zero abbreviation offset");
881
0
    }
882
21
    auto *unit_contrib = header.m_index_entry->getContribution();
883
21
    if (!unit_contrib || unit_contrib->Length != header.m_length + 4) {
884
0
      return llvm::createStringError(llvm::inconvertibleErrorCode(),
885
0
                                     "Inconsistent DWARF package unit index");
886
0
    }
887
21
    auto *abbr_entry =
888
21
        header.m_index_entry->getContribution(llvm::DW_SECT_ABBREV);
889
21
    if (!abbr_entry) {
890
0
      return llvm::createStringError(
891
0
          llvm::inconvertibleErrorCode(),
892
0
          "DWARF package index missing abbreviation column");
893
0
    }
894
21
    header.m_abbr_offset = abbr_entry->Offset;
895
21
  }
896
28.3k
  if (header.IsTypeUnit()) {
897
11
    header.m_type_hash = data.GetU64(offset_ptr);
898
11
    header.m_type_offset = data.GetDWARFOffset(offset_ptr);
899
11
  }
900
901
28.3k
  bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
902
28.3k
  bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
903
28.3k
  bool addr_size_OK = (header.m_addr_size == 4) || 
(header.m_addr_size == 8)28.3k
;
904
28.3k
  bool type_offset_OK =
905
28.3k
      !header.IsTypeUnit() || 
(header.m_type_offset <= header.GetLength())11
;
906
907
28.3k
  if (!length_OK)
908
1
    return llvm::make_error<llvm::object::GenericBinaryError>(
909
1
        "Invalid unit length");
910
28.3k
  if (!version_OK)
911
5.86k
    return llvm::make_error<llvm::object::GenericBinaryError>(
912
5.86k
        "Unsupported unit version");
913
22.4k
  if (!addr_size_OK)
914
0
    return llvm::make_error<llvm::object::GenericBinaryError>(
915
0
        "Invalid unit address size");
916
22.4k
  if (!type_offset_OK)
917
0
    return llvm::make_error<llvm::object::GenericBinaryError>(
918
0
        "Type offset out of range");
919
920
22.4k
  return header;
921
22.4k
}
922
923
llvm::Expected<DWARFUnitSP>
924
DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
925
                   const DWARFDataExtractor &debug_info,
926
28.3k
                   DIERef::Section section, lldb::offset_t *offset_ptr) {
927
28.3k
  assert(debug_info.ValidOffset(*offset_ptr));
928
929
0
  auto expected_header = DWARFUnitHeader::extract(
930
28.3k
      debug_info, section, dwarf.GetDWARFContext(), offset_ptr);
931
28.3k
  if (!expected_header)
932
5.86k
    return expected_header.takeError();
933
934
22.4k
  const DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev();
935
22.4k
  if (!abbr)
936
0
    return llvm::make_error<llvm::object::GenericBinaryError>(
937
0
        "No debug_abbrev data");
938
939
22.4k
  bool abbr_offset_OK =
940
22.4k
      dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
941
22.4k
          expected_header->GetAbbrOffset());
942
22.4k
  if (!abbr_offset_OK)
943
0
    return llvm::make_error<llvm::object::GenericBinaryError>(
944
0
        "Abbreviation offset for unit is not valid");
945
946
22.4k
  const DWARFAbbreviationDeclarationSet *abbrevs =
947
22.4k
      abbr->GetAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
948
22.4k
  if (!abbrevs)
949
0
    return llvm::make_error<llvm::object::GenericBinaryError>(
950
0
        "No abbrev exists at the specified offset.");
951
952
22.4k
  bool is_dwo = dwarf.GetDWARFContext().isDwo();
953
22.4k
  if (expected_header->IsTypeUnit())
954
11
    return DWARFUnitSP(new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs,
955
11
                                         section, is_dwo));
956
22.4k
  return DWARFUnitSP(new DWARFCompileUnit(dwarf, uid, *expected_header,
957
22.4k
                                          *abbrevs, section, is_dwo));
958
22.4k
}
959
960
2.41M
const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
961
2.41M
  return m_section == DIERef::Section::DebugTypes
962
2.41M
             ? 
m_dwarf.GetDWARFContext().getOrLoadDebugTypesData()137
963
2.41M
             : 
m_dwarf.GetDWARFContext().getOrLoadDebugInfoData()2.41M
;
964
2.41M
}
965
966
2.34M
uint32_t DWARFUnit::GetHeaderByteSize() const {
967
2.34M
  switch (m_header.GetUnitType()) {
968
2.34M
  case llvm::dwarf::DW_UT_compile:
969
2.34M
  case llvm::dwarf::DW_UT_partial:
970
2.34M
    return GetVersion() < 5 ? 
112.34M
:
12327
;
971
25
  case llvm::dwarf::DW_UT_skeleton:
972
218
  case llvm::dwarf::DW_UT_split_compile:
973
218
    return 20;
974
51
  case llvm::dwarf::DW_UT_type:
975
55
  case llvm::dwarf::DW_UT_split_type:
976
55
    return GetVersion() < 5 ? 
2349
:
246
;
977
2.34M
  }
978
0
  llvm_unreachable("invalid UnitType.");
979
0
}
980
981
llvm::Optional<uint64_t>
982
240
DWARFUnit::GetStringOffsetSectionItem(uint32_t index) const {
983
240
  offset_t offset = GetStrOffsetsBase() + index * 4;
984
240
  return m_dwarf.GetDWARFContext().getOrLoadStrOffsetsData().GetU32(&offset);
985
240
}
986
987
llvm::Expected<DWARFRangeList>
988
1.20k
DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
989
1.20k
  if (GetVersion() <= 4) {
990
1.17k
    const DWARFDebugRanges *debug_ranges = m_dwarf.GetDebugRanges();
991
1.17k
    if (!debug_ranges)
992
3
      return llvm::make_error<llvm::object::GenericBinaryError>(
993
3
          "No debug_ranges section");
994
1.17k
    DWARFRangeList ranges;
995
1.17k
    debug_ranges->FindRanges(this, offset, ranges);
996
1.17k
    return ranges;
997
1.17k
  }
998
999
31
  if (!GetRnglistTable())
1000
0
    return llvm::createStringError(errc::invalid_argument,
1001
0
                                   "missing or invalid range list table");
1002
1003
31
  llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVM();
1004
1005
  // As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1006
31
  data.setAddressSize(m_header.GetAddressByteSize());
1007
31
  auto range_list_or_error = GetRnglistTable()->findList(data, offset);
1008
31
  if (!range_list_or_error)
1009
0
    return range_list_or_error.takeError();
1010
1011
31
  llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1012
31
      range_list_or_error->getAbsoluteRanges(
1013
31
          llvm::object::SectionedAddress{GetBaseAddress()},
1014
31
          GetAddressByteSize(), [&](uint32_t index) {
1015
15
            uint32_t index_size = GetAddressByteSize();
1016
15
            dw_offset_t addr_base = GetAddrBase();
1017
15
            lldb::offset_t offset = addr_base + index * index_size;
1018
15
            return llvm::object::SectionedAddress{
1019
15
                m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1020
15
                    &offset, index_size)};
1021
15
          });
1022
31
  if (!llvm_ranges)
1023
0
    return llvm_ranges.takeError();
1024
1025
31
  DWARFRangeList ranges;
1026
47
  for (const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1027
47
    ranges.Append(DWARFRangeList::Entry(llvm_range.LowPC,
1028
47
                                        llvm_range.HighPC - llvm_range.LowPC));
1029
47
  }
1030
31
  return ranges;
1031
31
}
1032
1033
llvm::Expected<DWARFRangeList>
1034
28
DWARFUnit::FindRnglistFromIndex(uint32_t index) {
1035
28
  llvm::Expected<uint64_t> maybe_offset = GetRnglistOffset(index);
1036
28
  if (!maybe_offset)
1037
6
    return maybe_offset.takeError();
1038
22
  return FindRnglistFromOffset(*maybe_offset);
1039
28
}