Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
Warning:line 348, column 7
Value stored to 'length' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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