1 //===- DWARFUnit.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
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/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"
38 class DWARFAbbreviationDeclarationSet;
40 class DWARFDebugAbbrev;
43 /// Base class for all DWARFUnitSection classes. This provides the
44 /// functionality common to all unit types.
45 class DWARFUnitSectionBase {
47 /// Returns the Unit that contains the given section offset in the
48 /// same section this Unit originated from.
49 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
51 void parse(DWARFContext &C, const DWARFSection &Section);
52 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
53 DWARFUnitIndex *Index = nullptr);
56 ~DWARFUnitSectionBase() = default;
58 virtual void parseImpl(DWARFContext &Context, const DWARFSection &Section,
59 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
60 StringRef SS, const DWARFSection &SOS,
61 const DWARFSection *AOS, const DWARFSection &LS,
62 bool isLittleEndian, bool isDWO) = 0;
65 const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
66 DWARFSectionKind Kind);
68 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
69 template<typename UnitType>
70 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
71 public DWARFUnitSectionBase {
75 using UnitVector = SmallVectorImpl<std::unique_ptr<UnitType>>;
76 using iterator = typename UnitVector::iterator;
77 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
79 UnitType *getUnitForOffset(uint32_t Offset) const override {
80 auto *CU = std::upper_bound(
81 this->begin(), this->end(), Offset,
82 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
83 return LHS < RHS->getNextUnitOffset();
85 if (CU != this->end())
91 void parseImpl(DWARFContext &Context, const DWARFSection &Section,
92 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
93 StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
94 const DWARFSection &LS, bool LE, bool IsDWO) override {
97 const auto &Index = getDWARFUnitIndex(Context, UnitType::Section);
98 DataExtractor Data(Section.Data, LE, 0);
100 while (Data.isValidOffset(Offset)) {
101 auto U = llvm::make_unique<UnitType>(Context, Section, DA, RS, SS, SOS,
102 AOS, LS, LE, IsDWO, *this,
103 Index.getFromOffset(Offset));
104 if (!U->extract(Data, &Offset))
106 this->push_back(std::move(U));
107 Offset = this->back()->getNextUnitOffset();
114 DWARFContext &Context;
115 /// Section containing this DWARFUnit.
116 const DWARFSection &InfoSection;
118 const DWARFDebugAbbrev *Abbrev;
119 const DWARFSection *RangeSection;
120 uint32_t RangeSectionBase;
121 const DWARFSection &LineSection;
122 StringRef StringSection;
123 const DWARFSection &StringOffsetSection;
124 uint64_t StringOffsetSectionBase = 0;
125 const DWARFSection *AddrOffsetSection;
126 uint32_t AddrOffsetSectionBase;
129 const DWARFUnitSectionBase &UnitSection;
131 // Version, address size, and DWARF format.
132 DWARFFormParams FormParams;
136 const DWARFAbbreviationDeclarationSet *Abbrevs;
139 /// The compile unit debug information entry items.
140 std::vector<DWARFDebugInfoEntry> DieArray;
142 /// Map from range's start address to end address and corresponding DIE.
143 /// IntervalMap does not support range removal, as a result, we use the
144 /// std::map::upper_bound for address range lookup.
145 std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
147 using die_iterator_range =
148 iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
150 std::shared_ptr<DWARFUnit> DWO;
152 const DWARFUnitIndex::Entry *IndexEntry;
154 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
155 auto First = DieArray.data();
156 assert(Die >= First && Die < First + DieArray.size());
161 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
163 /// Size in bytes of the unit header.
164 virtual uint32_t getHeaderSize() const { return getVersion() <= 4 ? 11 : 12; }
167 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
168 const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
169 const DWARFSection &SOS, const DWARFSection *AOS,
170 const DWARFSection &LS, bool LE, bool IsDWO,
171 const DWARFUnitSectionBase &UnitSection,
172 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
174 virtual ~DWARFUnit();
176 DWARFContext& getContext() const { return Context; }
178 const DWARFSection &getLineSection() const { return LineSection; }
179 StringRef getStringSection() const { return StringSection; }
180 const DWARFSection &getStringOffsetSection() const {
181 return StringOffsetSection;
184 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
185 AddrOffsetSection = AOS;
186 AddrOffsetSectionBase = Base;
189 /// Recursively update address to Die map.
190 void updateAddressDieMap(DWARFDie Die);
192 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
194 RangeSectionBase = Base;
197 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
198 bool getStringOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
200 DWARFDataExtractor getDebugInfoExtractor() const {
201 return DWARFDataExtractor(InfoSection, isLittleEndian,
202 getAddressByteSize());
205 DataExtractor getStringExtractor() const {
206 return DataExtractor(StringSection, false, 0);
209 const RelocAddrMap *getRelocMap() const { return &InfoSection.Relocs; }
210 const RelocAddrMap &getStringOffsetsRelocMap() const {
211 return StringOffsetSection.Relocs;
214 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
216 /// extractRangeList - extracts the range list referenced by this compile
217 /// unit from .debug_ranges section. Returns true on success.
218 /// Requires that compile unit is already extracted.
219 bool extractRangeList(uint32_t RangeListOffset,
220 DWARFDebugRangeList &RangeList) const;
222 uint32_t getOffset() const { return Offset; }
223 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
224 uint32_t getLength() const { return Length; }
226 const DWARFFormParams &getFormParams() const { return FormParams; }
227 uint16_t getVersion() const { return FormParams.Version; }
228 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
229 uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
230 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
231 uint8_t getDwarfOffsetByteSize() const {
232 return FormParams.getDwarfOffsetByteSize();
235 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
239 uint8_t getUnitType() const { return UnitType; }
241 static bool isValidUnitType(uint8_t UnitType) {
242 return UnitType == dwarf::DW_UT_compile || UnitType == dwarf::DW_UT_type ||
243 UnitType == dwarf::DW_UT_partial ||
244 UnitType == dwarf::DW_UT_skeleton ||
245 UnitType == dwarf::DW_UT_split_compile ||
246 UnitType == dwarf::DW_UT_split_type;
249 /// \brief Return the number of bytes for the header of a unit of
252 /// This function must be called with a valid unit type which in
253 /// DWARF5 is defined as one of the following six types.
254 static uint32_t getDWARF5HeaderSize(uint8_t UnitType) {
256 case dwarf::DW_UT_compile:
257 case dwarf::DW_UT_partial:
259 case dwarf::DW_UT_skeleton:
260 case dwarf::DW_UT_split_compile:
262 case dwarf::DW_UT_type:
263 case dwarf::DW_UT_split_type:
266 llvm_unreachable("Invalid UnitType.");
269 uint64_t getBaseAddress() const { return BaseAddr; }
271 void setBaseAddress(uint64_t base_addr) {
272 BaseAddr = base_addr;
275 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
276 extractDIEsIfNeeded(ExtractUnitDIEOnly);
277 if (DieArray.empty())
279 return DWARFDie(this, &DieArray[0]);
282 const char *getCompilationDir();
283 Optional<uint64_t> getDWOId();
285 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
287 /// getInlinedChainForAddress - fetches inlined chain for a given address.
288 /// Returns empty chain if there is no subprogram containing address. The
289 /// chain is valid as long as parsed compile unit DIEs are not cleared.
290 void getInlinedChainForAddress(uint64_t Address,
291 SmallVectorImpl<DWARFDie> &InlinedChain);
293 /// getUnitSection - Return the DWARFUnitSection containing this unit.
294 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
296 /// \brief Returns the number of DIEs in the unit. Parses the unit
298 unsigned getNumDIEs() {
299 extractDIEsIfNeeded(false);
300 return DieArray.size();
303 /// \brief Return the index of a DIE inside the unit's DIE vector.
305 /// It is illegal to call this method with a DIE that hasn't be
306 /// created by this unit. In other word, it's illegal to call this
307 /// method on a DIE that isn't accessible by following
308 /// children/sibling links starting from this unit's getUnitDIE().
309 uint32_t getDIEIndex(const DWARFDie &D) {
310 return getDIEIndex(D.getDebugInfoEntry());
313 /// \brief Return the DIE object at the given index.
314 DWARFDie getDIEAtIndex(unsigned Index) {
315 assert(Index < DieArray.size());
316 return DWARFDie(this, &DieArray[Index]);
319 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
320 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
322 /// \brief Return the DIE object for a given offset inside the
323 /// unit's DIE vector.
325 /// The unit needs to have its DIEs extracted for this method to work.
326 DWARFDie getDIEForOffset(uint32_t Offset) {
327 extractDIEsIfNeeded(false);
328 assert(!DieArray.empty());
329 auto it = std::lower_bound(
330 DieArray.begin(), DieArray.end(), Offset,
331 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
332 return LHS.getOffset() < Offset;
334 if (it != DieArray.end() && it->getOffset() == Offset)
335 return DWARFDie(this, &*it);
339 uint32_t getLineTableOffset() const {
341 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
342 return Contrib->Offset;
346 die_iterator_range dies() {
347 extractDIEsIfNeeded(false);
348 return die_iterator_range(DieArray.begin(), DieArray.end());
352 /// Size in bytes of the .debug_info data associated with this compile unit.
353 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
355 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
356 /// hasn't already been done. Returns the number of DIEs parsed at this call.
357 size_t extractDIEsIfNeeded(bool CUDieOnly);
359 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
360 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
361 std::vector<DWARFDebugInfoEntry> &DIEs) const;
363 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
364 void clearDIEs(bool KeepCUDie);
366 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
367 /// it was actually constructed.
370 /// getSubroutineForAddress - Returns subprogram DIE with address range
371 /// encompassing the provided address. The pointer is alive as long as parsed
372 /// compile unit DIEs are not cleared.
373 DWARFDie getSubroutineForAddress(uint64_t Address);
376 } // end namespace llvm
378 #endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H