1 //===-- DWARFUnit.h ---------------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #ifndef SymbolFileDWARF_DWARFUnit_h_
10 #define SymbolFileDWARF_DWARFUnit_h_
13 #include "DWARFDebugInfoEntry.h"
14 #include "lldb/lldb-enumerations.h"
15 #include "llvm/Support/RWMutex.h"
19 class DWARFCompileUnit;
21 class SymbolFileDWARF;
22 class SymbolFileDWARFDwo;
24 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
34 /// Base class describing the header of any kind of "unit." Some information
35 /// is specific to certain unit types. We separate this class out so we can
36 /// parse the header before deciding what specific kind of unit to construct.
37 class DWARFUnitHeader {
38 dw_offset_t m_offset = 0;
39 dw_offset_t m_length = 0;
40 uint16_t m_version = 0;
41 dw_offset_t m_abbr_offset = 0;
42 uint8_t m_unit_type = 0;
43 uint8_t m_addr_size = 0;
45 uint64_t m_type_hash = 0;
46 uint32_t m_type_offset = 0;
48 uint64_t m_dwo_id = 0;
50 DWARFUnitHeader() = default;
53 dw_offset_t GetOffset() const { return m_offset; }
54 uint16_t GetVersion() const { return m_version; }
55 uint16_t GetAddressByteSize() const { return m_addr_size; }
56 dw_offset_t GetLength() const { return m_length; }
57 dw_offset_t GetAbbrOffset() const { return m_abbr_offset; }
58 uint8_t GetUnitType() const { return m_unit_type; }
59 uint64_t GetTypeHash() const { return m_type_hash; }
60 dw_offset_t GetTypeOffset() const { return m_type_offset; }
61 bool IsTypeUnit() const {
62 return m_unit_type == DW_UT_type || m_unit_type == DW_UT_split_type;
64 uint32_t GetNextUnitOffset() const { return m_offset + m_length + 4; }
66 static llvm::Expected<DWARFUnitHeader>
67 extract(const lldb_private::DWARFDataExtractor &data, DIERef::Section section,
68 lldb::offset_t *offset_ptr);
71 class DWARFUnit : public lldb_private::UserID {
72 using die_iterator_range =
73 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
76 static llvm::Expected<DWARFUnitSP>
77 extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid,
78 const lldb_private::DWARFDataExtractor &debug_info,
79 DIERef::Section section, lldb::offset_t *offset_ptr);
82 void ExtractUnitDIEIfNeeded();
83 void ExtractDIEsIfNeeded();
85 class ScopedExtractDIEs {
88 bool m_clear_dies = false;
89 ScopedExtractDIEs(DWARFUnit &cu);
91 DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs);
92 ScopedExtractDIEs(ScopedExtractDIEs &&rhs);
93 ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs);
95 ScopedExtractDIEs ExtractDIEsScoped();
97 DWARFDIE LookupAddress(const dw_addr_t address);
98 size_t AppendDIEsWithTag(const dw_tag_t tag, std::vector<DWARFDIE> &dies,
99 uint32_t depth = UINT32_MAX) const;
100 bool Verify(lldb_private::Stream *s) const;
101 virtual void Dump(lldb_private::Stream *s) const = 0;
102 /// Get the data that contains the DIE information for this unit.
104 /// This will return the correct bytes that contain the data for
105 /// this DWARFUnit. It could be .debug_info or .debug_types
106 /// depending on where the data for this unit originates.
109 /// The correct data for the DIE information in this unit.
110 const lldb_private::DWARFDataExtractor &GetData() const;
112 /// Get the size in bytes of the unit header.
115 /// Byte size of the unit header
116 uint32_t GetHeaderByteSize() const;
118 // Offset of the initial length field.
119 dw_offset_t GetOffset() const { return m_header.GetOffset(); }
120 /// Get the size in bytes of the length field in the header.
122 /// In DWARF32 this is just 4 bytes
125 /// Byte size of the compile unit header length field
126 size_t GetLengthByteSize() const { return 4; }
128 bool ContainsDIEOffset(dw_offset_t die_offset) const {
129 return die_offset >= GetFirstDIEOffset() &&
130 die_offset < GetNextUnitOffset();
132 dw_offset_t GetFirstDIEOffset() const {
133 return GetOffset() + GetHeaderByteSize();
135 dw_offset_t GetNextUnitOffset() const { return m_header.GetNextUnitOffset(); }
136 // Size of the CU data (without initial length and without header).
137 size_t GetDebugInfoSize() const;
138 // Size of the CU data incl. header but without initial length.
139 uint32_t GetLength() const { return m_header.GetLength(); }
140 uint16_t GetVersion() const { return m_header.GetVersion(); }
141 const DWARFAbbreviationDeclarationSet *GetAbbreviations() const;
142 dw_offset_t GetAbbrevOffset() const;
143 uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); }
144 dw_addr_t GetAddrBase() const { return m_addr_base; }
145 dw_addr_t GetBaseAddress() const { return m_base_addr; }
146 dw_offset_t GetLineTableOffset();
147 dw_addr_t GetRangesBase() const { return m_ranges_base; }
148 dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; }
149 void SetAddrBase(dw_addr_t addr_base);
150 void SetRangesBase(dw_addr_t ranges_base);
151 void SetStrOffsetsBase(dw_offset_t str_offsets_base);
152 virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0;
154 lldb::ByteOrder GetByteOrder() const;
156 lldb_private::TypeSystem *GetTypeSystem();
158 const DWARFDebugAranges &GetFunctionAranges();
160 void SetBaseAddress(dw_addr_t base_addr);
162 DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); }
164 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); }
166 DWARFDIE GetDIE(dw_offset_t die_offset);
168 DWARFUnit &GetNonSkeletonUnit();
170 static uint8_t GetAddressByteSize(const DWARFUnit *cu);
172 static uint8_t GetDefaultAddressSize();
174 void *GetUserData() const;
176 void SetUserData(void *d);
178 bool Supports_DW_AT_APPLE_objc_complete_type();
180 bool DW_AT_decl_file_attributes_are_invalid();
182 bool Supports_unnamed_objc_bitfields();
184 SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; }
186 DWARFProducer GetProducer();
188 uint32_t GetProducerVersionMajor();
190 uint32_t GetProducerVersionMinor();
192 uint32_t GetProducerVersionUpdate();
194 static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val);
196 lldb::LanguageType GetLanguageType();
198 bool GetIsOptimized();
200 const lldb_private::FileSpec &GetCompilationDirectory();
201 const lldb_private::FileSpec &GetAbsolutePath();
202 lldb_private::FileSpec GetFile(size_t file_idx);
203 lldb_private::FileSpec::Style GetPathStyle();
205 SymbolFileDWARFDwo *GetDwoSymbolFile() const;
207 die_iterator_range dies() {
208 ExtractDIEsIfNeeded();
209 return die_iterator_range(m_die_array.begin(), m_die_array.end());
212 DIERef::Section GetDebugSection() const { return m_section; }
214 uint8_t GetUnitType() const { return m_header.GetUnitType(); }
215 bool IsTypeUnit() const { return m_header.IsTypeUnit(); }
217 /// Return a list of address ranges resulting from a (possibly encoded)
218 /// range list starting at a given offset in the appropriate ranges section.
219 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset) const;
221 /// Return a list of address ranges retrieved from an encoded range
222 /// list whose offset is found via a table lookup given an index (DWARF v5
224 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index) const;
227 DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid,
228 const DWARFUnitHeader &header,
229 const DWARFAbbreviationDeclarationSet &abbrevs,
230 DIERef::Section section);
232 llvm::Error ExtractHeader(SymbolFileDWARF &dwarf,
233 const lldb_private::DWARFDataExtractor &data,
234 lldb::offset_t *offset_ptr);
236 // Get the DWARF unit DWARF debug information entry. Parse the single DIE
238 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() {
239 ExtractUnitDIEIfNeeded();
240 // m_first_die_mutex is not required as m_first_die is never cleared.
246 // Get all DWARF debug informration entries. Parse all DIEs if needed.
247 const DWARFDebugInfoEntry *DIEPtr() {
248 ExtractDIEsIfNeeded();
249 if (m_die_array.empty())
251 return &m_die_array[0];
254 SymbolFileDWARF &m_dwarf;
255 std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;
256 DWARFUnitHeader m_header;
257 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr;
258 void *m_user_data = nullptr;
259 // The compile unit debug information entry item
260 DWARFDebugInfoEntry::collection m_die_array;
261 mutable llvm::sys::RWMutex m_die_array_mutex;
262 // It is used for tracking of ScopedExtractDIEs instances.
263 mutable llvm::sys::RWMutex m_die_array_scoped_mutex;
264 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked()
265 // as someone called ExtractDIEsIfNeeded().
266 std::atomic<bool> m_cancel_scopes;
267 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE.
268 // But the first element of m_die_array after ExtractUnitDIEIfNeeded()
269 // would possibly move in memory after later ExtractDIEsIfNeeded().
270 DWARFDebugInfoEntry m_first_die;
271 llvm::sys::RWMutex m_first_die_mutex;
272 // A table similar to the .debug_aranges table, but this one points to the
273 // exact DW_TAG_subprogram DIEs
274 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up;
275 dw_addr_t m_base_addr = 0;
276 DWARFProducer m_producer = eProducerInvalid;
277 uint32_t m_producer_version_major = 0;
278 uint32_t m_producer_version_minor = 0;
279 uint32_t m_producer_version_update = 0;
280 lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown;
281 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate;
282 llvm::Optional<lldb_private::FileSpec> m_comp_dir;
283 llvm::Optional<lldb_private::FileSpec> m_file_spec;
284 dw_addr_t m_addr_base = 0; // Value of DW_AT_addr_base
285 dw_addr_t m_ranges_base = 0; // Value of DW_AT_ranges_base
287 /// Value of DW_AT_stmt_list.
288 dw_offset_t m_line_table_offset = DW_INVALID_OFFSET;
290 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base.
291 const DIERef::Section m_section;
294 void ParseProducerInfo();
295 void ExtractDIEsRWLocked();
296 void ClearDIEsRWLocked();
298 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die);
300 void ComputeCompDirAndGuessPathStyle();
301 void ComputeAbsolutePath();
303 DISALLOW_COPY_AND_ASSIGN(DWARFUnit);
306 #endif // SymbolFileDWARF_DWARFUnit_h_