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