#include "DwarfFile.h"
#include "DwarfDebug.h"
#include "DwarfUnit.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/LEB128.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/ADT/STLExtras.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(DwarfDebug *DD, const MCSymbol *ASectionSym) {
for (const auto &TheU : CUs) {
DIE &Die = TheU->getUnitDie();
const MCSection *USection = TheU->getSection();
Asm->OutStreamer.SwitchSection(USection);
Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Asm->OutStreamer.AddComment("Length of Unit");
Asm->EmitInt32(TheU->getHeaderSize() + Die.getSize());
TheU->emitHeader(ASectionSym);
DD->emitDIE(Die);
Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
}
}
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());
const 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);
for (const DIEAbbrev *Abbrev : Abbreviations) {
Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Abbrev->Emit(Asm);
}
Asm->EmitULEB128(0, "EOM(3)");
}
}
void DwarfFile::emitStrings(const MCSection *StrSection,
const MCSection *OffsetSection,
const MCSymbol *StrSecSym) {
StrPool.emit(*Asm, StrSection, OffsetSection, StrSecSym);
}
}