1 //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H 11 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H 12 13 #include "llvm/ADT/Optional.h" 14 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/SmallVector.h" 16 #include "llvm/ADT/StringRef.h" 17 #include "llvm/ADT/iterator_range.h" 18 #include "llvm/BinaryFormat/Dwarf.h" 19 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" 20 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" 21 #include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h" 22 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 23 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 24 #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 25 #include "llvm/DebugInfo/DWARF/DWARFSection.h" 26 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 27 #include "llvm/Support/DataExtractor.h" 28 #include <algorithm> 29 #include <cassert> 30 #include <cstddef> 31 #include <cstdint> 32 #include <map> 33 #include <memory> 34 #include <utility> 35 #include <vector> 36 37 namespace llvm { 38 39 class DWARFAbbreviationDeclarationSet; 40 class DWARFContext; 41 class DWARFDebugAbbrev; 42 class DWARFUnit; 43 44 /// Base class describing the header of any kind of "unit." Some information 45 /// is specific to certain unit types. We separate this class out so we can 46 /// parse the header before deciding what specific kind of unit to construct. 47 class DWARFUnitHeader { 48 // Offset within section. 49 uint32_t Offset = 0; 50 // Version, address size, and DWARF format. 51 dwarf::FormParams FormParams; 52 uint32_t Length = 0; 53 uint64_t AbbrOffset = 0; 54 55 // For DWO units only. 56 const DWARFUnitIndex::Entry *IndexEntry = nullptr; 57 58 // For type units only. 59 uint64_t TypeHash = 0; 60 uint32_t TypeOffset = 0; 61 62 // For v5 split or skeleton compile units only. 63 Optional<uint64_t> DWOId; 64 65 // Unit type as parsed, or derived from the section kind. 66 uint8_t UnitType = 0; 67 68 // Size as parsed. uint8_t for compactness. 69 uint8_t Size = 0; 70 71 public: 72 /// Parse a unit header from \p debug_info starting at \p offset_ptr. 73 bool extract(DWARFContext &Context, const DWARFDataExtractor &debug_info, 74 uint32_t *offset_ptr, DWARFSectionKind Kind = DW_SECT_INFO, 75 const DWARFUnitIndex *Index = nullptr, 76 const DWARFUnitIndex::Entry *Entry = nullptr); getOffset()77 uint32_t getOffset() const { return Offset; } getFormParams()78 const dwarf::FormParams &getFormParams() const { return FormParams; } getVersion()79 uint16_t getVersion() const { return FormParams.Version; } getFormat()80 dwarf::DwarfFormat getFormat() const { return FormParams.Format; } getAddressByteSize()81 uint8_t getAddressByteSize() const { return FormParams.AddrSize; } getRefAddrByteSize()82 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); } getDwarfOffsetByteSize()83 uint8_t getDwarfOffsetByteSize() const { 84 return FormParams.getDwarfOffsetByteSize(); 85 } getLength()86 uint32_t getLength() const { return Length; } getAbbrOffset()87 uint64_t getAbbrOffset() const { return AbbrOffset; } getDWOId()88 Optional<uint64_t> getDWOId() const { return DWOId; } setDWOId(uint64_t Id)89 void setDWOId(uint64_t Id) { 90 assert((!DWOId || *DWOId == Id) && "setting DWOId to a different value"); 91 DWOId = Id; 92 } getIndexEntry()93 const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; } getTypeHash()94 uint64_t getTypeHash() const { return TypeHash; } getTypeOffset()95 uint32_t getTypeOffset() const { return TypeOffset; } getUnitType()96 uint8_t getUnitType() const { return UnitType; } isTypeUnit()97 bool isTypeUnit() const { 98 return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type; 99 } getSize()100 uint8_t getSize() const { return Size; } 101 // FIXME: Support DWARF64. getNextUnitOffset()102 uint32_t getNextUnitOffset() const { return Offset + Length + 4; } 103 }; 104 105 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context, 106 DWARFSectionKind Kind); 107 108 /// Describe a collection of units. Intended to hold all units either from 109 /// .debug_info and .debug_types, or from .debug_info.dwo and .debug_types.dwo. 110 class DWARFUnitVector final : public SmallVector<std::unique_ptr<DWARFUnit>, 1> { 111 std::function<std::unique_ptr<DWARFUnit>(uint32_t, DWARFSectionKind, 112 const DWARFSection *, 113 const DWARFUnitIndex::Entry *)> 114 Parser; 115 int NumInfoUnits = -1; 116 117 public: 118 using UnitVector = SmallVectorImpl<std::unique_ptr<DWARFUnit>>; 119 using iterator = typename UnitVector::iterator; 120 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>; 121 122 DWARFUnit *getUnitForOffset(uint32_t Offset) const; 123 DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E); 124 125 /// Read units from a .debug_info or .debug_types section. Calls made 126 /// before finishedInfoUnits() are assumed to be for .debug_info sections, 127 /// calls after finishedInfoUnits() are for .debug_types sections. Caller 128 /// must not mix calls to addUnitsForSection and addUnitsForDWOSection. 129 void addUnitsForSection(DWARFContext &C, const DWARFSection &Section, 130 DWARFSectionKind SectionKind); 131 /// Read units from a .debug_info.dwo or .debug_types.dwo section. Calls 132 /// made before finishedInfoUnits() are assumed to be for .debug_info.dwo 133 /// sections, calls after finishedInfoUnits() are for .debug_types.dwo 134 /// sections. Caller must not mix calls to addUnitsForSection and 135 /// addUnitsForDWOSection. 136 void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection, 137 DWARFSectionKind SectionKind, bool Lazy = false); 138 139 /// Add an existing DWARFUnit to this UnitVector. This is used by the DWARF 140 /// verifier to process unit separately. 141 DWARFUnit *addUnit(std::unique_ptr<DWARFUnit> Unit); 142 143 /// Returns number of all units held by this instance. getNumUnits()144 unsigned getNumUnits() const { return size(); } 145 /// Returns number of units from all .debug_info[.dwo] sections. getNumInfoUnits()146 unsigned getNumInfoUnits() const { 147 return NumInfoUnits == -1 ? size() : NumInfoUnits; 148 } 149 /// Returns number of units from all .debug_types[.dwo] sections. getNumTypesUnits()150 unsigned getNumTypesUnits() const { return size() - NumInfoUnits; } 151 /// Indicate that parsing .debug_info[.dwo] is done, and remaining units 152 /// will be from .debug_types[.dwo]. finishedInfoUnits()153 void finishedInfoUnits() { NumInfoUnits = size(); } 154 155 private: 156 void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj, 157 const DWARFSection &Section, const DWARFDebugAbbrev *DA, 158 const DWARFSection *RS, const DWARFSection *LocSection, 159 StringRef SS, const DWARFSection &SOS, 160 const DWARFSection *AOS, const DWARFSection &LS, bool LE, 161 bool IsDWO, bool Lazy, DWARFSectionKind SectionKind); 162 }; 163 164 /// Represents base address of the CU. 165 /// Represents a unit's contribution to the string offsets table. 166 struct StrOffsetsContributionDescriptor { 167 uint64_t Base = 0; 168 /// The contribution size not including the header. 169 uint64_t Size = 0; 170 /// Format and version. 171 dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32}; 172 StrOffsetsContributionDescriptorStrOffsetsContributionDescriptor173 StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size, 174 uint8_t Version, dwarf::DwarfFormat Format) 175 : Base(Base), Size(Size), FormParams({Version, 0, Format}) {} 176 getVersionStrOffsetsContributionDescriptor177 uint8_t getVersion() const { return FormParams.Version; } getFormatStrOffsetsContributionDescriptor178 dwarf::DwarfFormat getFormat() const { return FormParams.Format; } getDwarfOffsetByteSizeStrOffsetsContributionDescriptor179 uint8_t getDwarfOffsetByteSize() const { 180 return FormParams.getDwarfOffsetByteSize(); 181 } 182 /// Determine whether a contribution to the string offsets table is 183 /// consistent with the relevant section size and that its length is 184 /// a multiple of the size of one of its entries. 185 Optional<StrOffsetsContributionDescriptor> 186 validateContributionSize(DWARFDataExtractor &DA); 187 }; 188 189 class DWARFUnit { 190 DWARFContext &Context; 191 /// Section containing this DWARFUnit. 192 const DWARFSection &InfoSection; 193 194 DWARFUnitHeader Header; 195 const DWARFDebugAbbrev *Abbrev; 196 const DWARFSection *RangeSection; 197 uint32_t RangeSectionBase; 198 /// We either keep track of the location list section or its data, depending 199 /// on whether we are handling a split DWARF section or not. 200 union { 201 const DWARFSection *LocSection; 202 StringRef LocSectionData; 203 }; 204 const DWARFSection &LineSection; 205 StringRef StringSection; 206 const DWARFSection &StringOffsetSection; 207 const DWARFSection *AddrOffsetSection; 208 uint32_t AddrOffsetSectionBase = 0; 209 bool isLittleEndian; 210 bool IsDWO; 211 const DWARFUnitVector &UnitVector; 212 213 /// Start, length, and DWARF format of the unit's contribution to the string 214 /// offsets table (DWARF v5). 215 Optional<StrOffsetsContributionDescriptor> StringOffsetsTableContribution; 216 217 /// A table of range lists (DWARF v5 and later). 218 Optional<DWARFDebugRnglistTable> RngListTable; 219 220 mutable const DWARFAbbreviationDeclarationSet *Abbrevs; 221 llvm::Optional<SectionedAddress> BaseAddr; 222 /// The compile unit debug information entry items. 223 std::vector<DWARFDebugInfoEntry> DieArray; 224 225 /// Map from range's start address to end address and corresponding DIE. 226 /// IntervalMap does not support range removal, as a result, we use the 227 /// std::map::upper_bound for address range lookup. 228 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap; 229 230 using die_iterator_range = 231 iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>; 232 233 std::shared_ptr<DWARFUnit> DWO; 234 getDIEIndex(const DWARFDebugInfoEntry * Die)235 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) { 236 auto First = DieArray.data(); 237 assert(Die >= First && Die < First + DieArray.size()); 238 return Die - First; 239 } 240 241 protected: getHeader()242 const DWARFUnitHeader &getHeader() const { return Header; } 243 244 /// Size in bytes of the parsed unit header. getHeaderSize()245 uint32_t getHeaderSize() const { return Header.getSize(); } 246 247 /// Find the unit's contribution to the string offsets table and determine its 248 /// length and form. The given offset is expected to be derived from the unit 249 /// DIE's DW_AT_str_offsets_base attribute. 250 Optional<StrOffsetsContributionDescriptor> 251 determineStringOffsetsTableContribution(DWARFDataExtractor &DA); 252 253 /// Find the unit's contribution to the string offsets table and determine its 254 /// length and form. The given offset is expected to be 0 in a dwo file or, 255 /// in a dwp file, the start of the unit's contribution to the string offsets 256 /// table section (as determined by the index table). 257 Optional<StrOffsetsContributionDescriptor> 258 determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA); 259 260 public: 261 DWARFUnit(DWARFContext &Context, const DWARFSection &Section, 262 const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA, 263 const DWARFSection *RS, const DWARFSection *LocSection, 264 StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS, 265 const DWARFSection &LS, bool LE, bool IsDWO, 266 const DWARFUnitVector &UnitVector); 267 268 virtual ~DWARFUnit(); 269 isDWOUnit()270 bool isDWOUnit() const { return IsDWO; } getContext()271 DWARFContext& getContext() const { return Context; } getInfoSection()272 const DWARFSection &getInfoSection() const { return InfoSection; } getLocSection()273 const DWARFSection *getLocSection() const { return LocSection; } getLocSectionData()274 StringRef getLocSectionData() const { return LocSectionData; } getOffset()275 uint32_t getOffset() const { return Header.getOffset(); } getFormParams()276 const dwarf::FormParams &getFormParams() const { 277 return Header.getFormParams(); 278 } getVersion()279 uint16_t getVersion() const { return Header.getVersion(); } getAddressByteSize()280 uint8_t getAddressByteSize() const { return Header.getAddressByteSize(); } getRefAddrByteSize()281 uint8_t getRefAddrByteSize() const { return Header.getRefAddrByteSize(); } getDwarfOffsetByteSize()282 uint8_t getDwarfOffsetByteSize() const { 283 return Header.getDwarfOffsetByteSize(); 284 } getLength()285 uint32_t getLength() const { return Header.getLength(); } getUnitType()286 uint8_t getUnitType() const { return Header.getUnitType(); } isTypeUnit()287 bool isTypeUnit() const { return Header.isTypeUnit(); } getNextUnitOffset()288 uint32_t getNextUnitOffset() const { return Header.getNextUnitOffset(); } getLineSection()289 const DWARFSection &getLineSection() const { return LineSection; } getStringSection()290 StringRef getStringSection() const { return StringSection; } getStringOffsetSection()291 const DWARFSection &getStringOffsetSection() const { 292 return StringOffsetSection; 293 } 294 setAddrOffsetSection(const DWARFSection * AOS,uint32_t Base)295 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) { 296 AddrOffsetSection = AOS; 297 AddrOffsetSectionBase = Base; 298 } 299 300 /// Recursively update address to Die map. 301 void updateAddressDieMap(DWARFDie Die); 302 setRangesSection(const DWARFSection * RS,uint32_t Base)303 void setRangesSection(const DWARFSection *RS, uint32_t Base) { 304 RangeSection = RS; 305 RangeSectionBase = Base; 306 } 307 308 Optional<SectionedAddress> getAddrOffsetSectionItem(uint32_t Index) const; 309 Optional<uint64_t> getStringOffsetSectionItem(uint32_t Index) const; 310 311 DWARFDataExtractor getDebugInfoExtractor() const; 312 getStringExtractor()313 DataExtractor getStringExtractor() const { 314 return DataExtractor(StringSection, false, 0); 315 } 316 317 /// Extract the range list referenced by this compile unit from the 318 /// .debug_ranges section. If the extraction is unsuccessful, an error 319 /// is returned. Successful extraction requires that the compile unit 320 /// has already been extracted. 321 Error extractRangeList(uint32_t RangeListOffset, 322 DWARFDebugRangeList &RangeList) const; 323 void clear(); 324 325 const Optional<StrOffsetsContributionDescriptor> & getStringOffsetsTableContribution()326 getStringOffsetsTableContribution() const { 327 return StringOffsetsTableContribution; 328 } 329 getDwarfStringOffsetsByteSize()330 uint8_t getDwarfStringOffsetsByteSize() const { 331 assert(StringOffsetsTableContribution); 332 return StringOffsetsTableContribution->getDwarfOffsetByteSize(); 333 } 334 getStringOffsetsBase()335 uint64_t getStringOffsetsBase() const { 336 assert(StringOffsetsTableContribution); 337 return StringOffsetsTableContribution->Base; 338 } 339 340 const DWARFAbbreviationDeclarationSet *getAbbreviations() const; 341 isMatchingUnitTypeAndTag(uint8_t UnitType,dwarf::Tag Tag)342 static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) { 343 switch (UnitType) { 344 case dwarf::DW_UT_compile: 345 return Tag == dwarf::DW_TAG_compile_unit; 346 case dwarf::DW_UT_type: 347 return Tag == dwarf::DW_TAG_type_unit; 348 case dwarf::DW_UT_partial: 349 return Tag == dwarf::DW_TAG_partial_unit; 350 case dwarf::DW_UT_skeleton: 351 return Tag == dwarf::DW_TAG_skeleton_unit; 352 case dwarf::DW_UT_split_compile: 353 case dwarf::DW_UT_split_type: 354 return dwarf::isUnitType(Tag); 355 } 356 return false; 357 } 358 359 /// Return the number of bytes for the header of a unit of 360 /// UnitType type. 361 /// 362 /// This function must be called with a valid unit type which in 363 /// DWARF5 is defined as one of the following six types. getDWARF5HeaderSize(uint8_t UnitType)364 static uint32_t getDWARF5HeaderSize(uint8_t UnitType) { 365 switch (UnitType) { 366 case dwarf::DW_UT_compile: 367 case dwarf::DW_UT_partial: 368 return 12; 369 case dwarf::DW_UT_skeleton: 370 case dwarf::DW_UT_split_compile: 371 return 20; 372 case dwarf::DW_UT_type: 373 case dwarf::DW_UT_split_type: 374 return 24; 375 } 376 llvm_unreachable("Invalid UnitType."); 377 } 378 379 llvm::Optional<SectionedAddress> getBaseAddress(); 380 381 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) { 382 extractDIEsIfNeeded(ExtractUnitDIEOnly); 383 if (DieArray.empty()) 384 return DWARFDie(); 385 return DWARFDie(this, &DieArray[0]); 386 } 387 388 const char *getCompilationDir(); getDWOId()389 Optional<uint64_t> getDWOId() { 390 extractDIEsIfNeeded(/*CUDieOnly*/ true); 391 return getHeader().getDWOId(); 392 } setDWOId(uint64_t NewID)393 void setDWOId(uint64_t NewID) { Header.setDWOId(NewID); } 394 395 /// Return a vector of address ranges resulting from a (possibly encoded) 396 /// range list starting at a given offset in the appropriate ranges section. 397 Expected<DWARFAddressRangesVector> findRnglistFromOffset(uint32_t Offset); 398 399 /// Return a vector of address ranges retrieved from an encoded range 400 /// list whose offset is found via a table lookup given an index (DWARF v5 401 /// and later). 402 Expected<DWARFAddressRangesVector> findRnglistFromIndex(uint32_t Index); 403 404 /// Return a rangelist's offset based on an index. The index designates 405 /// an entry in the rangelist table's offset array and is supplied by 406 /// DW_FORM_rnglistx. getRnglistOffset(uint32_t Index)407 Optional<uint32_t> getRnglistOffset(uint32_t Index) { 408 if (RngListTable) 409 return RngListTable->getOffsetEntry(Index); 410 return None; 411 } 412 413 Expected<DWARFAddressRangesVector> collectAddressRanges(); 414 415 /// Returns subprogram DIE with address range encompassing the provided 416 /// address. The pointer is alive as long as parsed compile unit DIEs are not 417 /// cleared. 418 DWARFDie getSubroutineForAddress(uint64_t Address); 419 420 /// getInlinedChainForAddress - fetches inlined chain for a given address. 421 /// Returns empty chain if there is no subprogram containing address. The 422 /// chain is valid as long as parsed compile unit DIEs are not cleared. 423 void getInlinedChainForAddress(uint64_t Address, 424 SmallVectorImpl<DWARFDie> &InlinedChain); 425 426 /// Return the DWARFUnitVector containing this unit. getUnitVector()427 const DWARFUnitVector &getUnitVector() const { return UnitVector; } 428 429 /// Returns the number of DIEs in the unit. Parses the unit 430 /// if necessary. getNumDIEs()431 unsigned getNumDIEs() { 432 extractDIEsIfNeeded(false); 433 return DieArray.size(); 434 } 435 436 /// Return the index of a DIE inside the unit's DIE vector. 437 /// 438 /// It is illegal to call this method with a DIE that hasn't be 439 /// created by this unit. In other word, it's illegal to call this 440 /// method on a DIE that isn't accessible by following 441 /// children/sibling links starting from this unit's getUnitDIE(). getDIEIndex(const DWARFDie & D)442 uint32_t getDIEIndex(const DWARFDie &D) { 443 return getDIEIndex(D.getDebugInfoEntry()); 444 } 445 446 /// Return the DIE object at the given index. getDIEAtIndex(unsigned Index)447 DWARFDie getDIEAtIndex(unsigned Index) { 448 assert(Index < DieArray.size()); 449 return DWARFDie(this, &DieArray[Index]); 450 } 451 452 DWARFDie getParent(const DWARFDebugInfoEntry *Die); 453 DWARFDie getSibling(const DWARFDebugInfoEntry *Die); 454 DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die); 455 DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die); 456 DWARFDie getLastChild(const DWARFDebugInfoEntry *Die); 457 458 /// Return the DIE object for a given offset inside the 459 /// unit's DIE vector. 460 /// 461 /// The unit needs to have its DIEs extracted for this method to work. getDIEForOffset(uint32_t Offset)462 DWARFDie getDIEForOffset(uint32_t Offset) { 463 extractDIEsIfNeeded(false); 464 assert(!DieArray.empty()); 465 auto it = std::lower_bound( 466 DieArray.begin(), DieArray.end(), Offset, 467 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) { 468 return LHS.getOffset() < Offset; 469 }); 470 if (it != DieArray.end() && it->getOffset() == Offset) 471 return DWARFDie(this, &*it); 472 return DWARFDie(); 473 } 474 getLineTableOffset()475 uint32_t getLineTableOffset() const { 476 if (auto IndexEntry = Header.getIndexEntry()) 477 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE)) 478 return Contrib->Offset; 479 return 0; 480 } 481 dies()482 die_iterator_range dies() { 483 extractDIEsIfNeeded(false); 484 return die_iterator_range(DieArray.begin(), DieArray.end()); 485 } 486 487 virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0; 488 private: 489 /// Size in bytes of the .debug_info data associated with this compile unit. getDebugInfoSize()490 size_t getDebugInfoSize() const { 491 return Header.getLength() + 4 - getHeaderSize(); 492 } 493 494 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it 495 /// hasn't already been done. Returns the number of DIEs parsed at this call. 496 size_t extractDIEsIfNeeded(bool CUDieOnly); 497 498 /// extractDIEsToVector - Appends all parsed DIEs to a vector. 499 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs, 500 std::vector<DWARFDebugInfoEntry> &DIEs) const; 501 502 /// clearDIEs - Clear parsed DIEs to keep memory usage low. 503 void clearDIEs(bool KeepCUDie); 504 505 /// parseDWO - Parses .dwo file for current compile unit. Returns true if 506 /// it was actually constructed. 507 bool parseDWO(); 508 }; 509 510 } // end namespace llvm 511 512 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H 513