#ifndef LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
#define LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
#include "llvm/ADT/AddressRanges.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/CodeGen/DIE.h"
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
namespace llvm {
class DeclContext;
using RangesTy = AddressRangesMap<int64_t>;
struct PatchLocation {
DIE::value_iterator I;
PatchLocation() = default;
PatchLocation(DIE::value_iterator I) : I(I) {}
void set(uint64_t New) const {
assert(I);
const auto &Old = *I;
assert(Old.getType() == DIEValue::isInteger);
*I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
}
uint64_t get() const {
assert(I);
return I->getDIEInteger().getValue();
}
};
class CompileUnit {
public:
struct DIEInfo {
int64_t AddrAdjust;
DeclContext *Ctxt;
DIE *Clone;
uint32_t ParentIdx;
bool Keep : 1;
bool InDebugMap : 1;
bool Prune : 1;
bool Incomplete : 1;
bool InModuleScope : 1;
bool ODRMarkingDone : 1;
};
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
StringRef ClangModuleName)
: OrigUnit(OrigUnit), ID(ID), ClangModuleName(ClangModuleName) {
Info.resize(OrigUnit.getNumDIEs());
auto CUDie = OrigUnit.getUnitDIE(false);
if (!CUDie) {
HasODR = false;
return;
}
if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
*Lang == dwarf::DW_LANG_C_plus_plus_03 ||
*Lang == dwarf::DW_LANG_C_plus_plus_11 ||
*Lang == dwarf::DW_LANG_C_plus_plus_14 ||
*Lang == dwarf::DW_LANG_ObjC_plus_plus);
else
HasODR = false;
}
DWARFUnit &getOrigUnit() const { return OrigUnit; }
unsigned getUniqueID() const { return ID; }
void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); }
DIE *getOutputUnitDIE() const {
if (NewUnit)
return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
return nullptr;
}
bool hasODR() const { return HasODR; }
bool isClangModule() const { return !ClangModuleName.empty(); }
uint16_t getLanguage();
StringRef getSysRoot();
const std::string &getClangModuleName() const { return ClangModuleName; }
DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
DIEInfo &getInfo(const DWARFDie &Die) {
unsigned Idx = getOrigUnit().getDIEIndex(Die);
return Info[Idx];
}
uint64_t getStartOffset() const { return StartOffset; }
uint64_t getNextUnitOffset() const { return NextUnitOffset; }
void setStartOffset(uint64_t DebugInfoSize) { StartOffset = DebugInfoSize; }
uint64_t getLowPc() const { return LowPc; }
uint64_t getHighPc() const { return HighPc; }
bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
Optional<PatchLocation> getUnitRangesAttribute() const {
return UnitRangeAttribute;
}
const RangesTy &getFunctionRanges() const { return Ranges; }
const std::vector<PatchLocation> &getRangesAttributes() const {
return RangeAttributes;
}
const std::vector<std::pair<PatchLocation, int64_t>> &
getLocationAttributes() const {
return LocationAttributes;
}
void markEverythingAsKept();
uint64_t computeNextUnitOffset(uint16_t DwarfVersion);
void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
DeclContext *Ctxt, PatchLocation Attr);
void fixupForwardReferences();
void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name);
void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
bool SkipPubnamesSection = false);
void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
bool SkipPubnamesSection = false);
void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name,
bool ObjcClassImplementation,
uint32_t QualifiedNameHash);
struct AccelInfo {
DwarfStringPoolEntryRef Name;
const DIE *Die;
uint32_t QualifiedNameHash;
bool SkipPubSection;
bool ObjcClassImplementation;
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
bool SkipPubSection = false)
: Name(Name), Die(Die), SkipPubSection(SkipPubSection) {}
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die,
uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
: Name(Name), Die(Die), QualifiedNameHash(QualifiedNameHash),
SkipPubSection(false),
ObjcClassImplementation(ObjCClassIsImplementation) {}
};
const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
const std::vector<AccelInfo> &getObjC() const { return ObjC; }
MCSymbol *getLabelBegin() { return LabelBegin; }
void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
private:
DWARFUnit &OrigUnit;
unsigned ID;
std::vector<DIEInfo> Info; Optional<BasicDIEUnit> NewUnit;
MCSymbol *LabelBegin = nullptr;
uint64_t StartOffset;
uint64_t NextUnitOffset;
uint64_t LowPc = std::numeric_limits<uint64_t>::max();
uint64_t HighPc = 0;
std::vector<
std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
ForwardDIEReferences;
RangesTy Ranges;
SmallDenseMap<uint64_t, uint64_t, 1> Labels;
std::vector<PatchLocation> RangeAttributes;
Optional<PatchLocation> UnitRangeAttribute;
std::vector<std::pair<PatchLocation, int64_t>> LocationAttributes;
std::vector<AccelInfo> Pubnames;
std::vector<AccelInfo> Pubtypes;
std::vector<AccelInfo> Namespaces;
std::vector<AccelInfo> ObjC;
bool HasODR;
uint16_t Language = 0;
std::string SysRoot;
std::string ClangModuleName;
};
}
#endif