#define DEBUG_TYPE "dwarfdebug"
#include "DwarfDebug.h"
#include "DIE.h"
#include "DIEHash.h"
#include "DwarfAccelTable.h"
#include "DwarfUnit.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/Triple.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/DIBuilder.h"
#include "llvm/DebugInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/ValueHandle.h"
#include "llvm/Target/TargetFrameLowering.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetRegisterInfo.h"
using namespace llvm;
static cl::opt<bool>
DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
cl::desc("Disable debug info printing"));
static cl::opt<bool> UnknownLocations(
"use-unknown-locations", cl::Hidden,
cl::desc("Make an absence of debug location information explicit."),
cl::init(false));
static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
cl::desc("Add the CU hash as the dwo_id."),
cl::init(false));
static cl::opt<bool>
GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
cl::desc("Generate GNU-style pubnames and pubtypes"),
cl::init(false));
namespace {
enum DefaultOnOff {
Default,
Enable,
Disable
};
}
static cl::opt<DefaultOnOff>
DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
cl::desc("Output prototype dwarf accelerator tables."),
cl::values(clEnumVal(Default, "Default for platform"),
clEnumVal(Enable, "Enabled"),
clEnumVal(Disable, "Disabled"), clEnumValEnd),
cl::init(Default));
static cl::opt<DefaultOnOff>
SplitDwarf("split-dwarf", cl::Hidden,
cl::desc("Output DWARF5 split debug info."),
cl::values(clEnumVal(Default, "Default for platform"),
clEnumVal(Enable, "Enabled"),
clEnumVal(Disable, "Disabled"), clEnumValEnd),
cl::init(Default));
static cl::opt<DefaultOnOff>
DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
cl::desc("Generate DWARF pubnames and pubtypes sections"),
cl::values(clEnumVal(Default, "Default for platform"),
clEnumVal(Enable, "Enabled"),
clEnumVal(Disable, "Disabled"), clEnumValEnd),
cl::init(Default));
static cl::opt<unsigned>
DwarfVersionNumber("dwarf-version", cl::Hidden,
cl::desc("Generate DWARF for dwarf version."), cl::init(0));
static cl::opt<bool>
DwarfCURanges("generate-dwarf-cu-ranges", cl::Hidden,
cl::desc("Generate DW_AT_ranges for compile units"),
cl::init(false));
static const char *const DWARFGroupName = "DWARF Emission";
static const char *const DbgTimerName = "DWARF Debug Writer";
namespace llvm {
void DbgVariable::updateMachineInstr(const MachineInstr *M) {
MInsn = M;
assert(Var.getName() == MInsn->getDebugVariable().getName()
&& "variable and variable in DBG_VALUE do not match");
Var = MInsn->getDebugVariable();
}
template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
return DD->resolve(Ref);
}
DIType DbgVariable::getType() const {
DIType Ty = Var.getType();
if (Var.isBlockByrefVariable()) {
DIType subType = Ty;
uint16_t tag = Ty.getTag();
if (tag == dwarf::DW_TAG_pointer_type)
subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
DIArray Elements = DICompositeType(subType).getTypeArray();
for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
DIDerivedType DT(Elements.getElement(i));
if (getName() == DT.getName())
return (resolve(DT.getTypeDerivedFrom()));
}
}
return Ty;
}
}
static unsigned getDwarfVersionFromModule(const Module *M) {
Value *Val = M->getModuleFlag("Dwarf Version");
if (!Val)
return dwarf::DWARF_VERSION;
return cast<ConstantInt>(Val)->getZExtValue();
}
DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
: Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
PrevLabel(NULL), GlobalRangeCount(0),
InfoHolder(A, "info_string", DIEValueAllocator),
SkeletonHolder(A, "skel_string", DIEValueAllocator) {
DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
DwarfAddrSectionSym = 0;
DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
FunctionBeginSym = FunctionEndSym = 0;
CurFn = 0;
CurMI = 0;
bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
if (DwarfAccelTables == Default)
HasDwarfAccelTables = IsDarwin;
else
HasDwarfAccelTables = DwarfAccelTables == Enable;
if (SplitDwarf == Default)
HasSplitDwarf = false;
else
HasSplitDwarf = SplitDwarf == Enable;
if (DwarfPubSections == Default)
HasDwarfPubSections = !IsDarwin;
else
HasDwarfPubSections = DwarfPubSections == Enable;
HasCURanges = DwarfCURanges || TargetMachine::getFunctionSections();
DwarfVersion = DwarfVersionNumber
? DwarfVersionNumber
: getDwarfVersionFromModule(MMI->getModule());
{
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
beginModule();
}
}
static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
const char *SymbolStem = 0) {
Asm->OutStreamer.SwitchSection(Section);
if (!SymbolStem)
return 0;
MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
Asm->OutStreamer.EmitLabel(TmpSym);
return TmpSym;
}
DwarfFile::~DwarfFile() {
for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
I != E; ++I)
delete *I;
}
MCSymbol *DwarfFile::getStringPoolSym() {
return Asm->GetTempSymbol(StringPref);
}
MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry =
StringPool.GetOrCreateValue(Str).getValue();
if (Entry.first)
return Entry.first;
Entry.second = NextStringPoolNumber++;
return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
}
unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
std::pair<MCSymbol *, unsigned> &Entry =
StringPool.GetOrCreateValue(Str).getValue();
if (Entry.first)
return Entry.second;
Entry.second = NextStringPoolNumber++;
Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
return Entry.second;
}
unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym) {
return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
}
unsigned DwarfFile::getAddrPoolIndex(const MCExpr *Sym) {
std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
if (P.second)
++NextAddrPoolNumber;
return P.first->second;
}
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());
}
}
static bool isObjCClass(StringRef Name) {
return Name.startswith("+") || Name.startswith("-");
}
static bool hasObjCCategory(StringRef Name) {
if (!isObjCClass(Name))
return false;
return Name.find(") ") != StringRef::npos;
}
static void getObjCClassCategory(StringRef In, StringRef &Class,
StringRef &Category) {
if (!hasObjCCategory(In)) {
Class = In.slice(In.find('[') + 1, In.find(' '));
Category = "";
return;
}
Class = In.slice(In.find('[') + 1, In.find('('));
Category = In.slice(In.find('[') + 1, In.find(' '));
return;
}
static StringRef getObjCMethodName(StringRef In) {
return In.slice(In.find(' ') + 1, In.find(']'));
}
static bool SectionSort(const MCSection *A, const MCSection *B) {
std::string LA = (A ? A->getLabelBeginName() : "");
std::string LB = (B ? B->getLabelBeginName() : "");
return LA < LB;
}
static void addSubprogramNames(DwarfUnit *TheU, DISubprogram SP, DIE *Die) {
if (!SP.isDefinition())
return;
TheU->addAccelName(SP.getName(), Die);
if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
TheU->addAccelName(SP.getLinkageName(), Die);
if (isObjCClass(SP.getName())) {
StringRef Class, Category;
getObjCClassCategory(SP.getName(), Class, Category);
TheU->addAccelObjC(Class, Die);
if (Category != "")
TheU->addAccelObjC(Category, Die);
TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
}
}
bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
if (!Context)
return false;
DIDescriptor D(Context);
if (D.isSubprogram())
return true;
if (D.isType())
return isSubprogramContext(resolve(DIType(Context).getContext()));
return false;
}
DIE *DwarfDebug::updateSubprogramScopeDIE(DwarfCompileUnit *SPCU,
DISubprogram SP) {
DIE *SPDie = SPCU->getDIE(SP);
assert(SPDie && "Unable to find subprogram DIE!");
if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
SPDie =
SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
} else {
DISubprogram SPDecl = SP.getFunctionDeclaration();
if (!SPDecl.isSubprogram()) {
DIScope SPContext = resolve(SP.getContext());
if (SP.isDefinition() && !SPContext.isCompileUnit() &&
!SPContext.isModule() &&
!SPContext.isFile() && !isSubprogramContext(SPContext)) {
SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
DICompositeType SPTy = SP.getType();
DIArray Args = SPTy.getTypeArray();
uint16_t SPTag = SPTy.getTag();
if (SPTag == dwarf::DW_TAG_subroutine_type)
SPCU->constructSubprogramArguments(*SPDie, Args);
DIE *SPDeclDie = SPDie;
SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
*SPCU->getUnitDie());
SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
}
}
}
SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc, FunctionBeginSym);
SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc, FunctionEndSym);
RangeSpan Span(FunctionBeginSym, FunctionEndSym);
SPCU->addRange(llvm_move(Span));
const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
MachineLocation Location(RI->getFrameRegister(*Asm->MF));
SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
addSubprogramNames(SPCU, SP, SPDie);
return SPDie;
}
bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
if (Scope->isAbstractScope())
return false;
const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
if (Ranges.empty())
return true;
if (Ranges.size() > 1)
return false;
SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
MCSymbol *End = getLabelAfterInsn(RI->second);
return !End;
}
static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
dwarf::Attribute A, const MCSymbol *L,
const MCSymbol *Sec) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
U->addSectionLabel(D, A, L);
else
U->addSectionDelta(D, A, L, Sec);
}
void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
const SmallVectorImpl<InsnRange> &Range) {
MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
DwarfDebugRangeSectionSym);
RangeSpanList List(RangeSym);
for (SmallVectorImpl<InsnRange>::const_iterator RI = Range.begin(),
RE = Range.end();
RI != RE; ++RI) {
RangeSpan Span(getLabelBeforeInsn(RI->first),
getLabelAfterInsn(RI->second));
List.addRange(llvm_move(Span));
}
TheCU->addRangeList(llvm_move(List));
}
DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
LexicalScope *Scope) {
if (isLexicalScopeDIENull(Scope))
return 0;
DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
if (Scope->isAbstractScope())
return ScopeDIE;
const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
if (ScopeRanges.size() > 1) {
addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
return ScopeDIE;
}
SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
MCSymbol *Start = getLabelBeforeInsn(RI->first);
MCSymbol *End = getLabelAfterInsn(RI->second);
assert(End && "End label should not be null!");
assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
assert(End->isDefined() && "Invalid end label for an inlined scope!");
TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
return ScopeDIE;
}
DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
LexicalScope *Scope) {
const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
assert(!ScopeRanges.empty() &&
"LexicalScope does not have instruction markers!");
if (!Scope->getScopeNode())
return NULL;
DIScope DS(Scope->getScopeNode());
DISubprogram InlinedSP = getDISubprogram(DS);
DIE *OriginDIE = TheCU->getDIE(InlinedSP);
if (!OriginDIE) {
DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
return NULL;
}
DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
if (ScopeRanges.size() > 1)
addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
else {
SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
if (StartLabel == 0 || EndLabel == 0)
llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
assert(StartLabel->isDefined() &&
"Invalid starting label for an inlined scope!");
assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
}
InlinedSubprogramDIEs.insert(OriginDIE);
DILocation DL(Scope->getInlinedAt());
TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
TheCU->getUniqueID()));
TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
return ScopeDIE;
}
DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
LexicalScope *Scope,
SmallVectorImpl<DIE *> &Children) {
DIE *ObjectPointer = NULL;
if (LScopes.isCurrentFunctionScope(Scope)) {
for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
if (DbgVariable *ArgDV = CurrentFnArguments[i])
if (DIE *Arg =
TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Children.push_back(Arg);
if (ArgDV->isObjectPointer())
ObjectPointer = Arg;
}
DISubprogram SP(Scope->getScopeNode());
DIArray FnArgs = SP.getType().getTypeArray();
if (FnArgs.getElement(FnArgs.getNumElements()-1).isUnspecifiedParameter()) {
DIE *Ellipsis = new DIE(dwarf::DW_TAG_unspecified_parameters);
Children.push_back(Ellipsis);
}
}
const SmallVectorImpl<DbgVariable *> &Variables =
ScopeVariables.lookup(Scope);
for (unsigned i = 0, N = Variables.size(); i < N; ++i)
if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
Scope->isAbstractScope())) {
Children.push_back(Variable);
if (Variables[i]->isObjectPointer())
ObjectPointer = Variable;
}
const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Children.push_back(Nested);
return ObjectPointer;
}
DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
LexicalScope *Scope) {
if (!Scope || !Scope->getScopeNode())
return NULL;
DIScope DS(Scope->getScopeNode());
SmallVector<DIE *, 8> Children;
DIE *ObjectPointer = NULL;
bool ChildrenCreated = false;
DIE *ScopeDIE = NULL;
if (Scope->getInlinedAt())
ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
else if (DS.isSubprogram()) {
ProcessedSPNodes.insert(DS);
if (Scope->isAbstractScope()) {
ScopeDIE = TheCU->getDIE(DS);
if (ScopeDIE)
AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
} else
ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
} else {
if (isLexicalScopeDIENull(Scope))
return NULL;
ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
ChildrenCreated = true;
std::pair<ImportedEntityMap::const_iterator,
ImportedEntityMap::const_iterator> Range =
std::equal_range(
ScopesWithImportedEntities.begin(),
ScopesWithImportedEntities.end(),
std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
less_first());
if (Children.empty() && Range.first == Range.second)
return NULL;
ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
assert(ScopeDIE && "Scope DIE should not be null.");
for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
++i)
constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
}
if (!ScopeDIE) {
assert(Children.empty() &&
"We create children only when the scope DIE is not null.");
return NULL;
}
if (!ChildrenCreated)
ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
E = Children.end();
I != E; ++I)
ScopeDIE->addChild(*I);
if (DS.isSubprogram() && ObjectPointer != NULL)
TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
return ScopeDIE;
}
unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
unsigned CUID) {
if (Asm->OutStreamer.hasRawTextSupport())
CUID = 0;
if (FileName.empty())
return getOrCreateSourceID("<stdin>", StringRef(), CUID);
if (DirName == CompilationDir)
DirName = "";
unsigned SrcId = FileIDCUMap[CUID] + 1;
SmallString<128> NamePair;
NamePair += utostr(CUID);
NamePair += '\0';
NamePair += DirName;
NamePair += '\0'; NamePair += FileName;
StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
if (Ent.getValue() != SrcId)
return Ent.getValue();
FileIDCUMap[CUID] = SrcId;
Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
return SrcId;
}
void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
if (!GenerateGnuPubSections)
return;
addSectionLabel(Asm, U, D, dwarf::DW_AT_GNU_pubnames,
Asm->GetTempSymbol("gnu_pubnames", U->getUniqueID()),
DwarfGnuPubNamesSectionSym);
addSectionLabel(Asm, U, D, dwarf::DW_AT_GNU_pubtypes,
Asm->GetTempSymbol("gnu_pubtypes", U->getUniqueID()),
DwarfGnuPubTypesSectionSym);
}
DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
StringRef FN = DIUnit.getFilename();
CompilationDir = DIUnit.getDirectory();
DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
DwarfCompileUnit *NewCU = new DwarfCompileUnit(
InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
InfoHolder.addUnit(NewCU);
FileIDCUMap[NewCU->getUniqueID()] = 0;
getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
DIUnit.getLanguage());
NewCU->addString(Die, dwarf::DW_AT_name, FN);
MCSymbol *LineTableStartSym =
Asm->GetTempSymbol("line_table_start", NewCU->getUniqueID());
Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
NewCU->getUniqueID());
bool UseTheFirstCU =
Asm->OutStreamer.hasRawTextSupport() || (NewCU->getUniqueID() == 0);
if (!useSplitDwarf()) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list,
UseTheFirstCU ? Asm->GetTempSymbol("section_line")
: LineTableStartSym);
else if (UseTheFirstCU)
NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
else
NewCU->addSectionDelta(Die, dwarf::DW_AT_stmt_list, LineTableStartSym,
DwarfLineSectionSym);
if (!CompilationDir.empty())
NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
addGnuPubAttributes(NewCU, Die);
}
if (DIUnit.isOptimized())
NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
StringRef Flags = DIUnit.getFlags();
if (!Flags.empty())
NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
if (unsigned RVer = DIUnit.getRunTimeVersion())
NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
dwarf::DW_FORM_data1, RVer);
if (!FirstCU)
FirstCU = NewCU;
if (useSplitDwarf()) {
NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
DwarfInfoDWOSectionSym);
NewCU->setSkeleton(constructSkeletonCU(NewCU));
} else
NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
DwarfInfoSectionSym);
CUMap.insert(std::make_pair(DIUnit, NewCU));
CUDieMap.insert(std::make_pair(Die, NewCU));
return NewCU;
}
void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
const MDNode *N) {
DwarfCompileUnit *&CURef = SPMap[N];
if (CURef)
return;
CURef = TheCU;
DISubprogram SP(N);
if (!SP.isDefinition())
return;
DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
}
void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
const MDNode *N) {
DIImportedEntity Module(N);
if (!Module.Verify())
return;
if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
constructImportedEntityDIE(TheCU, Module, D);
}
void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
const MDNode *N, DIE *Context) {
DIImportedEntity Module(N);
if (!Module.Verify())
return;
return constructImportedEntityDIE(TheCU, Module, Context);
}
void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
const DIImportedEntity &Module,
DIE *Context) {
assert(Module.Verify() &&
"Use one of the MDNode * overloads to handle invalid metadata");
assert(Context && "Should always have a context for an imported_module");
DIE *IMDie = new DIE(Module.getTag());
TheCU->insertDIE(Module, IMDie);
DIE *EntityDie;
DIDescriptor Entity = Module.getEntity();
if (Entity.isNameSpace())
EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
if (Entity.isModule())
EntityDie = TheCU->getOrCreateModule(DIModule(Entity));
else if (Entity.isSubprogram())
EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
else if (Entity.isType())
EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
else
EntityDie = TheCU->getDIE(Entity);
unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
Module.getContext().getDirectory(),
TheCU->getUniqueID());
TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
StringRef Name = Module.getName();
if (!Name.empty())
TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
Context->addChild(IMDie);
}
void DwarfDebug::beginModule() {
if (DisableDebugInfoPrinting)
return;
const Module *M = MMI->getModule();
NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
if (!CU_Nodes)
return;
TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
emitSectionLabels();
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit CUNode(CU_Nodes->getOperand(i));
DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
DIArray ImportedEntities = CUNode.getImportedEntities();
for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
ScopesWithImportedEntities.push_back(std::make_pair(
DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
ImportedEntities.getElement(i)));
std::sort(ScopesWithImportedEntities.begin(),
ScopesWithImportedEntities.end(), less_first());
DIArray GVs = CUNode.getGlobalVariables();
for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
DIArray SPs = CUNode.getSubprograms();
for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
constructSubprogramDIE(CU, SPs.getElement(i));
DIArray EnumTypes = CUNode.getEnumTypes();
for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
DIArray RetainedTypes = CUNode.getRetainedTypes();
for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
}
MMI->setDebugInfoAvailability(true);
SectionMap[Asm->getObjFileLowering().getTextSection()];
}
void DwarfDebug::computeInlinedDIEs() {
for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
AE = InlinedSubprogramDIEs.end();
AI != AE; ++AI) {
DIE *ISP = *AI;
FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
}
for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
AE = AbstractSPDies.end();
AI != AE; ++AI) {
DIE *ISP = AI->second;
if (InlinedSubprogramDIEs.count(ISP))
continue;
FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
}
}
void DwarfDebug::collectDeadVariables() {
const Module *M = MMI->getModule();
if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
DICompileUnit TheCU(CU_Nodes->getOperand(i));
DIArray Subprograms = TheCU.getSubprograms();
for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
DISubprogram SP(Subprograms.getElement(i));
if (ProcessedSPNodes.count(SP) != 0)
continue;
if (!SP.isSubprogram())
continue;
if (!SP.isDefinition())
continue;
DIArray Variables = SP.getVariables();
if (Variables.getNumElements() == 0)
continue;
DwarfCompileUnit *SPCU =
static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
assert(SPCU && "Unable to find Compile Unit!");
constructSubprogramDIE(SPCU, SP);
DIE *SPDIE = SPCU->getDIE(SP);
for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
DIVariable DV(Variables.getElement(vi));
if (!DV.isVariable())
continue;
DbgVariable NewVar(DV, NULL, this);
if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
SPDIE->addChild(VariableDIE);
}
}
}
}
}
void DwarfDebug::finalizeModuleInfo() {
collectDeadVariables();
computeInlinedDIEs();
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
E = getUnits().end();
I != E; ++I) {
DwarfUnit *TheU = *I;
TheU->constructContainingTypeDIEs();
if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
DwarfCompileUnit *SkCU =
static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
if (useSplitDwarf()) {
uint64_t ID = 0;
if (GenerateCUHash) {
DIEHash CUHash;
ID = CUHash.computeCUSignature(*TheU->getUnitDie());
}
TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
dwarf::DW_FORM_data8, ID);
SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
dwarf::DW_FORM_data8, ID);
}
DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
if (useCURanges() && TheU->getRanges().size())
addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
DwarfDebugRangeSectionSym);
else
U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
0);
}
}
InfoHolder.computeSizeAndOffsets();
if (useSplitDwarf())
SkeletonHolder.computeSizeAndOffsets();
}
void DwarfDebug::endSections() {
for (size_t n = 0; n < ArangeLabels.size(); n++) {
const SymbolCU &SCU = ArangeLabels[n];
if (SCU.Sym->isInSection()) {
const MCSection *Section = &SCU.Sym->getSection();
if (!Section->getKind().isMetadata())
SectionMap[Section].push_back(SCU);
} else {
SectionMap[NULL].push_back(SCU);
}
}
std::vector<const MCSection *> Sections;
for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
it++) {
const MCSection *Section = it->first;
Sections.push_back(Section);
}
std::sort(Sections.begin(), Sections.end(), SectionSort);
for (unsigned ID = 0; ID < Sections.size(); ID++) {
const MCSection *Section = Sections[ID];
MCSymbol *Sym = NULL;
if (Section) {
Sym = Asm->GetTempSymbol("debug_end", ID);
Asm->OutStreamer.SwitchSection(Section);
Asm->OutStreamer.EmitLabel(Sym);
}
SectionMap[Section].push_back(SymbolCU(NULL, Sym));
}
}
void DwarfDebug::endModule() {
assert(CurFn == 0);
assert(CurMI == 0);
if (!FirstCU)
return;
endSections();
finalizeModuleInfo();
emitDebugStr();
emitDebugInfo();
emitAbbreviations();
emitDebugLoc();
emitDebugARanges();
emitDebugRanges();
if (useSplitDwarf()) {
emitDebugStrDWO();
emitDebugInfoDWO();
emitDebugAbbrevDWO();
InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
}
if (useDwarfAccelTables()) {
emitAccelNames();
emitAccelObjC();
emitAccelNamespaces();
emitAccelTypes();
}
if (HasDwarfPubSections) {
emitDebugPubNames(GenerateGnuPubSections);
emitDebugPubTypes(GenerateGnuPubSections);
}
SPMap.clear();
FirstCU = NULL;
}
DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
DebugLoc ScopeLoc) {
LLVMContext &Ctx = DV->getContext();
DIVariable Var = cleanseInlinedVariable(DV, Ctx);
DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
if (AbsDbgVariable)
return AbsDbgVariable;
LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
if (!Scope)
return NULL;
AbsDbgVariable = new DbgVariable(Var, NULL, this);
addScopeVariable(Scope, AbsDbgVariable);
AbstractVariables[Var] = AbsDbgVariable;
return AbsDbgVariable;
}
bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
if (!LScopes.isCurrentFunctionScope(Scope))
return false;
DIVariable DV = Var->getVariable();
if (DV.getTag() != dwarf::DW_TAG_arg_variable)
return false;
unsigned ArgNo = DV.getArgNumber();
if (ArgNo == 0)
return false;
size_t Size = CurrentFnArguments.size();
if (Size == 0)
CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
if (ArgNo > Size)
CurrentFnArguments.resize(ArgNo * 2);
CurrentFnArguments[ArgNo - 1] = Var;
return true;
}
void DwarfDebug::collectVariableInfoFromMMITable(
SmallPtrSet<const MDNode *, 16> &Processed) {
MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
VE = VMap.end();
VI != VE; ++VI) {
const MDNode *Var = VI->first;
if (!Var)
continue;
Processed.insert(Var);
DIVariable DV(Var);
const std::pair<unsigned, DebugLoc> &VP = VI->second;
LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
if (Scope == 0)
continue;
DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
RegVar->setFrameIndex(VP.first);
if (!addCurrentFnArgument(RegVar, Scope))
addScopeVariable(Scope, RegVar);
if (AbsDbgVariable)
AbsDbgVariable->setFrameIndex(VP.first);
}
}
static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
MI->getOperand(0).getReg() &&
(MI->getOperand(1).isImm() ||
(MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
}
static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
const MDNode *Var = MI->getDebugVariable();
assert(MI->getNumOperands() == 3);
if (MI->getOperand(0).isReg()) {
MachineLocation MLoc;
if (!MI->getOperand(1).isImm())
MLoc.set(MI->getOperand(0).getReg());
else
MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
return DebugLocEntry::Value(Var, MLoc);
}
if (MI->getOperand(0).isImm())
return DebugLocEntry::Value(Var, MI->getOperand(0).getImm());
if (MI->getOperand(0).isFPImm())
return DebugLocEntry::Value(Var, MI->getOperand(0).getFPImm());
if (MI->getOperand(0).isCImm())
return DebugLocEntry::Value(Var, MI->getOperand(0).getCImm());
llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
}
static bool piecesOverlap(DIVariable P1, DIVariable P2) {
if (!P1.isVariablePiece() || !P2.isVariablePiece())
return true;
return (P1.getPieceOffset() < (P2.getPieceOffset() + P2.getPieceSize())) &&
(P2.getPieceOffset() < (P1.getPieceOffset() + P1.getPieceSize()));
}
typedef SmallVectorImpl<const MachineInstr *>::const_iterator HistoryIter;
void DwarfDebug::
buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
const SmallVectorImpl<const MachineInstr *> &History,
DwarfCompileUnit *TheCU) {
typedef std::pair<DIVariable, DebugLocEntry::Value> Range;
SmallVector<Range, 4> OpenRanges;
for (HistoryIter HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
const MachineInstr *Begin = *HI;
if (!Begin->isDebugValue() ||
(Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
!Begin->getOperand(0).getReg())) {
OpenRanges.clear();
continue;
}
const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
const MCSymbol *SLabel = nullptr;
DIVariable DIVar = Begin->getDebugVariable();
auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(), [&](Range R){
return piecesOverlap(DIVar, R.first);
});
OpenRanges.erase(Last, OpenRanges.end());
HistoryIter Next = HI+1;
if (Next == HE)
SLabel = FunctionEndSym;
else {
const MachineInstr *End = *Next;
DEBUG(dbgs() << "DotDebugLoc Pair:\n"
<< "\t" << *Begin << "\t" << *End << "\n");
if (End->isDebugValue())
SLabel = getLabelBeforeInsn(End);
else {
SLabel = getLabelAfterInsn(End);
assert(SLabel && "Forgot label after clobber instruction");
}
}
auto Value = getDebugLocValue(Begin);
assert(FLabel != 0);
DebugLocEntry Loc(FLabel, SLabel, Value, TheCU);
if (DebugLoc.empty() || !DebugLoc.back().Merge(Loc)) {
for (auto Range : OpenRanges)
Loc.addValue(Range.second);
DebugLoc.push_back(std::move(Loc));
}
if (DIVar.isVariablePiece())
OpenRanges.push_back({DIVar, Value});
DEBUG(dbgs() << "Values:\n";
for (auto Value : DebugLoc.back().getValues())
Value.getVariable()->dump();
dbgs() << "-----\n");
}
}
void
DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
collectVariableInfoFromMMITable(Processed);
for (SmallVectorImpl<const MDNode *>::const_iterator
UVI = UserVariables.begin(),
UVE = UserVariables.end();
UVI != UVE; ++UVI) {
const MDNode *Var = *UVI;
if (Processed.count(Var))
continue;
SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
if (History.empty())
continue;
const MachineInstr *MInsn = History.front();
DIVariable DV(Var);
LexicalScope *Scope = NULL;
if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Scope = LScopes.getCurrentFunctionScope();
else if (MDNode *IA = DV.getInlinedAt())
Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
else
Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
if (!Scope)
continue;
Processed.insert(getEntireVariable(DV));
assert(MInsn->isDebugValue() && "History must begin with debug value");
DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
if (!addCurrentFnArgument(RegVar, Scope))
addScopeVariable(Scope, RegVar);
if (AbsVar)
AbsVar->updateMachineInstr(MInsn);
if (History.size() <= 1 ||
(History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
RegVar->updateMachineInstr(MInsn);
continue;
}
RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
buildLocationList(DotDebugLocEntries, History, TheCU);
DotDebugLocEntries.push_back(DebugLocEntry());
}
DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
DIVariable DV(Variables.getElement(i));
if (!DV || !DV.isVariable() || !Processed.insert(DV))
continue;
if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
}
}
MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
assert(Label && "Didn't insert label before instruction");
return Label;
}
MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
return LabelsAfterInsn.lookup(MI);
}
void DwarfDebug::beginInstruction(const MachineInstr *MI) {
assert(CurMI == 0);
CurMI = MI;
if (!MI->isDebugValue()) {
DebugLoc DL = MI->getDebugLoc();
if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
unsigned Flags = 0;
PrevInstLoc = DL;
if (DL == PrologEndLoc) {
Flags |= DWARF2_FLAG_PROLOGUE_END;
PrologEndLoc = DebugLoc();
}
if (PrologEndLoc.isUnknown())
Flags |= DWARF2_FLAG_IS_STMT;
if (!DL.isUnknown()) {
const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
} else
recordSourceLine(0, 0, 0, 0);
}
}
DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
LabelsBeforeInsn.find(MI);
if (I == LabelsBeforeInsn.end())
return;
if (I->second)
return;
if (!PrevLabel) {
PrevLabel = MMI->getContext().CreateTempSymbol();
Asm->OutStreamer.EmitLabel(PrevLabel);
}
I->second = PrevLabel;
}
void DwarfDebug::endInstruction() {
assert(CurMI != 0);
if (!CurMI->isDebugValue())
PrevLabel = 0;
DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
LabelsAfterInsn.find(CurMI);
CurMI = 0;
if (I == LabelsAfterInsn.end())
return;
if (I->second)
return;
if (!PrevLabel) {
PrevLabel = MMI->getContext().CreateTempSymbol();
Asm->OutStreamer.EmitLabel(PrevLabel);
}
I->second = PrevLabel;
}
void DwarfDebug::identifyScopeMarkers() {
SmallVector<LexicalScope *, 4> WorkList;
WorkList.push_back(LScopes.getCurrentFunctionScope());
while (!WorkList.empty()) {
LexicalScope *S = WorkList.pop_back_val();
const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
if (!Children.empty())
for (SmallVectorImpl<LexicalScope *>::const_iterator
SI = Children.begin(),
SE = Children.end();
SI != SE; ++SI)
WorkList.push_back(*SI);
if (S->isAbstractScope())
continue;
const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
if (Ranges.empty())
continue;
for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end();
RI != RE; ++RI) {
assert(RI->first && "InsnRange does not have first instruction!");
assert(RI->second && "InsnRange does not have second instruction!");
requestLabelBeforeInsn(RI->first);
requestLabelAfterInsn(RI->second);
}
}
}
static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
return DL.getScope(Ctx);
}
static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
const MDNode *Scope = getScopeNode(DL, Ctx);
DISubprogram SP = getDISubprogram(Scope);
if (SP.isSubprogram()) {
if (SP->getNumOperands() > 19)
return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
else
return DebugLoc::get(SP.getLineNumber(), 0, SP);
}
return DebugLoc();
}
static const MachineInstr *getFirstEpilogueInst(const MachineBasicBlock &MBB) {
auto LastMI = MBB.getLastNonDebugInstr();
if (LastMI == MBB.end() || !LastMI->isReturn())
return nullptr;
DebugLoc LastLoc = LastMI->getDebugLoc();
auto Res = LastMI;
for (MachineBasicBlock::const_reverse_iterator I(std::next(LastMI)); I != MBB.rend();
++I) {
if (I->getDebugLoc() != LastLoc)
return Res;
Res = std::prev(I.base());
}
return MBB.begin();
}
static void collectClobberedRegisters(const MachineInstr &MI,
const TargetRegisterInfo *TRI,
std::set<unsigned> &Regs) {
for (auto MO = MI.operands_begin(); MO != MI.operands_end(); ++MO) {
if (!MO->isReg() || !MO->isDef() || !MO->getReg())
continue;
for (MCRegAliasIterator AI(MO->getReg(), TRI, true); AI.isValid(); ++AI)
Regs.insert(*AI);
}
}
static void collectChangingRegs(const MachineFunction *MF,
const TargetRegisterInfo *TRI,
std::set<unsigned> &Regs) {
for (const auto &MBB : *MF) {
auto FirstEpilogueInst = getFirstEpilogueInst(MBB);
bool IsInEpilogue = false;
for (const auto &MI : MBB) {
IsInEpilogue |= &MI == FirstEpilogueInst;
if (!MI.getFlag(MachineInstr::FrameSetup) && !IsInEpilogue)
collectClobberedRegisters(MI, TRI, Regs);
}
}
}
static bool isChangingReg(std::set<unsigned> &ChangingRegs, const MachineInstr *MI) {
for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
MOE = MI->operands_end();
MOI != MOE; ++MOI) {
if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
continue;
if (ChangingRegs.count(MOI->getReg()))
return true;
}
return false;
}
void DwarfDebug::beginFunction(const MachineFunction *MF) {
CurFn = MF;
if (!MMI->hasDebugInfo())
return;
LScopes.initialize(*MF);
if (LScopes.empty())
return;
assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
identifyScopeMarkers();
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
assert(TheCU && "Unable to find compile unit!");
if (Asm->OutStreamer.hasRawTextSupport())
Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
else
Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Asm->OutStreamer.EmitLabel(FunctionBeginSym);
const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
std::set<unsigned> ChangingRegs;
collectChangingRegs(MF, TRI, ChangingRegs);
for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
++I) {
bool AtBlockEntry = true;
for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
II != IE; ++II) {
const MachineInstr *MI = II;
if (MI->isDebugValue()) {
assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
DIVariable Var = MI->getDebugVariable();
if (isDbgValueInDefinedReg(MI))
LiveUserVar[MI->getOperand(0).getReg()] = Var;
DIVariable EntireVar = getEntireVariable(Var);
SmallVectorImpl<const MachineInstr *> &History = DbgValues[EntireVar];
if (History.empty())
UserVariables.push_back(EntireVar);
else{
const MachineInstr *Prev = History.back();
if (Prev->isDebugValue()) {
if (History.size() >= 2 &&
Prev->isIdenticalTo(History[History.size() - 2])) {
DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
<< "\t" << *Prev << "\t"
<< *History[History.size() - 2] << "\n");
History.pop_back();
}
MachineFunction::const_iterator PrevMBB = Prev->getParent();
if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
isDbgValueInDefinedReg(Prev)) {
MachineBasicBlock::const_iterator LastMI =
PrevMBB->getLastNonDebugInstr();
if (LastMI == PrevMBB->end()) {
DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
<< "\t" << *Prev << "\n");
History.pop_back();
} else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
History.push_back(LastMI);
}
}
}
History.push_back(MI);
} else {
if (!MI->isLabel())
AtBlockEntry = false;
if (!MI->getFlag(MachineInstr::FrameSetup) &&
(PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
PrologEndLoc = MI->getDebugLoc();
for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
MOE = MI->operands_end();
MOI != MOE; ++MOI) {
if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
continue;
for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
++AI) {
unsigned Reg = *AI;
const MDNode *Var = LiveUserVar[Reg];
if (!Var)
continue;
LiveUserVar[Reg] = 0;
DIVariable EntireVar = getEntireVariable(DIVariable(Var));
DbgValueHistoryMap::iterator HistI = DbgValues.find(EntireVar);
if (HistI == DbgValues.end())
continue;
SmallVectorImpl<const MachineInstr *> &History = HistI->second;
if (History.empty())
continue;
const MachineInstr *Prev = History.back();
if (!Prev->isDebugValue() ||
(Prev->getParent() != MI->getParent() && isChangingReg(ChangingRegs, MI)))
continue;
if (!isDbgValueInDefinedReg(Prev) ||
Prev->getOperand(0).getReg() != Reg)
continue;
History.push_back(MI);
}
}
}
}
}
for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
I != E; ++I) {
SmallVectorImpl<const MachineInstr *> &History = I->second;
if (History.empty())
continue;
const MachineInstr *Prev = History.back();
if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
const MachineBasicBlock *PrevMBB = Prev->getParent();
MachineBasicBlock::const_iterator LastMI =
PrevMBB->getLastNonDebugInstr();
if (LastMI == PrevMBB->end())
History.pop_back();
else if (PrevMBB != &PrevMBB->getParent()->back()) {
History.push_back(LastMI);
}
}
if (History.empty())
continue;
DIVariable DV(History.front()->getDebugVariable());
if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
getDISubprogram(DV.getContext()).describes(MF->getFunction())) {
if (!DV.isVariablePiece())
LabelsBeforeInsn[History.front()] = FunctionBeginSym;
else {
for (auto HI = History.begin(); HI != History.end(); ++HI) {
if (!(*HI)->isDebugValue())
break;
DIVariable Piece = (*HI)->getDebugVariable();
if (std::all_of(History.begin(), HI, [&](const MachineInstr *Pred){
return !piecesOverlap(Piece, Pred->getDebugVariable());
}))
LabelsBeforeInsn[*HI] = FunctionBeginSym;
else
break;
}
}
}
for (const MachineInstr *MI : History) {
if (MI->isDebugValue())
requestLabelBeforeInsn(MI);
else
requestLabelAfterInsn(MI);
}
}
PrevInstLoc = DebugLoc();
PrevLabel = FunctionBeginSym;
if (!PrologEndLoc.isUnknown()) {
DebugLoc FnStartDL =
getFnDebugLoc(PrologEndLoc, MF->getFunction()->getContext());
recordSourceLine(
FnStartDL.getLine(), FnStartDL.getCol(),
FnStartDL.getScope(MF->getFunction()->getContext()),
DWARF2_FLAG_IS_STMT);
}
}
void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
DIVariable DV = Var->getVariable();
if (unsigned ArgNum = DV.getArgNumber()) {
SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
while (I != Vars.end()) {
unsigned CurNum = (*I)->getVariable().getArgNumber();
if (CurNum == 0)
break;
if (CurNum > ArgNum)
break;
++I;
}
Vars.insert(I, Var);
return;
}
Vars.push_back(Var);
}
void DwarfDebug::endFunction(const MachineFunction *MF) {
if (CurFn == 0)
CurFn = MF;
else
assert(CurFn == MF);
assert(CurFn != 0);
if (!MMI->hasDebugInfo() || LScopes.empty()) {
CurFn = 0;
return;
}
FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Asm->OutStreamer.EmitLabel(FunctionEndSym);
Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
SmallPtrSet<const MDNode *, 16> ProcessedVars;
collectVariableInfo(ProcessedVars);
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
assert(TheCU && "Unable to find compile unit!");
ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
for (unsigned i = 0, e = AList.size(); i != e; ++i) {
LexicalScope *AScope = AList[i];
DISubprogram SP(AScope->getScopeNode());
if (SP.isSubprogram()) {
DIArray Variables = SP.getVariables();
for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
DIVariable DV(Variables.getElement(i));
if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
continue;
LLVMContext &Ctx = DV->getContext();
DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
if (AbstractVariables.lookup(CleanDV))
continue;
if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
}
}
if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
constructScopeDIE(TheCU, AScope);
}
DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
E = ScopeVariables.end();
I != E; ++I)
DeleteContainerPointers(I->second);
ScopeVariables.clear();
DeleteContainerPointers(CurrentFnArguments);
UserVariables.clear();
DbgValues.clear();
AbstractVariables.clear();
LabelsBeforeInsn.clear();
LabelsAfterInsn.clear();
PrevLabel = NULL;
CurFn = 0;
}
void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
unsigned Flags) {
StringRef Fn;
StringRef Dir;
unsigned Src = 1;
if (S) {
DIDescriptor Scope(S);
if (Scope.isCompileUnit()) {
DICompileUnit CU(S);
Fn = CU.getFilename();
Dir = CU.getDirectory();
} else if (Scope.isFile()) {
DIFile F(S);
Fn = F.getFilename();
Dir = F.getDirectory();
} else if (Scope.isSubprogram()) {
DISubprogram SP(S);
Fn = SP.getFilename();
Dir = SP.getDirectory();
} else if (Scope.isLexicalBlockFile()) {
DILexicalBlockFile DBF(S);
Fn = DBF.getFilename();
Dir = DBF.getDirectory();
} else if (Scope.isLexicalBlock()) {
DILexicalBlock DB(S);
Fn = DB.getFilename();
Dir = DB.getDirectory();
} else
llvm_unreachable("Unexpected scope info");
Src = getOrCreateSourceID(
Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
}
Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
}
unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
const std::vector<DIE *> &Children = Die->getChildren();
assignAbbrevNumber(Die->getAbbrev());
const DIEAbbrev &Abbrev = Die->getAbbrev();
Die->setOffset(Offset);
Offset += MCAsmInfo::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());
if (!Children.empty()) {
assert(Abbrev.getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
"Children flag not set");
for (unsigned j = 0, M = Children.size(); j < M; ++j)
Offset = computeSizeAndOffset(Children[j], Offset);
Offset += sizeof(int8_t);
}
Die->setSize(Offset - Die->getOffset());
return Offset;
}
void DwarfFile::computeSizeAndOffsets() {
unsigned SecOffset = 0;
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = CUs.begin(),
E = CUs.end();
I != E; ++I) {
(*I)->setDebugInfoOffset(SecOffset);
unsigned Offset = sizeof(int32_t) + (*I)->getHeaderSize();
unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
SecOffset += EndOffset;
}
}
void DwarfDebug::emitSectionLabels() {
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
DwarfInfoSectionSym =
emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
if (useSplitDwarf())
DwarfInfoDWOSectionSym =
emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
DwarfAbbrevSectionSym =
emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
if (useSplitDwarf())
DwarfAbbrevDWOSectionSym = emitSectionSym(
Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
emitSectionSym(Asm, TLOF.getDwarfARangesSection());
if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
emitSectionSym(Asm, MacroInfo);
DwarfLineSectionSym =
emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
emitSectionSym(Asm, TLOF.getDwarfLocSection());
if (GenerateGnuPubSections) {
DwarfGnuPubNamesSectionSym =
emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
DwarfGnuPubTypesSectionSym =
emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
} else if (HasDwarfPubSections) {
emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
}
DwarfStrSectionSym =
emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
if (useSplitDwarf()) {
DwarfStrDWOSectionSym =
emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
DwarfAddrSectionSym =
emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
}
DwarfDebugRangeSectionSym =
emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
DwarfDebugLocSectionSym =
emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
}
void DwarfDebug::emitDIE(DIE *Die) {
const DIEAbbrev &Abbrev = Die->getAbbrev();
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
"] 0x" + Twine::utohexstr(Die->getOffset()) +
":0x" + Twine::utohexstr(Die->getSize()) + " " +
dwarf::TagString(Abbrev.getTag()));
Asm->EmitULEB128(Abbrev.getNumber());
const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
for (unsigned i = 0, N = Values.size(); i < N; ++i) {
dwarf::Attribute Attr = AbbrevData[i].getAttribute();
dwarf::Form Form = AbbrevData[i].getForm();
assert(Form && "Too many attributes for DIE (check abbreviation)");
if (Asm->isVerbose())
Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
switch (Attr) {
case dwarf::DW_AT_abstract_origin:
case dwarf::DW_AT_type:
case dwarf::DW_AT_friend:
case dwarf::DW_AT_specification:
case dwarf::DW_AT_import:
case dwarf::DW_AT_containing_type: {
DIEEntry *E = cast<DIEEntry>(Values[i]);
DIE *Origin = E->getEntry();
unsigned Addr = Origin->getOffset();
if (Form == dwarf::DW_FORM_ref_addr) {
assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
DwarfCompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
assert(CU && "CUDie should belong to a CU.");
Addr += CU->getDebugInfoOffset();
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Asm->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
DIEEntry::getRefAddrSize(Asm));
else
Asm->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
CU->getSectionSym(),
DIEEntry::getRefAddrSize(Asm));
} else {
assert(Die->getUnit() == Origin->getUnit() &&
"The referenced DIE should belong to the same CU in ref4");
Asm->EmitInt32(Addr);
}
break;
}
case dwarf::DW_AT_location: {
if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
else
Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
} else {
Values[i]->EmitValue(Asm, Form);
}
break;
}
case dwarf::DW_AT_accessibility: {
if (Asm->isVerbose()) {
DIEInteger *V = cast<DIEInteger>(Values[i]);
Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
}
Values[i]->EmitValue(Asm, Form);
break;
}
default:
Values[i]->EmitValue(Asm, Form);
break;
}
}
if (Abbrev.getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
const std::vector<DIE *> &Children = Die->getChildren();
for (unsigned j = 0, M = Children.size(); j < M; ++j)
emitDIE(Children[j]);
Asm->OutStreamer.AddComment("End Of Children Mark");
Asm->EmitInt8(0);
}
}
void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
const MCSymbol *ASectionSym) {
for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
I != E; ++I) {
DwarfUnit *TheU = *I;
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(ASection, ASectionSym);
DD->emitDIE(Die);
Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
}
}
void DwarfDebug::emitDebugInfo() {
DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
DwarfAbbrevSectionSym);
}
void DwarfDebug::emitAbbreviations() {
DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
}
void DwarfFile::emitAbbrevs(const MCSection *Section) {
if (!Abbreviations.empty()) {
Asm->OutStreamer.SwitchSection(Section);
for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
const DIEAbbrev *Abbrev = Abbreviations[i];
Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Abbrev->Emit(Asm);
}
Asm->EmitULEB128(0, "EOM(3)");
}
}
void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Asm->OutStreamer.AddComment("Extended Op");
Asm->EmitInt8(0);
Asm->OutStreamer.AddComment("Op size");
Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Asm->OutStreamer.AddComment("DW_LNE_set_address");
Asm->EmitInt8(dwarf::DW_LNE_set_address);
Asm->OutStreamer.AddComment("Section end label");
Asm->OutStreamer.EmitSymbolValue(
Asm->GetTempSymbol("section_end", SectionEnd),
Asm->getDataLayout().getPointerSize());
Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
Asm->EmitInt8(0);
Asm->EmitInt8(1);
Asm->EmitInt8(1);
}
void DwarfDebug::emitAccelNames() {
DwarfAccelTable AT(
DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
E = getUnits().end();
I != E; ++I) {
DwarfUnit *TheU = *I;
const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
for (StringMap<std::vector<const DIE *> >::const_iterator
GI = Names.begin(),
GE = Names.end();
GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<const DIE *> &Entities = GI->second;
for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
DE = Entities.end();
DI != DE; ++DI)
AT.AddName(Name, *DI);
}
}
AT.FinalizeTable(Asm, "Names");
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfAccelNamesSection());
MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
Asm->OutStreamer.EmitLabel(SectionBegin);
AT.Emit(Asm, SectionBegin, &InfoHolder);
}
void DwarfDebug::emitAccelObjC() {
DwarfAccelTable AT(
DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
E = getUnits().end();
I != E; ++I) {
DwarfUnit *TheU = *I;
const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
for (StringMap<std::vector<const DIE *> >::const_iterator
GI = Names.begin(),
GE = Names.end();
GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<const DIE *> &Entities = GI->second;
for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
DE = Entities.end();
DI != DE; ++DI)
AT.AddName(Name, *DI);
}
}
AT.FinalizeTable(Asm, "ObjC");
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfAccelObjCSection());
MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
Asm->OutStreamer.EmitLabel(SectionBegin);
AT.Emit(Asm, SectionBegin, &InfoHolder);
}
void DwarfDebug::emitAccelNamespaces() {
DwarfAccelTable AT(
DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
E = getUnits().end();
I != E; ++I) {
DwarfUnit *TheU = *I;
const StringMap<std::vector<const DIE *> > &Names =
TheU->getAccelNamespace();
for (StringMap<std::vector<const DIE *> >::const_iterator
GI = Names.begin(),
GE = Names.end();
GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<const DIE *> &Entities = GI->second;
for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
DE = Entities.end();
DI != DE; ++DI)
AT.AddName(Name, *DI);
}
}
AT.FinalizeTable(Asm, "namespac");
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
Asm->OutStreamer.EmitLabel(SectionBegin);
AT.Emit(Asm, SectionBegin, &InfoHolder);
}
void DwarfDebug::emitAccelTypes() {
std::vector<DwarfAccelTable::Atom> Atoms;
Atoms.push_back(
DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Atoms.push_back(
DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
Atoms.push_back(
DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
DwarfAccelTable AT(Atoms);
for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
E = getUnits().end();
I != E; ++I) {
DwarfUnit *TheU = *I;
const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
TheU->getAccelTypes();
for (StringMap<
std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
GI = Names.begin(),
GE = Names.end();
GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<std::pair<const DIE *, unsigned> > &Entities =
GI->second;
for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
DI = Entities.begin(),
DE = Entities.end();
DI != DE; ++DI)
AT.AddName(Name, DI->first, DI->second);
}
}
AT.FinalizeTable(Asm, "types");
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfAccelTypesSection());
MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
Asm->OutStreamer.EmitLabel(SectionBegin);
AT.Emit(Asm, SectionBegin, &InfoHolder);
}
static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
const DIE *Die) {
dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
if (SpecVal) {
DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
if (SpecDIE->findAttribute(dwarf::DW_AT_external))
Linkage = dwarf::GIEL_EXTERNAL;
} else if (Die->findAttribute(dwarf::DW_AT_external))
Linkage = dwarf::GIEL_EXTERNAL;
switch (Die->getTag()) {
case dwarf::DW_TAG_class_type:
case dwarf::DW_TAG_structure_type:
case dwarf::DW_TAG_union_type:
case dwarf::DW_TAG_enumeration_type:
return dwarf::PubIndexEntryDescriptor(
dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
? dwarf::GIEL_STATIC
: dwarf::GIEL_EXTERNAL);
case dwarf::DW_TAG_typedef:
case dwarf::DW_TAG_base_type:
case dwarf::DW_TAG_subrange_type:
return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
case dwarf::DW_TAG_namespace:
return dwarf::GIEK_TYPE;
case dwarf::DW_TAG_subprogram:
return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
case dwarf::DW_TAG_constant:
case dwarf::DW_TAG_variable:
return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
case dwarf::DW_TAG_enumerator:
return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
dwarf::GIEL_STATIC);
default:
return dwarf::GIEK_NONE;
}
}
void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
const MCSection *PSec =
GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
: Asm->getObjFileLowering().getDwarfPubNamesSection();
DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
const SmallVectorImpl<DwarfUnit *> &Units = Holder.getUnits();
for (unsigned i = 0; i != Units.size(); ++i) {
DwarfUnit *TheU = Units[i];
unsigned ID = TheU->getUniqueID();
Asm->OutStreamer.SwitchSection(PSec);
if (GnuStyle)
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames", ID));
Asm->OutStreamer.AddComment("Length of Public Names Info");
MCSymbol *BeginLabel = Asm->GetTempSymbol("pubnames_begin", ID);
MCSymbol *EndLabel = Asm->GetTempSymbol("pubnames_end", ID);
Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Asm->OutStreamer.EmitLabel(BeginLabel);
Asm->OutStreamer.AddComment("DWARF Version");
Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Asm->OutStreamer.AddComment("Compilation Unit Length");
Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalNames();
for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
GE = Globals.end();
GI != GE; ++GI) {
const char *Name = GI->getKeyData();
const DIE *Entity = GI->second;
Asm->OutStreamer.AddComment("DIE offset");
Asm->EmitInt32(Entity->getOffset());
if (GnuStyle) {
dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
Asm->OutStreamer.AddComment(
Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
Asm->EmitInt8(Desc.toBits());
}
Asm->OutStreamer.AddComment("External Name");
Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
}
Asm->OutStreamer.AddComment("End Mark");
Asm->EmitInt32(0);
Asm->OutStreamer.EmitLabel(EndLabel);
}
}
void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
const MCSection *PSec =
GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
: Asm->getObjFileLowering().getDwarfPubTypesSection();
DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
const SmallVectorImpl<DwarfUnit *> &Units = Holder.getUnits();
for (unsigned i = 0; i != Units.size(); ++i) {
DwarfUnit *TheU = Units[i];
unsigned ID = TheU->getUniqueID();
Asm->OutStreamer.SwitchSection(PSec);
if (GnuStyle)
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes", ID));
Asm->OutStreamer.AddComment("Length of Public Types Info");
MCSymbol *BeginLabel = Asm->GetTempSymbol("pubtypes_begin", ID);
MCSymbol *EndLabel = Asm->GetTempSymbol("pubtypes_end", ID);
Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Asm->OutStreamer.EmitLabel(BeginLabel);
Asm->OutStreamer.AddComment("DWARF Version");
Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Asm->OutStreamer.AddComment("Compilation Unit Length");
Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalTypes();
for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
GE = Globals.end();
GI != GE; ++GI) {
const char *Name = GI->getKeyData();
const DIE *Entity = GI->second;
Asm->OutStreamer.AddComment("DIE offset");
Asm->EmitInt32(Entity->getOffset());
if (GnuStyle) {
dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
Asm->OutStreamer.AddComment(
Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
Asm->EmitInt8(Desc.toBits());
}
Asm->OutStreamer.AddComment("External Name");
Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
}
Asm->OutStreamer.AddComment("End Mark");
Asm->EmitInt32(0);
Asm->OutStreamer.EmitLabel(EndLabel);
}
}
void DwarfDebug::emitLocPieces(const DITypeIdentifierMap &Map,
ArrayRef<DebugLocEntry::Value> Values) {
typedef DebugLocEntry::Value Piece;
SmallVector<Piece, 4> Pieces(Values.begin(), Values.end());
assert(std::all_of(Pieces.begin(), Pieces.end(), [](Piece &P) {
return DIVariable(P.getVariable()).isVariablePiece();
}) && "all values are expected to be pieces");
std::sort(Pieces.begin(), Pieces.end(), [](const Piece &A, const Piece &B) {
DIVariable VarA(A.getVariable());
DIVariable VarB(B.getVariable());
return VarA.getPieceOffset() < VarB.getPieceOffset();
});
Pieces.erase(std::unique(Pieces.begin(), Pieces.end(),
[] (const Piece &A,const Piece &B){
return A.getVariable() == B.getVariable();
}), Pieces.end());
unsigned Offset = 0;
for (auto Piece : Pieces) {
DIVariable Var(Piece.getVariable());
unsigned PieceOffset = Var.getPieceOffset();
unsigned PieceSize = Var.getPieceSize();
assert(Offset <= PieceOffset && "overlapping pieces in DebugLocEntry");
if (Offset < PieceOffset) {
Asm->EmitDwarfOpPiece((PieceOffset-Offset)*8);
Offset += PieceOffset-Offset;
}
Offset += PieceSize;
assert(!Var.isIndirect() && "indirect address for piece");
#ifndef NDEBUG
unsigned VarSize = Var.getSizeInBits(Map);
assert(PieceSize+PieceOffset <= VarSize/8
&& "piece is larger than or outside of variable");
assert(PieceSize*8 != VarSize
&& "piece covers entire variable");
#endif
if (Piece.isLocation() && Piece.getLoc().isReg())
Asm->EmitDwarfRegOpPiece(Piece.getLoc(), PieceSize*8);
else {
emitDebugLocValue(Piece);
Asm->EmitDwarfOpPiece(PieceSize*8);
}
}
}
void DwarfDebug::emitDebugLocEntry(const DebugLocEntry &Entry) {
const DebugLocEntry::Value Value = Entry.getValues()[0];
DIVariable DV(Value.getVariable());
if (DV.isVariablePiece())
return emitLocPieces(TypeIdentifierMap, Entry.getValues());
assert(Entry.getValues().size() == 1 && "only pieces may have >1 value");
emitDebugLocValue(Value);
}
void DwarfDebug::emitDebugLocValue(const DebugLocEntry::Value &Value) {
DIVariable DV(Value.getVariable());
if (Value.isInt()) {
DIBasicType BTy(DV.getType());
if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Asm->OutStreamer.AddComment("DW_OP_consts");
Asm->EmitInt8(dwarf::DW_OP_consts);
Asm->EmitSLEB128(Value.getInt());
} else {
Asm->OutStreamer.AddComment("DW_OP_constu");
Asm->EmitInt8(dwarf::DW_OP_constu);
Asm->EmitULEB128(Value.getInt());
}
} else if (Value.isLocation()) {
MachineLocation Loc = Value.getLoc();
if (!DV.hasComplexAddress())
Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
else {
unsigned N = DV.getNumAddrElements();
unsigned i = 0;
if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
if (Loc.getOffset()) {
i = 2;
Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Asm->OutStreamer.AddComment("DW_OP_deref");
Asm->EmitInt8(dwarf::DW_OP_deref);
Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
Asm->EmitSLEB128(DV.getAddrElement(1));
} else {
MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
i = 2;
}
} else {
Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
}
for (; i < N; ++i) {
uint64_t Element = DV.getAddrElement(i);
if (Element == DIBuilder::OpPlus) {
Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
Asm->EmitULEB128(DV.getAddrElement(++i));
} else if (Element == DIBuilder::OpDeref) {
if (!Loc.isReg()) {
Asm->OutStreamer.AddComment("DW_OP_deref");
Asm->EmitInt8(dwarf::DW_OP_deref);
}
} else if (Element == DIBuilder::OpPiece) {
i += 3;
} else
llvm_unreachable("unknown Opcode found in complex address");
}
}
}
}
void DwarfFile::emitStrings(const MCSection *StrSection,
const MCSection *OffsetSection = NULL,
const MCSymbol *StrSecSym = NULL) {
if (StringPool.empty())
return;
Asm->OutStreamer.SwitchSection(StrSection);
SmallVector<
std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
64> Entries;
for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
I = StringPool.begin(),
E = StringPool.end();
I != E; ++I)
Entries.push_back(std::make_pair(I->second.second, &*I));
array_pod_sort(Entries.begin(), Entries.end());
for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Asm->OutStreamer.EmitBytes(
StringRef(Entries[i].second->getKeyData(),
Entries[i].second->getKeyLength() + 1));
}
if (OffsetSection) {
Asm->OutStreamer.SwitchSection(OffsetSection);
unsigned offset = 0;
unsigned size = 4; for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Asm->OutStreamer.EmitIntValue(offset, size);
offset += Entries[i].second->getKeyLength() + 1;
}
}
}
void DwarfFile::emitAddresses(const MCSection *AddrSection) {
if (AddressPool.empty())
return;
Asm->OutStreamer.SwitchSection(AddrSection);
SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
E = AddressPool.end();
I != E; ++I)
Entries[I->second] = I->first;
for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
if (const MCExpr *Expr = Entries[i])
Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
else
Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
}
}
void DwarfDebug::emitDebugStr() {
DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
}
void DwarfDebug::emitDebugLoc() {
if (DotDebugLocEntries.empty())
return;
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfLocSection());
unsigned char Size = Asm->getDataLayout().getPointerSize();
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
unsigned index = 1;
for (auto &Entry : DotDebugLocEntries) {
if (Entry.isEmpty()) {
Asm->OutStreamer.EmitIntValue(0, Size);
Asm->OutStreamer.EmitIntValue(0, Size);
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
} else {
Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
Asm->OutStreamer.AddComment("Loc expr size");
MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
Asm->EmitLabelDifference(end, begin, 2);
Asm->OutStreamer.EmitLabel(begin);
emitDebugLocEntry(Entry);
Asm->OutStreamer.EmitLabel(end);
}
++index;
}
}
struct SymbolCUSorter {
SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
const MCStreamer &Streamer;
bool operator()(const SymbolCU &A, const SymbolCU &B) {
unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
if (IA == 0)
IA = (unsigned)(-1);
if (IB == 0)
IB = (unsigned)(-1);
return IA < IB;
}
};
static bool CUSort(const DwarfUnit *A, const DwarfUnit *B) {
return (A->getUniqueID() < B->getUniqueID());
}
struct ArangeSpan {
const MCSymbol *Start, *End;
};
void DwarfDebug::emitDebugARanges() {
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfARangesSection());
typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
SpansType Spans;
std::vector<const MCSection *> Sections;
for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
it++) {
const MCSection *Section = it->first;
Sections.push_back(Section);
}
std::sort(Sections.begin(), Sections.end(), SectionSort);
for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
const MCSection *Section = Sections[SecIdx];
SmallVector<SymbolCU, 8> &List = SectionMap[Section];
if (List.size() < 2)
continue;
SymbolCUSorter sorter(Asm->OutStreamer);
std::sort(List.begin(), List.end(), sorter);
if (Section == NULL) {
for (size_t n = 0; n < List.size(); n++) {
const SymbolCU &Cur = List[n];
ArangeSpan Span;
Span.Start = Cur.Sym;
Span.End = NULL;
if (Cur.CU)
Spans[Cur.CU].push_back(Span);
}
} else {
const MCSymbol *StartSym = List[0].Sym;
for (size_t n = 1; n < List.size(); n++) {
const SymbolCU &Prev = List[n - 1];
const SymbolCU &Cur = List[n];
if (Cur.CU != Prev.CU) {
ArangeSpan Span;
Span.Start = StartSym;
Span.End = Cur.Sym;
Spans[Prev.CU].push_back(Span);
StartSym = Cur.Sym;
}
}
}
}
unsigned PtrSize = Asm->getDataLayout().getPointerSize();
std::vector<DwarfCompileUnit *> CUs;
for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
DwarfCompileUnit *CU = it->first;
CUs.push_back(CU);
}
std::sort(CUs.begin(), CUs.end(), CUSort);
for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
DwarfCompileUnit *CU = CUs[CUIdx];
std::vector<ArangeSpan> &List = Spans[CU];
unsigned ContentSize =
sizeof(int16_t) + sizeof(int32_t) + sizeof(int8_t) + sizeof(int8_t);
unsigned TupleSize = PtrSize * 2;
unsigned Padding =
OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
ContentSize += Padding;
ContentSize += (List.size() + 1) * TupleSize;
Asm->OutStreamer.AddComment("Length of ARange Set");
Asm->EmitInt32(ContentSize);
Asm->OutStreamer.AddComment("DWARF Arange version number");
Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Asm->OutStreamer.AddComment("Address Size (in bytes)");
Asm->EmitInt8(PtrSize);
Asm->OutStreamer.AddComment("Segment Size (in bytes)");
Asm->EmitInt8(0);
Asm->OutStreamer.EmitFill(Padding, 0xff);
for (unsigned n = 0; n < List.size(); n++) {
const ArangeSpan &Span = List[n];
Asm->EmitLabelReference(Span.Start, PtrSize);
if (Span.End) {
Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
} else {
uint64_t Size = SymSize[Span.Start];
if (Size == 0)
Size = 1;
Asm->OutStreamer.EmitIntValue(Size, PtrSize);
}
}
Asm->OutStreamer.AddComment("ARange terminator");
Asm->OutStreamer.EmitIntValue(0, PtrSize);
Asm->OutStreamer.EmitIntValue(0, PtrSize);
}
}
void DwarfDebug::emitDebugRanges() {
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfRangesSection());
unsigned char Size = Asm->getDataLayout().getPointerSize();
for (DenseMap<const MDNode *, DwarfCompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end();
I != E; ++I) {
DwarfCompileUnit *TheCU = I->second;
Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
const SmallVectorImpl<RangeSpanList> &RangeLists = TheCU->getRangeLists();
for (SmallVectorImpl<RangeSpanList>::const_iterator I = RangeLists.begin(),
E = RangeLists.end();
I != E; ++I) {
const RangeSpanList &List = *I;
Asm->OutStreamer.EmitLabel(List.getSym());
for (SmallVectorImpl<RangeSpan>::const_iterator
RI = List.getRanges().begin(),
RE = List.getRanges().end();
RI != RE; ++RI) {
const RangeSpan &Range = *RI;
const MCSymbol *Begin = Range.getStart();
const MCSymbol *End = Range.getEnd();
assert(Begin && "Range without a begin symbol?");
assert(End && "Range without an end symbol?");
Asm->OutStreamer.EmitSymbolValue(Begin, Size);
Asm->OutStreamer.EmitSymbolValue(End, Size);
}
Asm->OutStreamer.EmitIntValue(0, Size);
Asm->OutStreamer.EmitIntValue(0, Size);
}
if (useCURanges()) {
Asm->OutStreamer.EmitLabel(
Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
const SmallVectorImpl<RangeSpan> &Ranges = TheCU->getRanges();
for (uint32_t i = 0, e = Ranges.size(); i != e; ++i) {
RangeSpan Range = Ranges[i];
const MCSymbol *Begin = Range.getStart();
const MCSymbol *End = Range.getEnd();
assert(Begin && "Range without a begin symbol?");
assert(End && "Range without an end symbol?");
Asm->OutStreamer.EmitSymbolValue(Begin, Size);
Asm->OutStreamer.EmitSymbolValue(End, Size);
}
Asm->OutStreamer.EmitIntValue(0, Size);
Asm->OutStreamer.EmitIntValue(0, Size);
}
}
}
void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
DwarfUnit *NewU) {
NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
U->getCUNode().getSplitDebugFilename());
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
else
NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
if (!CompilationDir.empty())
NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
addGnuPubAttributes(NewU, Die);
SkeletonHolder.addUnit(NewU);
}
DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
DwarfCompileUnit *NewCU = new DwarfCompileUnit(
CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
DwarfInfoSectionSym);
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list, DwarfLineSectionSym);
else
NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
initSkeletonUnit(CU, Die, NewCU);
return NewCU;
}
DwarfTypeUnit *DwarfDebug::constructSkeletonTU(const DwarfTypeUnit *TU) {
DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
DwarfTypeUnit *NewTU = new DwarfTypeUnit(
TU->getUniqueID(), Die, TU->getCUNode(), Asm, this, &SkeletonHolder);
NewTU->setTypeSignature(TU->getTypeSignature());
NewTU->setType(NULL);
NewTU->initSection(
Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
initSkeletonUnit(TU, Die, NewTU);
return NewTU;
}
void DwarfDebug::emitDebugInfoDWO() {
assert(useSplitDwarf() && "No split dwarf debug info?");
InfoHolder.emitUnits(this,
Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
DwarfAbbrevDWOSectionSym);
}
void DwarfDebug::emitDebugAbbrevDWO() {
assert(useSplitDwarf() && "No split dwarf?");
InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
}
void DwarfDebug::emitDebugStrDWO() {
assert(useSplitDwarf() && "No split dwarf?");
const MCSection *OffSec =
Asm->getObjFileLowering().getDwarfStrOffDWOSection();
const MCSymbol *StrSym = DwarfStrSectionSym;
InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
OffSec, StrSym);
}
void DwarfDebug::addDwarfTypeUnitType(DICompileUnit CUNode,
StringRef Identifier, DIE *RefDie,
DICompositeType CTy) {
const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
if (TU) {
CUMap.begin()->second->addDIETypeSignature(RefDie, *TU);
return;
}
DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
DwarfTypeUnit *NewTU = new DwarfTypeUnit(
InfoHolder.getUnits().size(), UnitDie, CUNode, Asm, this, &InfoHolder);
TU = NewTU;
InfoHolder.addUnit(NewTU);
NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
CUNode.getLanguage());
MD5 Hash;
Hash.update(Identifier);
MD5::MD5Result Result;
Hash.final(Result);
uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
NewTU->setTypeSignature(Signature);
if (useSplitDwarf())
NewTU->setSkeleton(constructSkeletonTU(NewTU));
NewTU->setType(NewTU->createTypeDIE(CTy));
NewTU->initSection(
useSplitDwarf()
? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
: Asm->getObjFileLowering().getDwarfTypesSection(Signature));
CUMap.begin()->second->addDIETypeSignature(RefDie, *NewTU);
}