DWARFUnit.cpp revision 360660
1//===-- DWARFUnit.cpp -------------------------------------------*- C++ -*-===//
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/Host/StringConvert.h"
13#include "lldb/Symbol/ObjectFile.h"
14#include "lldb/Utility/LLDBAssert.h"
15#include "lldb/Utility/StreamString.h"
16#include "lldb/Utility/Timer.h"
17#include "llvm/Object/Error.h"
18
19#include "DWARFCompileUnit.h"
20#include "DWARFDebugAranges.h"
21#include "DWARFDebugInfo.h"
22#include "DWARFTypeUnit.h"
23#include "LogChannelDWARF.h"
24#include "SymbolFileDWARFDwo.h"
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace std;
29
30extern int g_verbose;
31
32DWARFUnit::DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
33                     const DWARFUnitHeader &header,
34                     const DWARFAbbreviationDeclarationSet &abbrevs,
35                     DIERef::Section section)
36    : UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs),
37      m_cancel_scopes(false), m_section(section) {}
38
39DWARFUnit::~DWARFUnit() = default;
40
41// Parses first DIE of a compile unit.
42void DWARFUnit::ExtractUnitDIEIfNeeded() {
43  {
44    llvm::sys::ScopedReader lock(m_first_die_mutex);
45    if (m_first_die)
46      return; // Already parsed
47  }
48  llvm::sys::ScopedWriter lock(m_first_die_mutex);
49  if (m_first_die)
50    return; // Already parsed
51
52  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
53  Timer scoped_timer(func_cat, "%8.8x: DWARFUnit::ExtractUnitDIEIfNeeded()",
54                     GetOffset());
55
56  // Set the offset to that of the first DIE and calculate the start of the
57  // next compilation unit header.
58  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  const DWARFDataExtractor &data = GetData();
63  if (offset < GetNextUnitOffset() &&
64      m_first_die.Extract(data, this, &offset)) {
65    AddUnitDIE(m_first_die);
66    return;
67  }
68}
69
70// Parses a compile unit and indexes its DIEs if it hasn't already been done.
71// It will leave this compile unit extracted forever.
72void DWARFUnit::ExtractDIEsIfNeeded() {
73  m_cancel_scopes = true;
74
75  {
76    llvm::sys::ScopedReader lock(m_die_array_mutex);
77    if (!m_die_array.empty())
78      return; // Already parsed
79  }
80  llvm::sys::ScopedWriter lock(m_die_array_mutex);
81  if (!m_die_array.empty())
82    return; // Already parsed
83
84  ExtractDIEsRWLocked();
85}
86
87// Parses a compile unit and indexes its DIEs if it hasn't already been done.
88// It will clear this compile unit after returned instance gets out of scope,
89// no other ScopedExtractDIEs instance is running for this compile unit
90// and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs
91// lifetime.
92DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() {
93  ScopedExtractDIEs scoped(*this);
94
95  {
96    llvm::sys::ScopedReader lock(m_die_array_mutex);
97    if (!m_die_array.empty())
98      return scoped; // Already parsed
99  }
100  llvm::sys::ScopedWriter lock(m_die_array_mutex);
101  if (!m_die_array.empty())
102    return scoped; // Already parsed
103
104  // Otherwise m_die_array would be already populated.
105  lldbassert(!m_cancel_scopes);
106
107  ExtractDIEsRWLocked();
108  scoped.m_clear_dies = true;
109  return scoped;
110}
111
112DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) {
113  m_cu->m_die_array_scoped_mutex.lock_shared();
114}
115
116DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
117  if (!m_cu)
118    return;
119  m_cu->m_die_array_scoped_mutex.unlock_shared();
120  if (!m_clear_dies || m_cu->m_cancel_scopes)
121    return;
122  // Be sure no other ScopedExtractDIEs is running anymore.
123  llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
124  llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
125  if (m_cu->m_cancel_scopes)
126    return;
127  m_cu->ClearDIEsRWLocked();
128}
129
130DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs)
131    : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
132  rhs.m_cu = nullptr;
133}
134
135DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=(
136    DWARFUnit::ScopedExtractDIEs &&rhs) {
137  m_cu = rhs.m_cu;
138  rhs.m_cu = nullptr;
139  m_clear_dies = rhs.m_clear_dies;
140  return *this;
141}
142
143// Parses a compile unit and indexes its DIEs, m_die_array_mutex must be
144// held R/W and m_die_array must be empty.
145void DWARFUnit::ExtractDIEsRWLocked() {
146  llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex);
147
148  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
149  Timer scoped_timer(func_cat, "%8.8x: DWARFUnit::ExtractDIEsIfNeeded()",
150                     GetOffset());
151
152  // Set the offset to that of the first DIE and calculate the start of the
153  // next compilation unit header.
154  lldb::offset_t offset = GetFirstDIEOffset();
155  lldb::offset_t next_cu_offset = GetNextUnitOffset();
156
157  DWARFDebugInfoEntry die;
158
159  uint32_t depth = 0;
160  // We are in our compile unit, parse starting at the offset we were told to
161  // parse
162  const DWARFDataExtractor &data = GetData();
163  std::vector<uint32_t> die_index_stack;
164  die_index_stack.reserve(32);
165  die_index_stack.push_back(0);
166  bool prev_die_had_children = false;
167  while (offset < next_cu_offset && die.Extract(data, this, &offset)) {
168    const bool null_die = die.IsNULL();
169    if (depth == 0) {
170      assert(m_die_array.empty() && "Compile unit DIE already added");
171
172      // The average bytes per DIE entry has been seen to be around 14-20 so
173      // lets pre-reserve half of that since we are now stripping the NULL
174      // tags.
175
176      // Only reserve the memory if we are adding children of the main
177      // compile unit DIE. The compile unit DIE is always the first entry, so
178      // if our size is 1, then we are adding the first compile unit child
179      // DIE and should reserve the memory.
180      m_die_array.reserve(GetDebugInfoSize() / 24);
181      m_die_array.push_back(die);
182
183      if (!m_first_die)
184        AddUnitDIE(m_die_array.front());
185
186      // With -fsplit-dwarf-inlining, clang will emit non-empty skeleton compile
187      // units. We are not able to access these DIE *and* the dwo file
188      // simultaneously. We also don't need to do that as the dwo file will
189      // contain a superset of information. So, we don't even attempt to parse
190      // any remaining DIEs.
191      if (m_dwo_symbol_file) {
192        m_die_array.front().SetHasChildren(false);
193        break;
194      }
195
196    } else {
197      if (null_die) {
198        if (prev_die_had_children) {
199          // This will only happen if a DIE says is has children but all it
200          // contains is a NULL tag. Since we are removing the NULL DIEs from
201          // the list (saves up to 25% in C++ code), we need a way to let the
202          // DIE know that it actually doesn't have children.
203          if (!m_die_array.empty())
204            m_die_array.back().SetHasChildren(false);
205        }
206      } else {
207        die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
208
209        if (die_index_stack.back())
210          m_die_array[die_index_stack.back()].SetSiblingIndex(
211              m_die_array.size() - die_index_stack.back());
212
213        // Only push the DIE if it isn't a NULL DIE
214        m_die_array.push_back(die);
215      }
216    }
217
218    if (null_die) {
219      // NULL DIE.
220      if (!die_index_stack.empty())
221        die_index_stack.pop_back();
222
223      if (depth > 0)
224        --depth;
225      prev_die_had_children = false;
226    } else {
227      die_index_stack.back() = m_die_array.size() - 1;
228      // Normal DIE
229      const bool die_has_children = die.HasChildren();
230      if (die_has_children) {
231        die_index_stack.push_back(0);
232        ++depth;
233      }
234      prev_die_had_children = die_has_children;
235    }
236
237    if (depth == 0)
238      break; // We are done with this compile unit!
239  }
240
241  if (!m_die_array.empty()) {
242    if (m_first_die) {
243      // Only needed for the assertion.
244      m_first_die.SetHasChildren(m_die_array.front().HasChildren());
245      lldbassert(m_first_die == m_die_array.front());
246    }
247    m_first_die = m_die_array.front();
248  }
249
250  m_die_array.shrink_to_fit();
251
252  if (m_dwo_symbol_file) {
253    DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
254    dwo_cu->ExtractDIEsIfNeeded();
255  }
256}
257
258// This is used when a split dwarf is enabled.
259// A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute
260// that points to the first string offset of the CU contribution to the
261// .debug_str_offsets. At the same time, the corresponding split debug unit also
262// may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and
263// for that case, we should find the offset (skip the section header).
264static void SetDwoStrOffsetsBase(DWARFUnit *dwo_cu) {
265  lldb::offset_t baseOffset = 0;
266
267  const DWARFDataExtractor &strOffsets =
268      dwo_cu->GetSymbolFileDWARF().GetDWARFContext().getOrLoadStrOffsetsData();
269  uint64_t length = strOffsets.GetU32(&baseOffset);
270  if (length == 0xffffffff)
271    length = strOffsets.GetU64(&baseOffset);
272
273  // Check version.
274  if (strOffsets.GetU16(&baseOffset) < 5)
275    return;
276
277  // Skip padding.
278  baseOffset += 2;
279
280  dwo_cu->SetStrOffsetsBase(baseOffset);
281}
282
283// m_die_array_mutex must be already held as read/write.
284void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) {
285  llvm::Optional<uint64_t> addr_base, gnu_addr_base, ranges_base,
286      gnu_ranges_base;
287
288  DWARFAttributes attributes;
289  size_t num_attributes = cu_die.GetAttributes(this, attributes);
290  for (size_t i = 0; i < num_attributes; ++i) {
291    dw_attr_t attr = attributes.AttributeAtIndex(i);
292    DWARFFormValue form_value;
293    if (!attributes.ExtractFormValueAtIndex(i, form_value))
294      continue;
295    switch (attr) {
296    case DW_AT_addr_base:
297      addr_base = form_value.Unsigned();
298      SetAddrBase(*addr_base);
299      break;
300    case DW_AT_rnglists_base:
301      ranges_base = form_value.Unsigned();
302      SetRangesBase(*ranges_base);
303      break;
304    case DW_AT_str_offsets_base:
305      SetStrOffsetsBase(form_value.Unsigned());
306      break;
307    case DW_AT_low_pc:
308      SetBaseAddress(form_value.Address());
309      break;
310    case DW_AT_entry_pc:
311      // If the value was already set by DW_AT_low_pc, don't update it.
312      if (m_base_addr == LLDB_INVALID_ADDRESS)
313        SetBaseAddress(form_value.Address());
314      break;
315    case DW_AT_stmt_list:
316      m_line_table_offset = form_value.Unsigned();
317      break;
318    case DW_AT_GNU_addr_base:
319      gnu_addr_base = form_value.Unsigned();
320      break;
321    case DW_AT_GNU_ranges_base:
322      gnu_ranges_base = form_value.Unsigned();
323      break;
324    }
325  }
326
327  std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
328      m_dwarf.GetDwoSymbolFileForCompileUnit(*this, cu_die);
329  if (!dwo_symbol_file)
330    return;
331
332  DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
333  if (!dwo_cu)
334    return; // Can't fetch the compile unit from the dwo file.
335
336  DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
337  if (!dwo_cu_die.IsValid())
338    return; // Can't fetch the compile unit DIE from the dwo file.
339
340  uint64_t main_dwo_id =
341      cu_die.GetAttributeValueAsUnsigned(this, DW_AT_GNU_dwo_id, 0);
342  uint64_t sub_dwo_id =
343      dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
344  if (main_dwo_id != sub_dwo_id)
345    return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to
346  // a differectn compilation.
347
348  m_dwo_symbol_file = std::move(dwo_symbol_file);
349
350  // Here for DWO CU we want to use the address base set in the skeleton unit
351  // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base
352  // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_*
353  // attributes which were applicable to the DWO units. The corresponding
354  // DW_AT_* attributes standardized in DWARF v5 are also applicable to the main
355  // unit in contrast.
356  if (addr_base)
357    dwo_cu->SetAddrBase(*addr_base);
358  else if (gnu_addr_base)
359    dwo_cu->SetAddrBase(*gnu_addr_base);
360
361  if (ranges_base)
362    dwo_cu->SetRangesBase(*ranges_base);
363  else if (gnu_ranges_base)
364    dwo_cu->SetRangesBase(*gnu_ranges_base);
365
366  for (size_t i = 0; i < m_dwo_symbol_file->DebugInfo()->GetNumUnits(); ++i) {
367    DWARFUnit *unit = m_dwo_symbol_file->DebugInfo()->GetUnitAtIndex(i);
368    SetDwoStrOffsetsBase(unit);
369  }
370}
371
372DWARFDIE DWARFUnit::LookupAddress(const dw_addr_t address) {
373  if (DIE()) {
374    const DWARFDebugAranges &func_aranges = GetFunctionAranges();
375
376    // Re-check the aranges auto pointer contents in case it was created above
377    if (!func_aranges.IsEmpty())
378      return GetDIE(func_aranges.FindAddress(address));
379  }
380  return DWARFDIE();
381}
382
383size_t DWARFUnit::AppendDIEsWithTag(const dw_tag_t tag,
384                                    std::vector<DWARFDIE> &dies,
385                                    uint32_t depth) const {
386  size_t old_size = dies.size();
387  {
388    llvm::sys::ScopedReader lock(m_die_array_mutex);
389    DWARFDebugInfoEntry::const_iterator pos;
390    DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
391    for (pos = m_die_array.begin(); pos != end; ++pos) {
392      if (pos->Tag() == tag)
393        dies.emplace_back(this, &(*pos));
394    }
395  }
396
397  // Return the number of DIEs added to the collection
398  return dies.size() - old_size;
399}
400
401size_t DWARFUnit::GetDebugInfoSize() const {
402  return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
403}
404
405const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
406  return m_abbrevs;
407}
408
409dw_offset_t DWARFUnit::GetAbbrevOffset() const {
410  return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
411}
412
413dw_offset_t DWARFUnit::GetLineTableOffset() {
414  ExtractUnitDIEIfNeeded();
415  return m_line_table_offset;
416}
417
418void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; }
419
420void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) {
421  m_ranges_base = ranges_base;
422}
423
424void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) {
425  m_str_offsets_base = str_offsets_base;
426}
427
428// It may be called only with m_die_array_mutex held R/W.
429void DWARFUnit::ClearDIEsRWLocked() {
430  m_die_array.clear();
431  m_die_array.shrink_to_fit();
432
433  if (m_dwo_symbol_file)
434    m_dwo_symbol_file->GetCompileUnit()->ClearDIEsRWLocked();
435}
436
437lldb::ByteOrder DWARFUnit::GetByteOrder() const {
438  return m_dwarf.GetObjectFile()->GetByteOrder();
439}
440
441TypeSystem *DWARFUnit::GetTypeSystem() {
442  return m_dwarf.GetTypeSystemForLanguage(GetLanguageType());
443}
444
445void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
446
447// Compare function DWARFDebugAranges::Range structures
448static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
449                             const dw_offset_t die_offset) {
450  return die.GetOffset() < die_offset;
451}
452
453// GetDIE()
454//
455// Get the DIE (Debug Information Entry) with the specified offset by first
456// checking if the DIE is contained within this compile unit and grabbing the
457// DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
458DWARFDIE
459DWARFUnit::GetDIE(dw_offset_t die_offset) {
460  if (die_offset != DW_INVALID_OFFSET) {
461    if (GetDwoSymbolFile())
462      return GetDwoSymbolFile()->GetCompileUnit()->GetDIE(die_offset);
463
464    if (ContainsDIEOffset(die_offset)) {
465      ExtractDIEsIfNeeded();
466      DWARFDebugInfoEntry::const_iterator end = m_die_array.cend();
467      DWARFDebugInfoEntry::const_iterator pos =
468          lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset);
469      if (pos != end) {
470        if (die_offset == (*pos).GetOffset())
471          return DWARFDIE(this, &(*pos));
472      }
473    } else
474      GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
475          "GetDIE for DIE 0x%" PRIx32 " is outside of its CU 0x%" PRIx32,
476          die_offset, GetOffset());
477  }
478  return DWARFDIE(); // Not found
479}
480
481DWARFUnit &DWARFUnit::GetNonSkeletonUnit() {
482  if (SymbolFileDWARFDwo *dwo = GetDwoSymbolFile())
483    return *dwo->GetCompileUnit();
484  return *this;
485}
486
487uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
488  if (cu)
489    return cu->GetAddressByteSize();
490  return DWARFUnit::GetDefaultAddressSize();
491}
492
493uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
494
495void *DWARFUnit::GetUserData() const { return m_user_data; }
496
497void DWARFUnit::SetUserData(void *d) {
498  m_user_data = d;
499  if (m_dwo_symbol_file)
500    m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
501}
502
503bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
504  return GetProducer() != eProducerLLVMGCC;
505}
506
507bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
508  // llvm-gcc makes completely invalid decl file attributes and won't ever be
509  // fixed, so we need to know to ignore these.
510  return GetProducer() == eProducerLLVMGCC;
511}
512
513bool DWARFUnit::Supports_unnamed_objc_bitfields() {
514  if (GetProducer() == eProducerClang) {
515    const uint32_t major_version = GetProducerVersionMajor();
516    return major_version > 425 ||
517           (major_version == 425 && GetProducerVersionUpdate() >= 13);
518  }
519  return true; // Assume all other compilers didn't have incorrect ObjC bitfield
520               // info
521}
522
523void DWARFUnit::ParseProducerInfo() {
524  m_producer_version_major = UINT32_MAX;
525  m_producer_version_minor = UINT32_MAX;
526  m_producer_version_update = UINT32_MAX;
527
528  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
529  if (die) {
530
531    const char *producer_cstr =
532        die->GetAttributeValueAsString(this, DW_AT_producer, nullptr);
533    if (producer_cstr) {
534      RegularExpression llvm_gcc_regex(
535          llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple "
536                          "Inc\\. build [0-9]+\\) \\(LLVM build "
537                          "[\\.0-9]+\\)$"));
538      if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) {
539        m_producer = eProducerLLVMGCC;
540      } else if (strstr(producer_cstr, "clang")) {
541        static RegularExpression g_clang_version_regex(
542            llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
543        RegularExpression::Match regex_match(3);
544        if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr),
545                                          &regex_match)) {
546          std::string str;
547          if (regex_match.GetMatchAtIndex(producer_cstr, 1, str))
548            m_producer_version_major =
549                StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
550          if (regex_match.GetMatchAtIndex(producer_cstr, 2, str))
551            m_producer_version_minor =
552                StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
553          if (regex_match.GetMatchAtIndex(producer_cstr, 3, str))
554            m_producer_version_update =
555                StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
556        }
557        m_producer = eProducerClang;
558      } else if (strstr(producer_cstr, "GNU"))
559        m_producer = eProducerGCC;
560    }
561  }
562  if (m_producer == eProducerInvalid)
563    m_producer = eProcucerOther;
564}
565
566DWARFProducer DWARFUnit::GetProducer() {
567  if (m_producer == eProducerInvalid)
568    ParseProducerInfo();
569  return m_producer;
570}
571
572uint32_t DWARFUnit::GetProducerVersionMajor() {
573  if (m_producer_version_major == 0)
574    ParseProducerInfo();
575  return m_producer_version_major;
576}
577
578uint32_t DWARFUnit::GetProducerVersionMinor() {
579  if (m_producer_version_minor == 0)
580    ParseProducerInfo();
581  return m_producer_version_minor;
582}
583
584uint32_t DWARFUnit::GetProducerVersionUpdate() {
585  if (m_producer_version_update == 0)
586    ParseProducerInfo();
587  return m_producer_version_update;
588}
589LanguageType DWARFUnit::LanguageTypeFromDWARF(uint64_t val) {
590  // Note: user languages between lo_user and hi_user must be handled
591  // explicitly here.
592  switch (val) {
593  case DW_LANG_Mips_Assembler:
594    return eLanguageTypeMipsAssembler;
595  case DW_LANG_GOOGLE_RenderScript:
596    return eLanguageTypeExtRenderScript;
597  default:
598    return static_cast<LanguageType>(val);
599  }
600}
601
602LanguageType DWARFUnit::GetLanguageType() {
603  if (m_language_type != eLanguageTypeUnknown)
604    return m_language_type;
605
606  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
607  if (die)
608    m_language_type = LanguageTypeFromDWARF(
609        die->GetAttributeValueAsUnsigned(this, DW_AT_language, 0));
610  return m_language_type;
611}
612
613bool DWARFUnit::GetIsOptimized() {
614  if (m_is_optimized == eLazyBoolCalculate) {
615    const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
616    if (die) {
617      m_is_optimized = eLazyBoolNo;
618      if (die->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized, 0) ==
619          1) {
620        m_is_optimized = eLazyBoolYes;
621      }
622    }
623  }
624  return m_is_optimized == eLazyBoolYes;
625}
626
627FileSpec::Style DWARFUnit::GetPathStyle() {
628  if (!m_comp_dir)
629    ComputeCompDirAndGuessPathStyle();
630  return m_comp_dir->GetPathStyle();
631}
632
633const FileSpec &DWARFUnit::GetCompilationDirectory() {
634  if (!m_comp_dir)
635    ComputeCompDirAndGuessPathStyle();
636  return *m_comp_dir;
637}
638
639const FileSpec &DWARFUnit::GetAbsolutePath() {
640  if (!m_file_spec)
641    ComputeAbsolutePath();
642  return *m_file_spec;
643}
644
645FileSpec DWARFUnit::GetFile(size_t file_idx) {
646  return m_dwarf.GetFile(*this, file_idx);
647}
648
649// DWARF2/3 suggests the form hostname:pathname for compilation directory.
650// Remove the host part if present.
651static llvm::StringRef
652removeHostnameFromPathname(llvm::StringRef path_from_dwarf) {
653  llvm::StringRef host, path;
654  std::tie(host, path) = path_from_dwarf.split(':');
655
656  if (host.contains('/'))
657    return path_from_dwarf;
658
659  // check whether we have a windows path, and so the first character is a
660  // drive-letter not a hostname.
661  if (host.size() == 1 && llvm::isAlpha(host[0]) && path.startswith("\\"))
662    return path_from_dwarf;
663
664  return path;
665}
666
667static FileSpec resolveCompDir(const FileSpec &path) {
668  bool is_symlink = SymbolFileDWARF::GetSymlinkPaths().FindFileIndex(
669                        0, path, /*full*/ true) != UINT32_MAX;
670
671  if (!is_symlink)
672    return path;
673
674  namespace fs = llvm::sys::fs;
675  if (fs::get_file_type(path.GetPath(), false) != fs::file_type::symlink_file)
676    return path;
677
678  FileSpec resolved_symlink;
679  const auto error = FileSystem::Instance().Readlink(path, resolved_symlink);
680  if (error.Success())
681    return resolved_symlink;
682
683  return path;
684}
685
686void DWARFUnit::ComputeCompDirAndGuessPathStyle() {
687  m_comp_dir = FileSpec();
688  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
689  if (!die)
690    return;
691
692  llvm::StringRef comp_dir = removeHostnameFromPathname(
693      die->GetAttributeValueAsString(this, DW_AT_comp_dir, nullptr));
694  if (!comp_dir.empty()) {
695    FileSpec::Style comp_dir_style =
696        FileSpec::GuessPathStyle(comp_dir).getValueOr(FileSpec::Style::native);
697    m_comp_dir = resolveCompDir(FileSpec(comp_dir, comp_dir_style));
698  } else {
699    // Try to detect the style based on the DW_AT_name attribute, but just store
700    // the detected style in the m_comp_dir field.
701    const char *name =
702        die->GetAttributeValueAsString(this, DW_AT_name, nullptr);
703    m_comp_dir = FileSpec(
704        "", FileSpec::GuessPathStyle(name).getValueOr(FileSpec::Style::native));
705  }
706}
707
708void DWARFUnit::ComputeAbsolutePath() {
709  m_file_spec = FileSpec();
710  const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
711  if (!die)
712    return;
713
714  m_file_spec =
715      FileSpec(die->GetAttributeValueAsString(this, DW_AT_name, nullptr),
716               GetPathStyle());
717
718  if (m_file_spec->IsRelative())
719    m_file_spec->MakeAbsolute(GetCompilationDirectory());
720}
721
722SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() const {
723  return m_dwo_symbol_file.get();
724}
725
726const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
727  if (m_func_aranges_up == nullptr) {
728    m_func_aranges_up.reset(new DWARFDebugAranges());
729    const DWARFDebugInfoEntry *die = DIEPtr();
730    if (die)
731      die->BuildFunctionAddressRangeTable(this, m_func_aranges_up.get());
732
733    if (m_dwo_symbol_file) {
734      DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
735      const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr();
736      if (dwo_die)
737        dwo_die->BuildFunctionAddressRangeTable(dwo_cu,
738                                                m_func_aranges_up.get());
739    }
740
741    const bool minimize = false;
742    m_func_aranges_up->Sort(minimize);
743  }
744  return *m_func_aranges_up;
745}
746
747llvm::Expected<DWARFUnitHeader>
748DWARFUnitHeader::extract(const DWARFDataExtractor &data, DIERef::Section section,
749                         lldb::offset_t *offset_ptr) {
750  DWARFUnitHeader header;
751  header.m_offset = *offset_ptr;
752  header.m_length = data.GetDWARFInitialLength(offset_ptr);
753  header.m_version = data.GetU16(offset_ptr);
754  if (header.m_version == 5) {
755    header.m_unit_type = data.GetU8(offset_ptr);
756    header.m_addr_size = data.GetU8(offset_ptr);
757    header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
758    if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton)
759      header.m_dwo_id = data.GetU64(offset_ptr);
760  } else {
761    header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
762    header.m_addr_size = data.GetU8(offset_ptr);
763    header.m_unit_type =
764        section == DIERef::Section::DebugTypes ? DW_UT_type : DW_UT_compile;
765  }
766
767  if (header.IsTypeUnit()) {
768    header.m_type_hash = data.GetU64(offset_ptr);
769    header.m_type_offset = data.GetDWARFOffset(offset_ptr);
770  }
771
772  bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
773  bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
774  bool addr_size_OK = (header.m_addr_size == 4) || (header.m_addr_size == 8);
775  bool type_offset_OK =
776      !header.IsTypeUnit() || (header.m_type_offset <= header.GetLength());
777
778  if (!length_OK)
779    return llvm::make_error<llvm::object::GenericBinaryError>(
780        "Invalid unit length");
781  if (!version_OK)
782    return llvm::make_error<llvm::object::GenericBinaryError>(
783        "Unsupported unit version");
784  if (!addr_size_OK)
785    return llvm::make_error<llvm::object::GenericBinaryError>(
786        "Invalid unit address size");
787  if (!type_offset_OK)
788    return llvm::make_error<llvm::object::GenericBinaryError>(
789        "Type offset out of range");
790
791  return header;
792}
793
794llvm::Expected<DWARFUnitSP>
795DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
796                   const DWARFDataExtractor &debug_info,
797                   DIERef::Section section, lldb::offset_t *offset_ptr) {
798  assert(debug_info.ValidOffset(*offset_ptr));
799
800  auto expected_header =
801      DWARFUnitHeader::extract(debug_info, section, offset_ptr);
802  if (!expected_header)
803    return expected_header.takeError();
804
805  const DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev();
806  if (!abbr)
807    return llvm::make_error<llvm::object::GenericBinaryError>(
808        "No debug_abbrev data");
809
810  bool abbr_offset_OK =
811      dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
812          expected_header->GetAbbrOffset());
813  if (!abbr_offset_OK)
814    return llvm::make_error<llvm::object::GenericBinaryError>(
815        "Abbreviation offset for unit is not valid");
816
817  const DWARFAbbreviationDeclarationSet *abbrevs =
818      abbr->GetAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
819  if (!abbrevs)
820    return llvm::make_error<llvm::object::GenericBinaryError>(
821        "No abbrev exists at the specified offset.");
822
823  if (expected_header->IsTypeUnit())
824    return DWARFUnitSP(
825        new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs, section));
826  return DWARFUnitSP(
827      new DWARFCompileUnit(dwarf, uid, *expected_header, *abbrevs, section));
828}
829
830const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
831  return m_section == DIERef::Section::DebugTypes
832             ? m_dwarf.GetDWARFContext().getOrLoadDebugTypesData()
833             : m_dwarf.GetDWARFContext().getOrLoadDebugInfoData();
834}
835
836uint32_t DWARFUnit::GetHeaderByteSize() const {
837  switch (m_header.GetUnitType()) {
838  case llvm::dwarf::DW_UT_compile:
839  case llvm::dwarf::DW_UT_partial:
840    return GetVersion() < 5 ? 11 : 12;
841  case llvm::dwarf::DW_UT_skeleton:
842  case llvm::dwarf::DW_UT_split_compile:
843    return 20;
844  case llvm::dwarf::DW_UT_type:
845  case llvm::dwarf::DW_UT_split_type:
846    return GetVersion() < 5 ? 23 : 24;
847  }
848  llvm_unreachable("invalid UnitType.");
849}
850
851llvm::Expected<DWARFRangeList>
852DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) const {
853  const DWARFDebugRangesBase *debug_ranges;
854  llvm::StringRef section;
855  if (GetVersion() <= 4) {
856    debug_ranges = m_dwarf.GetDebugRanges();
857    section = "debug_ranges";
858  } else {
859    debug_ranges = m_dwarf.GetDebugRngLists();
860    section = "debug_rnglists";
861  }
862  if (!debug_ranges)
863    return llvm::make_error<llvm::object::GenericBinaryError>("No " + section +
864                                                              " section");
865
866  DWARFRangeList ranges;
867  debug_ranges->FindRanges(this, offset, ranges);
868  return ranges;
869}
870
871llvm::Expected<DWARFRangeList>
872DWARFUnit::FindRnglistFromIndex(uint32_t index) const {
873  const DWARFDebugRangesBase *debug_rnglists = m_dwarf.GetDebugRngLists();
874  if (!debug_rnglists)
875    return llvm::make_error<llvm::object::GenericBinaryError>(
876        "No debug_rnglists section");
877  return FindRnglistFromOffset(debug_rnglists->GetOffset(index));
878}
879