1//===-- DWARFUnit.h ---------------------------------------------*- C++ -*-===//
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#ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
10#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
11
12#include "DWARFDIE.h"
13#include "DWARFDebugInfoEntry.h"
14#include "lldb/Utility/XcodeSDK.h"
15#include "lldb/lldb-enumerations.h"
16#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
18#include "llvm/Support/RWMutex.h"
19#include <atomic>
20#include <optional>
21
22namespace lldb_private::plugin {
23namespace dwarf {
24class DWARFUnit;
25class DWARFCompileUnit;
26class NameToDIE;
27class SymbolFileDWARF;
28class SymbolFileDWARFDwo;
29
30typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
31
32enum DWARFProducer {
33 eProducerInvalid = 0,
34 eProducerClang,
35 eProducerGCC,
36 eProducerLLVMGCC,
37 eProducerSwift,
38 eProducerOther
39};
40
41/// Base class describing the header of any kind of "unit." Some information
42/// is specific to certain unit types. We separate this class out so we can
43/// parse the header before deciding what specific kind of unit to construct.
44class DWARFUnitHeader {
45 dw_offset_t m_offset = 0;
46 dw_offset_t m_length = 0;
47 uint16_t m_version = 0;
48 dw_offset_t m_abbr_offset = 0;
49
50 const llvm::DWARFUnitIndex::Entry *m_index_entry = nullptr;
51
52 uint8_t m_unit_type = 0;
53 uint8_t m_addr_size = 0;
54
55 uint64_t m_type_hash = 0;
56 uint32_t m_type_offset = 0;
57
58 std::optional<uint64_t> m_dwo_id;
59
60 DWARFUnitHeader() = default;
61
62public:
63 dw_offset_t GetOffset() const { return m_offset; }
64 uint16_t GetVersion() const { return m_version; }
65 uint16_t GetAddressByteSize() const { return m_addr_size; }
66 dw_offset_t GetLength() const { return m_length; }
67 dw_offset_t GetAbbrOffset() const { return m_abbr_offset; }
68 uint8_t GetUnitType() const { return m_unit_type; }
69 const llvm::DWARFUnitIndex::Entry *GetIndexEntry() const {
70 return m_index_entry;
71 }
72 uint64_t GetTypeHash() const { return m_type_hash; }
73 dw_offset_t GetTypeOffset() const { return m_type_offset; }
74 std::optional<uint64_t> GetDWOId() const { return m_dwo_id; }
75 bool IsTypeUnit() const {
76 return m_unit_type == llvm::dwarf::DW_UT_type ||
77 m_unit_type == llvm::dwarf::DW_UT_split_type;
78 }
79 dw_offset_t GetNextUnitOffset() const { return m_offset + m_length + 4; }
80
81 llvm::Error ApplyIndexEntry(const llvm::DWARFUnitIndex::Entry *index_entry);
82
83 static llvm::Expected<DWARFUnitHeader> extract(const DWARFDataExtractor &data,
84 DIERef::Section section,
85 DWARFContext &dwarf_context,
86 lldb::offset_t *offset_ptr);
87};
88
89class DWARFUnit : public UserID {
90 using die_iterator_range =
91 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
92
93public:
94 static llvm::Expected<DWARFUnitSP>
95 extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid,
96 const DWARFDataExtractor &debug_info, DIERef::Section section,
97 lldb::offset_t *offset_ptr);
98 virtual ~DWARFUnit();
99
100 bool IsDWOUnit() { return m_is_dwo; }
101 /// Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's
102 /// DW_AT_dwo_id or DW_AT_GNU_dwo_id for DWARF4 and earlier.
103 std::optional<uint64_t> GetDWOId();
104 /// Get the DWO ID from the DWARFUnitHeader only. DWARF5 skeleton units have
105 /// the DWO ID in the compile unit header and we sometimes only want to access
106 /// this cheap value without causing the more expensive attribute fetches that
107 /// GetDWOId() uses.
108 std::optional<uint64_t> GetHeaderDWOId() { return m_header.GetDWOId(); }
109 void ExtractUnitDIEIfNeeded();
110 void ExtractUnitDIENoDwoIfNeeded();
111 void ExtractDIEsIfNeeded();
112
113 class ScopedExtractDIEs {
114 DWARFUnit *m_cu;
115
116 public:
117 bool m_clear_dies = false;
118 ScopedExtractDIEs(DWARFUnit &cu);
119 ~ScopedExtractDIEs();
120 ScopedExtractDIEs(const ScopedExtractDIEs &) = delete;
121 const ScopedExtractDIEs &operator=(const ScopedExtractDIEs &) = delete;
122 ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
123 ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
124 };
125 ScopedExtractDIEs ExtractDIEsScoped();
126
127 bool Verify(Stream *s) const;
128 virtual void Dump(Stream *s) const = 0;
129 /// Get the data that contains the DIE information for this unit.
130 ///
131 /// This will return the correct bytes that contain the data for
132 /// this DWARFUnit. It could be .debug_info or .debug_types
133 /// depending on where the data for this unit originates.
134 ///
135 /// \return
136 /// The correct data for the DIE information in this unit.
137 const DWARFDataExtractor &GetData() const;
138
139 /// Get the size in bytes of the unit header.
140 ///
141 /// \return
142 /// Byte size of the unit header
143 uint32_t GetHeaderByteSize() const;
144
145 // Offset of the initial length field.
146 dw_offset_t GetOffset() const { return m_header.GetOffset(); }
147 /// Get the size in bytes of the length field in the header.
148 ///
149 /// In DWARF32 this is just 4 bytes
150 ///
151 /// \return
152 /// Byte size of the compile unit header length field
153 size_t GetLengthByteSize() const { return 4; }
154
155 bool ContainsDIEOffset(dw_offset_t die_offset) const {
156 return die_offset >= GetFirstDIEOffset() &&
157 die_offset < GetNextUnitOffset();
158 }
159 dw_offset_t GetFirstDIEOffset() const {
160 return GetOffset() + GetHeaderByteSize();
161 }
162 dw_offset_t GetNextUnitOffset() const { return m_header.GetNextUnitOffset(); }
163 // Size of the CU data (without initial length and without header).
164 size_t GetDebugInfoSize() const;
165 // Size of the CU data incl. header but without initial length.
166 dw_offset_t GetLength() const { return m_header.GetLength(); }
167 uint16_t GetVersion() const { return m_header.GetVersion(); }
168 const llvm::DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
169 dw_offset_t GetAbbrevOffset() const;
170 uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
171 dw_addr_t GetAddrBase() const { return m_addr_base.value_or(u: 0); }
172 dw_addr_t GetBaseAddress() const { return m_base_addr; }
173 dw_offset_t GetLineTableOffset();
174 dw_addr_t GetRangesBase() const { return m_ranges_base; }
175 dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }
176 void SetAddrBase(dw_addr_t addr_base);
177 void SetLoclistsBase(dw_addr_t loclists_base);
178 void SetRangesBase(dw_addr_t ranges_base);
179 void SetStrOffsetsBase(dw_offset_t str_offsets_base);
180 virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;
181
182 dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const;
183
184 lldb::ByteOrder GetByteOrder() const;
185
186 const DWARFDebugAranges &GetFunctionAranges();
187
188 void SetBaseAddress(dw_addr_t base_addr);
189
190 DWARFBaseDIE GetUnitDIEOnly() { return {this, GetUnitDIEPtrOnly()}; }
191
192 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
193
194 DWARFDIE GetDIE(dw_offset_t die_offset);
195
196 /// Returns the AT_Name of the DIE at `die_offset`, if it exists, without
197 /// parsing the entire compile unit. An empty is string is returned upon
198 /// error or if the attribute is not present.
199 llvm::StringRef PeekDIEName(dw_offset_t die_offset);
200
201 DWARFUnit &GetNonSkeletonUnit();
202
203 static uint8_t GetAddressByteSize(const DWARFUnit *cu);
204
205 static uint8_t GetDefaultAddressSize();
206
207 lldb_private::CompileUnit *GetLLDBCompUnit() const { return m_lldb_cu; }
208
209 void SetLLDBCompUnit(lldb_private::CompileUnit *cu) { m_lldb_cu = cu; }
210
211 /// Get the skeleton compile unit for a DWO file.
212 ///
213 /// We need to keep track of the skeleton compile unit for a DWO file so
214 /// we can access it. Sometimes this value is cached when the skeleton
215 /// compile unit is first parsed, but if a .dwp file parses all of the
216 /// DWARFUnits in the file, the skeleton compile unit might not have been
217 /// parsed yet, to there might not be a backlink. This accessor handles
218 /// both cases correctly and avoids crashes.
219 DWARFCompileUnit *GetSkeletonUnit();
220
221 void SetSkeletonUnit(DWARFUnit *skeleton_unit);
222
223 bool Supports_DW_AT_APPLE_objc_complete_type();
224
225 bool DW_AT_decl_file_attributes_are_invalid();
226
227 bool Supports_unnamed_objc_bitfields();
228
229 SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; }
230
231 DWARFProducer GetProducer();
232
233 llvm::VersionTuple GetProducerVersion();
234
235 uint64_t GetDWARFLanguageType();
236
237 bool GetIsOptimized();
238
239 const FileSpec &GetCompilationDirectory();
240 const FileSpec &GetAbsolutePath();
241 FileSpec GetFile(size_t file_idx);
242 FileSpec::Style GetPathStyle();
243
244 SymbolFileDWARFDwo *GetDwoSymbolFile(bool load_all_debug_info = true);
245
246 die_iterator_range dies() {
247 ExtractDIEsIfNeeded();
248 return die_iterator_range(m_die_array.begin(), m_die_array.end());
249 }
250
251 DIERef::Section GetDebugSection() const { return m_section; }
252
253 uint8_t GetUnitType() const { return m_header.GetUnitType(); }
254 bool IsTypeUnit() const { return m_header.IsTypeUnit(); }
255 /// Note that this check only works for DWARF5+.
256 bool IsSkeletonUnit() const {
257 return GetUnitType() == llvm::dwarf::DW_UT_skeleton;
258 }
259
260 std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const;
261
262 /// Return a list of address ranges resulting from a (possibly encoded)
263 /// range list starting at a given offset in the appropriate ranges section.
264 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset);
265
266 /// Return a list of address ranges retrieved from an encoded range
267 /// list whose offset is found via a table lookup given an index (DWARF v5
268 /// and later).
269 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index);
270
271 /// Return a rangelist's offset based on an index. The index designates
272 /// an entry in the rangelist table's offset array and is supplied by
273 /// DW_FORM_rnglistx.
274 llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index);
275
276 std::optional<uint64_t> GetLoclistOffset(uint32_t Index) {
277 if (!m_loclist_table_header)
278 return std::nullopt;
279
280 std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(
281 Data: m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(), Index);
282 if (!Offset)
283 return std::nullopt;
284 return *Offset + m_loclists_base;
285 }
286
287 /// Return the location table for parsing the given location list data. The
288 /// format is chosen according to the unit type. Never returns null.
289 std::unique_ptr<llvm::DWARFLocationTable>
290 GetLocationTable(const DataExtractor &data) const;
291
292 DWARFDataExtractor GetLocationData() const;
293
294 /// Returns true if any DIEs in the unit match any DW_TAG values in \a tags.
295 ///
296 /// \param[in] tags
297 /// An array of dw_tag_t values to check all abbrevitions for.
298 ///
299 /// \returns
300 /// True if any DIEs match any tag in \a tags, false otherwise.
301 bool HasAny(llvm::ArrayRef<dw_tag_t> tags);
302
303 /// Get the fission .dwo file specific error for this compile unit.
304 ///
305 /// The skeleton compile unit only can have a DWO error. Any other type
306 /// of DWARFUnit will not have a valid DWO error.
307 ///
308 /// \returns
309 /// A valid DWO error if there is a problem with anything in the
310 /// locating or parsing inforamtion in the .dwo file
311 const Status &GetDwoError() const { return m_dwo_error; }
312
313 /// Set the fission .dwo file specific error for this compile unit.
314 ///
315 /// This helps tracks issues that arise when trying to locate or parse a
316 /// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other
317 /// .dwo errors can be stored in each compile unit so the issues can be
318 /// communicated to the user.
319 void SetDwoError(const Status &error) { m_dwo_error = error; }
320
321protected:
322 DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
323 const DWARFUnitHeader &header,
324 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
325 DIERef::Section section, bool is_dwo);
326
327 llvm::Error ExtractHeader(SymbolFileDWARF &dwarf,
328 const DWARFDataExtractor &data,
329 lldb::offset_t *offset_ptr);
330
331 // Get the DWARF unit DWARF debug information entry. Parse the single DIE
332 // if needed.
333 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
334 ExtractUnitDIENoDwoIfNeeded();
335 // m_first_die_mutex is not required as m_first_die is never cleared.
336 if (!m_first_die)
337 return nullptr;
338 return &m_first_die;
339 }
340
341 // Get all DWARF debug informration entries. Parse all DIEs if needed.
342 const DWARFDebugInfoEntry *DIEPtr() {
343 ExtractDIEsIfNeeded();
344 if (m_die_array.empty())
345 return nullptr;
346 return &m_die_array[0];
347 }
348
349 const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable();
350
351 DWARFDataExtractor GetRnglistData() const;
352
353 SymbolFileDWARF &m_dwarf;
354 std::shared_ptr<DWARFUnit> m_dwo;
355 DWARFUnitHeader m_header;
356 const llvm::DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
357 lldb_private::CompileUnit *m_lldb_cu = nullptr;
358 // If this is a DWO file, we have a backlink to our skeleton compile unit.
359 DWARFUnit *m_skeleton_unit = nullptr;
360 // The compile unit debug information entry item
361 DWARFDebugInfoEntry::collection m_die_array;
362 mutable llvm::sys::RWMutex m_die_array_mutex;
363 // It is used for tracking of ScopedExtractDIEs instances.
364 mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
365 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
366 // as someone called ExtractDIEsIfNeeded().
367 std::atomic<bool> m_cancel_scopes;
368 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
369 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
370 // would possibly move in memory after later ExtractDIEsIfNeeded().
371 DWARFDebugInfoEntry m_first_die;
372 llvm::sys::RWMutex m_first_die_mutex;
373 // A table similar to the .debug_aranges table, but this one points to the
374 // exact DW_TAG_subprogram DIEs
375 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
376 dw_addr_t m_base_addr = 0;
377 DWARFProducer m_producer = eProducerInvalid;
378 llvm::VersionTuple m_producer_version;
379 std::optional<uint64_t> m_language_type;
380 LazyBool m_is_optimized = eLazyBoolCalculate;
381 std::optional<FileSpec> m_comp_dir;
382 std::optional<FileSpec> m_file_spec;
383 std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base.
384 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base.
385 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base.
386 std::optional<uint64_t> m_gnu_addr_base;
387 std::optional<uint64_t> m_gnu_ranges_base;
388
389 /// Value of DW_AT_stmt_list.
390 dw_offset_t m_line_table_offset = DW_INVALID_OFFSET;
391
392 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
393
394 std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table;
395 bool m_rnglist_table_done = false;
396 std::optional<llvm::DWARFListTableHeader> m_loclist_table_header;
397
398 const DIERef::Section m_section;
399 bool m_is_dwo;
400 bool m_has_parsed_non_skeleton_unit;
401 /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
402 std::optional<uint64_t> m_dwo_id;
403 /// If we get an error when trying to load a .dwo file, save that error here.
404 /// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found
405 /// but DWO ID doesn't match, etc.
406 Status m_dwo_error;
407
408private:
409 void ParseProducerInfo();
410 void ExtractDIEsRWLocked();
411 void ClearDIEsRWLocked();
412
413 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
414 void SetDwoStrOffsetsBase();
415
416 void ComputeCompDirAndGuessPathStyle();
417 void ComputeAbsolutePath();
418
419 DWARFUnit(const DWARFUnit &) = delete;
420 const DWARFUnit &operator=(const DWARFUnit &) = delete;
421};
422} // namespace dwarf
423} // namespace lldb_private::plugin
424
425#endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H
426

source code of lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h