#include "DwarfFile.h"
#include "DwarfDebug.h"
#include "DwarfUnit.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
namespace llvm {
DwarfFile::DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA)
: Asm(AP), StrPool(DA, *Asm, Pref) {}
DwarfFile::~DwarfFile() {}
void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
if (InSet == &Abbrev) {
Abbreviations.push_back(&Abbrev);
Abbrev.setNumber(Abbreviations.size());
} else {
Abbrev.setNumber(InSet->getNumber());
}
}
void DwarfFile::addUnit(std::unique_ptr<DwarfUnit> U) {
CUs.push_back(std::move(U));
}
void DwarfFile::emitUnits(bool UseOffsets) {
for (const auto &TheU : CUs) {
DIE &Die = TheU->getUnitDie();
const MCSection *USection = TheU->getSection();
Asm->OutStreamer.SwitchSection(USection);
TheU->emitHeader(UseOffsets);
Asm->emitDwarfDIE(Die);
}
}
void DwarfFile::computeSizeAndOffsets() {
unsigned SecOffset = 0;
for (const auto &TheU : CUs) {
TheU->setDebugInfoOffset(SecOffset);
unsigned Offset = sizeof(int32_t) + TheU->getHeaderSize();
unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
SecOffset += EndOffset;
}
}
unsigned DwarfFile::computeSizeAndOffset(DIE &Die, unsigned Offset) {
assignAbbrevNumber(Die.getAbbrev());
const DIEAbbrev &Abbrev = Die.getAbbrev();
Die.setOffset(Offset);
Offset += getULEB128Size(Die.getAbbrevNumber());
const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i)
Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
auto &Children = Die.getChildren();
if (!Children.empty()) {
assert(Abbrev.hasChildren() && "Children flag not set");
for (auto &Child : Children)
Offset = computeSizeAndOffset(Child, Offset);
Offset += sizeof(int8_t);
}
Die.setSize(Offset - Die.getOffset());
return Offset;
}
void DwarfFile::emitAbbrevs(const MCSection *Section) {
if (!Abbreviations.empty()) {
Asm->OutStreamer.SwitchSection(Section);
Asm->emitDwarfAbbrevs(Abbreviations);
}
}
void DwarfFile::emitStrings(const MCSection *StrSection,
const MCSection *OffsetSection) {
StrPool.emit(*Asm, StrSection, OffsetSection);
}
bool DwarfFile::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
const DILocalVariable *DV = Var->getVariable();
if (unsigned ArgNum = DV->getArg()) {
auto I = Vars.begin();
while (I != Vars.end()) {
unsigned CurNum = (*I)->getVariable()->getArg();
if (CurNum == 0)
break;
if (CurNum > ArgNum)
break;
if (CurNum == ArgNum) {
(*I)->addMMIEntry(*Var);
return false;
}
++I;
}
Vars.insert(I, Var);
return true;
}
Vars.push_back(Var);
return true;
}
}