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 
DWARFUnit(SymbolFileDWARF & dwarf,lldb::user_id_t uid,const DWARFUnitHeader & header,const DWARFAbbreviationDeclarationSet & abbrevs,DIERef::Section section,bool is_dwo)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.
ExtractUnitDIENoDwoIfNeeded()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.
ExtractUnitDIEIfNeeded()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.
ExtractDIEsIfNeeded()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.
ExtractDIEsScoped()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 
ScopedExtractDIEs(DWARFUnit & cu)163 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) {
164   m_cu->m_die_array_scoped_mutex.lock_shared();
165 }
166 
~ScopedExtractDIEs()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 
ScopedExtractDIEs(ScopedExtractDIEs && rhs)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 
operator =(DWARFUnit::ScopedExtractDIEs && rhs)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.
ExtractDIEsRWLocked()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).
SetDwoStrOffsetsBase()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 
GetDWOId()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.
AddUnitDIE(const DWARFDebugInfoEntry & cu_die)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 
GetDebugInfoSize() const411 size_t DWARFUnit::GetDebugInfoSize() const {
412   return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
413 }
414 
GetAbbreviations() const415 const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
416   return m_abbrevs;
417 }
418 
GetAbbrevOffset() const419 dw_offset_t DWARFUnit::GetAbbrevOffset() const {
420   return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
421 }
422 
GetLineTableOffset()423 dw_offset_t DWARFUnit::GetLineTableOffset() {
424   ExtractUnitDIENoDwoIfNeeded();
425   return m_line_table_offset;
426 }
427 
SetAddrBase(dw_addr_t addr_base)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>
ParseListTableHeader(const llvm::DWARFDataExtractor & data,uint64_t offset,DwarfFormat format)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 
SetLoclistsBase(dw_addr_t loclists_base)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>
GetLocationTable(const DataExtractor & data) const491 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 
GetLocationData() const501 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 
GetRnglistData() const515 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 
SetRangesBase(dw_addr_t ranges_base)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> &
GetRnglistTable()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.
GetRnglistOffset(uint32_t Index)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 
SetStrOffsetsBase(dw_offset_t str_offsets_base)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.
ClearDIEsRWLocked()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 
GetByteOrder() const588 lldb::ByteOrder DWARFUnit::GetByteOrder() const {
589   return m_dwarf.GetObjectFile()->GetByteOrder();
590 }
591 
SetBaseAddress(dw_addr_t base_addr)592 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
593 
594 // Compare function DWARFDebugAranges::Range structures
CompareDIEOffset(const DWARFDebugInfoEntry & die,const dw_offset_t die_offset)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
GetDIE(dw_offset_t die_offset)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 
GetNonSkeletonUnit()627 DWARFUnit &DWARFUnit::GetNonSkeletonUnit() {
628   ExtractUnitDIEIfNeeded();
629   if (m_dwo)
630     return *m_dwo;
631   return *this;
632 }
633 
GetAddressByteSize(const DWARFUnit * cu)634 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
635   if (cu)
636     return cu->GetAddressByteSize();
637   return DWARFUnit::GetDefaultAddressSize();
638 }
639 
GetDefaultAddressSize()640 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
641 
GetUserData() const642 void *DWARFUnit::GetUserData() const { return m_user_data; }
643 
SetUserData(void * d)644 void DWARFUnit::SetUserData(void *d) { m_user_data = d; }
645 
Supports_DW_AT_APPLE_objc_complete_type()646 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
647   return GetProducer() != eProducerLLVMGCC;
648 }
649 
DW_AT_decl_file_attributes_are_invalid()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 
Supports_unnamed_objc_bitfields()656 bool DWARFUnit::Supports_unnamed_objc_bitfields() {
657   if (GetProducer() == eProducerClang)
658     return GetProducerVersion() >= llvm::VersionTuple(425, 0, 13);
659   // Assume all other compilers didn't have incorrect ObjC bitfield info.
660   return true;
661 }
662 
ParseProducerInfo()663 void DWARFUnit::ParseProducerInfo() {
664   m_producer = eProducerOther;
665   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
666   if (!die)
667     return;
668 
669   llvm::StringRef producer(
670       die->GetAttributeValueAsString(this, DW_AT_producer, nullptr));
671   if (producer.empty())
672     return;
673 
674   static const RegularExpression g_swiftlang_version_regex(
675       llvm::StringRef(R"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
676   static const RegularExpression g_clang_version_regex(
677       llvm::StringRef(R"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
678   static const RegularExpression g_llvm_gcc_regex(
679       llvm::StringRef(R"(4\.[012]\.[01] )"
680                       R"(\(Based on Apple Inc\. build [0-9]+\) )"
681                       R"(\(LLVM build [\.0-9]+\)$)"));
682 
683   llvm::SmallVector<llvm::StringRef, 3> matches;
684   if (g_swiftlang_version_regex.Execute(producer, &matches)) {
685       m_producer_version.tryParse(matches[1]);
686     m_producer = eProducerSwift;
687   } else if (producer.contains("clang")) {
688     if (g_clang_version_regex.Execute(producer, &matches))
689       m_producer_version.tryParse(matches[1]);
690     m_producer = eProducerClang;
691   } else if (producer.contains("GNU")) {
692     m_producer = eProducerGCC;
693   } else if (g_llvm_gcc_regex.Execute(producer)) {
694     m_producer = eProducerLLVMGCC;
695   }
696 }
697 
GetProducer()698 DWARFProducer DWARFUnit::GetProducer() {
699   if (m_producer == eProducerInvalid)
700     ParseProducerInfo();
701   return m_producer;
702 }
703 
GetProducerVersion()704 llvm::VersionTuple DWARFUnit::GetProducerVersion() {
705   if (m_producer_version.empty())
706     ParseProducerInfo();
707   return m_producer_version;
708 }
709 
GetDWARFLanguageType()710 uint64_t DWARFUnit::GetDWARFLanguageType() {
711   if (m_language_type)
712     return *m_language_type;
713 
714   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
715   if (!die)
716     m_language_type = 0;
717   else
718     m_language_type = die->GetAttributeValueAsUnsigned(this, DW_AT_language, 0);
719   return *m_language_type;
720 }
721 
GetIsOptimized()722 bool DWARFUnit::GetIsOptimized() {
723   if (m_is_optimized == eLazyBoolCalculate) {
724     const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
725     if (die) {
726       m_is_optimized = eLazyBoolNo;
727       if (die->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized, 0) ==
728           1) {
729         m_is_optimized = eLazyBoolYes;
730       }
731     }
732   }
733   return m_is_optimized == eLazyBoolYes;
734 }
735 
GetPathStyle()736 FileSpec::Style DWARFUnit::GetPathStyle() {
737   if (!m_comp_dir)
738     ComputeCompDirAndGuessPathStyle();
739   return m_comp_dir->GetPathStyle();
740 }
741 
GetCompilationDirectory()742 const FileSpec &DWARFUnit::GetCompilationDirectory() {
743   if (!m_comp_dir)
744     ComputeCompDirAndGuessPathStyle();
745   return *m_comp_dir;
746 }
747 
GetAbsolutePath()748 const FileSpec &DWARFUnit::GetAbsolutePath() {
749   if (!m_file_spec)
750     ComputeAbsolutePath();
751   return *m_file_spec;
752 }
753 
GetFile(size_t file_idx)754 FileSpec DWARFUnit::GetFile(size_t file_idx) {
755   return m_dwarf.GetFile(*this, file_idx);
756 }
757 
758 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
759 // Remove the host part if present.
760 static llvm::StringRef
removeHostnameFromPathname(llvm::StringRef path_from_dwarf)761 removeHostnameFromPathname(llvm::StringRef path_from_dwarf) {
762   if (!path_from_dwarf.contains(':'))
763     return path_from_dwarf;
764   llvm::StringRef host, path;
765   std::tie(host, path) = path_from_dwarf.split(':');
766 
767   if (host.contains('/'))
768     return path_from_dwarf;
769 
770   // check whether we have a windows path, and so the first character is a
771   // drive-letter not a hostname.
772   if (host.size() == 1 && llvm::isAlpha(host[0]) && path.startswith("\\"))
773     return path_from_dwarf;
774 
775   return path;
776 }
777 
ComputeCompDirAndGuessPathStyle()778 void DWARFUnit::ComputeCompDirAndGuessPathStyle() {
779   m_comp_dir = FileSpec();
780   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
781   if (!die)
782     return;
783 
784   llvm::StringRef comp_dir = removeHostnameFromPathname(
785       die->GetAttributeValueAsString(this, DW_AT_comp_dir, nullptr));
786   if (!comp_dir.empty()) {
787     FileSpec::Style comp_dir_style =
788         FileSpec::GuessPathStyle(comp_dir).getValueOr(FileSpec::Style::native);
789     m_comp_dir = FileSpec(comp_dir, comp_dir_style);
790   } else {
791     // Try to detect the style based on the DW_AT_name attribute, but just store
792     // the detected style in the m_comp_dir field.
793     const char *name =
794         die->GetAttributeValueAsString(this, DW_AT_name, nullptr);
795     m_comp_dir = FileSpec(
796         "", FileSpec::GuessPathStyle(name).getValueOr(FileSpec::Style::native));
797   }
798 }
799 
ComputeAbsolutePath()800 void DWARFUnit::ComputeAbsolutePath() {
801   m_file_spec = FileSpec();
802   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
803   if (!die)
804     return;
805 
806   m_file_spec =
807       FileSpec(die->GetAttributeValueAsString(this, DW_AT_name, nullptr),
808                GetPathStyle());
809 
810   if (m_file_spec->IsRelative())
811     m_file_spec->MakeAbsolute(GetCompilationDirectory());
812 }
813 
GetDwoSymbolFile()814 SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() {
815   ExtractUnitDIEIfNeeded();
816   if (m_dwo)
817     return &llvm::cast<SymbolFileDWARFDwo>(m_dwo->GetSymbolFileDWARF());
818   return nullptr;
819 }
820 
GetFunctionAranges()821 const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
822   if (m_func_aranges_up == nullptr) {
823     m_func_aranges_up = std::make_unique<DWARFDebugAranges>();
824     const DWARFDebugInfoEntry *die = DIEPtr();
825     if (die)
826       die->BuildFunctionAddressRangeTable(this, m_func_aranges_up.get());
827 
828     if (m_dwo) {
829       const DWARFDebugInfoEntry *dwo_die = m_dwo->DIEPtr();
830       if (dwo_die)
831         dwo_die->BuildFunctionAddressRangeTable(m_dwo.get(),
832                                                 m_func_aranges_up.get());
833     }
834 
835     const bool minimize = false;
836     m_func_aranges_up->Sort(minimize);
837   }
838   return *m_func_aranges_up;
839 }
840 
841 llvm::Expected<DWARFUnitHeader>
extract(const DWARFDataExtractor & data,DIERef::Section section,lldb_private::DWARFContext & context,lldb::offset_t * offset_ptr)842 DWARFUnitHeader::extract(const DWARFDataExtractor &data,
843                          DIERef::Section section,
844                          lldb_private::DWARFContext &context,
845                          lldb::offset_t *offset_ptr) {
846   DWARFUnitHeader header;
847   header.m_offset = *offset_ptr;
848   header.m_length = data.GetDWARFInitialLength(offset_ptr);
849   header.m_version = data.GetU16(offset_ptr);
850   if (header.m_version == 5) {
851     header.m_unit_type = data.GetU8(offset_ptr);
852     header.m_addr_size = data.GetU8(offset_ptr);
853     header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
854     if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton ||
855         header.m_unit_type == llvm::dwarf::DW_UT_split_compile)
856       header.m_dwo_id = data.GetU64(offset_ptr);
857   } else {
858     header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
859     header.m_addr_size = data.GetU8(offset_ptr);
860     header.m_unit_type =
861         section == DIERef::Section::DebugTypes ? DW_UT_type : DW_UT_compile;
862   }
863 
864   if (context.isDwo()) {
865     if (header.IsTypeUnit()) {
866       header.m_index_entry =
867           context.GetAsLLVM().getTUIndex().getFromOffset(header.m_offset);
868     } else {
869       header.m_index_entry =
870           context.GetAsLLVM().getCUIndex().getFromOffset(header.m_offset);
871     }
872   }
873 
874   if (header.m_index_entry) {
875     if (header.m_abbr_offset) {
876       return llvm::createStringError(
877           llvm::inconvertibleErrorCode(),
878           "Package unit with a non-zero abbreviation offset");
879     }
880     auto *unit_contrib = header.m_index_entry->getContribution();
881     if (!unit_contrib || unit_contrib->Length != header.m_length + 4) {
882       return llvm::createStringError(llvm::inconvertibleErrorCode(),
883                                      "Inconsistent DWARF package unit index");
884     }
885     auto *abbr_entry =
886         header.m_index_entry->getContribution(llvm::DW_SECT_ABBREV);
887     if (!abbr_entry) {
888       return llvm::createStringError(
889           llvm::inconvertibleErrorCode(),
890           "DWARF package index missing abbreviation column");
891     }
892     header.m_abbr_offset = abbr_entry->Offset;
893   }
894   if (header.IsTypeUnit()) {
895     header.m_type_hash = data.GetU64(offset_ptr);
896     header.m_type_offset = data.GetDWARFOffset(offset_ptr);
897   }
898 
899   bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
900   bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
901   bool addr_size_OK = (header.m_addr_size == 4) || (header.m_addr_size == 8);
902   bool type_offset_OK =
903       !header.IsTypeUnit() || (header.m_type_offset <= header.GetLength());
904 
905   if (!length_OK)
906     return llvm::make_error<llvm::object::GenericBinaryError>(
907         "Invalid unit length");
908   if (!version_OK)
909     return llvm::make_error<llvm::object::GenericBinaryError>(
910         "Unsupported unit version");
911   if (!addr_size_OK)
912     return llvm::make_error<llvm::object::GenericBinaryError>(
913         "Invalid unit address size");
914   if (!type_offset_OK)
915     return llvm::make_error<llvm::object::GenericBinaryError>(
916         "Type offset out of range");
917 
918   return header;
919 }
920 
921 llvm::Expected<DWARFUnitSP>
extract(SymbolFileDWARF & dwarf,user_id_t uid,const DWARFDataExtractor & debug_info,DIERef::Section section,lldb::offset_t * offset_ptr)922 DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
923                    const DWARFDataExtractor &debug_info,
924                    DIERef::Section section, lldb::offset_t *offset_ptr) {
925   assert(debug_info.ValidOffset(*offset_ptr));
926 
927   auto expected_header = DWARFUnitHeader::extract(
928       debug_info, section, dwarf.GetDWARFContext(), offset_ptr);
929   if (!expected_header)
930     return expected_header.takeError();
931 
932   const DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev();
933   if (!abbr)
934     return llvm::make_error<llvm::object::GenericBinaryError>(
935         "No debug_abbrev data");
936 
937   bool abbr_offset_OK =
938       dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
939           expected_header->GetAbbrOffset());
940   if (!abbr_offset_OK)
941     return llvm::make_error<llvm::object::GenericBinaryError>(
942         "Abbreviation offset for unit is not valid");
943 
944   const DWARFAbbreviationDeclarationSet *abbrevs =
945       abbr->GetAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
946   if (!abbrevs)
947     return llvm::make_error<llvm::object::GenericBinaryError>(
948         "No abbrev exists at the specified offset.");
949 
950   bool is_dwo = dwarf.GetDWARFContext().isDwo();
951   if (expected_header->IsTypeUnit())
952     return DWARFUnitSP(new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs,
953                                          section, is_dwo));
954   return DWARFUnitSP(new DWARFCompileUnit(dwarf, uid, *expected_header,
955                                           *abbrevs, section, is_dwo));
956 }
957 
GetData() const958 const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
959   return m_section == DIERef::Section::DebugTypes
960              ? m_dwarf.GetDWARFContext().getOrLoadDebugTypesData()
961              : m_dwarf.GetDWARFContext().getOrLoadDebugInfoData();
962 }
963 
GetHeaderByteSize() const964 uint32_t DWARFUnit::GetHeaderByteSize() const {
965   switch (m_header.GetUnitType()) {
966   case llvm::dwarf::DW_UT_compile:
967   case llvm::dwarf::DW_UT_partial:
968     return GetVersion() < 5 ? 11 : 12;
969   case llvm::dwarf::DW_UT_skeleton:
970   case llvm::dwarf::DW_UT_split_compile:
971     return 20;
972   case llvm::dwarf::DW_UT_type:
973   case llvm::dwarf::DW_UT_split_type:
974     return GetVersion() < 5 ? 23 : 24;
975   }
976   llvm_unreachable("invalid UnitType.");
977 }
978 
979 llvm::Optional<uint64_t>
GetStringOffsetSectionItem(uint32_t index) const980 DWARFUnit::GetStringOffsetSectionItem(uint32_t index) const {
981   offset_t offset = GetStrOffsetsBase() + index * 4;
982   return m_dwarf.GetDWARFContext().getOrLoadStrOffsetsData().GetU32(&offset);
983 }
984 
985 llvm::Expected<DWARFRangeList>
FindRnglistFromOffset(dw_offset_t offset)986 DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
987   if (GetVersion() <= 4) {
988     const DWARFDebugRanges *debug_ranges = m_dwarf.GetDebugRanges();
989     if (!debug_ranges)
990       return llvm::make_error<llvm::object::GenericBinaryError>(
991           "No debug_ranges section");
992     DWARFRangeList ranges;
993     debug_ranges->FindRanges(this, offset, ranges);
994     return ranges;
995   }
996 
997   if (!GetRnglistTable())
998     return llvm::createStringError(errc::invalid_argument,
999                                    "missing or invalid range list table");
1000 
1001   llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVM();
1002 
1003   // As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1004   data.setAddressSize(m_header.GetAddressByteSize());
1005   auto range_list_or_error = GetRnglistTable()->findList(data, offset);
1006   if (!range_list_or_error)
1007     return range_list_or_error.takeError();
1008 
1009   llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1010       range_list_or_error->getAbsoluteRanges(
1011           llvm::object::SectionedAddress{GetBaseAddress()},
1012           GetAddressByteSize(), [&](uint32_t index) {
1013             uint32_t index_size = GetAddressByteSize();
1014             dw_offset_t addr_base = GetAddrBase();
1015             lldb::offset_t offset = addr_base + index * index_size;
1016             return llvm::object::SectionedAddress{
1017                 m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1018                     &offset, index_size)};
1019           });
1020   if (!llvm_ranges)
1021     return llvm_ranges.takeError();
1022 
1023   DWARFRangeList ranges;
1024   for (const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1025     ranges.Append(DWARFRangeList::Entry(llvm_range.LowPC,
1026                                         llvm_range.HighPC - llvm_range.LowPC));
1027   }
1028   return ranges;
1029 }
1030 
1031 llvm::Expected<DWARFRangeList>
FindRnglistFromIndex(uint32_t index)1032 DWARFUnit::FindRnglistFromIndex(uint32_t index) {
1033   llvm::Expected<uint64_t> maybe_offset = GetRnglistOffset(index);
1034   if (!maybe_offset)
1035     return maybe_offset.takeError();
1036   return FindRnglistFromOffset(*maybe_offset);
1037 }
1038