1 //===-- DWARFUnit.cpp -----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/DebugInfo/DWARF/DWARFAddressRange.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
19 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20 #include "llvm/Object/Error.h"
22 #include "DWARFCompileUnit.h"
23 #include "DWARFDebugAranges.h"
24 #include "DWARFDebugInfo.h"
25 #include "DWARFTypeUnit.h"
26 #include "LogChannelDWARF.h"
27 #include "SymbolFileDWARFDwo.h"
31 using namespace lldb_private
;
32 using namespace lldb_private::dwarf
;
33 using namespace lldb_private::plugin::dwarf
;
37 DWARFUnit::DWARFUnit(SymbolFileDWARF
&dwarf
, lldb::user_id_t uid
,
38 const llvm::DWARFUnitHeader
&header
,
39 const llvm::DWARFAbbreviationDeclarationSet
&abbrevs
,
40 DIERef::Section section
, bool is_dwo
)
41 : UserID(uid
), m_dwarf(dwarf
), m_header(header
), m_abbrevs(&abbrevs
),
42 m_cancel_scopes(false), m_section(section
), m_is_dwo(is_dwo
),
43 m_has_parsed_non_skeleton_unit(false), m_dwo_id(header
.getDWOId()) {}
45 DWARFUnit::~DWARFUnit() = default;
47 // Parses first DIE of a compile unit, excluding DWO.
48 void DWARFUnit::ExtractUnitDIENoDwoIfNeeded() {
50 llvm::sys::ScopedReader
lock(m_first_die_mutex
);
52 return; // Already parsed
54 llvm::sys::ScopedWriter
lock(m_first_die_mutex
);
56 return; // Already parsed
58 ElapsedTime
elapsed(m_dwarf
.GetDebugInfoParseTimeRef());
60 // Set the offset to that of the first DIE and calculate the start of the
61 // next compilation unit header.
62 lldb::offset_t offset
= GetFirstDIEOffset();
64 // We are in our compile unit, parse starting at the offset we were told to
66 const DWARFDataExtractor
&data
= GetData();
67 if (offset
< GetNextUnitOffset() &&
68 m_first_die
.Extract(data
, *this, &offset
)) {
69 AddUnitDIE(m_first_die
);
74 // Parses first DIE of a compile unit including DWO.
75 void DWARFUnit::ExtractUnitDIEIfNeeded() {
76 ExtractUnitDIENoDwoIfNeeded();
78 if (m_has_parsed_non_skeleton_unit
)
81 m_has_parsed_non_skeleton_unit
= true;
85 return; // No DWO file.
87 std::shared_ptr
<SymbolFileDWARFDwo
> dwo_symbol_file
=
88 m_dwarf
.GetDwoSymbolFileForCompileUnit(*this, m_first_die
);
92 DWARFUnit
*dwo_cu
= dwo_symbol_file
->GetDWOCompileUnitForHash(*m_dwo_id
);
95 SetDwoError(Status::FromErrorStringWithFormatv(
96 "unable to load .dwo file from \"{0}\" due to ID ({1:x16}) mismatch "
97 "for skeleton DIE at {2:x8}",
98 dwo_symbol_file
->GetObjectFile()->GetFileSpec().GetPath(), *m_dwo_id
,
99 m_first_die
.GetOffset()));
100 return; // Can't fetch the compile unit from the dwo file.
103 // Link the DWO unit to this object, if it hasn't been linked already (this
104 // can happen when we have an index, and the DWO unit is parsed first).
105 if (!dwo_cu
->LinkToSkeletonUnit(*this)) {
106 SetDwoError(Status::FromErrorStringWithFormatv(
107 "multiple compile units with Dwo ID {0:x16}", *m_dwo_id
));
111 DWARFBaseDIE dwo_cu_die
= dwo_cu
->GetUnitDIEOnly();
112 if (!dwo_cu_die
.IsValid()) {
113 // Can't fetch the compile unit DIE from the dwo file.
114 SetDwoError(Status::FromErrorStringWithFormatv(
115 "unable to extract compile unit DIE from .dwo file for skeleton "
117 m_first_die
.GetOffset()));
121 // Here for DWO CU we want to use the address base set in the skeleton unit
122 // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base
123 // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_*
124 // attributes which were applicable to the DWO units. The corresponding
125 // DW_AT_* attributes standardized in DWARF v5 are also applicable to the
126 // main unit in contrast.
128 dwo_cu
->SetAddrBase(*m_addr_base
);
129 else if (m_gnu_addr_base
)
130 dwo_cu
->SetAddrBase(*m_gnu_addr_base
);
132 if (GetVersion() <= 4 && m_gnu_ranges_base
)
133 dwo_cu
->SetRangesBase(*m_gnu_ranges_base
);
134 else if (dwo_symbol_file
->GetDWARFContext()
135 .getOrLoadRngListsData()
137 dwo_cu
->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32
));
139 if (GetVersion() >= 5 &&
140 dwo_symbol_file
->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
142 dwo_cu
->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32
));
144 dwo_cu
->SetBaseAddress(GetBaseAddress());
146 m_dwo
= std::shared_ptr
<DWARFUnit
>(std::move(dwo_symbol_file
), dwo_cu
);
149 // Parses a compile unit and indexes its DIEs if it hasn't already been done.
150 // It will leave this compile unit extracted forever.
151 void DWARFUnit::ExtractDIEsIfNeeded() {
152 m_cancel_scopes
= true;
155 llvm::sys::ScopedReader
lock(m_die_array_mutex
);
156 if (!m_die_array
.empty())
157 return; // Already parsed
159 llvm::sys::ScopedWriter
lock(m_die_array_mutex
);
160 if (!m_die_array
.empty())
161 return; // Already parsed
163 ExtractDIEsRWLocked();
166 // Parses a compile unit and indexes its DIEs if it hasn't already been done.
167 // It will clear this compile unit after returned instance gets out of scope,
168 // no other ScopedExtractDIEs instance is running for this compile unit
169 // and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs
171 DWARFUnit::ScopedExtractDIEs
DWARFUnit::ExtractDIEsScoped() {
172 ScopedExtractDIEs
scoped(*this);
175 llvm::sys::ScopedReader
lock(m_die_array_mutex
);
176 if (!m_die_array
.empty())
177 return scoped
; // Already parsed
179 llvm::sys::ScopedWriter
lock(m_die_array_mutex
);
180 if (!m_die_array
.empty())
181 return scoped
; // Already parsed
183 // Otherwise m_die_array would be already populated.
184 lldbassert(!m_cancel_scopes
);
186 ExtractDIEsRWLocked();
187 scoped
.m_clear_dies
= true;
191 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit
&cu
) : m_cu(&cu
) {
192 m_cu
->m_die_array_scoped_mutex
.lock_shared();
195 DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
198 m_cu
->m_die_array_scoped_mutex
.unlock_shared();
199 if (!m_clear_dies
|| m_cu
->m_cancel_scopes
)
201 // Be sure no other ScopedExtractDIEs is running anymore.
202 llvm::sys::ScopedWriter
lock_scoped(m_cu
->m_die_array_scoped_mutex
);
203 llvm::sys::ScopedWriter
lock(m_cu
->m_die_array_mutex
);
204 if (m_cu
->m_cancel_scopes
)
206 m_cu
->ClearDIEsRWLocked();
209 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs
&&rhs
)
210 : m_cu(rhs
.m_cu
), m_clear_dies(rhs
.m_clear_dies
) {
214 DWARFUnit::ScopedExtractDIEs
&
215 DWARFUnit::ScopedExtractDIEs::operator=(DWARFUnit::ScopedExtractDIEs
&&rhs
) {
218 m_clear_dies
= rhs
.m_clear_dies
;
222 // Parses a compile unit and indexes its DIEs, m_die_array_mutex must be
223 // held R/W and m_die_array must be empty.
224 void DWARFUnit::ExtractDIEsRWLocked() {
225 llvm::sys::ScopedWriter
first_die_lock(m_first_die_mutex
);
227 ElapsedTime
elapsed(m_dwarf
.GetDebugInfoParseTimeRef());
230 llvm::formatv("{0:x16}: DWARFUnit::ExtractDIEsIfNeeded()", GetOffset())
234 // Set the offset to that of the first DIE and calculate the start of the
235 // next compilation unit header.
236 lldb::offset_t offset
= GetFirstDIEOffset();
237 lldb::offset_t next_cu_offset
= GetNextUnitOffset();
239 DWARFDebugInfoEntry die
;
242 // We are in our compile unit, parse starting at the offset we were told to
244 const DWARFDataExtractor
&data
= GetData();
245 std::vector
<uint32_t> die_index_stack
;
246 die_index_stack
.reserve(32);
247 die_index_stack
.push_back(0);
248 bool prev_die_had_children
= false;
249 while (offset
< next_cu_offset
&& die
.Extract(data
, *this, &offset
)) {
250 const bool null_die
= die
.IsNULL();
252 assert(m_die_array
.empty() && "Compile unit DIE already added");
254 // The average bytes per DIE entry has been seen to be around 14-20 so
255 // lets pre-reserve half of that since we are now stripping the NULL
258 // Only reserve the memory if we are adding children of the main
259 // compile unit DIE. The compile unit DIE is always the first entry, so
260 // if our size is 1, then we are adding the first compile unit child
261 // DIE and should reserve the memory.
262 m_die_array
.reserve(GetDebugInfoSize() / 24);
263 m_die_array
.push_back(die
);
266 AddUnitDIE(m_die_array
.front());
268 // With -fsplit-dwarf-inlining, clang will emit non-empty skeleton compile
269 // units. We are not able to access these DIE *and* the dwo file
270 // simultaneously. We also don't need to do that as the dwo file will
271 // contain a superset of information. So, we don't even attempt to parse
272 // any remaining DIEs.
274 m_die_array
.front().SetHasChildren(false);
280 if (prev_die_had_children
) {
281 // This will only happen if a DIE says is has children but all it
282 // contains is a NULL tag. Since we are removing the NULL DIEs from
283 // the list (saves up to 25% in C++ code), we need a way to let the
284 // DIE know that it actually doesn't have children.
285 if (!m_die_array
.empty())
286 m_die_array
.back().SetHasChildren(false);
289 die
.SetParentIndex(m_die_array
.size() - die_index_stack
[depth
- 1]);
291 if (die_index_stack
.back())
292 m_die_array
[die_index_stack
.back()].SetSiblingIndex(
293 m_die_array
.size() - die_index_stack
.back());
295 // Only push the DIE if it isn't a NULL DIE
296 m_die_array
.push_back(die
);
302 if (!die_index_stack
.empty())
303 die_index_stack
.pop_back();
307 prev_die_had_children
= false;
309 die_index_stack
.back() = m_die_array
.size() - 1;
311 const bool die_has_children
= die
.HasChildren();
312 if (die_has_children
) {
313 die_index_stack
.push_back(0);
316 prev_die_had_children
= die_has_children
;
320 break; // We are done with this compile unit!
323 if (!m_die_array
.empty()) {
324 // The last die cannot have children (if it did, it wouldn't be the last
325 // one). This only makes a difference for malformed dwarf that does not have
326 // a terminating null die.
327 m_die_array
.back().SetHasChildren(false);
330 // Only needed for the assertion.
331 m_first_die
.SetHasChildren(m_die_array
.front().HasChildren());
332 lldbassert(m_first_die
== m_die_array
.front());
334 m_first_die
= m_die_array
.front();
337 m_die_array
.shrink_to_fit();
340 m_dwo
->ExtractDIEsIfNeeded();
343 // This is used when a split dwarf is enabled.
344 // A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute
345 // that points to the first string offset of the CU contribution to the
346 // .debug_str_offsets. At the same time, the corresponding split debug unit also
347 // may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and
348 // for that case, we should find the offset (skip the section header).
349 void DWARFUnit::SetDwoStrOffsetsBase() {
350 lldb::offset_t baseOffset
= 0;
352 if (const llvm::DWARFUnitIndex::Entry
*entry
= m_header
.getIndexEntry()) {
353 if (const auto *contribution
=
354 entry
->getContribution(llvm::DW_SECT_STR_OFFSETS
))
355 baseOffset
= contribution
->getOffset();
360 if (GetVersion() >= 5) {
361 const DWARFDataExtractor
&strOffsets
=
362 GetSymbolFileDWARF().GetDWARFContext().getOrLoadStrOffsetsData();
363 uint64_t length
= strOffsets
.GetU32(&baseOffset
);
364 if (length
== 0xffffffff)
365 length
= strOffsets
.GetU64(&baseOffset
);
368 if (strOffsets
.GetU16(&baseOffset
) < 5)
375 SetStrOffsetsBase(baseOffset
);
378 std::optional
<uint64_t> DWARFUnit::GetDWOId() {
379 ExtractUnitDIENoDwoIfNeeded();
383 // m_die_array_mutex must be already held as read/write.
384 void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry
&cu_die
) {
385 DWARFAttributes attributes
= cu_die
.GetAttributes(this);
387 // Extract DW_AT_addr_base first, as other attributes may need it.
388 for (size_t i
= 0; i
< attributes
.Size(); ++i
) {
389 if (attributes
.AttributeAtIndex(i
) != DW_AT_addr_base
)
391 DWARFFormValue form_value
;
392 if (attributes
.ExtractFormValueAtIndex(i
, form_value
)) {
393 SetAddrBase(form_value
.Unsigned());
398 for (size_t i
= 0; i
< attributes
.Size(); ++i
) {
399 dw_attr_t attr
= attributes
.AttributeAtIndex(i
);
400 DWARFFormValue form_value
;
401 if (!attributes
.ExtractFormValueAtIndex(i
, form_value
))
406 case DW_AT_loclists_base
:
407 SetLoclistsBase(form_value
.Unsigned());
409 case DW_AT_rnglists_base
:
410 SetRangesBase(form_value
.Unsigned());
412 case DW_AT_str_offsets_base
:
413 SetStrOffsetsBase(form_value
.Unsigned());
416 SetBaseAddress(form_value
.Address());
419 // If the value was already set by DW_AT_low_pc, don't update it.
420 if (m_base_addr
== LLDB_INVALID_ADDRESS
)
421 SetBaseAddress(form_value
.Address());
423 case DW_AT_stmt_list
:
424 m_line_table_offset
= form_value
.Unsigned();
426 case DW_AT_GNU_addr_base
:
427 m_gnu_addr_base
= form_value
.Unsigned();
429 case DW_AT_GNU_ranges_base
:
430 m_gnu_ranges_base
= form_value
.Unsigned();
432 case DW_AT_GNU_dwo_id
:
433 m_dwo_id
= form_value
.Unsigned();
439 m_has_parsed_non_skeleton_unit
= true;
440 SetDwoStrOffsetsBase();
445 size_t DWARFUnit::GetDebugInfoSize() const {
446 return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
449 const llvm::DWARFAbbreviationDeclarationSet
*
450 DWARFUnit::GetAbbreviations() const {
454 dw_offset_t
DWARFUnit::GetAbbrevOffset() const {
455 return m_abbrevs
? m_abbrevs
->getOffset() : DW_INVALID_OFFSET
;
458 dw_offset_t
DWARFUnit::GetLineTableOffset() {
459 ExtractUnitDIENoDwoIfNeeded();
460 return m_line_table_offset
;
463 void DWARFUnit::SetAddrBase(dw_addr_t addr_base
) { m_addr_base
= addr_base
; }
465 // Parse the rangelist table header, including the optional array of offsets
466 // following it (DWARF v5 and later).
467 template <typename ListTableType
>
468 static llvm::Expected
<ListTableType
>
469 ParseListTableHeader(const llvm::DWARFDataExtractor
&data
, uint64_t offset
,
470 DwarfFormat format
) {
471 // We are expected to be called with Offset 0 or pointing just past the table
472 // header. Correct Offset in the latter case so that it points to the start
475 // This means DW_AT_rnglists_base is missing and therefore DW_FORM_rnglistx
476 // cannot be handled. Returning a default-constructed ListTableType allows
477 // DW_FORM_sec_offset to be supported.
478 return ListTableType();
481 uint64_t HeaderSize
= llvm::DWARFListTableHeader::getHeaderSize(format
);
482 if (offset
< HeaderSize
)
483 return llvm::createStringError(std::errc::invalid_argument
,
484 "did not detect a valid"
485 " list table with base = 0x%" PRIx64
"\n",
487 offset
-= HeaderSize
;
489 if (llvm::Error E
= Table
.extractHeaderAndOffsets(data
, &offset
))
494 void DWARFUnit::SetLoclistsBase(dw_addr_t loclists_base
) {
496 if (const llvm::DWARFUnitIndex::Entry
*entry
= m_header
.getIndexEntry()) {
497 const auto *contribution
= entry
->getContribution(llvm::DW_SECT_LOCLISTS
);
499 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
500 "Failed to find location list contribution for CU with DWO Id "
505 offset
+= contribution
->getOffset();
507 m_loclists_base
= loclists_base
;
509 uint64_t header_size
= llvm::DWARFListTableHeader::getHeaderSize(DWARF32
);
510 if (loclists_base
< header_size
)
513 m_loclist_table_header
.emplace(".debug_loclists", "locations");
514 offset
+= loclists_base
- header_size
;
515 if (llvm::Error E
= m_loclist_table_header
->extract(
516 m_dwarf
.GetDWARFContext().getOrLoadLocListsData().GetAsLLVMDWARF(),
518 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
519 "Failed to extract location list table at offset {0:x16} (location "
520 "list base: {1:x16}): {2}",
521 offset
, loclists_base
, toString(std::move(E
)).c_str());
525 std::unique_ptr
<llvm::DWARFLocationTable
>
526 DWARFUnit::GetLocationTable(const DataExtractor
&data
) const {
527 llvm::DWARFDataExtractor
llvm_data(
528 data
.GetData(), data
.GetByteOrder() == lldb::eByteOrderLittle
,
529 data
.GetAddressByteSize());
531 if (m_is_dwo
|| GetVersion() >= 5)
532 return std::make_unique
<llvm::DWARFDebugLoclists
>(llvm_data
, GetVersion());
533 return std::make_unique
<llvm::DWARFDebugLoc
>(llvm_data
);
536 DWARFDataExtractor
DWARFUnit::GetLocationData() const {
537 DWARFContext
&Ctx
= GetSymbolFileDWARF().GetDWARFContext();
538 const DWARFDataExtractor
&data
=
539 GetVersion() >= 5 ? Ctx
.getOrLoadLocListsData() : Ctx
.getOrLoadLocData();
540 if (const llvm::DWARFUnitIndex::Entry
*entry
= m_header
.getIndexEntry()) {
541 if (const auto *contribution
= entry
->getContribution(
542 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS
: llvm::DW_SECT_EXT_LOC
))
543 return DWARFDataExtractor(data
, contribution
->getOffset(),
544 contribution
->getLength32());
545 return DWARFDataExtractor();
550 DWARFDataExtractor
DWARFUnit::GetRnglistData() const {
551 DWARFContext
&Ctx
= GetSymbolFileDWARF().GetDWARFContext();
552 const DWARFDataExtractor
&data
= Ctx
.getOrLoadRngListsData();
553 if (const llvm::DWARFUnitIndex::Entry
*entry
= m_header
.getIndexEntry()) {
554 if (const auto *contribution
=
555 entry
->getContribution(llvm::DW_SECT_RNGLISTS
))
556 return DWARFDataExtractor(data
, contribution
->getOffset(),
557 contribution
->getLength32());
558 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
559 "Failed to find range list contribution for CU with signature {0:x16}",
560 entry
->getSignature());
562 return DWARFDataExtractor();
567 void DWARFUnit::SetRangesBase(dw_addr_t ranges_base
) {
568 lldbassert(!m_rnglist_table_done
);
570 m_ranges_base
= ranges_base
;
573 const std::optional
<llvm::DWARFDebugRnglistTable
> &
574 DWARFUnit::GetRnglistTable() {
575 if (GetVersion() >= 5 && !m_rnglist_table_done
) {
576 m_rnglist_table_done
= true;
577 if (auto table_or_error
=
578 ParseListTableHeader
<llvm::DWARFDebugRnglistTable
>(
579 GetRnglistData().GetAsLLVMDWARF(), m_ranges_base
, DWARF32
))
580 m_rnglist_table
= std::move(table_or_error
.get());
582 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
583 "Failed to extract range list table at offset {0:x16}: {1}",
584 m_ranges_base
, toString(table_or_error
.takeError()).c_str());
586 return m_rnglist_table
;
589 // This function is called only for DW_FORM_rnglistx.
590 llvm::Expected
<uint64_t> DWARFUnit::GetRnglistOffset(uint32_t Index
) {
591 if (!GetRnglistTable())
592 return llvm::createStringError(std::errc::invalid_argument
,
593 "missing or invalid range list table");
595 return llvm::createStringError(
596 std::errc::invalid_argument
,
597 llvm::formatv("DW_FORM_rnglistx cannot be used without "
598 "DW_AT_rnglists_base for CU at {0:x16}",
602 if (std::optional
<uint64_t> off
= GetRnglistTable()->getOffsetEntry(
603 GetRnglistData().GetAsLLVM(), Index
))
604 return *off
+ m_ranges_base
;
605 return llvm::createStringError(
606 std::errc::invalid_argument
,
607 "invalid range list table index %u; OffsetEntryCount is %u, "
608 "DW_AT_rnglists_base is %" PRIu64
,
609 Index
, GetRnglistTable()->getOffsetEntryCount(), m_ranges_base
);
612 void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base
) {
613 m_str_offsets_base
= str_offsets_base
;
616 dw_addr_t
DWARFUnit::ReadAddressFromDebugAddrSection(uint32_t index
) const {
617 uint32_t index_size
= GetAddressByteSize();
618 dw_offset_t addr_base
= GetAddrBase();
619 dw_addr_t offset
= addr_base
+ static_cast<dw_addr_t
>(index
) * index_size
;
620 const DWARFDataExtractor
&data
=
621 m_dwarf
.GetDWARFContext().getOrLoadAddrData();
622 if (data
.ValidOffsetForDataOfSize(offset
, index_size
))
623 return data
.GetMaxU64_unchecked(&offset
, index_size
);
624 return LLDB_INVALID_ADDRESS
;
627 // It may be called only with m_die_array_mutex held R/W.
628 void DWARFUnit::ClearDIEsRWLocked() {
630 m_die_array
.shrink_to_fit();
632 if (m_dwo
&& !m_dwo
->m_cancel_scopes
)
633 m_dwo
->ClearDIEsRWLocked();
636 lldb::ByteOrder
DWARFUnit::GetByteOrder() const {
637 return m_dwarf
.GetObjectFile()->GetByteOrder();
640 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr
) { m_base_addr
= base_addr
; }
642 // Compare function DWARFDebugAranges::Range structures
643 static bool CompareDIEOffset(const DWARFDebugInfoEntry
&die
,
644 const dw_offset_t die_offset
) {
645 return die
.GetOffset() < die_offset
;
650 // Get the DIE (Debug Information Entry) with the specified offset by first
651 // checking if the DIE is contained within this compile unit and grabbing the
652 // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
654 DWARFUnit::GetDIE(dw_offset_t die_offset
) {
655 if (die_offset
== DW_INVALID_OFFSET
)
656 return DWARFDIE(); // Not found
658 if (!ContainsDIEOffset(die_offset
)) {
659 GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
660 "GetDIE for DIE {0:x16} is outside of its CU {1:x16}", die_offset
,
662 return DWARFDIE(); // Not found
665 ExtractDIEsIfNeeded();
666 DWARFDebugInfoEntry::const_iterator end
= m_die_array
.cend();
667 DWARFDebugInfoEntry::const_iterator pos
=
668 lower_bound(m_die_array
.cbegin(), end
, die_offset
, CompareDIEOffset
);
670 if (pos
!= end
&& die_offset
== (*pos
).GetOffset())
671 return DWARFDIE(this, &(*pos
));
672 return DWARFDIE(); // Not found
675 llvm::StringRef
DWARFUnit::PeekDIEName(dw_offset_t die_offset
) {
676 DWARFDebugInfoEntry die
;
677 if (!die
.Extract(GetData(), *this, &die_offset
))
678 return llvm::StringRef();
680 // Does die contain a DW_AT_Name?
681 if (const char *name
=
682 die
.GetAttributeValueAsString(this, DW_AT_name
, nullptr))
685 // Does its DW_AT_specification or DW_AT_abstract_origin contain an AT_Name?
686 for (auto attr
: {DW_AT_specification
, DW_AT_abstract_origin
}) {
687 DWARFFormValue form_value
;
688 if (!die
.GetAttributeValue(this, attr
, form_value
))
690 auto [unit
, offset
] = form_value
.ReferencedUnitAndOffset();
692 if (auto name
= unit
->PeekDIEName(offset
); !name
.empty())
696 return llvm::StringRef();
699 DWARFUnit
&DWARFUnit::GetNonSkeletonUnit() {
700 ExtractUnitDIEIfNeeded();
706 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit
*cu
) {
708 return cu
->GetAddressByteSize();
709 return DWARFUnit::GetDefaultAddressSize();
712 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
714 DWARFCompileUnit
*DWARFUnit::GetSkeletonUnit() {
715 if (m_skeleton_unit
.load() == nullptr && IsDWOUnit()) {
716 SymbolFileDWARFDwo
*dwo
=
717 llvm::dyn_cast_or_null
<SymbolFileDWARFDwo
>(&GetSymbolFileDWARF());
718 // Do a reverse lookup if the skeleton compile unit wasn't set.
719 DWARFUnit
*candidate_skeleton_unit
=
720 dwo
? dwo
->GetBaseSymbolFile().GetSkeletonUnit(this) : nullptr;
721 if (candidate_skeleton_unit
)
722 (void)LinkToSkeletonUnit(*candidate_skeleton_unit
);
723 // Linking may fail due to a race, so be sure to return the actual value.
725 return llvm::dyn_cast_or_null
<DWARFCompileUnit
>(m_skeleton_unit
.load());
728 bool DWARFUnit::LinkToSkeletonUnit(DWARFUnit
&skeleton_unit
) {
729 DWARFUnit
*expected_unit
= nullptr;
730 if (m_skeleton_unit
.compare_exchange_strong(expected_unit
, &skeleton_unit
))
732 if (expected_unit
== &skeleton_unit
) {
733 // Exchange failed because it already contained the right value.
736 return false; // Already linked to a different unit.
739 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
740 return GetProducer() != eProducerLLVMGCC
;
743 bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
744 // llvm-gcc makes completely invalid decl file attributes and won't ever be
745 // fixed, so we need to know to ignore these.
746 return GetProducer() == eProducerLLVMGCC
;
749 bool DWARFUnit::Supports_unnamed_objc_bitfields() {
750 if (GetProducer() == eProducerClang
)
751 return GetProducerVersion() >= llvm::VersionTuple(425, 0, 13);
752 // Assume all other compilers didn't have incorrect ObjC bitfield info.
756 void DWARFUnit::ParseProducerInfo() {
757 m_producer
= eProducerOther
;
758 const DWARFDebugInfoEntry
*die
= GetUnitDIEPtrOnly();
762 llvm::StringRef
producer(
763 die
->GetAttributeValueAsString(this, DW_AT_producer
, nullptr));
764 if (producer
.empty())
767 static const RegularExpression
g_swiftlang_version_regex(
768 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
769 static const RegularExpression
g_clang_version_regex(
770 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
771 static const RegularExpression
g_llvm_gcc_regex(
772 llvm::StringRef(R
"(4\.[012]\.[01] )"
773 R
"(\(Based on Apple Inc\. build [0-9]+\) )"
774 R
"(\(LLVM build [\.0-9]+\)$)"));
776 llvm::SmallVector
<llvm::StringRef
, 3> matches
;
777 if (g_swiftlang_version_regex
.Execute(producer
, &matches
)) {
778 m_producer_version
.tryParse(matches
[1]);
779 m_producer
= eProducerSwift
;
780 } else if (producer
.contains("clang")) {
781 if (g_clang_version_regex
.Execute(producer
, &matches
))
782 m_producer_version
.tryParse(matches
[1]);
783 m_producer
= eProducerClang
;
784 } else if (producer
.contains("GNU")) {
785 m_producer
= eProducerGCC
;
786 } else if (g_llvm_gcc_regex
.Execute(producer
)) {
787 m_producer
= eProducerLLVMGCC
;
791 DWARFProducer
DWARFUnit::GetProducer() {
792 if (m_producer
== eProducerInvalid
)
797 llvm::VersionTuple
DWARFUnit::GetProducerVersion() {
798 if (m_producer_version
.empty())
800 return m_producer_version
;
803 uint64_t DWARFUnit::GetDWARFLanguageType() {
805 return *m_language_type
;
807 const DWARFDebugInfoEntry
*die
= GetUnitDIEPtrOnly();
811 m_language_type
= die
->GetAttributeValueAsUnsigned(this, DW_AT_language
, 0);
812 return *m_language_type
;
815 bool DWARFUnit::GetIsOptimized() {
816 if (m_is_optimized
== eLazyBoolCalculate
) {
817 const DWARFDebugInfoEntry
*die
= GetUnitDIEPtrOnly();
819 m_is_optimized
= eLazyBoolNo
;
820 if (die
->GetAttributeValueAsUnsigned(this, DW_AT_APPLE_optimized
, 0) ==
822 m_is_optimized
= eLazyBoolYes
;
826 return m_is_optimized
== eLazyBoolYes
;
829 FileSpec::Style
DWARFUnit::GetPathStyle() {
831 ComputeCompDirAndGuessPathStyle();
832 return m_comp_dir
->GetPathStyle();
835 const FileSpec
&DWARFUnit::GetCompilationDirectory() {
837 ComputeCompDirAndGuessPathStyle();
841 const FileSpec
&DWARFUnit::GetAbsolutePath() {
843 ComputeAbsolutePath();
847 FileSpec
DWARFUnit::GetFile(size_t file_idx
) {
848 return m_dwarf
.GetFile(*this, file_idx
);
851 // DWARF2/3 suggests the form hostname:pathname for compilation directory.
852 // Remove the host part if present.
853 static llvm::StringRef
854 removeHostnameFromPathname(llvm::StringRef path_from_dwarf
) {
855 if (!path_from_dwarf
.contains(':'))
856 return path_from_dwarf
;
857 llvm::StringRef host
, path
;
858 std::tie(host
, path
) = path_from_dwarf
.split(':');
860 if (host
.contains('/'))
861 return path_from_dwarf
;
863 // check whether we have a windows path, and so the first character is a
864 // drive-letter not a hostname.
865 if (host
.size() == 1 && llvm::isAlpha(host
[0]) &&
866 (path
.starts_with("\\") || path
.starts_with("/")))
867 return path_from_dwarf
;
872 void DWARFUnit::ComputeCompDirAndGuessPathStyle() {
873 m_comp_dir
= FileSpec();
874 const DWARFDebugInfoEntry
*die
= GetUnitDIEPtrOnly();
878 llvm::StringRef comp_dir
= removeHostnameFromPathname(
879 die
->GetAttributeValueAsString(this, DW_AT_comp_dir
, nullptr));
880 if (!comp_dir
.empty()) {
881 FileSpec::Style comp_dir_style
=
882 FileSpec::GuessPathStyle(comp_dir
).value_or(FileSpec::Style::native
);
883 m_comp_dir
= FileSpec(comp_dir
, comp_dir_style
);
885 // Try to detect the style based on the DW_AT_name attribute, but just store
886 // the detected style in the m_comp_dir field.
888 die
->GetAttributeValueAsString(this, DW_AT_name
, nullptr);
889 m_comp_dir
= FileSpec(
890 "", FileSpec::GuessPathStyle(name
).value_or(FileSpec::Style::native
));
894 void DWARFUnit::ComputeAbsolutePath() {
895 m_file_spec
= FileSpec();
896 const DWARFDebugInfoEntry
*die
= GetUnitDIEPtrOnly();
901 FileSpec(die
->GetAttributeValueAsString(this, DW_AT_name
, nullptr),
904 if (m_file_spec
->IsRelative())
905 m_file_spec
->MakeAbsolute(GetCompilationDirectory());
908 SymbolFileDWARFDwo
*DWARFUnit::GetDwoSymbolFile(bool load_all_debug_info
) {
909 if (load_all_debug_info
)
910 ExtractUnitDIEIfNeeded();
912 return &llvm::cast
<SymbolFileDWARFDwo
>(m_dwo
->GetSymbolFileDWARF());
916 const DWARFDebugAranges
&DWARFUnit::GetFunctionAranges() {
917 if (m_func_aranges_up
== nullptr) {
918 m_func_aranges_up
= std::make_unique
<DWARFDebugAranges
>();
919 const DWARFDebugInfoEntry
*die
= DIEPtr();
921 die
->BuildFunctionAddressRangeTable(this, m_func_aranges_up
.get());
924 const DWARFDebugInfoEntry
*dwo_die
= m_dwo
->DIEPtr();
926 dwo_die
->BuildFunctionAddressRangeTable(m_dwo
.get(),
927 m_func_aranges_up
.get());
930 const bool minimize
= false;
931 m_func_aranges_up
->Sort(minimize
);
933 return *m_func_aranges_up
;
936 llvm::Expected
<DWARFUnitSP
>
937 DWARFUnit::extract(SymbolFileDWARF
&dwarf
, user_id_t uid
,
938 const DWARFDataExtractor
&debug_info
,
939 DIERef::Section section
, lldb::offset_t
*offset_ptr
) {
940 assert(debug_info
.ValidOffset(*offset_ptr
));
942 DWARFContext
&context
= dwarf
.GetDWARFContext();
944 // FIXME: Either properly map between DIERef::Section and
945 // llvm::DWARFSectionKind or switch to llvm's definition entirely.
946 llvm::DWARFSectionKind section_kind_llvm
=
947 section
== DIERef::Section::DebugInfo
948 ? llvm::DWARFSectionKind::DW_SECT_INFO
949 : llvm::DWARFSectionKind::DW_SECT_EXT_TYPES
;
951 llvm::DWARFDataExtractor debug_info_llvm
= debug_info
.GetAsLLVMDWARF();
952 llvm::DWARFUnitHeader header
;
953 if (llvm::Error extract_err
= header
.extract(
954 context
.GetAsLLVM(), debug_info_llvm
, offset_ptr
, section_kind_llvm
))
955 return std::move(extract_err
);
957 if (context
.isDwo()) {
958 const llvm::DWARFUnitIndex::Entry
*entry
= nullptr;
959 const llvm::DWARFUnitIndex
&index
= header
.isTypeUnit()
960 ? context
.GetAsLLVM().getTUIndex()
961 : context
.GetAsLLVM().getCUIndex();
963 if (header
.isTypeUnit())
964 entry
= index
.getFromHash(header
.getTypeHash());
965 else if (auto dwo_id
= header
.getDWOId())
966 entry
= index
.getFromHash(*dwo_id
);
969 entry
= index
.getFromOffset(header
.getOffset());
971 if (llvm::Error err
= header
.applyIndexEntry(entry
))
972 return std::move(err
);
975 const llvm::DWARFDebugAbbrev
*abbr
= dwarf
.DebugAbbrev();
977 return llvm::make_error
<llvm::object::GenericBinaryError
>(
978 "No debug_abbrev data");
980 bool abbr_offset_OK
=
981 dwarf
.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
982 header
.getAbbrOffset());
984 return llvm::make_error
<llvm::object::GenericBinaryError
>(
985 "Abbreviation offset for unit is not valid");
987 llvm::Expected
<const llvm::DWARFAbbreviationDeclarationSet
*> abbrevs_or_err
=
988 abbr
->getAbbreviationDeclarationSet(header
.getAbbrOffset());
990 return abbrevs_or_err
.takeError();
992 const llvm::DWARFAbbreviationDeclarationSet
*abbrevs
= *abbrevs_or_err
;
994 return llvm::make_error
<llvm::object::GenericBinaryError
>(
995 "No abbrev exists at the specified offset.");
997 bool is_dwo
= dwarf
.GetDWARFContext().isDwo();
998 if (header
.isTypeUnit())
1000 new DWARFTypeUnit(dwarf
, uid
, header
, *abbrevs
, section
, is_dwo
));
1002 new DWARFCompileUnit(dwarf
, uid
, header
, *abbrevs
, section
, is_dwo
));
1005 const lldb_private::DWARFDataExtractor
&DWARFUnit::GetData() const {
1006 return m_section
== DIERef::Section::DebugTypes
1007 ? m_dwarf
.GetDWARFContext().getOrLoadDebugTypesData()
1008 : m_dwarf
.GetDWARFContext().getOrLoadDebugInfoData();
1011 uint32_t DWARFUnit::GetHeaderByteSize() const {
1012 switch (m_header
.getUnitType()) {
1013 case llvm::dwarf::DW_UT_compile
:
1014 case llvm::dwarf::DW_UT_partial
:
1015 return GetVersion() < 5 ? 11 : 12;
1016 case llvm::dwarf::DW_UT_skeleton
:
1017 case llvm::dwarf::DW_UT_split_compile
:
1019 case llvm::dwarf::DW_UT_type
:
1020 case llvm::dwarf::DW_UT_split_type
:
1021 return GetVersion() < 5 ? 23 : 24;
1023 llvm_unreachable("invalid UnitType.");
1026 std::optional
<uint64_t>
1027 DWARFUnit::GetStringOffsetSectionItem(uint32_t index
) const {
1028 lldb::offset_t offset
= GetStrOffsetsBase() + index
* 4;
1029 return m_dwarf
.GetDWARFContext().getOrLoadStrOffsetsData().GetU32(&offset
);
1032 llvm::Expected
<DWARFRangeList
>
1033 DWARFUnit::FindRnglistFromOffset(dw_offset_t offset
) {
1034 llvm::DWARFAddressRangesVector llvm_ranges
;
1035 if (GetVersion() <= 4) {
1036 llvm::DWARFDataExtractor data
=
1037 m_dwarf
.GetDWARFContext().getOrLoadRangesData().GetAsLLVMDWARF();
1038 data
.setAddressSize(m_header
.getAddressByteSize());
1040 llvm::DWARFDebugRangeList list
;
1041 if (llvm::Error e
= list
.extract(data
, &offset
))
1043 llvm_ranges
= list
.getAbsoluteRanges(
1044 llvm::object::SectionedAddress
{GetBaseAddress()});
1046 if (!GetRnglistTable())
1047 return llvm::createStringError(std::errc::invalid_argument
,
1048 "missing or invalid range list table");
1050 llvm::DWARFDataExtractor data
= GetRnglistData().GetAsLLVMDWARF();
1052 // As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1053 data
.setAddressSize(m_header
.getAddressByteSize());
1054 auto range_list_or_error
= GetRnglistTable()->findList(data
, offset
);
1055 if (!range_list_or_error
)
1056 return range_list_or_error
.takeError();
1058 llvm::Expected
<llvm::DWARFAddressRangesVector
> expected_llvm_ranges
=
1059 range_list_or_error
->getAbsoluteRanges(
1060 llvm::object::SectionedAddress
{GetBaseAddress()},
1061 GetAddressByteSize(), [&](uint32_t index
) {
1062 uint32_t index_size
= GetAddressByteSize();
1063 dw_offset_t addr_base
= GetAddrBase();
1064 lldb::offset_t offset
=
1065 addr_base
+ static_cast<lldb::offset_t
>(index
) * index_size
;
1066 return llvm::object::SectionedAddress
{
1067 m_dwarf
.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1068 &offset
, index_size
)};
1070 if (!expected_llvm_ranges
)
1071 return expected_llvm_ranges
.takeError();
1072 llvm_ranges
= std::move(*expected_llvm_ranges
);
1075 DWARFRangeList ranges
;
1076 for (const llvm::DWARFAddressRange
&llvm_range
: llvm_ranges
) {
1077 ranges
.Append(DWARFRangeList::Entry(llvm_range
.LowPC
,
1078 llvm_range
.HighPC
- llvm_range
.LowPC
));
1084 llvm::Expected
<DWARFRangeList
> DWARFUnit::FindRnglistFromIndex(uint32_t index
) {
1085 llvm::Expected
<uint64_t> maybe_offset
= GetRnglistOffset(index
);
1087 return maybe_offset
.takeError();
1088 return FindRnglistFromOffset(*maybe_offset
);
1091 bool DWARFUnit::HasAny(llvm::ArrayRef
<dw_tag_t
> tags
) {
1092 ExtractUnitDIEIfNeeded();
1094 return m_dwo
->HasAny(tags
);
1096 for (const auto &die
: m_die_array
) {
1097 for (const auto tag
: tags
) {
1098 if (tag
== die
.Tag())