180814287SRaphael Isemann //===-- DWARFUnit.cpp -----------------------------------------------------===//
22ccddfe3SJan Kratochvil //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62ccddfe3SJan Kratochvil //
72ccddfe3SJan Kratochvil //===----------------------------------------------------------------------===//
82ccddfe3SJan Kratochvil 
92ccddfe3SJan Kratochvil #include "DWARFUnit.h"
102ccddfe3SJan Kratochvil 
112ccddfe3SJan Kratochvil #include "lldb/Core/Module.h"
122ccddfe3SJan Kratochvil #include "lldb/Symbol/ObjectFile.h"
131bbff452SJan Kratochvil #include "lldb/Utility/LLDBAssert.h"
14b7b2424fSPavel Labath #include "lldb/Utility/StreamString.h"
15d950892cSJan Kratochvil #include "lldb/Utility/Timer.h"
16290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
1703c4bf73SPavel Labath #include "llvm/Object/Error.h"
182ccddfe3SJan Kratochvil 
1903c4bf73SPavel Labath #include "DWARFCompileUnit.h"
20d950892cSJan Kratochvil #include "DWARFDebugAranges.h"
212ccddfe3SJan Kratochvil #include "DWARFDebugInfo.h"
2280233daeSPavel Labath #include "DWARFTypeUnit.h"
232ccddfe3SJan Kratochvil #include "LogChannelDWARF.h"
242ccddfe3SJan Kratochvil #include "SymbolFileDWARFDwo.h"
252ccddfe3SJan Kratochvil 
262ccddfe3SJan Kratochvil using namespace lldb;
272ccddfe3SJan Kratochvil using namespace lldb_private;
28ae869d44SShafik Yaghmour using namespace lldb_private::dwarf;
292ccddfe3SJan Kratochvil 
302ccddfe3SJan Kratochvil extern int g_verbose;
312ccddfe3SJan Kratochvil 
DWARFUnit(SymbolFileDWARF & dwarf,lldb::user_id_t uid,const DWARFUnitHeader & header,const DWARFAbbreviationDeclarationSet & abbrevs,DIERef::Section section,bool is_dwo)326a2eb367SPavel Labath DWARFUnit::DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
3303c4bf73SPavel Labath                      const DWARFUnitHeader &header,
3480233daeSPavel Labath                      const DWARFAbbreviationDeclarationSet &abbrevs,
359bb01efaSPavel Labath                      DIERef::Section section, bool is_dwo)
3603c4bf73SPavel Labath     : UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs),
37a39b1982SJorge Gorbe Moya       m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
38fb09f365SEric Leese       m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.GetDWOId()) {}
392ccddfe3SJan Kratochvil 
4003c4bf73SPavel Labath DWARFUnit::~DWARFUnit() = default;
412ccddfe3SJan Kratochvil 
42fb09f365SEric Leese // Parses first DIE of a compile unit, excluding DWO.
ExtractUnitDIENoDwoIfNeeded()43fb09f365SEric Leese void DWARFUnit::ExtractUnitDIENoDwoIfNeeded() {
44c8e357f7SJan Kratochvil   {
45c8e357f7SJan Kratochvil     llvm::sys::ScopedReader lock(m_first_die_mutex);
46c8e357f7SJan Kratochvil     if (m_first_die)
47c8e357f7SJan Kratochvil       return; // Already parsed
48c8e357f7SJan Kratochvil   }
49c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter lock(m_first_die_mutex);
501bbff452SJan Kratochvil   if (m_first_die)
511bbff452SJan Kratochvil     return; // Already parsed
521bbff452SJan Kratochvil 
532887d9fdSGreg Clayton   ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef());
54fb09f365SEric Leese   LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractUnitDIENoDwoIfNeeded()",
55fb09f365SEric Leese                      GetOffset());
561bbff452SJan Kratochvil 
571bbff452SJan Kratochvil   // Set the offset to that of the first DIE and calculate the start of the
581bbff452SJan Kratochvil   // next compilation unit header.
591bbff452SJan Kratochvil   lldb::offset_t offset = GetFirstDIEOffset();
601bbff452SJan Kratochvil 
611bbff452SJan Kratochvil   // We are in our compile unit, parse starting at the offset we were told to
621bbff452SJan Kratochvil   // parse
631bbff452SJan Kratochvil   const DWARFDataExtractor &data = GetData();
6460562737SJan Kratochvil   if (offset < GetNextUnitOffset() &&
65a05fda68SFangrui Song       m_first_die.Extract(data, this, &offset)) {
661bbff452SJan Kratochvil     AddUnitDIE(m_first_die);
671bbff452SJan Kratochvil     return;
681bbff452SJan Kratochvil   }
691bbff452SJan Kratochvil }
701bbff452SJan Kratochvil 
71fb09f365SEric Leese // Parses first DIE of a compile unit including DWO.
ExtractUnitDIEIfNeeded()72fb09f365SEric Leese void DWARFUnit::ExtractUnitDIEIfNeeded() {
73fb09f365SEric Leese   ExtractUnitDIENoDwoIfNeeded();
74fb09f365SEric Leese 
75fb09f365SEric Leese   if (m_has_parsed_non_skeleton_unit)
76fb09f365SEric Leese     return;
77fb09f365SEric Leese 
78fb09f365SEric Leese   m_has_parsed_non_skeleton_unit = true;
79fb09f365SEric Leese 
80fb09f365SEric Leese   std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
81fb09f365SEric Leese       m_dwarf.GetDwoSymbolFileForCompileUnit(*this, m_first_die);
82fb09f365SEric Leese   if (!dwo_symbol_file)
83fb09f365SEric Leese     return;
84fb09f365SEric Leese 
85fb09f365SEric Leese   DWARFUnit *dwo_cu = dwo_symbol_file->GetDWOCompileUnitForHash(m_dwo_id);
86fb09f365SEric Leese 
87fb09f365SEric Leese   if (!dwo_cu)
88fb09f365SEric Leese     return; // Can't fetch the compile unit from the dwo file.
89fb09f365SEric Leese   dwo_cu->SetUserData(this);
90fb09f365SEric Leese 
91fb09f365SEric Leese   DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
92fb09f365SEric Leese   if (!dwo_cu_die.IsValid())
93fb09f365SEric Leese     return; // Can't fetch the compile unit DIE from the dwo file.
94fb09f365SEric Leese 
95fb09f365SEric Leese   // Here for DWO CU we want to use the address base set in the skeleton unit
96fb09f365SEric Leese   // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base
97fb09f365SEric Leese   // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_*
98fb09f365SEric Leese   // attributes which were applicable to the DWO units. The corresponding
99fb09f365SEric Leese   // DW_AT_* attributes standardized in DWARF v5 are also applicable to the
100fb09f365SEric Leese   // main unit in contrast.
101fb09f365SEric Leese   if (m_addr_base)
102fb09f365SEric Leese     dwo_cu->SetAddrBase(*m_addr_base);
103fb09f365SEric Leese   else if (m_gnu_addr_base)
104fb09f365SEric Leese     dwo_cu->SetAddrBase(*m_gnu_addr_base);
105fb09f365SEric Leese 
106fb09f365SEric Leese   if (GetVersion() <= 4 && m_gnu_ranges_base)
107fb09f365SEric Leese     dwo_cu->SetRangesBase(*m_gnu_ranges_base);
108fb09f365SEric Leese   else if (dwo_symbol_file->GetDWARFContext()
109fb09f365SEric Leese                .getOrLoadRngListsData()
110fb09f365SEric Leese                .GetByteSize() > 0)
111fb09f365SEric Leese     dwo_cu->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
112fb09f365SEric Leese 
113fb09f365SEric Leese   if (GetVersion() >= 5 &&
114fb09f365SEric Leese       dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
115fb09f365SEric Leese           0)
116fb09f365SEric Leese     dwo_cu->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
117fb09f365SEric Leese 
118fb09f365SEric Leese   dwo_cu->SetBaseAddress(GetBaseAddress());
119fb09f365SEric Leese 
120fb09f365SEric Leese   m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
121fb09f365SEric Leese }
122fb09f365SEric Leese 
12305097246SAdrian Prantl // Parses a compile unit and indexes its DIEs if it hasn't already been done.
124c8e357f7SJan Kratochvil // It will leave this compile unit extracted forever.
ExtractDIEsIfNeeded()125c8e357f7SJan Kratochvil void DWARFUnit::ExtractDIEsIfNeeded() {
126c8e357f7SJan Kratochvil   m_cancel_scopes = true;
127c8e357f7SJan Kratochvil 
128c8e357f7SJan Kratochvil   {
129c8e357f7SJan Kratochvil     llvm::sys::ScopedReader lock(m_die_array_mutex);
1301bbff452SJan Kratochvil     if (!m_die_array.empty())
131c8e357f7SJan Kratochvil       return; // Already parsed
132c8e357f7SJan Kratochvil   }
133c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter lock(m_die_array_mutex);
134c8e357f7SJan Kratochvil   if (!m_die_array.empty())
135c8e357f7SJan Kratochvil     return; // Already parsed
136c8e357f7SJan Kratochvil 
137c8e357f7SJan Kratochvil   ExtractDIEsRWLocked();
138c8e357f7SJan Kratochvil }
139c8e357f7SJan Kratochvil 
140c8e357f7SJan Kratochvil // Parses a compile unit and indexes its DIEs if it hasn't already been done.
141c8e357f7SJan Kratochvil // It will clear this compile unit after returned instance gets out of scope,
142c8e357f7SJan Kratochvil // no other ScopedExtractDIEs instance is running for this compile unit
143c8e357f7SJan Kratochvil // and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs
144c8e357f7SJan Kratochvil // lifetime.
ExtractDIEsScoped()145c8e357f7SJan Kratochvil DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() {
1466a2eb367SPavel Labath   ScopedExtractDIEs scoped(*this);
147c8e357f7SJan Kratochvil 
148c8e357f7SJan Kratochvil   {
149c8e357f7SJan Kratochvil     llvm::sys::ScopedReader lock(m_die_array_mutex);
150c8e357f7SJan Kratochvil     if (!m_die_array.empty())
1513489b1adSPavel Labath       return scoped; // Already parsed
152c8e357f7SJan Kratochvil   }
153c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter lock(m_die_array_mutex);
154c8e357f7SJan Kratochvil   if (!m_die_array.empty())
1553489b1adSPavel Labath     return scoped; // Already parsed
156c8e357f7SJan Kratochvil 
157c8e357f7SJan Kratochvil   // Otherwise m_die_array would be already populated.
158c8e357f7SJan Kratochvil   lldbassert(!m_cancel_scopes);
159c8e357f7SJan Kratochvil 
160c8e357f7SJan Kratochvil   ExtractDIEsRWLocked();
161c8e357f7SJan Kratochvil   scoped.m_clear_dies = true;
162c8e357f7SJan Kratochvil   return scoped;
163c8e357f7SJan Kratochvil }
164c8e357f7SJan Kratochvil 
ScopedExtractDIEs(DWARFUnit & cu)1656a2eb367SPavel Labath DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit &cu) : m_cu(&cu) {
166c8e357f7SJan Kratochvil   m_cu->m_die_array_scoped_mutex.lock_shared();
167c8e357f7SJan Kratochvil }
168c8e357f7SJan Kratochvil 
~ScopedExtractDIEs()169c8e357f7SJan Kratochvil DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
170c8e357f7SJan Kratochvil   if (!m_cu)
171c8e357f7SJan Kratochvil     return;
172c8e357f7SJan Kratochvil   m_cu->m_die_array_scoped_mutex.unlock_shared();
173c8e357f7SJan Kratochvil   if (!m_clear_dies || m_cu->m_cancel_scopes)
174c8e357f7SJan Kratochvil     return;
175c8e357f7SJan Kratochvil   // Be sure no other ScopedExtractDIEs is running anymore.
176c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
177c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
178c8e357f7SJan Kratochvil   if (m_cu->m_cancel_scopes)
179c8e357f7SJan Kratochvil     return;
180c8e357f7SJan Kratochvil   m_cu->ClearDIEsRWLocked();
181c8e357f7SJan Kratochvil }
182c8e357f7SJan Kratochvil 
ScopedExtractDIEs(ScopedExtractDIEs && rhs)183c8e357f7SJan Kratochvil DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs)
184c8e357f7SJan Kratochvil     : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
185c8e357f7SJan Kratochvil   rhs.m_cu = nullptr;
186c8e357f7SJan Kratochvil }
187c8e357f7SJan Kratochvil 
operator =(DWARFUnit::ScopedExtractDIEs && rhs)188c8e357f7SJan Kratochvil DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=(
189c8e357f7SJan Kratochvil     DWARFUnit::ScopedExtractDIEs &&rhs) {
190c8e357f7SJan Kratochvil   m_cu = rhs.m_cu;
191c8e357f7SJan Kratochvil   rhs.m_cu = nullptr;
192c8e357f7SJan Kratochvil   m_clear_dies = rhs.m_clear_dies;
193c8e357f7SJan Kratochvil   return *this;
194c8e357f7SJan Kratochvil }
195c8e357f7SJan Kratochvil 
196c8e357f7SJan Kratochvil // Parses a compile unit and indexes its DIEs, m_die_array_mutex must be
197c8e357f7SJan Kratochvil // held R/W and m_die_array must be empty.
ExtractDIEsRWLocked()198c8e357f7SJan Kratochvil void DWARFUnit::ExtractDIEsRWLocked() {
199c8e357f7SJan Kratochvil   llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex);
200d950892cSJan Kratochvil 
2012887d9fdSGreg Clayton   ElapsedTime elapsed(m_dwarf.GetDebugInfoParseTimeRef());
2025c1c8443SJonas Devlieghere   LLDB_SCOPED_TIMERF("%8.8x: DWARFUnit::ExtractDIEsIfNeeded()", GetOffset());
203d950892cSJan Kratochvil 
204d950892cSJan Kratochvil   // Set the offset to that of the first DIE and calculate the start of the
205d950892cSJan Kratochvil   // next compilation unit header.
206d950892cSJan Kratochvil   lldb::offset_t offset = GetFirstDIEOffset();
20760562737SJan Kratochvil   lldb::offset_t next_cu_offset = GetNextUnitOffset();
208d950892cSJan Kratochvil 
209d950892cSJan Kratochvil   DWARFDebugInfoEntry die;
210d950892cSJan Kratochvil 
2111a2d25fcSPavel Labath   uint32_t depth = 0;
21205097246SAdrian Prantl   // We are in our compile unit, parse starting at the offset we were told to
21305097246SAdrian Prantl   // parse
214f56c30d1SGreg Clayton   const DWARFDataExtractor &data = GetData();
215d950892cSJan Kratochvil   std::vector<uint32_t> die_index_stack;
216d950892cSJan Kratochvil   die_index_stack.reserve(32);
2171a2d25fcSPavel Labath   die_index_stack.push_back(0);
218d950892cSJan Kratochvil   bool prev_die_had_children = false;
219a05fda68SFangrui Song   while (offset < next_cu_offset && die.Extract(data, this, &offset)) {
220d950892cSJan Kratochvil     const bool null_die = die.IsNULL();
2211a2d25fcSPavel Labath     if (depth == 0) {
2221bbff452SJan Kratochvil       assert(m_die_array.empty() && "Compile unit DIE already added");
2231bbff452SJan Kratochvil 
2241bbff452SJan Kratochvil       // The average bytes per DIE entry has been seen to be around 14-20 so
2251bbff452SJan Kratochvil       // lets pre-reserve half of that since we are now stripping the NULL
2261bbff452SJan Kratochvil       // tags.
2271bbff452SJan Kratochvil 
2281bbff452SJan Kratochvil       // Only reserve the memory if we are adding children of the main
2291bbff452SJan Kratochvil       // compile unit DIE. The compile unit DIE is always the first entry, so
2301bbff452SJan Kratochvil       // if our size is 1, then we are adding the first compile unit child
2311bbff452SJan Kratochvil       // DIE and should reserve the memory.
2321bbff452SJan Kratochvil       m_die_array.reserve(GetDebugInfoSize() / 24);
2331bbff452SJan Kratochvil       m_die_array.push_back(die);
2341bbff452SJan Kratochvil 
2351bbff452SJan Kratochvil       if (!m_first_die)
2361bbff452SJan Kratochvil         AddUnitDIE(m_die_array.front());
237da7f0336SPavel Labath 
238da7f0336SPavel Labath       // With -fsplit-dwarf-inlining, clang will emit non-empty skeleton compile
239da7f0336SPavel Labath       // units. We are not able to access these DIE *and* the dwo file
240da7f0336SPavel Labath       // simultaneously. We also don't need to do that as the dwo file will
241da7f0336SPavel Labath       // contain a superset of information. So, we don't even attempt to parse
242da7f0336SPavel Labath       // any remaining DIEs.
2439dc84e9bSPavel Labath       if (m_dwo) {
244da7f0336SPavel Labath         m_die_array.front().SetHasChildren(false);
245da7f0336SPavel Labath         break;
246da7f0336SPavel Labath       }
247da7f0336SPavel Labath 
248d950892cSJan Kratochvil     } else {
249d950892cSJan Kratochvil       if (null_die) {
250d950892cSJan Kratochvil         if (prev_die_had_children) {
25105097246SAdrian Prantl           // This will only happen if a DIE says is has children but all it
25205097246SAdrian Prantl           // contains is a NULL tag. Since we are removing the NULL DIEs from
25305097246SAdrian Prantl           // the list (saves up to 25% in C++ code), we need a way to let the
25405097246SAdrian Prantl           // DIE know that it actually doesn't have children.
255d950892cSJan Kratochvil           if (!m_die_array.empty())
256ea58c633SJan Kratochvil             m_die_array.back().SetHasChildren(false);
257d950892cSJan Kratochvil         }
258d950892cSJan Kratochvil       } else {
2591a2d25fcSPavel Labath         die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
260d950892cSJan Kratochvil 
261d950892cSJan Kratochvil         if (die_index_stack.back())
262d950892cSJan Kratochvil           m_die_array[die_index_stack.back()].SetSiblingIndex(
263d950892cSJan Kratochvil               m_die_array.size() - die_index_stack.back());
264d950892cSJan Kratochvil 
265d950892cSJan Kratochvil         // Only push the DIE if it isn't a NULL DIE
266d950892cSJan Kratochvil         m_die_array.push_back(die);
267d950892cSJan Kratochvil       }
268d950892cSJan Kratochvil     }
269d950892cSJan Kratochvil 
270d950892cSJan Kratochvil     if (null_die) {
271d950892cSJan Kratochvil       // NULL DIE.
2721a2d25fcSPavel Labath       if (!die_index_stack.empty())
273d950892cSJan Kratochvil         die_index_stack.pop_back();
2741a2d25fcSPavel Labath 
2751a2d25fcSPavel Labath       if (depth > 0)
2761a2d25fcSPavel Labath         --depth;
277d950892cSJan Kratochvil       prev_die_had_children = false;
278d950892cSJan Kratochvil     } else {
2791a2d25fcSPavel Labath       die_index_stack.back() = m_die_array.size() - 1;
280d950892cSJan Kratochvil       // Normal DIE
281d950892cSJan Kratochvil       const bool die_has_children = die.HasChildren();
2821a2d25fcSPavel Labath       if (die_has_children) {
283d950892cSJan Kratochvil         die_index_stack.push_back(0);
2841a2d25fcSPavel Labath         ++depth;
2851a2d25fcSPavel Labath       }
286d950892cSJan Kratochvil       prev_die_had_children = die_has_children;
287d950892cSJan Kratochvil     }
28818e98645SPavel Labath 
2891a2d25fcSPavel Labath     if (depth == 0)
29018e98645SPavel Labath       break; // We are done with this compile unit!
291d950892cSJan Kratochvil   }
292d950892cSJan Kratochvil 
2931bbff452SJan Kratochvil   if (!m_die_array.empty()) {
2942ecf9281SPavel Labath     // The last die cannot have children (if it did, it wouldn't be the last one).
2952ecf9281SPavel Labath     // This only makes a difference for malformed dwarf that does not have a
2962ecf9281SPavel Labath     // terminating null die.
2972ecf9281SPavel Labath     m_die_array.back().SetHasChildren(false);
2982ecf9281SPavel Labath 
299faec6dd9SJan Kratochvil     if (m_first_die) {
300faec6dd9SJan Kratochvil       // Only needed for the assertion.
301ea58c633SJan Kratochvil       m_first_die.SetHasChildren(m_die_array.front().HasChildren());
302faec6dd9SJan Kratochvil       lldbassert(m_first_die == m_die_array.front());
303faec6dd9SJan Kratochvil     }
3041bbff452SJan Kratochvil     m_first_die = m_die_array.front();
305d950892cSJan Kratochvil   }
306d950892cSJan Kratochvil 
307a375349cSJan Kratochvil   m_die_array.shrink_to_fit();
3081bbff452SJan Kratochvil 
3099dc84e9bSPavel Labath   if (m_dwo)
3109dc84e9bSPavel Labath     m_dwo->ExtractDIEsIfNeeded();
3111bbff452SJan Kratochvil }
3121bbff452SJan Kratochvil 
313c1d3f713SGeorge Rimar // This is used when a split dwarf is enabled.
314c1d3f713SGeorge Rimar // A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute
315c1d3f713SGeorge Rimar // that points to the first string offset of the CU contribution to the
316c1d3f713SGeorge Rimar // .debug_str_offsets. At the same time, the corresponding split debug unit also
317c1d3f713SGeorge Rimar // may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and
318c1d3f713SGeorge Rimar // for that case, we should find the offset (skip the section header).
SetDwoStrOffsetsBase()31900539d8bSPavel Labath void DWARFUnit::SetDwoStrOffsetsBase() {
320c1d3f713SGeorge Rimar   lldb::offset_t baseOffset = 0;
321c1d3f713SGeorge Rimar 
32200539d8bSPavel Labath   if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
323f13ce15dSIgor Kudrin     if (const auto *contribution =
324f13ce15dSIgor Kudrin             entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
32500539d8bSPavel Labath       baseOffset = contribution->Offset;
32600539d8bSPavel Labath     else
32700539d8bSPavel Labath       return;
32800539d8bSPavel Labath   }
32900539d8bSPavel Labath 
33000539d8bSPavel Labath   if (GetVersion() >= 5) {
331c1d3f713SGeorge Rimar     const DWARFDataExtractor &strOffsets =
33200539d8bSPavel Labath         GetSymbolFileDWARF().GetDWARFContext().getOrLoadStrOffsetsData();
333c1d3f713SGeorge Rimar     uint64_t length = strOffsets.GetU32(&baseOffset);
334c1d3f713SGeorge Rimar     if (length == 0xffffffff)
335c1d3f713SGeorge Rimar       length = strOffsets.GetU64(&baseOffset);
336c1d3f713SGeorge Rimar 
337c1d3f713SGeorge Rimar     // Check version.
338c1d3f713SGeorge Rimar     if (strOffsets.GetU16(&baseOffset) < 5)
339c1d3f713SGeorge Rimar       return;
340c1d3f713SGeorge Rimar 
341c1d3f713SGeorge Rimar     // Skip padding.
342c1d3f713SGeorge Rimar     baseOffset += 2;
34300539d8bSPavel Labath   }
344c1d3f713SGeorge Rimar 
34500539d8bSPavel Labath   SetStrOffsetsBase(baseOffset);
346c1d3f713SGeorge Rimar }
347c1d3f713SGeorge Rimar 
GetDWOId()348a39b1982SJorge Gorbe Moya uint64_t DWARFUnit::GetDWOId() {
349fb09f365SEric Leese   ExtractUnitDIENoDwoIfNeeded();
350a39b1982SJorge Gorbe Moya   return m_dwo_id;
351a39b1982SJorge Gorbe Moya }
352a39b1982SJorge Gorbe Moya 
353c8e357f7SJan Kratochvil // m_die_array_mutex must be already held as read/write.
AddUnitDIE(const DWARFDebugInfoEntry & cu_die)3541bbff452SJan Kratochvil void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) {
355ca9c3de1SPavel Labath   DWARFAttributes attributes;
356ca9c3de1SPavel Labath   size_t num_attributes = cu_die.GetAttributes(this, attributes);
357f69f9234SPavel Labath 
358f69f9234SPavel Labath   // Extract DW_AT_addr_base first, as other attributes may need it.
359f69f9234SPavel Labath   for (size_t i = 0; i < num_attributes; ++i) {
360f69f9234SPavel Labath     if (attributes.AttributeAtIndex(i) != DW_AT_addr_base)
361f69f9234SPavel Labath       continue;
362f69f9234SPavel Labath     DWARFFormValue form_value;
363f69f9234SPavel Labath     if (attributes.ExtractFormValueAtIndex(i, form_value)) {
364fb09f365SEric Leese       SetAddrBase(form_value.Unsigned());
365f69f9234SPavel Labath       break;
366f69f9234SPavel Labath     }
367f69f9234SPavel Labath   }
368f69f9234SPavel Labath 
369ca9c3de1SPavel Labath   for (size_t i = 0; i < num_attributes; ++i) {
370ca9c3de1SPavel Labath     dw_attr_t attr = attributes.AttributeAtIndex(i);
371ca9c3de1SPavel Labath     DWARFFormValue form_value;
372ca9c3de1SPavel Labath     if (!attributes.ExtractFormValueAtIndex(i, form_value))
373ca9c3de1SPavel Labath       continue;
374ca9c3de1SPavel Labath     switch (attr) {
375c8b74ee2SPavel Labath     case DW_AT_loclists_base:
376c8b74ee2SPavel Labath       SetLoclistsBase(form_value.Unsigned());
377c8b74ee2SPavel Labath       break;
378ca9c3de1SPavel Labath     case DW_AT_rnglists_base:
37908d33aa6SJan Kratochvil       SetRangesBase(form_value.Unsigned());
380ca9c3de1SPavel Labath       break;
381ca9c3de1SPavel Labath     case DW_AT_str_offsets_base:
382ca9c3de1SPavel Labath       SetStrOffsetsBase(form_value.Unsigned());
383ca9c3de1SPavel Labath       break;
384ca9c3de1SPavel Labath     case DW_AT_low_pc:
385ca9c3de1SPavel Labath       SetBaseAddress(form_value.Address());
386ca9c3de1SPavel Labath       break;
387ca9c3de1SPavel Labath     case DW_AT_entry_pc:
388ca9c3de1SPavel Labath       // If the value was already set by DW_AT_low_pc, don't update it.
389ca9c3de1SPavel Labath       if (m_base_addr == LLDB_INVALID_ADDRESS)
390ca9c3de1SPavel Labath         SetBaseAddress(form_value.Address());
391ca9c3de1SPavel Labath       break;
392ca9c3de1SPavel Labath     case DW_AT_stmt_list:
393ca9c3de1SPavel Labath       m_line_table_offset = form_value.Unsigned();
394ca9c3de1SPavel Labath       break;
395ca9c3de1SPavel Labath     case DW_AT_GNU_addr_base:
396fb09f365SEric Leese       m_gnu_addr_base = form_value.Unsigned();
397ca9c3de1SPavel Labath       break;
398ca9c3de1SPavel Labath     case DW_AT_GNU_ranges_base:
399fb09f365SEric Leese       m_gnu_ranges_base = form_value.Unsigned();
400ca9c3de1SPavel Labath       break;
401a39b1982SJorge Gorbe Moya     case DW_AT_GNU_dwo_id:
402a39b1982SJorge Gorbe Moya       m_dwo_id = form_value.Unsigned();
403a39b1982SJorge Gorbe Moya       break;
404ca9c3de1SPavel Labath     }
405ca9c3de1SPavel Labath   }
406d950892cSJan Kratochvil 
40700539d8bSPavel Labath   if (m_is_dwo) {
408fb09f365SEric Leese     m_has_parsed_non_skeleton_unit = true;
40900539d8bSPavel Labath     SetDwoStrOffsetsBase();
4109bb01efaSPavel Labath     return;
41100539d8bSPavel Labath   }
4122ccddfe3SJan Kratochvil }
4132ccddfe3SJan Kratochvil 
GetDebugInfoSize() const4142ccddfe3SJan Kratochvil size_t DWARFUnit::GetDebugInfoSize() const {
415f56c30d1SGreg Clayton   return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
4162ccddfe3SJan Kratochvil }
4172ccddfe3SJan Kratochvil 
GetAbbreviations() const4182ccddfe3SJan Kratochvil const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
419d950892cSJan Kratochvil   return m_abbrevs;
4202ccddfe3SJan Kratochvil }
4212ccddfe3SJan Kratochvil 
GetAbbrevOffset() const4222ccddfe3SJan Kratochvil dw_offset_t DWARFUnit::GetAbbrevOffset() const {
423d950892cSJan Kratochvil   return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
4242ccddfe3SJan Kratochvil }
4252ccddfe3SJan Kratochvil 
GetLineTableOffset()426ca9c3de1SPavel Labath dw_offset_t DWARFUnit::GetLineTableOffset() {
427fb09f365SEric Leese   ExtractUnitDIENoDwoIfNeeded();
428ca9c3de1SPavel Labath   return m_line_table_offset;
429ca9c3de1SPavel Labath }
430ca9c3de1SPavel Labath 
SetAddrBase(dw_addr_t addr_base)431c3e5dec0SGeorge Rimar void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; }
432c3e5dec0SGeorge Rimar 
433f5767e28SPavel Labath // Parse the rangelist table header, including the optional array of offsets
434f5767e28SPavel Labath // following it (DWARF v5 and later).
435f5767e28SPavel Labath template <typename ListTableType>
436f5767e28SPavel Labath static llvm::Expected<ListTableType>
ParseListTableHeader(const llvm::DWARFDataExtractor & data,uint64_t offset,DwarfFormat format)437f5767e28SPavel Labath ParseListTableHeader(const llvm::DWARFDataExtractor &data, uint64_t offset,
438f5767e28SPavel Labath                      DwarfFormat format) {
439f5767e28SPavel Labath   // We are expected to be called with Offset 0 or pointing just past the table
440f5767e28SPavel Labath   // header. Correct Offset in the latter case so that it points to the start
441f5767e28SPavel Labath   // of the header.
442e21a21a9SJan Kratochvil   if (offset == 0) {
443e21a21a9SJan Kratochvil     // This means DW_AT_rnglists_base is missing and therefore DW_FORM_rnglistx
444e21a21a9SJan Kratochvil     // cannot be handled. Returning a default-constructed ListTableType allows
445e21a21a9SJan Kratochvil     // DW_FORM_sec_offset to be supported.
446e21a21a9SJan Kratochvil     return ListTableType();
447e21a21a9SJan Kratochvil   }
448e21a21a9SJan Kratochvil 
449f5767e28SPavel Labath   uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
450f5767e28SPavel Labath   if (offset < HeaderSize)
4519bd72b5cSShafik Yaghmour     return llvm::createStringError(std::errc::invalid_argument,
452f5767e28SPavel Labath                                    "did not detect a valid"
453f5767e28SPavel Labath                                    " list table with base = 0x%" PRIx64 "\n",
454f5767e28SPavel Labath                                    offset);
455f5767e28SPavel Labath   offset -= HeaderSize;
456f5767e28SPavel Labath   ListTableType Table;
457f5767e28SPavel Labath   if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
458f5767e28SPavel Labath     return std::move(E);
459f5767e28SPavel Labath   return Table;
460f5767e28SPavel Labath }
461f5767e28SPavel Labath 
SetLoclistsBase(dw_addr_t loclists_base)462c8b74ee2SPavel Labath void DWARFUnit::SetLoclistsBase(dw_addr_t loclists_base) {
4630092dbcdSKim-Anh Tran   uint64_t offset = 0;
4640092dbcdSKim-Anh Tran   if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
4650092dbcdSKim-Anh Tran     const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
4660092dbcdSKim-Anh Tran     if (!contribution) {
4670092dbcdSKim-Anh Tran       GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
4680092dbcdSKim-Anh Tran           "Failed to find location list contribution for CU with DWO Id "
4690092dbcdSKim-Anh Tran           "0x%" PRIx64,
4700092dbcdSKim-Anh Tran           this->GetDWOId());
4710092dbcdSKim-Anh Tran       return;
4720092dbcdSKim-Anh Tran     }
4730092dbcdSKim-Anh Tran     offset += contribution->Offset;
4740092dbcdSKim-Anh Tran   }
475c8b74ee2SPavel Labath   m_loclists_base = loclists_base;
476c8b74ee2SPavel Labath 
477c8b74ee2SPavel Labath   uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
478c8b74ee2SPavel Labath   if (loclists_base < header_size)
479c8b74ee2SPavel Labath     return;
480c8b74ee2SPavel Labath 
481c8b74ee2SPavel Labath   m_loclist_table_header.emplace(".debug_loclists", "locations");
4820092dbcdSKim-Anh Tran   offset += loclists_base - header_size;
483c8b74ee2SPavel Labath   if (llvm::Error E = m_loclist_table_header->extract(
4844b5bc388SPavel Labath           m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(),
4854b5bc388SPavel Labath           &offset)) {
486c8b74ee2SPavel Labath     GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
4870092dbcdSKim-Anh Tran         "Failed to extract location list table at offset 0x%" PRIx64
4880092dbcdSKim-Anh Tran         " (location list base: 0x%" PRIx64 "): %s",
4890092dbcdSKim-Anh Tran         offset, loclists_base, toString(std::move(E)).c_str());
490c8b74ee2SPavel Labath   }
491c8b74ee2SPavel Labath }
492c8b74ee2SPavel Labath 
493cd5da94dSPavel Labath std::unique_ptr<llvm::DWARFLocationTable>
GetLocationTable(const DataExtractor & data) const494cd5da94dSPavel Labath DWARFUnit::GetLocationTable(const DataExtractor &data) const {
495cd5da94dSPavel Labath   llvm::DWARFDataExtractor llvm_data(
496512c03baSFangrui Song       data.GetData(), data.GetByteOrder() == lldb::eByteOrderLittle,
497512c03baSFangrui Song       data.GetAddressByteSize());
498cd5da94dSPavel Labath 
499cd5da94dSPavel Labath   if (m_is_dwo || GetVersion() >= 5)
500cd5da94dSPavel Labath     return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data, GetVersion());
501cd5da94dSPavel Labath   return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
502cd5da94dSPavel Labath }
503cd5da94dSPavel Labath 
GetLocationData() const5048131cb6eSPavel Labath DWARFDataExtractor DWARFUnit::GetLocationData() const {
5054b5bc388SPavel Labath   DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
5068131cb6eSPavel Labath   const DWARFDataExtractor &data =
5078131cb6eSPavel Labath       GetVersion() >= 5 ? Ctx.getOrLoadLocListsData() : Ctx.getOrLoadLocData();
5088131cb6eSPavel Labath   if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
5090092dbcdSKim-Anh Tran     if (const auto *contribution = entry->getContribution(
5100092dbcdSKim-Anh Tran             GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
5118131cb6eSPavel Labath       return DWARFDataExtractor(data, contribution->Offset,
5128131cb6eSPavel Labath                                 contribution->Length);
5138131cb6eSPavel Labath     return DWARFDataExtractor();
5148131cb6eSPavel Labath   }
5158131cb6eSPavel Labath   return data;
516cd5da94dSPavel Labath }
517cd5da94dSPavel Labath 
GetRnglistData() const518ec671f3eSKim-Anh Tran DWARFDataExtractor DWARFUnit::GetRnglistData() const {
519ec671f3eSKim-Anh Tran   DWARFContext &Ctx = GetSymbolFileDWARF().GetDWARFContext();
520ec671f3eSKim-Anh Tran   const DWARFDataExtractor &data = Ctx.getOrLoadRngListsData();
521ec671f3eSKim-Anh Tran   if (const llvm::DWARFUnitIndex::Entry *entry = m_header.GetIndexEntry()) {
522ec671f3eSKim-Anh Tran     if (const auto *contribution =
523ec671f3eSKim-Anh Tran             entry->getContribution(llvm::DW_SECT_RNGLISTS))
524ec671f3eSKim-Anh Tran       return DWARFDataExtractor(data, contribution->Offset,
525ec671f3eSKim-Anh Tran                                 contribution->Length);
526ec671f3eSKim-Anh Tran     GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
527ec671f3eSKim-Anh Tran         "Failed to find range list contribution for CU with signature "
528ec671f3eSKim-Anh Tran         "0x%" PRIx64,
529ec671f3eSKim-Anh Tran         entry->getSignature());
530ec671f3eSKim-Anh Tran 
531ec671f3eSKim-Anh Tran     return DWARFDataExtractor();
532ec671f3eSKim-Anh Tran   }
533ec671f3eSKim-Anh Tran   return data;
534ec671f3eSKim-Anh Tran }
535ec671f3eSKim-Anh Tran 
SetRangesBase(dw_addr_t ranges_base)536c3e5dec0SGeorge Rimar void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) {
537d1310817SJan Kratochvil   lldbassert(!m_rnglist_table_done);
538d1310817SJan Kratochvil 
539d950892cSJan Kratochvil   m_ranges_base = ranges_base;
540d1310817SJan Kratochvil }
541f5767e28SPavel Labath 
542278df285SJan Kratochvil const llvm::Optional<llvm::DWARFDebugRnglistTable> &
GetRnglistTable()543278df285SJan Kratochvil DWARFUnit::GetRnglistTable() {
544d1310817SJan Kratochvil   if (GetVersion() >= 5 && !m_rnglist_table_done) {
545d1310817SJan Kratochvil     m_rnglist_table_done = true;
546d1310817SJan Kratochvil     if (auto table_or_error =
547d1310817SJan Kratochvil             ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
548ec671f3eSKim-Anh Tran                 GetRnglistData().GetAsLLVM(), m_ranges_base, DWARF32))
549f5767e28SPavel Labath       m_rnglist_table = std::move(table_or_error.get());
550f5767e28SPavel Labath     else
551f5767e28SPavel Labath       GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
552f5767e28SPavel Labath           "Failed to extract range list table at offset 0x%" PRIx64 ": %s",
553d1310817SJan Kratochvil           m_ranges_base, toString(table_or_error.takeError()).c_str());
554c3e5dec0SGeorge Rimar   }
5559dd861a4SJan Kratochvil   return m_rnglist_table;
5569dd861a4SJan Kratochvil }
5579dd861a4SJan Kratochvil 
558d1310817SJan Kratochvil // This function is called only for DW_FORM_rnglistx.
GetRnglistOffset(uint32_t Index)5596d19c84cSJan Kratochvil llvm::Expected<uint64_t> DWARFUnit::GetRnglistOffset(uint32_t Index) {
560278df285SJan Kratochvil   if (!GetRnglistTable())
5619bd72b5cSShafik Yaghmour     return llvm::createStringError(std::errc::invalid_argument,
5626d19c84cSJan Kratochvil                                    "missing or invalid range list table");
5636d19c84cSJan Kratochvil   if (!m_ranges_base)
5649bd72b5cSShafik Yaghmour     return llvm::createStringError(std::errc::invalid_argument,
5656d19c84cSJan Kratochvil                                    "DW_FORM_rnglistx cannot be used without "
5666d19c84cSJan Kratochvil                                    "DW_AT_rnglists_base for CU at 0x%8.8x",
567d1310817SJan Kratochvil                                    GetOffset());
568278df285SJan Kratochvil   if (llvm::Optional<uint64_t> off = GetRnglistTable()->getOffsetEntry(
569ec671f3eSKim-Anh Tran           GetRnglistData().GetAsLLVM(), Index))
5709dd861a4SJan Kratochvil     return *off + m_ranges_base;
5716d19c84cSJan Kratochvil   return llvm::createStringError(
5729bd72b5cSShafik Yaghmour       std::errc::invalid_argument,
5736d19c84cSJan Kratochvil       "invalid range list table index %u; OffsetEntryCount is %u, "
5746d19c84cSJan Kratochvil       "DW_AT_rnglists_base is %" PRIu64,
575278df285SJan Kratochvil       Index, GetRnglistTable()->getOffsetEntryCount(), m_ranges_base);
5769dd861a4SJan Kratochvil }
5779dd861a4SJan Kratochvil 
SetStrOffsetsBase(dw_offset_t str_offsets_base)578c1d3f713SGeorge Rimar void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) {
579c1d3f713SGeorge Rimar   m_str_offsets_base = str_offsets_base;
580c1d3f713SGeorge Rimar }
581c1d3f713SGeorge Rimar 
ReadAddressFromDebugAddrSection(uint32_t index) const582*b74a01a8SZequan Wu dw_addr_t DWARFUnit::ReadAddressFromDebugAddrSection(uint32_t index) const {
583*b74a01a8SZequan Wu   uint32_t index_size = GetAddressByteSize();
584*b74a01a8SZequan Wu   dw_offset_t addr_base = GetAddrBase();
585*b74a01a8SZequan Wu   dw_addr_t offset = addr_base + index * index_size;
586*b74a01a8SZequan Wu   const DWARFDataExtractor &data =
587*b74a01a8SZequan Wu       m_dwarf.GetDWARFContext().getOrLoadAddrData();
588*b74a01a8SZequan Wu   if (data.ValidOffsetForDataOfSize(offset, index_size))
589*b74a01a8SZequan Wu     return data.GetMaxU64_unchecked(&offset, index_size);
590*b74a01a8SZequan Wu   return LLDB_INVALID_ADDRESS;
591*b74a01a8SZequan Wu }
592*b74a01a8SZequan Wu 
593c8e357f7SJan Kratochvil // It may be called only with m_die_array_mutex held R/W.
ClearDIEsRWLocked()594c8e357f7SJan Kratochvil void DWARFUnit::ClearDIEsRWLocked() {
5951bbff452SJan Kratochvil   m_die_array.clear();
5961bbff452SJan Kratochvil   m_die_array.shrink_to_fit();
5972ccddfe3SJan Kratochvil 
5989dc84e9bSPavel Labath   if (m_dwo)
5999dc84e9bSPavel Labath     m_dwo->ClearDIEsRWLocked();
600d950892cSJan Kratochvil }
601d950892cSJan Kratochvil 
GetByteOrder() const6022ccddfe3SJan Kratochvil lldb::ByteOrder DWARFUnit::GetByteOrder() const {
6036a2eb367SPavel Labath   return m_dwarf.GetObjectFile()->GetByteOrder();
6042ccddfe3SJan Kratochvil }
6052ccddfe3SJan Kratochvil 
SetBaseAddress(dw_addr_t base_addr)606d950892cSJan Kratochvil void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
6072ccddfe3SJan Kratochvil 
6082ccddfe3SJan Kratochvil // Compare function DWARFDebugAranges::Range structures
CompareDIEOffset(const DWARFDebugInfoEntry & die,const dw_offset_t die_offset)6092ccddfe3SJan Kratochvil static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
6102ccddfe3SJan Kratochvil                              const dw_offset_t die_offset) {
6112ccddfe3SJan Kratochvil   return die.GetOffset() < die_offset;
6122ccddfe3SJan Kratochvil }
6132ccddfe3SJan Kratochvil 
6142ccddfe3SJan Kratochvil // GetDIE()
6152ccddfe3SJan Kratochvil //
61605097246SAdrian Prantl // Get the DIE (Debug Information Entry) with the specified offset by first
61705097246SAdrian Prantl // checking if the DIE is contained within this compile unit and grabbing the
61805097246SAdrian Prantl // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
6192ccddfe3SJan Kratochvil DWARFDIE
GetDIE(dw_offset_t die_offset)6202ccddfe3SJan Kratochvil DWARFUnit::GetDIE(dw_offset_t die_offset) {
6217611c5bbSJan Kratochvil   if (die_offset == DW_INVALID_OFFSET)
6227611c5bbSJan Kratochvil     return DWARFDIE(); // Not found
6237611c5bbSJan Kratochvil 
6247611c5bbSJan Kratochvil   if (!ContainsDIEOffset(die_offset)) {
6257611c5bbSJan Kratochvil     GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
6267611c5bbSJan Kratochvil         "GetDIE for DIE 0x%" PRIx32 " is outside of its CU 0x%" PRIx32,
6277611c5bbSJan Kratochvil         die_offset, GetOffset());
6287611c5bbSJan Kratochvil     return DWARFDIE(); // Not found
6297611c5bbSJan Kratochvil   }
6307611c5bbSJan Kratochvil 
6311bbff452SJan Kratochvil   ExtractDIEsIfNeeded();
6322e5be0cbSJan Kratochvil   DWARFDebugInfoEntry::const_iterator end = m_die_array.cend();
6332e5be0cbSJan Kratochvil   DWARFDebugInfoEntry::const_iterator pos =
6342e5be0cbSJan Kratochvil       lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset);
6357611c5bbSJan Kratochvil 
6367611c5bbSJan Kratochvil   if (pos != end && die_offset == (*pos).GetOffset())
637c4d65751SJan Kratochvil     return DWARFDIE(this, &(*pos));
6382ccddfe3SJan Kratochvil   return DWARFDIE(); // Not found
6392ccddfe3SJan Kratochvil }
6402ccddfe3SJan Kratochvil 
GetNonSkeletonUnit()6413b926988SPavel Labath DWARFUnit &DWARFUnit::GetNonSkeletonUnit() {
6429dc84e9bSPavel Labath   ExtractUnitDIEIfNeeded();
6439dc84e9bSPavel Labath   if (m_dwo)
6449dc84e9bSPavel Labath     return *m_dwo;
6453b926988SPavel Labath   return *this;
6463b926988SPavel Labath }
6473b926988SPavel Labath 
GetAddressByteSize(const DWARFUnit * cu)6482ccddfe3SJan Kratochvil uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
6492ccddfe3SJan Kratochvil   if (cu)
6502ccddfe3SJan Kratochvil     return cu->GetAddressByteSize();
651d950892cSJan Kratochvil   return DWARFUnit::GetDefaultAddressSize();
6522ccddfe3SJan Kratochvil }
6532ccddfe3SJan Kratochvil 
GetDefaultAddressSize()654d950892cSJan Kratochvil uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
6552ccddfe3SJan Kratochvil 
GetUserData() const656d950892cSJan Kratochvil void *DWARFUnit::GetUserData() const { return m_user_data; }
6572ccddfe3SJan Kratochvil 
SetUserData(void * d)658e3aa062aSPavel Labath void DWARFUnit::SetUserData(void *d) { m_user_data = d; }
6592ccddfe3SJan Kratochvil 
Supports_DW_AT_APPLE_objc_complete_type()6602ccddfe3SJan Kratochvil bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
661a6682a41SJonas Devlieghere   return GetProducer() != eProducerLLVMGCC;
6622ccddfe3SJan Kratochvil }
6632ccddfe3SJan Kratochvil 
DW_AT_decl_file_attributes_are_invalid()6642ccddfe3SJan Kratochvil bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
66505097246SAdrian Prantl   // llvm-gcc makes completely invalid decl file attributes and won't ever be
66605097246SAdrian Prantl   // fixed, so we need to know to ignore these.
6672ccddfe3SJan Kratochvil   return GetProducer() == eProducerLLVMGCC;
6682ccddfe3SJan Kratochvil }
6692ccddfe3SJan Kratochvil 
Supports_unnamed_objc_bitfields()6702ccddfe3SJan Kratochvil bool DWARFUnit::Supports_unnamed_objc_bitfields() {
67114aa3f37SAdrian Prantl   if (GetProducer() == eProducerClang)
67214aa3f37SAdrian Prantl     return GetProducerVersion() >= llvm::VersionTuple(425, 0, 13);
67314aa3f37SAdrian Prantl   // Assume all other compilers didn't have incorrect ObjC bitfield info.
67414aa3f37SAdrian Prantl   return true;
6752ccddfe3SJan Kratochvil }
6762ccddfe3SJan Kratochvil 
ParseProducerInfo()677d950892cSJan Kratochvil void DWARFUnit::ParseProducerInfo() {
6782edb9058SAdrian Prantl   m_producer = eProducerOther;
679d950892cSJan Kratochvil   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
6802edb9058SAdrian Prantl   if (!die)
6812edb9058SAdrian Prantl     return;
6822edb9058SAdrian Prantl 
6832edb9058SAdrian Prantl   llvm::StringRef producer(
6842edb9058SAdrian Prantl       die->GetAttributeValueAsString(this, DW_AT_producer, nullptr));
6852edb9058SAdrian Prantl   if (producer.empty())
6862edb9058SAdrian Prantl     return;
6872edb9058SAdrian Prantl 
6884651576eSAdrian Prantl   static const RegularExpression g_swiftlang_version_regex(
6894651576eSAdrian Prantl       llvm::StringRef(R"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
6904651576eSAdrian Prantl   static const RegularExpression g_clang_version_regex(
6914651576eSAdrian Prantl       llvm::StringRef(R"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
6924651576eSAdrian Prantl   static const RegularExpression g_llvm_gcc_regex(
6938c5f3348SAdrian Prantl       llvm::StringRef(R"(4\.[012]\.[01] )"
6948c5f3348SAdrian Prantl                       R"(\(Based on Apple Inc\. build [0-9]+\) )"
6958c5f3348SAdrian Prantl                       R"(\(LLVM build [\.0-9]+\)$)"));
6968c5f3348SAdrian Prantl 
6978c5f3348SAdrian Prantl   llvm::SmallVector<llvm::StringRef, 3> matches;
6984651576eSAdrian Prantl   if (g_swiftlang_version_regex.Execute(producer, &matches)) {
6994651576eSAdrian Prantl       m_producer_version.tryParse(matches[1]);
7004651576eSAdrian Prantl     m_producer = eProducerSwift;
7014651576eSAdrian Prantl   } else if (producer.contains("clang")) {
70214aa3f37SAdrian Prantl     if (g_clang_version_regex.Execute(producer, &matches))
70314aa3f37SAdrian Prantl       m_producer_version.tryParse(matches[1]);
704d950892cSJan Kratochvil     m_producer = eProducerClang;
7054651576eSAdrian Prantl   } else if (producer.contains("GNU")) {
706d950892cSJan Kratochvil     m_producer = eProducerGCC;
7074651576eSAdrian Prantl   } else if (g_llvm_gcc_regex.Execute(producer)) {
7084651576eSAdrian Prantl     m_producer = eProducerLLVMGCC;
7094651576eSAdrian Prantl   }
710d950892cSJan Kratochvil }
7112ccddfe3SJan Kratochvil 
GetProducer()7122ccddfe3SJan Kratochvil DWARFProducer DWARFUnit::GetProducer() {
713d950892cSJan Kratochvil   if (m_producer == eProducerInvalid)
714d950892cSJan Kratochvil     ParseProducerInfo();
715d950892cSJan Kratochvil   return m_producer;
7162ccddfe3SJan Kratochvil }
7172ccddfe3SJan Kratochvil 
GetProducerVersion()71814aa3f37SAdrian Prantl llvm::VersionTuple DWARFUnit::GetProducerVersion() {
71914aa3f37SAdrian Prantl   if (m_producer_version.empty())
720d950892cSJan Kratochvil     ParseProducerInfo();
72114aa3f37SAdrian Prantl   return m_producer_version;
7222ccddfe3SJan Kratochvil }
7232ccddfe3SJan Kratochvil 
GetDWARFLanguageType()724789beeecSJan Kratochvil uint64_t DWARFUnit::GetDWARFLanguageType() {
725789beeecSJan Kratochvil   if (m_language_type)
726789beeecSJan Kratochvil     return *m_language_type;
727d950892cSJan Kratochvil 
728d950892cSJan Kratochvil   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
729789beeecSJan Kratochvil   if (!die)
730789beeecSJan Kratochvil     m_language_type = 0;
731789beeecSJan Kratochvil   else
732789beeecSJan Kratochvil     m_language_type = die->GetAttributeValueAsUnsigned(this, DW_AT_language, 0);
733789beeecSJan Kratochvil   return *m_language_type;
7342ccddfe3SJan Kratochvil }
7352ccddfe3SJan Kratochvil 
GetIsOptimized()7362ccddfe3SJan Kratochvil bool DWARFUnit::GetIsOptimized() {
737d950892cSJan Kratochvil   if (m_is_optimized == eLazyBoolCalculate) {
738d950892cSJan Kratochvil     const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
739d950892cSJan Kratochvil     if (die) {
740d950892cSJan Kratochvil       m_is_optimized = eLazyBoolNo;
741b7a19321SJan Kratochvil       if (die->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized, 0) ==
742b7a19321SJan Kratochvil           1) {
743d950892cSJan Kratochvil         m_is_optimized = eLazyBoolYes;
744d950892cSJan Kratochvil       }
745d950892cSJan Kratochvil     }
746d950892cSJan Kratochvil   }
747d950892cSJan Kratochvil   return m_is_optimized == eLazyBoolYes;
7482ccddfe3SJan Kratochvil }
7492ccddfe3SJan Kratochvil 
GetPathStyle()7507d36d723SPavel Labath FileSpec::Style DWARFUnit::GetPathStyle() {
7517d36d723SPavel Labath   if (!m_comp_dir)
7527d36d723SPavel Labath     ComputeCompDirAndGuessPathStyle();
7537d36d723SPavel Labath   return m_comp_dir->GetPathStyle();
7547d36d723SPavel Labath }
7557d36d723SPavel Labath 
GetCompilationDirectory()7567d36d723SPavel Labath const FileSpec &DWARFUnit::GetCompilationDirectory() {
7577d36d723SPavel Labath   if (!m_comp_dir)
7587d36d723SPavel Labath     ComputeCompDirAndGuessPathStyle();
7597d36d723SPavel Labath   return *m_comp_dir;
7607d36d723SPavel Labath }
7617d36d723SPavel Labath 
GetAbsolutePath()762ca9c3de1SPavel Labath const FileSpec &DWARFUnit::GetAbsolutePath() {
763ca9c3de1SPavel Labath   if (!m_file_spec)
764ca9c3de1SPavel Labath     ComputeAbsolutePath();
765ca9c3de1SPavel Labath   return *m_file_spec;
766ca9c3de1SPavel Labath }
767ca9c3de1SPavel Labath 
GetFile(size_t file_idx)768ca9c3de1SPavel Labath FileSpec DWARFUnit::GetFile(size_t file_idx) {
7696a2eb367SPavel Labath   return m_dwarf.GetFile(*this, file_idx);
770ca9c3de1SPavel Labath }
771ca9c3de1SPavel Labath 
7727d36d723SPavel Labath // DWARF2/3 suggests the form hostname:pathname for compilation directory.
7737d36d723SPavel Labath // Remove the host part if present.
7747d36d723SPavel Labath static llvm::StringRef
removeHostnameFromPathname(llvm::StringRef path_from_dwarf)7757d36d723SPavel Labath removeHostnameFromPathname(llvm::StringRef path_from_dwarf) {
77646f02fc9SPavel Labath   if (!path_from_dwarf.contains(':'))
77746f02fc9SPavel Labath     return path_from_dwarf;
7787d36d723SPavel Labath   llvm::StringRef host, path;
7797d36d723SPavel Labath   std::tie(host, path) = path_from_dwarf.split(':');
7807d36d723SPavel Labath 
7817d36d723SPavel Labath   if (host.contains('/'))
7827d36d723SPavel Labath     return path_from_dwarf;
7837d36d723SPavel Labath 
7847d36d723SPavel Labath   // check whether we have a windows path, and so the first character is a
7857d36d723SPavel Labath   // drive-letter not a hostname.
786b548f584SMartin Storsjö   if (host.size() == 1 && llvm::isAlpha(host[0]) &&
787b548f584SMartin Storsjö       (path.startswith("\\") || path.startswith("/")))
7887d36d723SPavel Labath     return path_from_dwarf;
7897d36d723SPavel Labath 
7907d36d723SPavel Labath   return path;
7917d36d723SPavel Labath }
7927d36d723SPavel Labath 
ComputeCompDirAndGuessPathStyle()7937d36d723SPavel Labath void DWARFUnit::ComputeCompDirAndGuessPathStyle() {
7947d36d723SPavel Labath   m_comp_dir = FileSpec();
7957d36d723SPavel Labath   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
7967d36d723SPavel Labath   if (!die)
7977d36d723SPavel Labath     return;
7987d36d723SPavel Labath 
7997d36d723SPavel Labath   llvm::StringRef comp_dir = removeHostnameFromPathname(
800248a1305SKonrad Kleine       die->GetAttributeValueAsString(this, DW_AT_comp_dir, nullptr));
8017d36d723SPavel Labath   if (!comp_dir.empty()) {
802841bea93SPavel Labath     FileSpec::Style comp_dir_style =
803aa88161bSKazu Hirata         FileSpec::GuessPathStyle(comp_dir).value_or(FileSpec::Style::native);
80427df2d9fSPavel Labath     m_comp_dir = FileSpec(comp_dir, comp_dir_style);
8057d36d723SPavel Labath   } else {
8067d36d723SPavel Labath     // Try to detect the style based on the DW_AT_name attribute, but just store
8077d36d723SPavel Labath     // the detected style in the m_comp_dir field.
808248a1305SKonrad Kleine     const char *name =
809248a1305SKonrad Kleine         die->GetAttributeValueAsString(this, DW_AT_name, nullptr);
810841bea93SPavel Labath     m_comp_dir = FileSpec(
811aa88161bSKazu Hirata         "", FileSpec::GuessPathStyle(name).value_or(FileSpec::Style::native));
8127d36d723SPavel Labath   }
8137d36d723SPavel Labath }
8147d36d723SPavel Labath 
ComputeAbsolutePath()815ca9c3de1SPavel Labath void DWARFUnit::ComputeAbsolutePath() {
816ca9c3de1SPavel Labath   m_file_spec = FileSpec();
817ca9c3de1SPavel Labath   const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
818ca9c3de1SPavel Labath   if (!die)
819ca9c3de1SPavel Labath     return;
820ca9c3de1SPavel Labath 
821ca9c3de1SPavel Labath   m_file_spec =
822ca9c3de1SPavel Labath       FileSpec(die->GetAttributeValueAsString(this, DW_AT_name, nullptr),
823ca9c3de1SPavel Labath                GetPathStyle());
824ca9c3de1SPavel Labath 
825ca9c3de1SPavel Labath   if (m_file_spec->IsRelative())
826ca9c3de1SPavel Labath     m_file_spec->MakeAbsolute(GetCompilationDirectory());
827ca9c3de1SPavel Labath }
828ca9c3de1SPavel Labath 
GetDwoSymbolFile()829e65282deSPavel Labath SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() {
830e65282deSPavel Labath   ExtractUnitDIEIfNeeded();
8319dc84e9bSPavel Labath   if (m_dwo)
8329dc84e9bSPavel Labath     return &llvm::cast<SymbolFileDWARFDwo>(m_dwo->GetSymbolFileDWARF());
8339dc84e9bSPavel Labath   return nullptr;
8342ccddfe3SJan Kratochvil }
8352ccddfe3SJan Kratochvil 
GetFunctionAranges()836d950892cSJan Kratochvil const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
837248a1305SKonrad Kleine   if (m_func_aranges_up == nullptr) {
83806412daeSJonas Devlieghere     m_func_aranges_up = std::make_unique<DWARFDebugAranges>();
839d950892cSJan Kratochvil     const DWARFDebugInfoEntry *die = DIEPtr();
840d950892cSJan Kratochvil     if (die)
841b7a19321SJan Kratochvil       die->BuildFunctionAddressRangeTable(this, m_func_aranges_up.get());
842d950892cSJan Kratochvil 
8439dc84e9bSPavel Labath     if (m_dwo) {
8449dc84e9bSPavel Labath       const DWARFDebugInfoEntry *dwo_die = m_dwo->DIEPtr();
845d950892cSJan Kratochvil       if (dwo_die)
8469dc84e9bSPavel Labath         dwo_die->BuildFunctionAddressRangeTable(m_dwo.get(),
847d5b44036SJonas Devlieghere                                                 m_func_aranges_up.get());
848d950892cSJan Kratochvil     }
849d950892cSJan Kratochvil 
850d950892cSJan Kratochvil     const bool minimize = false;
851d5b44036SJonas Devlieghere     m_func_aranges_up->Sort(minimize);
852d950892cSJan Kratochvil   }
853d5b44036SJonas Devlieghere   return *m_func_aranges_up;
854d950892cSJan Kratochvil }
855f56c30d1SGreg Clayton 
85603c4bf73SPavel Labath llvm::Expected<DWARFUnitHeader>
extract(const DWARFDataExtractor & data,DIERef::Section section,lldb_private::DWARFContext & context,lldb::offset_t * offset_ptr)85767f63f3fSPavel Labath DWARFUnitHeader::extract(const DWARFDataExtractor &data,
858979ca1c0SJorge Gorbe Moya                          DIERef::Section section,
859979ca1c0SJorge Gorbe Moya                          lldb_private::DWARFContext &context,
860979ca1c0SJorge Gorbe Moya                          lldb::offset_t *offset_ptr) {
86103c4bf73SPavel Labath   DWARFUnitHeader header;
86203c4bf73SPavel Labath   header.m_offset = *offset_ptr;
86303c4bf73SPavel Labath   header.m_length = data.GetDWARFInitialLength(offset_ptr);
86403c4bf73SPavel Labath   header.m_version = data.GetU16(offset_ptr);
86503c4bf73SPavel Labath   if (header.m_version == 5) {
86603c4bf73SPavel Labath     header.m_unit_type = data.GetU8(offset_ptr);
86703c4bf73SPavel Labath     header.m_addr_size = data.GetU8(offset_ptr);
86803c4bf73SPavel Labath     header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
869a39b1982SJorge Gorbe Moya     if (header.m_unit_type == llvm::dwarf::DW_UT_skeleton ||
870a39b1982SJorge Gorbe Moya         header.m_unit_type == llvm::dwarf::DW_UT_split_compile)
87103c4bf73SPavel Labath       header.m_dwo_id = data.GetU64(offset_ptr);
87203c4bf73SPavel Labath   } else {
87303c4bf73SPavel Labath     header.m_abbr_offset = data.GetDWARFOffset(offset_ptr);
87403c4bf73SPavel Labath     header.m_addr_size = data.GetU8(offset_ptr);
87580233daeSPavel Labath     header.m_unit_type =
87680233daeSPavel Labath         section == DIERef::Section::DebugTypes ? DW_UT_type : DW_UT_compile;
87703c4bf73SPavel Labath   }
87803c4bf73SPavel Labath 
879979ca1c0SJorge Gorbe Moya   if (header.IsTypeUnit()) {
8800da640cbSDavid Blaikie     header.m_type_hash = data.GetU64(offset_ptr);
8810da640cbSDavid Blaikie     header.m_type_offset = data.GetDWARFOffset(offset_ptr);
882979ca1c0SJorge Gorbe Moya   }
8830da640cbSDavid Blaikie 
8840da640cbSDavid Blaikie   if (context.isDwo()) {
8850da640cbSDavid Blaikie     const llvm::DWARFUnitIndex *Index;
8860da640cbSDavid Blaikie     if (header.IsTypeUnit()) {
8870da640cbSDavid Blaikie       Index = &context.GetAsLLVM().getTUIndex();
8880da640cbSDavid Blaikie       if (*Index)
8890da640cbSDavid Blaikie         header.m_index_entry = Index->getFromHash(header.m_type_hash);
8900da640cbSDavid Blaikie     } else {
8910da640cbSDavid Blaikie       Index = &context.GetAsLLVM().getCUIndex();
8920da640cbSDavid Blaikie       if (*Index && header.m_version >= 5)
8930da640cbSDavid Blaikie         header.m_index_entry = Index->getFromHash(header.m_dwo_id);
8940da640cbSDavid Blaikie     }
8950da640cbSDavid Blaikie     if (!header.m_index_entry)
8960da640cbSDavid Blaikie       header.m_index_entry = Index->getFromOffset(header.m_offset);
897979ca1c0SJorge Gorbe Moya   }
898979ca1c0SJorge Gorbe Moya 
89967f63f3fSPavel Labath   if (header.m_index_entry) {
90067f63f3fSPavel Labath     if (header.m_abbr_offset) {
90167f63f3fSPavel Labath       return llvm::createStringError(
90267f63f3fSPavel Labath           llvm::inconvertibleErrorCode(),
90367f63f3fSPavel Labath           "Package unit with a non-zero abbreviation offset");
90467f63f3fSPavel Labath     }
905f13ce15dSIgor Kudrin     auto *unit_contrib = header.m_index_entry->getContribution();
90667f63f3fSPavel Labath     if (!unit_contrib || unit_contrib->Length != header.m_length + 4) {
90767f63f3fSPavel Labath       return llvm::createStringError(llvm::inconvertibleErrorCode(),
90867f63f3fSPavel Labath                                      "Inconsistent DWARF package unit index");
90967f63f3fSPavel Labath     }
910f13ce15dSIgor Kudrin     auto *abbr_entry =
911f13ce15dSIgor Kudrin         header.m_index_entry->getContribution(llvm::DW_SECT_ABBREV);
91267f63f3fSPavel Labath     if (!abbr_entry) {
91367f63f3fSPavel Labath       return llvm::createStringError(
91467f63f3fSPavel Labath           llvm::inconvertibleErrorCode(),
91567f63f3fSPavel Labath           "DWARF package index missing abbreviation column");
91667f63f3fSPavel Labath     }
91767f63f3fSPavel Labath     header.m_abbr_offset = abbr_entry->Offset;
91867f63f3fSPavel Labath   }
919f750842cSPavel Labath 
92003c4bf73SPavel Labath   bool length_OK = data.ValidOffset(header.GetNextUnitOffset() - 1);
92103c4bf73SPavel Labath   bool version_OK = SymbolFileDWARF::SupportedVersion(header.m_version);
92203c4bf73SPavel Labath   bool addr_size_OK = (header.m_addr_size == 4) || (header.m_addr_size == 8);
923f750842cSPavel Labath   bool type_offset_OK =
924f750842cSPavel Labath       !header.IsTypeUnit() || (header.m_type_offset <= header.GetLength());
92503c4bf73SPavel Labath 
92603c4bf73SPavel Labath   if (!length_OK)
92703c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
92803c4bf73SPavel Labath         "Invalid unit length");
92903c4bf73SPavel Labath   if (!version_OK)
93003c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
93103c4bf73SPavel Labath         "Unsupported unit version");
93203c4bf73SPavel Labath   if (!addr_size_OK)
93303c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
93403c4bf73SPavel Labath         "Invalid unit address size");
935f750842cSPavel Labath   if (!type_offset_OK)
936f750842cSPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
937f750842cSPavel Labath         "Type offset out of range");
93803c4bf73SPavel Labath 
93903c4bf73SPavel Labath   return header;
94003c4bf73SPavel Labath }
94103c4bf73SPavel Labath 
94203c4bf73SPavel Labath llvm::Expected<DWARFUnitSP>
extract(SymbolFileDWARF & dwarf,user_id_t uid,const DWARFDataExtractor & debug_info,DIERef::Section section,lldb::offset_t * offset_ptr)9436a2eb367SPavel Labath DWARFUnit::extract(SymbolFileDWARF &dwarf, user_id_t uid,
9446a2eb367SPavel Labath                    const DWARFDataExtractor &debug_info,
945979ca1c0SJorge Gorbe Moya                    DIERef::Section section, lldb::offset_t *offset_ptr) {
94603c4bf73SPavel Labath   assert(debug_info.ValidOffset(*offset_ptr));
94703c4bf73SPavel Labath 
948979ca1c0SJorge Gorbe Moya   auto expected_header = DWARFUnitHeader::extract(
949979ca1c0SJorge Gorbe Moya       debug_info, section, dwarf.GetDWARFContext(), offset_ptr);
95003c4bf73SPavel Labath   if (!expected_header)
95103c4bf73SPavel Labath     return expected_header.takeError();
95203c4bf73SPavel Labath 
9536a2eb367SPavel Labath   const DWARFDebugAbbrev *abbr = dwarf.DebugAbbrev();
95403c4bf73SPavel Labath   if (!abbr)
95503c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
95603c4bf73SPavel Labath         "No debug_abbrev data");
95703c4bf73SPavel Labath 
95803c4bf73SPavel Labath   bool abbr_offset_OK =
9596a2eb367SPavel Labath       dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
96003c4bf73SPavel Labath           expected_header->GetAbbrOffset());
96103c4bf73SPavel Labath   if (!abbr_offset_OK)
96203c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
96303c4bf73SPavel Labath         "Abbreviation offset for unit is not valid");
96403c4bf73SPavel Labath 
96503c4bf73SPavel Labath   const DWARFAbbreviationDeclarationSet *abbrevs =
96603c4bf73SPavel Labath       abbr->GetAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
96703c4bf73SPavel Labath   if (!abbrevs)
96803c4bf73SPavel Labath     return llvm::make_error<llvm::object::GenericBinaryError>(
96903c4bf73SPavel Labath         "No abbrev exists at the specified offset.");
97003c4bf73SPavel Labath 
9719bb01efaSPavel Labath   bool is_dwo = dwarf.GetDWARFContext().isDwo();
97280233daeSPavel Labath   if (expected_header->IsTypeUnit())
9739bb01efaSPavel Labath     return DWARFUnitSP(new DWARFTypeUnit(dwarf, uid, *expected_header, *abbrevs,
9749bb01efaSPavel Labath                                          section, is_dwo));
9759bb01efaSPavel Labath   return DWARFUnitSP(new DWARFCompileUnit(dwarf, uid, *expected_header,
9769bb01efaSPavel Labath                                           *abbrevs, section, is_dwo));
97703c4bf73SPavel Labath }
97803c4bf73SPavel Labath 
GetData() const97903c4bf73SPavel Labath const lldb_private::DWARFDataExtractor &DWARFUnit::GetData() const {
98080233daeSPavel Labath   return m_section == DIERef::Section::DebugTypes
9816a2eb367SPavel Labath              ? m_dwarf.GetDWARFContext().getOrLoadDebugTypesData()
9826a2eb367SPavel Labath              : m_dwarf.GetDWARFContext().getOrLoadDebugInfoData();
98303c4bf73SPavel Labath }
98403c4bf73SPavel Labath 
GetHeaderByteSize() const98503c4bf73SPavel Labath uint32_t DWARFUnit::GetHeaderByteSize() const {
98603c4bf73SPavel Labath   switch (m_header.GetUnitType()) {
98703c4bf73SPavel Labath   case llvm::dwarf::DW_UT_compile:
98803c4bf73SPavel Labath   case llvm::dwarf::DW_UT_partial:
98903c4bf73SPavel Labath     return GetVersion() < 5 ? 11 : 12;
99003c4bf73SPavel Labath   case llvm::dwarf::DW_UT_skeleton:
99103c4bf73SPavel Labath   case llvm::dwarf::DW_UT_split_compile:
99203c4bf73SPavel Labath     return 20;
99303c4bf73SPavel Labath   case llvm::dwarf::DW_UT_type:
99403c4bf73SPavel Labath   case llvm::dwarf::DW_UT_split_type:
99503c4bf73SPavel Labath     return GetVersion() < 5 ? 23 : 24;
99603c4bf73SPavel Labath   }
99703c4bf73SPavel Labath   llvm_unreachable("invalid UnitType.");
99803c4bf73SPavel Labath }
99978cfe1e6SPavel Labath 
100000539d8bSPavel Labath llvm::Optional<uint64_t>
GetStringOffsetSectionItem(uint32_t index) const100100539d8bSPavel Labath DWARFUnit::GetStringOffsetSectionItem(uint32_t index) const {
100200539d8bSPavel Labath   offset_t offset = GetStrOffsetsBase() + index * 4;
100300539d8bSPavel Labath   return m_dwarf.GetDWARFContext().getOrLoadStrOffsetsData().GetU32(&offset);
100400539d8bSPavel Labath }
100500539d8bSPavel Labath 
100678cfe1e6SPavel Labath llvm::Expected<DWARFRangeList>
FindRnglistFromOffset(dw_offset_t offset)1007f5767e28SPavel Labath DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
100878cfe1e6SPavel Labath   if (GetVersion() <= 4) {
10096e1f3170SPavel Labath     const DWARFDebugRanges *debug_ranges = m_dwarf.GetDebugRanges();
101078cfe1e6SPavel Labath     if (!debug_ranges)
1011f5767e28SPavel Labath       return llvm::make_error<llvm::object::GenericBinaryError>(
1012f5767e28SPavel Labath           "No debug_ranges section");
101378cfe1e6SPavel Labath     DWARFRangeList ranges;
101478cfe1e6SPavel Labath     debug_ranges->FindRanges(this, offset, ranges);
101578cfe1e6SPavel Labath     return ranges;
101678cfe1e6SPavel Labath   }
101778cfe1e6SPavel Labath 
1018278df285SJan Kratochvil   if (!GetRnglistTable())
10199bd72b5cSShafik Yaghmour     return llvm::createStringError(std::errc::invalid_argument,
1020f5767e28SPavel Labath                                    "missing or invalid range list table");
1021f5767e28SPavel Labath 
1022ec671f3eSKim-Anh Tran   llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVM();
1023e21a21a9SJan Kratochvil 
1024e21a21a9SJan Kratochvil   // As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1025e21a21a9SJan Kratochvil   data.setAddressSize(m_header.GetAddressByteSize());
1026e21a21a9SJan Kratochvil   auto range_list_or_error = GetRnglistTable()->findList(data, offset);
1027f5767e28SPavel Labath   if (!range_list_or_error)
1028f5767e28SPavel Labath     return range_list_or_error.takeError();
1029f5767e28SPavel Labath 
1030f5767e28SPavel Labath   llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1031f5767e28SPavel Labath       range_list_or_error->getAbsoluteRanges(
1032f5767e28SPavel Labath           llvm::object::SectionedAddress{GetBaseAddress()},
10338036cf7fSDavid Blaikie           GetAddressByteSize(), [&](uint32_t index) {
1034f5767e28SPavel Labath             uint32_t index_size = GetAddressByteSize();
1035f5767e28SPavel Labath             dw_offset_t addr_base = GetAddrBase();
1036f5767e28SPavel Labath             lldb::offset_t offset = addr_base + index * index_size;
1037f5767e28SPavel Labath             return llvm::object::SectionedAddress{
1038f5767e28SPavel Labath                 m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1039f5767e28SPavel Labath                     &offset, index_size)};
1040f5767e28SPavel Labath           });
1041f5767e28SPavel Labath   if (!llvm_ranges)
1042f5767e28SPavel Labath     return llvm_ranges.takeError();
1043f5767e28SPavel Labath 
1044f5767e28SPavel Labath   DWARFRangeList ranges;
1045f5767e28SPavel Labath   for (const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1046f5767e28SPavel Labath     ranges.Append(DWARFRangeList::Entry(llvm_range.LowPC,
1047f5767e28SPavel Labath                                         llvm_range.HighPC - llvm_range.LowPC));
1048f5767e28SPavel Labath   }
1049f5767e28SPavel Labath   return ranges;
1050f5767e28SPavel Labath }
1051f5767e28SPavel Labath 
105278cfe1e6SPavel Labath llvm::Expected<DWARFRangeList>
FindRnglistFromIndex(uint32_t index)1053f5767e28SPavel Labath DWARFUnit::FindRnglistFromIndex(uint32_t index) {
10546d19c84cSJan Kratochvil   llvm::Expected<uint64_t> maybe_offset = GetRnglistOffset(index);
10556d19c84cSJan Kratochvil   if (!maybe_offset)
10566d19c84cSJan Kratochvil     return maybe_offset.takeError();
10576d19c84cSJan Kratochvil   return FindRnglistFromOffset(*maybe_offset);
105878cfe1e6SPavel Labath }
1059