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;
39 virtual ~DWARFUnitSectionBase() {}
42 /// Concrete instance of DWARFUnitSection, specialized for one Unit type.
43 template<typename UnitType>
44 class DWARFUnitSection : public SmallVector<std::unique_ptr<UnitType>, 1>,
45 public DWARFUnitSectionBase {
47 struct UnitOffsetComparator {
48 bool operator()(const std::unique_ptr<UnitType> &LHS,
49 const std::unique_ptr<UnitType> &RHS) const {
50 return LHS->getOffset() < RHS->getOffset();
52 bool operator()(const std::unique_ptr<UnitType> &LHS,
54 return LHS->getOffset() < RHS;
56 bool operator()(uint32_t LHS,
57 const std::unique_ptr<UnitType> &RHS) const {
58 return LHS < RHS->getOffset();
63 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
64 typedef typename UnitVector::iterator iterator;
65 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
67 UnitType *getUnitForOffset(uint32_t Offset) const {
68 auto *CU = std::lower_bound(this->begin(), this->end(), Offset,
69 UnitOffsetComparator());
70 if (CU != this->end())
77 DWARFContext &Context;
79 const DWARFDebugAbbrev *Abbrev;
80 StringRef InfoSection;
81 StringRef RangeSection;
82 uint32_t RangeSectionBase;
83 StringRef StringSection;
84 StringRef StringOffsetSection;
85 StringRef AddrOffsetSection;
86 uint32_t AddrOffsetSectionBase;
87 const RelocAddrMap *RelocMap;
89 const DWARFUnitSectionBase &UnitSection;
94 const DWARFAbbreviationDeclarationSet *Abbrevs;
97 // The compile unit debug information entry items.
98 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
101 object::OwningBinary<object::ObjectFile> DWOFile;
102 std::unique_ptr<DWARFContext> DWOContext;
105 DWOHolder(StringRef DWOPath);
106 DWARFUnit *getUnit() const { return DWOU; }
108 std::unique_ptr<DWOHolder> DWO;
111 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
112 /// Size in bytes of the unit header.
113 virtual uint32_t getHeaderSize() const { return 11; }
116 DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
117 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
118 const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
120 virtual ~DWARFUnit();
122 DWARFContext& getContext() const { return Context; }
124 StringRef getStringSection() const { return StringSection; }
125 StringRef getStringOffsetSection() const { return StringOffsetSection; }
126 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
127 AddrOffsetSection = AOS;
128 AddrOffsetSectionBase = Base;
130 void setRangesSection(StringRef RS, uint32_t Base) {
132 RangeSectionBase = Base;
135 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
136 // FIXME: Result should be uint64_t in DWARF64.
137 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
139 DataExtractor getDebugInfoExtractor() const {
140 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
142 DataExtractor getStringExtractor() const {
143 return DataExtractor(StringSection, false, 0);
146 const RelocAddrMap *getRelocMap() const { return RelocMap; }
148 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
150 /// extractRangeList - extracts the range list referenced by this compile
151 /// unit from .debug_ranges section. Returns true on success.
152 /// Requires that compile unit is already extracted.
153 bool extractRangeList(uint32_t RangeListOffset,
154 DWARFDebugRangeList &RangeList) const;
156 uint32_t getOffset() const { return Offset; }
157 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
158 uint32_t getLength() const { return Length; }
159 uint16_t getVersion() const { return Version; }
160 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
163 uint8_t getAddressByteSize() const { return AddrSize; }
164 uint64_t getBaseAddress() const { return BaseAddr; }
166 void setBaseAddress(uint64_t base_addr) {
167 BaseAddr = base_addr;
170 const DWARFDebugInfoEntryMinimal *
171 getCompileUnitDIE(bool extract_cu_die_only = true) {
172 extractDIEsIfNeeded(extract_cu_die_only);
173 return DieArray.empty() ? nullptr : &DieArray[0];
176 const char *getCompilationDir();
179 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
181 /// getInlinedChainForAddress - fetches inlined chain for a given address.
182 /// Returns empty chain if there is no subprogram containing address. The
183 /// chain is valid as long as parsed compile unit DIEs are not cleared.
184 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
186 /// getUnitSection - Return the DWARFUnitSection containing this unit.
187 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
190 /// Size in bytes of the .debug_info data associated with this compile unit.
191 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
193 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
194 /// hasn't already been done. Returns the number of DIEs parsed at this call.
195 size_t extractDIEsIfNeeded(bool CUDieOnly);
196 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
197 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
198 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
199 /// setDIERelations - We read in all of the DIE entries into our flat list
200 /// of DIE entries and now we need to go back through all of them and set the
201 /// parent, sibling and child pointers for quick DIE navigation.
202 void setDIERelations();
203 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
204 void clearDIEs(bool KeepCUDie);
206 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
207 /// it was actually constructed.
210 /// getSubprogramForAddress - Returns subprogram DIE with address range
211 /// encompassing the provided address. The pointer is alive as long as parsed
212 /// compile unit DIEs are not cleared.
213 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);