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