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