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 llvm::Expected<lldb_private::TypeSystem &> GetTypeSystem(); 156 157 const DWARFDebugAranges &GetFunctionAranges(); 158 159 void SetBaseAddress(dw_addr_t base_addr); 160 161 DWARFBaseDIE GetUnitDIEOnly() { return DWARFDIE(this, GetUnitDIEPtrOnly()); } 162 163 DWARFDIE DIE() { return DWARFDIE(this, DIEPtr()); } 164 165 DWARFDIE GetDIE(dw_offset_t die_offset); 166 167 DWARFUnit &GetNonSkeletonUnit(); 168 169 static uint8_t GetAddressByteSize(const DWARFUnit *cu); 170 171 static uint8_t GetDefaultAddressSize(); 172 173 void *GetUserData() const; 174 175 void SetUserData(void *d); 176 177 bool Supports_DW_AT_APPLE_objc_complete_type(); 178 179 bool DW_AT_decl_file_attributes_are_invalid(); 180 181 bool Supports_unnamed_objc_bitfields(); 182 183 SymbolFileDWARF &GetSymbolFileDWARF() const { return m_dwarf; } 184 185 DWARFProducer GetProducer(); 186 187 uint32_t GetProducerVersionMajor(); 188 189 uint32_t GetProducerVersionMinor(); 190 191 uint32_t GetProducerVersionUpdate(); 192 193 static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val); 194 195 lldb::LanguageType GetLanguageType(); 196 197 bool GetIsOptimized(); 198 199 const lldb_private::FileSpec &GetCompilationDirectory(); 200 const lldb_private::FileSpec &GetAbsolutePath(); 201 lldb_private::FileSpec GetFile(size_t file_idx); 202 lldb_private::FileSpec::Style GetPathStyle(); 203 204 SymbolFileDWARFDwo *GetDwoSymbolFile(); 205 206 die_iterator_range dies() { 207 ExtractDIEsIfNeeded(); 208 return die_iterator_range(m_die_array.begin(), m_die_array.end()); 209 } 210 211 DIERef::Section GetDebugSection() const { return m_section; } 212 213 uint8_t GetUnitType() const { return m_header.GetUnitType(); } 214 bool IsTypeUnit() const { return m_header.IsTypeUnit(); } 215 216 /// Return a list of address ranges resulting from a (possibly encoded) 217 /// range list starting at a given offset in the appropriate ranges section. 218 llvm::Expected<DWARFRangeList> FindRnglistFromOffset(dw_offset_t offset); 219 220 /// Return a list of address ranges retrieved from an encoded range 221 /// list whose offset is found via a table lookup given an index (DWARF v5 222 /// and later). 223 llvm::Expected<DWARFRangeList> FindRnglistFromIndex(uint32_t index); 224 225 /// Return a rangelist's offset based on an index. The index designates 226 /// an entry in the rangelist table's offset array and is supplied by 227 /// DW_FORM_rnglistx. 228 llvm::Optional<uint64_t> GetRnglistOffset(uint32_t Index) const { 229 if (!m_rnglist_table) 230 return llvm::None; 231 if (llvm::Optional<uint64_t> off = m_rnglist_table->getOffsetEntry(Index)) 232 return *off + m_ranges_base; 233 return llvm::None; 234 } 235 236 llvm::Optional<uint64_t> GetLoclistOffset(uint32_t Index) { 237 if (!m_loclist_table_header) 238 return llvm::None; 239 240 llvm::Optional<uint64_t> Offset = m_loclist_table_header->getOffsetEntry(Index); 241 if (!Offset) 242 return llvm::None; 243 return *Offset + m_loclists_base; 244 } 245 246 /// Return the location table for parsing the given location list data. The 247 /// format is chosen according to the unit type. Never returns null. 248 std::unique_ptr<llvm::DWARFLocationTable> 249 GetLocationTable(const lldb_private::DataExtractor &data) const; 250 251 const lldb_private::DWARFDataExtractor &GetLocationData() const; 252 253 protected: 254 DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, 255 const DWARFUnitHeader &header, 256 const DWARFAbbreviationDeclarationSet &abbrevs, 257 DIERef::Section section, bool is_dwo); 258 259 llvm::Error ExtractHeader(SymbolFileDWARF &dwarf, 260 const lldb_private::DWARFDataExtractor &data, 261 lldb::offset_t *offset_ptr); 262 263 // Get the DWARF unit DWARF debug information entry. Parse the single DIE 264 // if needed. 265 const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() { 266 ExtractUnitDIEIfNeeded(); 267 // m_first_die_mutex is not required as m_first_die is never cleared. 268 if (!m_first_die) 269 return NULL; 270 return &m_first_die; 271 } 272 273 // Get all DWARF debug informration entries. Parse all DIEs if needed. 274 const DWARFDebugInfoEntry *DIEPtr() { 275 ExtractDIEsIfNeeded(); 276 if (m_die_array.empty()) 277 return NULL; 278 return &m_die_array[0]; 279 } 280 281 SymbolFileDWARF &m_dwarf; 282 std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file; 283 DWARFUnitHeader m_header; 284 const DWARFAbbreviationDeclarationSet *m_abbrevs = nullptr; 285 void *m_user_data = nullptr; 286 // The compile unit debug information entry item 287 DWARFDebugInfoEntry::collection m_die_array; 288 mutable llvm::sys::RWMutex m_die_array_mutex; 289 // It is used for tracking of ScopedExtractDIEs instances. 290 mutable llvm::sys::RWMutex m_die_array_scoped_mutex; 291 // ScopedExtractDIEs instances should not call ClearDIEsRWLocked() 292 // as someone called ExtractDIEsIfNeeded(). 293 std::atomic<bool> m_cancel_scopes; 294 // GetUnitDIEPtrOnly() needs to return pointer to the first DIE. 295 // But the first element of m_die_array after ExtractUnitDIEIfNeeded() 296 // would possibly move in memory after later ExtractDIEsIfNeeded(). 297 DWARFDebugInfoEntry m_first_die; 298 llvm::sys::RWMutex m_first_die_mutex; 299 // A table similar to the .debug_aranges table, but this one points to the 300 // exact DW_TAG_subprogram DIEs 301 std::unique_ptr<DWARFDebugAranges> m_func_aranges_up; 302 dw_addr_t m_base_addr = 0; 303 DWARFProducer m_producer = eProducerInvalid; 304 uint32_t m_producer_version_major = 0; 305 uint32_t m_producer_version_minor = 0; 306 uint32_t m_producer_version_update = 0; 307 lldb::LanguageType m_language_type = lldb::eLanguageTypeUnknown; 308 lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate; 309 llvm::Optional<lldb_private::FileSpec> m_comp_dir; 310 llvm::Optional<lldb_private::FileSpec> m_file_spec; 311 dw_addr_t m_addr_base = 0; ///< Value of DW_AT_addr_base. 312 dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base. 313 dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base. 314 315 /// Value of DW_AT_stmt_list. 316 dw_offset_t m_line_table_offset = DW_INVALID_OFFSET; 317 318 dw_offset_t m_str_offsets_base = 0; // Value of DW_AT_str_offsets_base. 319 320 llvm::Optional<llvm::DWARFDebugRnglistTable> m_rnglist_table; 321 llvm::Optional<llvm::DWARFListTableHeader> m_loclist_table_header; 322 323 const DIERef::Section m_section; 324 bool m_is_dwo; 325 326 private: 327 void ParseProducerInfo(); 328 void ExtractDIEsRWLocked(); 329 void ClearDIEsRWLocked(); 330 331 void AddUnitDIE(const DWARFDebugInfoEntry &cu_die); 332 333 void ComputeCompDirAndGuessPathStyle(); 334 void ComputeAbsolutePath(); 335 336 DISALLOW_COPY_AND_ASSIGN(DWARFUnit); 337 }; 338 339 #endif // SymbolFileDWARF_DWARFUnit_h_ 340