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