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();
56 typedef llvm::SmallVectorImpl<std::unique_ptr<UnitType>> UnitVector;
57 typedef typename UnitVector::iterator iterator;
58 typedef llvm::iterator_range<typename UnitVector::iterator> iterator_range;
60 UnitType *getUnitForOffset(uint32_t Offset) const {
61 auto *CU = std::upper_bound(this->begin(), this->end(), Offset,
62 UnitOffsetComparator());
63 if (CU != this->end())
70 DWARFContext &Context;
72 const DWARFDebugAbbrev *Abbrev;
73 StringRef InfoSection;
74 StringRef RangeSection;
75 uint32_t RangeSectionBase;
76 StringRef StringSection;
77 StringRef StringOffsetSection;
78 StringRef AddrOffsetSection;
79 uint32_t AddrOffsetSectionBase;
80 const RelocAddrMap *RelocMap;
82 const DWARFUnitSectionBase &UnitSection;
87 const DWARFAbbreviationDeclarationSet *Abbrevs;
90 // The compile unit debug information entry items.
91 std::vector<DWARFDebugInfoEntryMinimal> DieArray;
94 object::OwningBinary<object::ObjectFile> DWOFile;
95 std::unique_ptr<DWARFContext> DWOContext;
98 DWOHolder(StringRef DWOPath);
99 DWARFUnit *getUnit() const { return DWOU; }
101 std::unique_ptr<DWOHolder> DWO;
104 virtual bool extractImpl(DataExtractor debug_info, uint32_t *offset_ptr);
105 /// Size in bytes of the unit header.
106 virtual uint32_t getHeaderSize() const { return 11; }
109 DWARFUnit(DWARFContext& Context, const DWARFDebugAbbrev *DA, StringRef IS,
110 StringRef RS, StringRef SS, StringRef SOS, StringRef AOS,
111 const RelocAddrMap *M, bool LE, const DWARFUnitSectionBase &UnitSection);
113 virtual ~DWARFUnit();
115 DWARFContext& getContext() const { return Context; }
117 StringRef getStringSection() const { return StringSection; }
118 StringRef getStringOffsetSection() const { return StringOffsetSection; }
119 void setAddrOffsetSection(StringRef AOS, uint32_t Base) {
120 AddrOffsetSection = AOS;
121 AddrOffsetSectionBase = Base;
123 void setRangesSection(StringRef RS, uint32_t Base) {
125 RangeSectionBase = Base;
128 bool getAddrOffsetSectionItem(uint32_t Index, uint64_t &Result) const;
129 // FIXME: Result should be uint64_t in DWARF64.
130 bool getStringOffsetSectionItem(uint32_t Index, uint32_t &Result) const;
132 DataExtractor getDebugInfoExtractor() const {
133 return DataExtractor(InfoSection, isLittleEndian, AddrSize);
135 DataExtractor getStringExtractor() const {
136 return DataExtractor(StringSection, false, 0);
139 const RelocAddrMap *getRelocMap() const { return RelocMap; }
141 bool extract(DataExtractor debug_info, uint32_t* offset_ptr);
143 /// extractRangeList - extracts the range list referenced by this compile
144 /// unit from .debug_ranges section. Returns true on success.
145 /// Requires that compile unit is already extracted.
146 bool extractRangeList(uint32_t RangeListOffset,
147 DWARFDebugRangeList &RangeList) const;
149 uint32_t getOffset() const { return Offset; }
150 uint32_t getNextUnitOffset() const { return Offset + Length + 4; }
151 uint32_t getLength() const { return Length; }
152 uint16_t getVersion() const { return Version; }
153 const DWARFAbbreviationDeclarationSet *getAbbreviations() const {
156 uint8_t getAddressByteSize() const { return AddrSize; }
157 uint64_t getBaseAddress() const { return BaseAddr; }
159 void setBaseAddress(uint64_t base_addr) {
160 BaseAddr = base_addr;
163 const DWARFDebugInfoEntryMinimal *
164 getCompileUnitDIE(bool extract_cu_die_only = true) {
165 extractDIEsIfNeeded(extract_cu_die_only);
166 return DieArray.empty() ? nullptr : &DieArray[0];
169 const char *getCompilationDir();
172 void collectAddressRanges(DWARFAddressRangesVector &CURanges);
174 /// getInlinedChainForAddress - fetches inlined chain for a given address.
175 /// Returns empty chain if there is no subprogram containing address. The
176 /// chain is valid as long as parsed compile unit DIEs are not cleared.
177 DWARFDebugInfoEntryInlinedChain getInlinedChainForAddress(uint64_t Address);
179 /// getUnitSection - Return the DWARFUnitSection containing this unit.
180 const DWARFUnitSectionBase &getUnitSection() const { return UnitSection; }
183 /// Size in bytes of the .debug_info data associated with this compile unit.
184 size_t getDebugInfoSize() const { return Length + 4 - getHeaderSize(); }
186 /// extractDIEsIfNeeded - Parses a compile unit and indexes its DIEs if it
187 /// hasn't already been done. Returns the number of DIEs parsed at this call.
188 size_t extractDIEsIfNeeded(bool CUDieOnly);
189 /// extractDIEsToVector - Appends all parsed DIEs to a vector.
190 void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
191 std::vector<DWARFDebugInfoEntryMinimal> &DIEs) const;
192 /// setDIERelations - We read in all of the DIE entries into our flat list
193 /// of DIE entries and now we need to go back through all of them and set the
194 /// parent, sibling and child pointers for quick DIE navigation.
195 void setDIERelations();
196 /// clearDIEs - Clear parsed DIEs to keep memory usage low.
197 void clearDIEs(bool KeepCUDie);
199 /// parseDWO - Parses .dwo file for current compile unit. Returns true if
200 /// it was actually constructed.
203 /// getSubprogramForAddress - Returns subprogram DIE with address range
204 /// encompassing the provided address. The pointer is alive as long as parsed
205 /// compile unit DIEs are not cleared.
206 const DWARFDebugInfoEntryMinimal *getSubprogramForAddress(uint64_t Address);