#ifndef LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
#define LLVM_DEBUGINFO_DWARF_DWARFUNIT_H
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/DebugInfo/DWARF/DWARFLocationExpression.h"
#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
#include "llvm/Support/DataExtractor.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <map>
#include <memory>
#include <set>
#include <utility>
#include <vector>
namespace llvm {
class DWARFAbbreviationDeclarationSet;
class DWARFContext;
class DWARFDebugAbbrev;
class DWARFUnit;
class DWARFDebugRangeList;
class DWARFLocationTable;
class DWARFObject;
class raw_ostream;
struct DIDumpOptions;
struct DWARFSection;
class DWARFUnitHeader {
uint64_t Offset = 0;
dwarf::FormParams FormParams;
uint64_t Length = 0;
uint64_t AbbrOffset = 0;
const DWARFUnitIndex::Entry *IndexEntry = nullptr;
uint64_t TypeHash = 0;
uint64_t TypeOffset = 0;
Optional<uint64_t> DWOId;
uint8_t UnitType = 0;
uint8_t Size = 0;
public:
bool extract(DWARFContext &Context, const DWARFDataExtractor &debug_info,
uint64_t *offset_ptr, DWARFSectionKind SectionKind);
bool applyIndexEntry(const DWARFUnitIndex::Entry *Entry);
uint64_t getOffset() const { return Offset; }
const dwarf::FormParams &getFormParams() const { return FormParams; }
uint16_t getVersion() const { return FormParams.Version; }
dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
uint8_t getAddressByteSize() const { return FormParams.AddrSize; }
uint8_t getRefAddrByteSize() const { return FormParams.getRefAddrByteSize(); }
uint8_t getDwarfOffsetByteSize() const {
return FormParams.getDwarfOffsetByteSize();
}
uint64_t getLength() const { return Length; }
uint64_t getAbbrOffset() const { return AbbrOffset; }
Optional<uint64_t> getDWOId() const { return DWOId; }
void setDWOId(uint64_t Id) {
assert((!DWOId || *DWOId == Id) && "setting DWOId to a different value");
DWOId = Id;
}
const DWARFUnitIndex::Entry *getIndexEntry() const { return IndexEntry; }
uint64_t getTypeHash() const { return TypeHash; }
uint64_t getTypeOffset() const { return TypeOffset; }
uint8_t getUnitType() const { return UnitType; }
bool isTypeUnit() const {
return UnitType == dwarf::DW_UT_type || UnitType == dwarf::DW_UT_split_type;
}
uint8_t getSize() const { return Size; }
uint8_t getUnitLengthFieldByteSize() const {
return dwarf::getUnitLengthFieldByteSize(FormParams.Format);
}
uint64_t getNextUnitOffset() const {
return Offset + Length + getUnitLengthFieldByteSize();
}
};
const DWARFUnitIndex &getDWARFUnitIndex(DWARFContext &Context,
DWARFSectionKind Kind);
bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U);
class DWARFUnitVector final : public SmallVector<std::unique_ptr<DWARFUnit>, 1> {
std::function<std::unique_ptr<DWARFUnit>(uint64_t, DWARFSectionKind,
const DWARFSection *,
const DWARFUnitIndex::Entry *)>
Parser;
int NumInfoUnits = -1;
public:
using UnitVector = SmallVectorImpl<std::unique_ptr<DWARFUnit>>;
using iterator = typename UnitVector::iterator;
using iterator_range = llvm::iterator_range<typename UnitVector::iterator>;
using compile_unit_range =
decltype(make_filter_range(std::declval<iterator_range>(), isCompileUnit));
DWARFUnit *getUnitForOffset(uint64_t Offset) const;
DWARFUnit *getUnitForIndexEntry(const DWARFUnitIndex::Entry &E);
void addUnitsForSection(DWARFContext &C, const DWARFSection &Section,
DWARFSectionKind SectionKind);
void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection,
DWARFSectionKind SectionKind, bool Lazy = false);
DWARFUnit *addUnit(std::unique_ptr<DWARFUnit> Unit);
unsigned getNumUnits() const { return size(); }
unsigned getNumInfoUnits() const {
return NumInfoUnits == -1 ? size() : NumInfoUnits;
}
unsigned getNumTypesUnits() const { return size() - NumInfoUnits; }
void finishedInfoUnits() { NumInfoUnits = size(); }
private:
void addUnitsImpl(DWARFContext &Context, const DWARFObject &Obj,
const DWARFSection &Section, const DWARFDebugAbbrev *DA,
const DWARFSection *RS, const DWARFSection *LocSection,
StringRef SS, const DWARFSection &SOS,
const DWARFSection *AOS, const DWARFSection &LS, bool LE,
bool IsDWO, bool Lazy, DWARFSectionKind SectionKind);
};
struct StrOffsetsContributionDescriptor {
uint64_t Base = 0;
uint64_t Size = 0;
dwarf::FormParams FormParams = {0, 0, dwarf::DwarfFormat::DWARF32};
StrOffsetsContributionDescriptor(uint64_t Base, uint64_t Size,
uint8_t Version, dwarf::DwarfFormat Format)
: Base(Base), Size(Size), FormParams({Version, 0, Format}) {}
StrOffsetsContributionDescriptor() = default;
uint8_t getVersion() const { return FormParams.Version; }
dwarf::DwarfFormat getFormat() const { return FormParams.Format; }
uint8_t getDwarfOffsetByteSize() const {
return FormParams.getDwarfOffsetByteSize();
}
Expected<StrOffsetsContributionDescriptor>
validateContributionSize(DWARFDataExtractor &DA);
};
class DWARFUnit {
DWARFContext &Context;
const DWARFSection &InfoSection;
DWARFUnitHeader Header;
const DWARFDebugAbbrev *Abbrev;
const DWARFSection *RangeSection;
uint64_t RangeSectionBase;
uint64_t LocSectionBase;
std::unique_ptr<DWARFLocationTable> LocTable;
const DWARFSection &LineSection;
StringRef StringSection;
const DWARFSection &StringOffsetSection;
const DWARFSection *AddrOffsetSection;
DWARFUnit *SU;
Optional<uint64_t> AddrOffsetSectionBase;
bool isLittleEndian;
bool IsDWO;
const DWARFUnitVector &UnitVector;
Optional<StrOffsetsContributionDescriptor> StringOffsetsTableContribution;
mutable const DWARFAbbreviationDeclarationSet *Abbrevs;
llvm::Optional<object::SectionedAddress> BaseAddr;
std::vector<DWARFDebugInfoEntry> DieArray;
std::map<uint64_t, std::pair<uint64_t, DWARFDie>> AddrDieMap;
std::map<uint64_t, std::pair<uint64_t, DWARFDie>> VariableDieMap;
DenseSet<uint64_t> RootsParsedForVariables;
using die_iterator_range =
iterator_range<std::vector<DWARFDebugInfoEntry>::iterator>;
std::shared_ptr<DWARFUnit> DWO;
uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) {
auto First = DieArray.data();
assert(Die >= First && Die < First + DieArray.size());
return Die - First;
}
protected:
const DWARFUnitHeader &getHeader() const { return Header; }
Expected<Optional<StrOffsetsContributionDescriptor>>
determineStringOffsetsTableContribution(DWARFDataExtractor &DA);
Expected<Optional<StrOffsetsContributionDescriptor>>
determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA);
public:
DWARFUnit(DWARFContext &Context, const DWARFSection &Section,
const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA,
const DWARFSection *RS, const DWARFSection *LocSection,
StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
const DWARFSection &LS, bool LE, bool IsDWO,
const DWARFUnitVector &UnitVector);
virtual ~DWARFUnit();
bool isDWOUnit() const { return IsDWO; }
DWARFContext& getContext() const { return Context; }
const DWARFSection &getInfoSection() const { return InfoSection; }
uint64_t getOffset() const { return Header.getOffset(); }
const dwarf::FormParams &getFormParams() const {
return Header.getFormParams();
}
uint16_t getVersion() const { return Header.getVersion(); }
uint8_t getAddressByteSize() const { return Header.getAddressByteSize(); }
uint8_t getRefAddrByteSize() const { return Header.getRefAddrByteSize(); }
uint8_t getDwarfOffsetByteSize() const {
return Header.getDwarfOffsetByteSize();
}
uint32_t getHeaderSize() const { return Header.getSize(); }
uint64_t getLength() const { return Header.getLength(); }
dwarf::DwarfFormat getFormat() const { return Header.getFormat(); }
uint8_t getUnitType() const { return Header.getUnitType(); }
bool isTypeUnit() const { return Header.isTypeUnit(); }
uint64_t getAbbrOffset() const { return Header.getAbbrOffset(); }
uint64_t getNextUnitOffset() const { return Header.getNextUnitOffset(); }
const DWARFSection &getLineSection() const { return LineSection; }
StringRef getStringSection() const { return StringSection; }
const DWARFSection &getStringOffsetSection() const {
return StringOffsetSection;
}
void setSkeletonUnit(DWARFUnit *SU) { this->SU = SU; }
DWARFUnit *getLinkedUnit() { return IsDWO ? SU : this; }
void setAddrOffsetSection(const DWARFSection *AOS, uint64_t Base) {
AddrOffsetSection = AOS;
AddrOffsetSectionBase = Base;
}
Optional<uint64_t> getAddrOffsetSectionBase() const {
return AddrOffsetSectionBase;
}
void updateAddressDieMap(DWARFDie Die);
void updateVariableDieMap(DWARFDie Die);
void setRangesSection(const DWARFSection *RS, uint64_t Base) {
RangeSection = RS;
RangeSectionBase = Base;
}
uint64_t getLocSectionBase() const {
return LocSectionBase;
}
Optional<object::SectionedAddress>
getAddrOffsetSectionItem(uint32_t Index) const;
Expected<uint64_t> getStringOffsetSectionItem(uint32_t Index) const;
DWARFDataExtractor getDebugInfoExtractor() const;
DataExtractor getStringExtractor() const {
return DataExtractor(StringSection, false, 0);
}
const DWARFLocationTable &getLocationTable() { return *LocTable; }
Error extractRangeList(uint64_t RangeListOffset,
DWARFDebugRangeList &RangeList) const;
void clear();
const Optional<StrOffsetsContributionDescriptor> &
getStringOffsetsTableContribution() const {
return StringOffsetsTableContribution;
}
uint8_t getDwarfStringOffsetsByteSize() const {
assert(StringOffsetsTableContribution);
return StringOffsetsTableContribution->getDwarfOffsetByteSize();
}
uint64_t getStringOffsetsBase() const {
assert(StringOffsetsTableContribution);
return StringOffsetsTableContribution->Base;
}
uint64_t getAbbreviationsOffset() const { return Header.getAbbrOffset(); }
const DWARFAbbreviationDeclarationSet *getAbbreviations() const;
static bool isMatchingUnitTypeAndTag(uint8_t UnitType, dwarf::Tag Tag) {
switch (UnitType) {
case dwarf::DW_UT_compile:
return Tag == dwarf::DW_TAG_compile_unit;
case dwarf::DW_UT_type:
return Tag == dwarf::DW_TAG_type_unit;
case dwarf::DW_UT_partial:
return Tag == dwarf::DW_TAG_partial_unit;
case dwarf::DW_UT_skeleton:
return Tag == dwarf::DW_TAG_skeleton_unit;
case dwarf::DW_UT_split_compile:
case dwarf::DW_UT_split_type:
return dwarf::isUnitType(Tag);
}
return false;
}
llvm::Optional<object::SectionedAddress> getBaseAddress();
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly = true) {
extractDIEsIfNeeded(ExtractUnitDIEOnly);
if (DieArray.empty())
return DWARFDie();
return DWARFDie(this, &DieArray[0]);
}
DWARFDie getNonSkeletonUnitDIE(bool ExtractUnitDIEOnly = true) {
parseDWO();
if (DWO)
return DWO->getUnitDIE(ExtractUnitDIEOnly);
return getUnitDIE(ExtractUnitDIEOnly);
}
const char *getCompilationDir();
Optional<uint64_t> getDWOId() {
extractDIEsIfNeeded( true);
return getHeader().getDWOId();
}
void setDWOId(uint64_t NewID) { Header.setDWOId(NewID); }
Expected<DWARFAddressRangesVector> findRnglistFromOffset(uint64_t Offset);
Expected<DWARFAddressRangesVector> findRnglistFromIndex(uint32_t Index);
Optional<uint64_t> getRnglistOffset(uint32_t Index);
Optional<uint64_t> getLoclistOffset(uint32_t Index);
Expected<DWARFAddressRangesVector> collectAddressRanges();
Expected<DWARFLocationExpressionsVector>
findLoclistFromOffset(uint64_t Offset);
DWARFDie getSubroutineForAddress(uint64_t Address);
DWARFDie getVariableForAddress(uint64_t Address);
void getInlinedChainForAddress(uint64_t Address,
SmallVectorImpl<DWARFDie> &InlinedChain);
const DWARFUnitVector &getUnitVector() const { return UnitVector; }
unsigned getNumDIEs() {
extractDIEsIfNeeded(false);
return DieArray.size();
}
uint32_t getDIEIndex(const DWARFDie &D) {
return getDIEIndex(D.getDebugInfoEntry());
}
DWARFDie getDIEAtIndex(unsigned Index) {
assert(Index < DieArray.size());
return DWARFDie(this, &DieArray[Index]);
}
DWARFDie getParent(const DWARFDebugInfoEntry *Die);
DWARFDie getSibling(const DWARFDebugInfoEntry *Die);
DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die);
DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die);
DWARFDie getLastChild(const DWARFDebugInfoEntry *Die);
DWARFDie getDIEForOffset(uint64_t Offset) {
extractDIEsIfNeeded(false);
auto It =
llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
return DIE.getOffset() < Offset;
});
if (It != DieArray.end() && It->getOffset() == Offset)
return DWARFDie(this, &*It);
return DWARFDie();
}
uint32_t getLineTableOffset() const {
if (auto IndexEntry = Header.getIndexEntry())
if (const auto *Contrib = IndexEntry->getContribution(DW_SECT_LINE))
return Contrib->Offset;
return 0;
}
die_iterator_range dies() {
extractDIEsIfNeeded(false);
return die_iterator_range(DieArray.begin(), DieArray.end());
}
virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
Error tryExtractDIEsIfNeeded(bool CUDieOnly);
private:
size_t getDebugInfoSize() const {
return Header.getLength() + Header.getUnitLengthFieldByteSize() -
getHeaderSize();
}
void extractDIEsIfNeeded(bool CUDieOnly);
void extractDIEsToVector(bool AppendCUDie, bool AppendNonCUDIEs,
std::vector<DWARFDebugInfoEntry> &DIEs) const;
void clearDIEs(bool KeepCUDie);
bool parseDWO();
};
inline bool isCompileUnit(const std::unique_ptr<DWARFUnit> &U) {
return !U->isTypeUnit();
}
}
#endif