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