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_LIB_DEBUGINFO_DWARFUNIT_H
11 #define LLVM_LIB_DEBUGINFO_DWARFUNIT_H
13 #include "DWARFDebugAbbrev.h"
14 #include "DWARFDebugInfoEntry.h"
15 #include "DWARFDebugRangeList.h"
16 #include "DWARFRelocMap.h"
26 class DWARFDebugAbbrev;
31 /// Base class for all DWARFUnitSection classes. This provides the
32 /// functionality common to all unit types.
33 class DWARFUnitSectionBase {
35 /// Returns the Unit that contains the given section offset in the
36 /// same section this Unit originated from.
37 virtual DWARFUnit *getUnitForOffset(uint32_t Offset) const = 0;
40 ~DWARFUnitSectionBase() {}
43 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
44 template<typename UnitType>
45 class DWARFUnitSection final : public SmallVector<std::unique_ptr<UnitType>, 1>,
46 public DWARFUnitSectionBase {
48 struct UnitOffsetComparator {
49 bool operator()(uint32_t LHS,
50 const std::unique_ptr<UnitType> &RHS) const {
51 return LHS < RHS->getNextUnitOffset();
57 DWARFUnitSection(DWARFUnitSection &&DUS) :
58 SmallVector<std::unique_ptr<UnitType>, 1>(std::move(DUS)) {}
60 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
61 typedef typename UnitVector::iterator iterator;
62 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
64 UnitType *getUnitForOffset(uint32_t Offset) const {
65 auto *CU = std::upper_bound(this->begin(), this->end(), Offset,
66 UnitOffsetComparator());
67 if (CU != this->end())
74 DWARFContext &Context;
76 const DWARFDebugAbbrev *Abbrev;
77 StringRef InfoSection;
78 StringRef RangeSection;
79 uint32_t RangeSectionBase;
80 StringRef StringSection;
81 StringRef StringOffsetSection;
82 StringRef AddrOffsetSection;
83 uint32_t AddrOffsetSectionBase;
84 const RelocAddrMap *RelocMap;
86 const DWARFUnitSectionBase &UnitSection;
91 const DWARFAbbreviationDeclarationSet *Abbrevs;
94 // The compile unit debug information entry items.
95 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
98 object::OwningBinary<object::ObjectFile> DWOFile;
99 std::unique_ptr<DWARFContext> DWOContext;
102 DWOHolder(StringRef DWOPath);
103 DWARFUnit *getUnit() const { return DWOU; }
105 std::unique_ptr<DWOHolder> DWO;
108 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
109 /// Size in bytes of the unit header.
110 virtual uint32_t getHeaderSize() const { return 11; }
113 DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
114 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
115 const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
117 virtual ~DWARFUnit();
119 DWARFContext& getContext() const { return Context; }
121 StringRef getStringSection() const { return StringSection; }
122 StringRef getStringOffsetSection() const { return StringOffsetSection; }
123 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
124 AddrOffsetSection = AOS;
125 AddrOffsetSectionBase = Base;
127 void setRangesSection(StringRef RS, uint32_t Base) {
129 RangeSectionBase = Base;
132 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
133 // FIXME: Result should be uint64_t in DWARF64.
134 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
136 DataExtractor getDebugInfoExtractor() const {
137 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
139 DataExtractor getStringExtractor() const {
140 return DataExtractor(StringSection, false, 0);
143 const RelocAddrMap *getRelocMap() const { return RelocMap; }
145 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
147 /// extractRangeList - extracts the range list referenced by this compile
148 /// unit from .debug_ranges section. Returns true on success.
149 /// Requires that compile unit is already extracted.
150 bool extractRangeList(uint32_t RangeListOffset,
151 DWARFDebugRangeList &RangeList) const;
153 uint32_t getOffset() const { return Offset; }
154 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
155 uint32_t getLength() const { return Length; }
156 uint16_t getVersion() const { return Version; }
157 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
160 uint8_t getAddressByteSize() const { return AddrSize; }
161 uint64_t getBaseAddress() const { return BaseAddr; }
163 void setBaseAddress(uint64_t base_addr) {
164 BaseAddr = base_addr;
167 const DWARFDebugInfoEntryMinimal *
168 getCompileUnitDIE(bool extract_cu_die_only = true) {
169 extractDIEsIfNeeded(extract_cu_die_only);
170 return DieArray.empty() ? nullptr : &DieArray[0];
173 const char *getCompilationDir();
176 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
178 /// getInlinedChainForAddress - fetches inlined chain for a given address.
179 /// Returns empty chain if there is no subprogram containing address. The
180 /// chain is valid as long as parsed compile unit DIEs are not cleared.
181 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
183 /// getUnitSection - Return the DWARFUnitSection containing this unit.
184 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
187 /// Size in bytes of the .debug_info data associated with this compile unit.
188 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
190 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
191 /// hasn't already been done. Returns the number of DIEs parsed at this call.
192 size_t extractDIEsIfNeeded(bool CUDieOnly);
193 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
194 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
195 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
196 /// setDIERelations - We read in all of the DIE entries into our flat list
197 /// of DIE entries and now we need to go back through all of them and set the
198 /// parent, sibling and child pointers for quick DIE navigation.
199 void setDIERelations();
200 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
201 void clearDIEs(bool KeepCUDie);
203 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
204 /// it was actually constructed.
207 /// getSubprogramForAddress - Returns subprogram DIE with address range
208 /// encompassing the provided address. The pointer is alive as long as parsed
209 /// compile unit DIEs are not cleared.
210 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);