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()(const std::unique_ptr<UnitType> &LHS,
50 const std::unique_ptr<UnitType> &RHS) const {
51 return LHS->getOffset() < RHS->getOffset();
53 bool operator()(const std::unique_ptr<UnitType> &LHS,
55 return LHS->getOffset() < RHS;
57 bool operator()(uint32_t LHS,
58 const std::unique_ptr<UnitType> &RHS) const {
59 return LHS < RHS->getOffset();
64 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
65 typedef typename UnitVector::iterator iterator;
66 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
68 UnitType *getUnitForOffset(uint32_t Offset) const {
69 auto *CU = std::lower_bound(this->begin(), this->end(), Offset,
70 UnitOffsetComparator());
71 if (CU != this->end())
78 DWARFContext &Context;
80 const DWARFDebugAbbrev *Abbrev;
81 StringRef InfoSection;
82 StringRef RangeSection;
83 uint32_t RangeSectionBase;
84 StringRef StringSection;
85 StringRef StringOffsetSection;
86 StringRef AddrOffsetSection;
87 uint32_t AddrOffsetSectionBase;
88 const RelocAddrMap *RelocMap;
90 const DWARFUnitSectionBase &UnitSection;
95 const DWARFAbbreviationDeclarationSet *Abbrevs;
98 // The compile unit debug information entry items.
99 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
102 object::OwningBinary<object::ObjectFile> DWOFile;
103 std::unique_ptr<DWARFContext> DWOContext;
106 DWOHolder(StringRef DWOPath);
107 DWARFUnit *getUnit() const { return DWOU; }
109 std::unique_ptr<DWOHolder> DWO;
112 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
113 /// Size in bytes of the unit header.
114 virtual uint32_t getHeaderSize() const { return 11; }
117 DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
118 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
119 const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
121 virtual ~DWARFUnit();
123 DWARFContext& getContext() const { return Context; }
125 StringRef getStringSection() const { return StringSection; }
126 StringRef getStringOffsetSection() const { return StringOffsetSection; }
127 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
128 AddrOffsetSection = AOS;
129 AddrOffsetSectionBase = Base;
131 void setRangesSection(StringRef RS, uint32_t Base) {
133 RangeSectionBase = Base;
136 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
137 // FIXME: Result should be uint64_t in DWARF64.
138 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
140 DataExtractor getDebugInfoExtractor() const {
141 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
143 DataExtractor getStringExtractor() const {
144 return DataExtractor(StringSection, false, 0);
147 const RelocAddrMap *getRelocMap() const { return RelocMap; }
149 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
151 /// extractRangeList - extracts the range list referenced by this compile
152 /// unit from .debug_ranges section. Returns true on success.
153 /// Requires that compile unit is already extracted.
154 bool extractRangeList(uint32_t RangeListOffset,
155 DWARFDebugRangeList &RangeList) const;
157 uint32_t getOffset() const { return Offset; }
158 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
159 uint32_t getLength() const { return Length; }
160 uint16_t getVersion() const { return Version; }
161 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
164 uint8_t getAddressByteSize() const { return AddrSize; }
165 uint64_t getBaseAddress() const { return BaseAddr; }
167 void setBaseAddress(uint64_t base_addr) {
168 BaseAddr = base_addr;
171 const DWARFDebugInfoEntryMinimal *
172 getCompileUnitDIE(bool extract_cu_die_only = true) {
173 extractDIEsIfNeeded(extract_cu_die_only);
174 return DieArray.empty() ? nullptr : &DieArray[0];
177 const char *getCompilationDir();
180 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
182 /// getInlinedChainForAddress - fetches inlined chain for a given address.
183 /// Returns empty chain if there is no subprogram containing address. The
184 /// chain is valid as long as parsed compile unit DIEs are not cleared.
185 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
187 /// getUnitSection - Return the DWARFUnitSection containing this unit.
188 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
191 /// Size in bytes of the .debug_info data associated with this compile unit.
192 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
194 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
195 /// hasn't already been done. Returns the number of DIEs parsed at this call.
196 size_t extractDIEsIfNeeded(bool CUDieOnly);
197 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
198 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
199 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
200 /// setDIERelations - We read in all of the DIE entries into our flat list
201 /// of DIE entries and now we need to go back through all of them and set the
202 /// parent, sibling and child pointers for quick DIE navigation.
203 void setDIERelations();
204 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
205 void clearDIEs(bool KeepCUDie);
207 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
208 /// it was actually constructed.
211 /// getSubprogramForAddress - Returns subprogram DIE with address range
212 /// encompassing the provided address. The pointer is alive as long as parsed
213 /// compile unit DIEs are not cleared.
214 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);