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