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/lldb-enumerations.h" 15 #include "llvm/Support/RWMutex.h" 16 #include <atomic> 17 18 class DWARFUnit; 19 class DWARFCompileUnit; 20 class NameToDIE; 21 class SymbolFileDWARF; 22 class SymbolFileDWARFDwo; 23 24 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP; 25 26 enum DWARFProducer { 27 eProducerInvalid = 0, 28 eProducerClang, 29 eProducerGCC, 30 eProducerLLVMGCC, 31 eProcucerOther 32 }; 33 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 43 const llvm::DWARFUnitIndex::Entry *m_index_entry = nullptr; 44 45 uint8_t m_unit_type = 0; 46 uint8_t m_addr_size = 0; 47 48 uint64_t m_type_hash = 0; 49 uint32_t m_type_offset = 0; 50 51 uint64_t m_dwo_id = 0; 52 53 DWARFUnitHeader() = default; 54 55 public: 56 dw_offset_t GetOffset() const { return m_offset; } 57 uint16_t GetVersion() const { return m_version; } 58 uint16_t GetAddressByteSize() const { return m_addr_size; } 59 dw_offset_t GetLength() const { return m_length; } 60 dw_offset_t GetAbbrOffset() const { return m_abbr_offset; } 61 uint8_t GetUnitType() const { return m_unit_type; } 62 const llvm::DWARFUnitIndex::Entry *GetIndexEntry() const { 63 return m_index_entry; 64 } 65 uint64_t GetTypeHash() const { return m_type_hash; } 66 dw_offset_t GetTypeOffset() const { return m_type_offset; } 67 bool IsTypeUnit() const { 68 return m_unit_type == DW_UT_type || m_unit_type == DW_UT_split_type; 69 } 70 uint32_t GetNextUnitOffset() const { return m_offset + m_length + 4; } 71 72 static llvm::Expected<DWARFUnitHeader> 73 extract(const lldb_private::DWARFDataExtractor &data, DIERef::Section section, 74 lldb::offset_t *offset_ptr, const llvm::DWARFUnitIndex *index); 75 }; 76 77 class DWARFUnit : public lldb_private::UserID { 78 using die_iterator_range = 79 llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>; 80 81 public: 82 static llvm::Expected<DWARFUnitSP> 83 extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, 84 const lldb_private::DWARFDataExtractor &debug_info, 85 DIERef::Section section, lldb::offset_t *offset_ptr, 86 const llvm::DWARFUnitIndex *index); 87 virtual ~DWARFUnit(); 88 89 bool IsDWOUnit() { return m_is_dwo; } 90 91 void ExtractUnitDIEIfNeeded(); 92 void ExtractDIEsIfNeeded(); 93 94 class ScopedExtractDIEs { 95 DWARFUnit *m_cu; 96 public: 97 bool m_clear_dies = false; 98 ScopedExtractDIEs(DWARFUnit &cu); 99 ~ScopedExtractDIEs(); 100 DISALLOW_COPY_AND_ASSIGN(ScopedExtractDIEs); 101 ScopedExtractDIEs(ScopedExtractDIEs &&rhs); 102 ScopedExtractDIEs &operator=(ScopedExtractDIEs &&rhs); 103 }; 104 ScopedExtractDIEs ExtractDIEsScoped(); 105 106 DWARFDIE LookupAddress(const dw_addr_t address); 107 bool Verify(lldb_private::Stream *s) const; 108 virtual void Dump(lldb_private::Stream *s) const = 0; 109 /// Get the data that contains the DIE information for this unit. 110 /// 111 /// This will return the correct bytes that contain the data for 112 /// this DWARFUnit. It could be .debug_info or .debug_types 113 /// depending on where the data for this unit originates. 114 /// 115 /// \return 116 /// The correct data for the DIE information in this unit. 117 const lldb_private::DWARFDataExtractor &GetData() const; 118 119 /// Get the size in bytes of the unit header. 120 /// 121 /// \return 122 /// Byte size of the unit header 123 uint32_t GetHeaderByteSize() const; 124 125 // Offset of the initial length field. 126 dw_offset_t GetOffset() const { return m_header.GetOffset(); } 127 /// Get the size in bytes of the length field in the header. 128 /// 129 /// In DWARF32 this is just 4 bytes 130 /// 131 /// \return 132 /// Byte size of the compile unit header length field 133 size_t GetLengthByteSize() const { return 4; } 134 135 bool ContainsDIEOffset(dw_offset_t die_offset) const { 136 return die_offset >= GetFirstDIEOffset() && 137 die_offset < GetNextUnitOffset(); 138 } 139 dw_offset_t GetFirstDIEOffset() const { 140 return GetOffset() + GetHeaderByteSize(); 141 } 142 dw_offset_t GetNextUnitOffset() const { return m_header.GetNextUnitOffset(); } 143 // Size of the CU data (without initial length and without header). 144 size_t GetDebugInfoSize() const; 145 // Size of the CU data incl. header but without initial length. 146 uint32_t GetLength() const { return m_header.GetLength(); } 147 uint16_t GetVersion() const { return m_header.GetVersion(); } 148 const DWARFAbbreviationDeclarationSet *GetAbbreviations() const; 149 dw_offset_t GetAbbrevOffset() const; 150 uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); } 151 dw_addr_t GetAddrBase() const { return m_addr_base; } 152 dw_addr_t GetBaseAddress() const { return m_base_addr; } 153 dw_offset_t GetLineTableOffset(); 154 dw_addr_t GetRangesBase() const { return m_ranges_base; } 155 dw_addr_t GetStrOffsetsBase() const { return m_str_offsets_base; } 156 void SetAddrBase(dw_addr_t addr_base); 157 void SetLoclistsBase(dw_addr_t loclists_base); 158 void SetRangesBase(dw_addr_t ranges_base); 159 void SetStrOffsetsBase(dw_offset_t str_offsets_base); 160 virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges) = 0; 161 162 lldb::ByteOrder GetByteOrder() const; 163 164 const DWARFDebugAranges &GetFunctionAranges(); 165 166 void SetBaseAddress(dw_addr_t base_addr); 167 168 DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); } 169 170 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); } 171 172 DWARFDIE GetDIE(dw_offset_t die_offset); 173 174 DWARFUnit &GetNonSkeletonUnit(); 175 176 static uint8_t GetAddressByteSize(const DWARFUnit *cu); 177 178 static uint8_t GetDefaultAddressSize(); 179 180 void *GetUserData() const; 181 182 void SetUserData(void *d); 183 184 bool Supports_DW_AT_APPLE_objc_complete_type(); 185 186 bool DW_AT_decl_file_attributes_are_invalid(); 187 188 bool Supports_unnamed_objc_bitfields(); 189 190 SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; } 191 192 DWARFProducer GetProducer(); 193 194 uint32_t GetProducerVersionMajor(); 195 196 uint32_t GetProducerVersionMinor(); 197 198 uint32_t GetProducerVersionUpdate(); 199 200 uint64_t GetDWARFLanguageType(); 201 202 bool GetIsOptimized(); 203 204 const lldb_private::FileSpec &GetCompilationDirectory(); 205 const lldb_private::FileSpec &GetAbsolutePath(); 206 lldb_private::FileSpec GetFile(size_t file_idx); 207 lldb_private::FileSpec::Style GetPathStyle(); 208 209 SymbolFileDWARFDwo *GetDwoSymbolFile(); 210 211 die_iterator_range dies() { 212 ExtractDIEsIfNeeded(); 213 return die_iterator_range(m_die_array.begin(), m_die_array.end()); 214 } 215 216 DIERef::Section GetDebugSection() const { return m_section; } 217 218 uint8_t GetUnitType() const { return m_header.GetUnitType(); } 219 bool IsTypeUnit() const { return m_header.IsTypeUnit(); } 220 221 llvm::Optional<uint64_t> GetStringOffsetSectionItem(uint32_t index) const; 222 223 /// Return a list of address ranges resulting from a (possibly encoded) 224 /// range list starting at a given offset in the appropriate ranges section. 225 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset); 226 227 /// Return a list of address ranges retrieved from an encoded range 228 /// list whose offset is found via a table lookup given an index (DWARF v5 229 /// and later). 230 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index); 231 232 /// Return a rangelist's offset based on an index. The index designates 233 /// an entry in the rangelist table's offset array and is supplied by 234 /// DW_FORM_rnglistx. 235 llvm::Optional<uint64_t> GetRnglistOffset(uint32_t Index) const { 236 if (!m_rnglist_table) 237 return llvm::None; 238 if (llvm::Optional<uint64_t> off = m_rnglist_table->getOffsetEntry(Index)) 239 return *off + m_ranges_base; 240 return llvm::None; 241 } 242 243 llvm::Optional<uint64_t> GetLoclistOffset(uint32_t Index) { 244 if (!m_loclist_table_header) 245 return llvm::None; 246 247 llvm::Optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(Index); 248 if (!Offset) 249 return llvm::None; 250 return *Offset + m_loclists_base; 251 } 252 253 /// Return the location table for parsing the given location list data. The 254 /// format is chosen according to the unit type. Never returns null. 255 std::unique_ptr<llvm::DWARFLocationTable> 256 GetLocationTable(const lldb_private::DataExtractor &data) const; 257 258 lldb_private::DWARFDataExtractor GetLocationData() const; 259 260 protected: 261 DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, 262 const DWARFUnitHeader &header, 263 const DWARFAbbreviationDeclarationSet &abbrevs, 264 DIERef::Section section, bool is_dwo); 265 266 llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, 267 const lldb_private::DWARFDataExtractor &data, 268 lldb::offset_t *offset_ptr); 269 270 // Get the DWARF unit DWARF debug information entry. Parse the single DIE 271 // if needed. 272 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() { 273 ExtractUnitDIEIfNeeded(); 274 // m_first_die_mutex is not required as m_first_die is never cleared. 275 if (!m_first_die) 276 return NULL; 277 return &m_first_die; 278 } 279 280 // Get all DWARF debug informration entries. Parse all DIEs if needed. 281 const DWARFDebugInfoEntry *DIEPtr() { 282 ExtractDIEsIfNeeded(); 283 if (m_die_array.empty()) 284 return NULL; 285 return &m_die_array[0]; 286 } 287 288 SymbolFileDWARF &m_dwarf; 289 std::shared_ptr<DWARFUnit> m_dwo; 290 DWARFUnitHeader m_header; 291 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr; 292 void *m_user_data = nullptr; 293 // The compile unit debug information entry item 294 DWARFDebugInfoEntry::collection m_die_array; 295 mutable llvm::sys::RWMutex m_die_array_mutex; 296 // It is used for tracking of ScopedExtractDIEs instances. 297 mutable llvm::sys::RWMutex m_die_array_scoped_mutex; 298 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked() 299 // as someone called ExtractDIEsIfNeeded(). 300 std::atomic<bool> m_cancel_scopes; 301 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE. 302 // But the first element of m_die_array after ExtractUnitDIEIfNeeded() 303 // would possibly move in memory after later ExtractDIEsIfNeeded(). 304 DWARFDebugInfoEntry m_first_die; 305 llvm::sys::RWMutex m_first_die_mutex; 306 // A table similar to the .debug_aranges table, but this one points to the 307 // exact DW_TAG_subprogram DIEs 308 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up; 309 dw_addr_t m_base_addr = 0; 310 DWARFProducer m_producer = eProducerInvalid; 311 uint32_t m_producer_version_major = 0; 312 uint32_t m_producer_version_minor = 0; 313 uint32_t m_producer_version_update = 0; 314 llvm::Optional<uint64_t> m_language_type; 315 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate; 316 llvm::Optional<lldb_private::FileSpec> m_comp_dir; 317 llvm::Optional<lldb_private::FileSpec> m_file_spec; 318 dw_addr_t m_addr_base = 0; ///< Value of DW_AT_addr_base. 319 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base. 320 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base. 321 322 /// Value of DW_AT_stmt_list. 323 dw_offset_t m_line_table_offset = DW_INVALID_OFFSET; 324 325 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base. 326 327 llvm::Optional<llvm::DWARFDebugRnglistTable> m_rnglist_table; 328 llvm::Optional<llvm::DWARFListTableHeader> m_loclist_table_header; 329 330 const DIERef::Section m_section; 331 bool m_is_dwo; 332 333 private: 334 void ParseProducerInfo(); 335 void ExtractDIEsRWLocked(); 336 void ClearDIEsRWLocked(); 337 338 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die); 339 void SetDwoStrOffsetsBase(); 340 341 void ComputeCompDirAndGuessPathStyle(); 342 void ComputeAbsolutePath(); 343 344 DISALLOW_COPY_AND_ASSIGN(DWARFUnit); 345 }; 346 347 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFUNIT_H 348