blob: fe3f573427b15422e000bc7d59436bf303c67819 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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/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
36namespace llvm {
37
38class DWARFAbbreviationDeclarationSet;
39class DWARFContext;
40class DWARFDebugAbbrev;
41class DWARFUnit;
42
43/// Base class for all DWARFUnitSection classes. This provides the
44/// functionality common to all unit types.
45class DWARFUnitSectionBase {
46public:
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;
50 virtual DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) = 0;
51
52 void parse(DWARFContext &C, const DWARFSection &Section);
53 void parseDWO(DWARFContext &C, const DWARFSection &DWOSection,
54 bool Lazy = false);
55
56protected:
57 ~DWARFUnitSectionBase() = default;
58
59 virtual void parseImpl(DWARFContext &Context, const DWARFObject &Obj,
60 const DWARFSection &Section,
61 const DWARFDebugAbbrev *DA, const DWARFSection *RS,
62 StringRef SS, const DWARFSection &SOS,
63 const DWARFSection *AOS, const DWARFSection &LS,
64 bool isLittleEndian, bool isDWO, bool Lazy) = 0;
65};
66
67const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
68 DWARFSectionKind Kind);
69
70/// Concrete instance of DWARFUnitSection, specialized for one Unit type.
71template<typename UnitType>
72class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
73 public DWARFUnitSectionBase {
74 bool Parsed = false;
75 std::function<std::unique_ptr<UnitType>(uint32_t)> Parser;
76
77public:
78 using UnitVector = SmallVectorImpl<std::unique_ptr<UnitType>>;
79 using iterator = typename UnitVector::iterator;
80 using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
81
82 UnitType *getUnitForOffset(uint32_t Offset) const override {
83 auto *CU = std::upper_bound(
84 this->begin(), this->end(), Offset,
85 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
86 return LHS < RHS->getNextUnitOffset();
87 });
88 if (CU != this->end() && (*CU)->getOffset() <= Offset)
89 return CU->get();
90 return nullptr;
91 }
92 UnitType *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E) override {
93 const auto *CUOff = E.getOffset(DW_SECT_INFO);
94 if (!CUOff)
95 return nullptr;
96
97 auto Offset = CUOff->Offset;
98
99 auto *CU = std::upper_bound(
100 this->begin(), this->end(), CUOff->Offset,
101 [](uint32_t LHS, const std::unique_ptr<UnitType> &RHS) {
102 return LHS < RHS->getNextUnitOffset();
103 });
104 if (CU != this->end() && (*CU)->getOffset() <= Offset)
105 return CU->get();
106
107 if (!Parser)
108 return nullptr;
109
110 auto U = Parser(Offset);
111 if (!U)
112 U = nullptr;
113
114 auto *NewCU = U.get();
115 this->insert(CU, std::move(U));
116 return NewCU;
117 }
118
119private:
120 void parseImpl(DWARFContext &Context, const DWARFObject &Obj,
121 const DWARFSection &Section, const DWARFDebugAbbrev *DA,
122 const DWARFSection *RS, StringRef SS, const DWARFSection &SOS,
123 const DWARFSection *AOS, const DWARFSection &LS, bool LE,
124 bool IsDWO, bool Lazy) override {
125 if (Parsed)
126 return;
127 DWARFDataExtractor Data(Obj, Section, LE, 0);
128 if (!Parser) {
129 const DWARFUnitIndex *Index = nullptr;
130 if (IsDWO)
131 Index = &getDWARFUnitIndex(Context, UnitType::Section);
132 Parser = [=, &Context, &Section, &SOS,
133 &LS](uint32_t Offset) -> std::unique_ptr<UnitType> {
134 if (!Data.isValidOffset(Offset))
135 return nullptr;
136 auto U = llvm::make_unique<UnitType>(
137 Context, Section, DA, RS, SS, SOS, AOS, LS, LE, IsDWO, *this,
138 Index ? Index->getFromOffset(Offset) : nullptr);
139 if (!U->extract(Data, &Offset))
140 return nullptr;
141 return U;
142 };
143 }
144 if (Lazy)
145 return;
146 auto I = this->begin();
147 uint32_t Offset = 0;
148 while (Data.isValidOffset(Offset)) {
149 if (I != this->end() && (*I)->getOffset() == Offset) {
150 ++I;
151 continue;
152 }
153 auto U = Parser(Offset);
154 if (!U)
155 break;
156 Offset = U->getNextUnitOffset();
157 I = std::next(this->insert(I, std::move(U)));
158 }
159 Parsed = true;
160 }
161};
162
163/// Represents base address of the CU.
164struct BaseAddress {
165 uint64_t Address;
166 uint64_t SectionIndex;
167};
168
169/// Represents a unit's contribution to the string offsets table.
170struct StrOffsetsContributionDescriptor {
171 uint64_t Base = 0;
172 uint64_t Size = 0;
173 /// Format and version.
174 dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32};
175
176 StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size,
177 uint8_t Version, dwarf::DwarfFormat Format)
178 : Base(Base), Size(Size), FormParams({Version, 0, Format}) {}
179
180 uint8_t getVersion() const { return FormParams.Version; }
181 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
182 uint8_t getDwarfOffsetByteSize() const {
183 return FormParams.getDwarfOffsetByteSize();
184 }
185 /// Determine whether a contribution to the string offsets table is
186 /// consistent with the relevant section size and that its length is
187 /// a multiple of the size of one of its entries.
188 Optional<StrOffsetsContributionDescriptor>
189 validateContributionSize(DWARFDataExtractor &DA);
190};
191
192class DWARFUnit {
193 DWARFContext &Context;
194 /// Section containing this DWARFUnit.
195 const DWARFSection &InfoSection;
196
197 const DWARFDebugAbbrev *Abbrev;
198 const DWARFSection *RangeSection;
199 uint32_t RangeSectionBase;
200 const DWARFSection &LineSection;
201 StringRef StringSection;
202 const DWARFSection &StringOffsetSection;
203 const DWARFSection *AddrOffsetSection;
204 uint32_t AddrOffsetSectionBase = 0;
205 bool isLittleEndian;
206 bool isDWO;
207 const DWARFUnitSectionBase &UnitSection;
208
209 // Version, address size, and DWARF format.
210 dwarf::FormParams FormParams;
211 /// Start, length, and DWARF format of the unit's contribution to the string
212 /// offsets table (DWARF v5).
213 Optional<StrOffsetsContributionDescriptor> StringOffsetsTableContribution;
214
215 uint32_t Offset;
216 uint32_t Length;
217 mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
218 uint64_t AbbrOffset;
219 uint8_t UnitType;
220 llvm::Optional<BaseAddress> BaseAddr;
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
234 const DWARFUnitIndex::Entry *IndexEntry;
235
236 uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
237 auto First = DieArray.data();
238 assert(Die >= First && Die < First + DieArray.size());
239 return Die - First;
240 }
241
242protected:
243 virtual bool extractImpl(const DWARFDataExtractor &debug_info,
244 uint32_t *offset_ptr);
245
246 /// Size in bytes of the unit header.
247 virtual uint32_t getHeaderSize() const { return getVersion() <= 4 ? 11 : 12; }
248
249 /// Find the unit's contribution to the string offsets table and determine its
250 /// length and form. The given offset is expected to be derived from the unit
251 /// DIE's DW_AT_str_offsets_base attribute.
252 Optional<StrOffsetsContributionDescriptor>
253 determineStringOffsetsTableContribution(DWARFDataExtractor &DA,
254 uint64_t Offset);
255
256 /// Find the unit's contribution to the string offsets table and determine its
257 /// length and form. The given offset is expected to be 0 in a dwo file or,
258 /// in a dwp file, the start of the unit's contribution to the string offsets
259 /// table section (as determined by the index table).
260 Optional<StrOffsetsContributionDescriptor>
261 determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA,
262 uint64_t Offset);
263
264public:
265 DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
266 const DWARFDebugAbbrev *DA, const DWARFSection *RS, StringRef SS,
267 const DWARFSection &SOS, const DWARFSection *AOS,
268 const DWARFSection &LS, bool LE, bool IsDWO,
269 const DWARFUnitSectionBase &UnitSection,
270 const DWARFUnitIndex::Entry *IndexEntry = nullptr);
271
272 virtual ~DWARFUnit();
273
274 DWARFContext& getContext() const { return Context; }
275
276 const DWARFSection &getLineSection() const { return LineSection; }
277 StringRef getStringSection() const { return StringSection; }
278 const DWARFSection &getStringOffsetSection() const {
279 return StringOffsetSection;
280 }
281
282 void setAddrOffsetSection(const DWARFSection *AOS, uint32_t Base) {
283 AddrOffsetSection = AOS;
284 AddrOffsetSectionBase = Base;
285 }
286
287 /// Recursively update address to Die map.
288 void updateAddressDieMap(DWARFDie Die);
289
290 void setRangesSection(const DWARFSection *RS, uint32_t Base) {
291 RangeSection = RS;
292 RangeSectionBase = Base;
293 }
294
295 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
296 bool getStringOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
297
298 DWARFDataExtractor getDebugInfoExtractor() const;
299
300 DataExtractor getStringExtractor() const {
301 return DataExtractor(StringSection, false, 0);
302 }
303
304 bool extract(const DWARFDataExtractor &debug_info, uint32_t *offset_ptr);
305
306 /// extractRangeList - extracts the range list referenced by this compile
307 /// unit from .debug_ranges section. Returns true on success.
308 /// Requires that compile unit is already extracted.
309 bool extractRangeList(uint32_t RangeListOffset,
310 DWARFDebugRangeList &RangeList) const;
311 void clear();
312 uint32_t getOffset() const { return Offset; }
313 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
314 uint32_t getLength() const { return Length; }
315
316 const Optional<StrOffsetsContributionDescriptor> &
317 getStringOffsetsTableContribution() const {
318 return StringOffsetsTableContribution;
319 }
320 const dwarf::FormParams &getFormParams() const { return FormParams; }
321 uint16_t getVersion() const { return FormParams.Version; }
322 dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
323 uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
324 uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
325 uint8_t getDwarfOffsetByteSize() const {
326 return FormParams.getDwarfOffsetByteSize();
327 }
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
341 uint8_t getUnitType() const { return UnitType; }
342
343 static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
344 switch (UnitType) {
345 case dwarf::DW_UT_compile:
346 return Tag == dwarf::DW_TAG_compile_unit;
347 case dwarf::DW_UT_type:
348 return Tag == dwarf::DW_TAG_type_unit;
349 case dwarf::DW_UT_partial:
350 return Tag == dwarf::DW_TAG_partial_unit;
351 case dwarf::DW_UT_skeleton:
352 return Tag == dwarf::DW_TAG_skeleton_unit;
353 case dwarf::DW_UT_split_compile:
354 case dwarf::DW_UT_split_type:
355 return dwarf::isUnitType(Tag);
356 }
357 return false;
358 }
359
360 /// \brief Return the number of bytes for the header of a unit of
361 /// UnitType type.
362 ///
363 /// This function must be called with a valid unit type which in
364 /// DWARF5 is defined as one of the following six types.
365 static uint32_t getDWARF5HeaderSize(uint8_t UnitType) {
366 switch (UnitType) {
367 case dwarf::DW_UT_compile:
368 case dwarf::DW_UT_partial:
369 return 12;
370 case dwarf::DW_UT_skeleton:
371 case dwarf::DW_UT_split_compile:
372 return 20;
373 case dwarf::DW_UT_type:
374 case dwarf::DW_UT_split_type:
375 return 24;
376 }
377 llvm_unreachable("Invalid UnitType.");
378 }
379
380 llvm::Optional<BaseAddress> getBaseAddress() const { return BaseAddr; }
381
382 void setBaseAddress(BaseAddress BaseAddr) { this->BaseAddr = BaseAddr; }
383
384 DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
385 extractDIEsIfNeeded(ExtractUnitDIEOnly);
386 if (DieArray.empty())
387 return DWARFDie();
388 return DWARFDie(this, &DieArray[0]);
389 }
390
391 const char *getCompilationDir();
392 Optional<uint64_t> getDWOId();
393
394 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
395
396 /// Returns subprogram DIE with address range encompassing the provided
397 /// address. The pointer is alive as long as parsed compile unit DIEs are not
398 /// cleared.
399 DWARFDie getSubroutineForAddress(uint64_t Address);
400
401 /// getInlinedChainForAddress - fetches inlined chain for a given address.
402 /// Returns empty chain if there is no subprogram containing address. The
403 /// chain is valid as long as parsed compile unit DIEs are not cleared.
404 void getInlinedChainForAddress(uint64_t Address,
405 SmallVectorImpl<DWARFDie> &InlinedChain);
406
407 /// getUnitSection - Return the DWARFUnitSection containing this unit.
408 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
409
410 /// \brief Returns the number of DIEs in the unit. Parses the unit
411 /// if necessary.
412 unsigned getNumDIEs() {
413 extractDIEsIfNeeded(false);
414 return DieArray.size();
415 }
416
417 /// \brief Return the index of a DIE inside the unit's DIE vector.
418 ///
419 /// It is illegal to call this method with a DIE that hasn't be
420 /// created by this unit. In other word, it's illegal to call this
421 /// method on a DIE that isn't accessible by following
422 /// children/sibling links starting from this unit's getUnitDIE().
423 uint32_t getDIEIndex(const DWARFDie &D) {
424 return getDIEIndex(D.getDebugInfoEntry());
425 }
426
427 /// \brief Return the DIE object at the given index.
428 DWARFDie getDIEAtIndex(unsigned Index) {
429 assert(Index < DieArray.size());
430 return DWARFDie(this, &DieArray[Index]);
431 }
432
433 DWARFDie getParent(const DWARFDebugInfoEntry *Die);
434 DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
435 DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
436
437 /// \brief Return the DIE object for a given offset inside the
438 /// unit's DIE vector.
439 ///
440 /// The unit needs to have its DIEs extracted for this method to work.
441 DWARFDie getDIEForOffset(uint32_t Offset) {
442 extractDIEsIfNeeded(false);
443 assert(!DieArray.empty());
444 auto it = std::lower_bound(
445 DieArray.begin(), DieArray.end(), Offset,
446 [](const DWARFDebugInfoEntry &LHS, uint32_t Offset) {
447 return LHS.getOffset() < Offset;
448 });
449 if (it != DieArray.end() && it->getOffset() == Offset)
450 return DWARFDie(this, &*it);
451 return DWARFDie();
452 }
453
454 uint32_t getLineTableOffset() const {
455 if (IndexEntry)
456 if (const auto *Contrib = IndexEntry->getOffset(DW_SECT_LINE))
457 return Contrib->Offset;
458 return 0;
459 }
460
461 die_iterator_range dies() {
462 extractDIEsIfNeeded(false);
463 return die_iterator_range(DieArray.begin(), DieArray.end());
464 }
465
466private:
467 /// Size in bytes of the .debug_info data associated with this compile unit.
468 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
469
470 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
471 /// hasn't already been done. Returns the number of DIEs parsed at this call.
472 size_t extractDIEsIfNeeded(bool CUDieOnly);
473
474 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
475 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
476 std::vector<DWARFDebugInfoEntry> &DIEs) const;
477
478 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
479 void clearDIEs(bool KeepCUDie);
480
481 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
482 /// it was actually constructed.
483 bool parseDWO();
484};
485
486} // end namespace llvm
487
488#endif // LLVM_DEBUGINFO_DWARF_DWARFUNIT_H