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: GetOffset()60 dw_offset_t GetOffset() const { return m_offset; } GetVersion()61 uint16_t GetVersion() const { return m_version; } GetAddressByteSize()62 uint16_t GetAddressByteSize() const { return m_addr_size; } GetLength()63 dw_offset_t GetLength() const { return m_length; } GetAbbrOffset()64 dw_offset_t GetAbbrOffset() const { return m_abbr_offset; } GetUnitType()65 uint8_t GetUnitType() const { return m_unit_type; } GetIndexEntry()66 const llvm::DWARFUnitIndex::Entry *GetIndexEntry() const { 67 return m_index_entry; 68 } GetTypeHash()69 uint64_t GetTypeHash() const { return m_type_hash; } GetTypeOffset()70 dw_offset_t GetTypeOffset() const { return m_type_offset; } GetDWOId()71 std::optional<uint64_t> GetDWOId() const { return m_dwo_id; } IsTypeUnit()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 } GetNextUnitOffset()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 IsDWOUnit()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. GetOffset()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 GetLengthByteSize()141 size_t GetLengthByteSize() const { return 4; } 142 ContainsDIEOffset(dw_offset_t die_offset)143 bool ContainsDIEOffset(dw_offset_t die_offset) const { 144 return die_offset >= GetFirstDIEOffset() && 145 die_offset < GetNextUnitOffset(); 146 } GetFirstDIEOffset()147 dw_offset_t GetFirstDIEOffset() const { 148 return GetOffset() + GetHeaderByteSize(); 149 } GetNextUnitOffset()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. GetLength()154 uint32_t GetLength() const { return m_header.GetLength(); } GetVersion()155 uint16_t GetVersion() const { return m_header.GetVersion(); } 156 const DWARFAbbreviationDeclarationSet *GetAbbreviations() const; 157 dw_offset_t GetAbbrevOffset() const; GetAddressByteSize()158 uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); } GetAddrBase()159 dw_addr_t GetAddrBase() const { return m_addr_base.value_or(0); } GetBaseAddress()160 dw_addr_t GetBaseAddress() const { return m_base_addr; } 161 dw_offset_t GetLineTableOffset(); GetRangesBase()162 dw_addr_t GetRangesBase() const { return m_ranges_base; } GetStrOffsetsBase()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 GetUnitDIEOnly()178 DWARFBaseDIE GetUnitDIEOnly() { return {this, GetUnitDIEPtrOnly()}; } 179 DIE()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 GetSymbolFileDWARF()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 dies()217 die_iterator_range dies() { 218 ExtractDIEsIfNeeded(); 219 return die_iterator_range(m_die_array.begin(), m_die_array.end()); 220 } 221 GetDebugSection()222 DIERef::Section GetDebugSection() const { return m_section; } 223 GetUnitType()224 uint8_t GetUnitType() const { return m_header.GetUnitType(); } IsTypeUnit()225 bool IsTypeUnit() const { return m_header.IsTypeUnit(); } 226 227 std::optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const; 228 229 /// Return a list of address ranges resulting from a (possibly encoded) 230 /// range list starting at a given offset in the appropriate ranges section. 231 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset); 232 233 /// Return a list of address ranges retrieved from an encoded range 234 /// list whose offset is found via a table lookup given an index (DWARF v5 235 /// and later). 236 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index); 237 238 /// Return a rangelist's offset based on an index. The index designates 239 /// an entry in the rangelist table's offset array and is supplied by 240 /// DW_FORM_rnglistx. 241 llvm::Expected<uint64_t> GetRnglistOffset(uint32_t Index); 242 GetLoclistOffset(uint32_t Index)243 std::optional<uint64_t> GetLoclistOffset(uint32_t Index) { 244 if (!m_loclist_table_header) 245 return std::nullopt; 246 247 std::optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry( 248 m_dwarf.GetDWARFContext().getOrLoadLocListsData().GetAsLLVM(), Index); 249 if (!Offset) 250 return std::nullopt; 251 return *Offset + m_loclists_base; 252 } 253 254 /// Return the location table for parsing the given location list data. The 255 /// format is chosen according to the unit type. Never returns null. 256 std::unique_ptr<llvm::DWARFLocationTable> 257 GetLocationTable(const lldb_private::DataExtractor &data) const; 258 259 lldb_private::DWARFDataExtractor GetLocationData() const; 260 261 /// Returns true if any DIEs in the unit match any DW_TAG values in \a tags. 262 /// 263 /// \param[in] tags 264 /// An array of dw_tag_t values to check all abbrevitions for. 265 /// 266 /// \returns 267 /// True if any DIEs match any tag in \a tags, false otherwise. 268 bool HasAny(llvm::ArrayRef<dw_tag_t> tags); 269 270 271 /// Get the fission .dwo file specific error for this compile unit. 272 /// 273 /// The skeleton compile unit only can have a DWO error. Any other type 274 /// of DWARFUnit will not have a valid DWO error. 275 /// 276 /// \returns 277 /// A valid DWO error if there is a problem with anything in the 278 /// locating or parsing inforamtion in the .dwo file GetDwoError()279 const lldb_private::Status &GetDwoError() const { return m_dwo_error; } 280 281 /// Set the fission .dwo file specific error for this compile unit. 282 /// 283 /// This helps tracks issues that arise when trying to locate or parse a 284 /// .dwo file. Things like a missing .dwo file, DWO ID mismatch, and other 285 /// .dwo errors can be stored in each compile unit so the issues can be 286 /// communicated to the user. SetDwoError(const lldb_private::Status & error)287 void SetDwoError(const lldb_private::Status &error) { m_dwo_error = error; } 288 289 protected: 290 DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, 291 const DWARFUnitHeader &header, 292 const DWARFAbbreviationDeclarationSet &abbrevs, 293 DIERef::Section section, bool is_dwo); 294 295 llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, 296 const lldb_private::DWARFDataExtractor &data, 297 lldb::offset_t *offset_ptr); 298 299 // Get the DWARF unit DWARF debug information entry. Parse the single DIE 300 // if needed. GetUnitDIEPtrOnly()301 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() { 302 ExtractUnitDIENoDwoIfNeeded(); 303 // m_first_die_mutex is not required as m_first_die is never cleared. 304 if (!m_first_die) 305 return nullptr; 306 return &m_first_die; 307 } 308 309 // Get all DWARF debug informration entries. Parse all DIEs if needed. DIEPtr()310 const DWARFDebugInfoEntry *DIEPtr() { 311 ExtractDIEsIfNeeded(); 312 if (m_die_array.empty()) 313 return nullptr; 314 return &m_die_array[0]; 315 } 316 317 const std::optional<llvm::DWARFDebugRnglistTable> &GetRnglistTable(); 318 319 lldb_private::DWARFDataExtractor GetRnglistData() const; 320 321 SymbolFileDWARF &m_dwarf; 322 std::shared_ptr<DWARFUnit> m_dwo; 323 DWARFUnitHeader m_header; 324 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr; 325 void *m_user_data = nullptr; 326 // The compile unit debug information entry item 327 DWARFDebugInfoEntry::collection m_die_array; 328 mutable llvm::sys::RWMutex m_die_array_mutex; 329 // It is used for tracking of ScopedExtractDIEs instances. 330 mutable llvm::sys::RWMutex m_die_array_scoped_mutex; 331 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked() 332 // as someone called ExtractDIEsIfNeeded(). 333 std::atomic<bool> m_cancel_scopes; 334 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE. 335 // But the first element of m_die_array after ExtractUnitDIEIfNeeded() 336 // would possibly move in memory after later ExtractDIEsIfNeeded(). 337 DWARFDebugInfoEntry m_first_die; 338 llvm::sys::RWMutex m_first_die_mutex; 339 // A table similar to the .debug_aranges table, but this one points to the 340 // exact DW_TAG_subprogram DIEs 341 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up; 342 dw_addr_t m_base_addr = 0; 343 DWARFProducer m_producer = eProducerInvalid; 344 llvm::VersionTuple m_producer_version; 345 std::optional<uint64_t> m_language_type; 346 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate; 347 std::optional<lldb_private::FileSpec> m_comp_dir; 348 std::optional<lldb_private::FileSpec> m_file_spec; 349 std::optional<dw_addr_t> m_addr_base; ///< Value of DW_AT_addr_base. 350 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base. 351 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base. 352 std::optional<uint64_t> m_gnu_addr_base; 353 std::optional<uint64_t> m_gnu_ranges_base; 354 355 /// Value of DW_AT_stmt_list. 356 dw_offset_t m_line_table_offset = DW_INVALID_OFFSET; 357 358 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base. 359 360 std::optional<llvm::DWARFDebugRnglistTable> m_rnglist_table; 361 bool m_rnglist_table_done = false; 362 std::optional<llvm::DWARFListTableHeader> m_loclist_table_header; 363 364 const DIERef::Section m_section; 365 bool m_is_dwo; 366 bool m_has_parsed_non_skeleton_unit; 367 /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5). 368 std::optional<uint64_t> m_dwo_id; 369 /// If we get an error when trying to load a .dwo file, save that error here. 370 /// Errors include .dwo/.dwp file not found, or the .dwp/.dwp file was found 371 /// but DWO ID doesn't match, etc. 372 lldb_private::Status m_dwo_error; 373 374 private: 375 void ParseProducerInfo(); 376 void ExtractDIEsRWLocked(); 377 void ClearDIEsRWLocked(); 378 379 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die); 380 void SetDwoStrOffsetsBase(); 381 382 void ComputeCompDirAndGuessPathStyle(); 383 void ComputeAbsolutePath(); 384 385 DWARFUnit(const DWARFUnit &) = delete; 386 const DWARFUnit &operator=(const DWARFUnit &) = delete; 387 }; 388 389 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H 390