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