#define DEBUG_TYPE "dwarfdebug"
#include "DwarfDebug.h"
#include "DIE.h"
#include "DIEHash.h"
#include "DwarfAccelTable.h"
#include "DwarfCompileUnit.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>
GenerateODRHash("generate-odr-hash", cl::Hidden,
cl::desc("Add an ODR hash to external type DIEs."),
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 prototype dwarf 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 const char *const DWARFGroupName = "DWARF Emission";
static const char *const DbgTimerName = "DWARF Debug Writer";
static const unsigned InitAbbreviationsSetSize = 9;
namespace llvm {
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 = DIDerivedType(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),
AbbreviationsSet(InitAbbreviationsSetSize),
SourceIdMap(DIEValueAllocator),
PrevLabel(NULL), GlobalCUIndexCount(0),
InfoHolder(A, &AbbreviationsSet, Abbreviations, "info_string",
DIEValueAllocator),
SkeletonAbbrevSet(InitAbbreviationsSetSize),
SkeletonHolder(A, &SkeletonAbbrevSet, SkeletonAbbrevs, "skel_string",
DIEValueAllocator) {
DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
DwarfStrSectionSym = TextSectionSym = 0;
DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
DwarfAddrSectionSym = 0;
DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
FunctionBeginSym = FunctionEndSym = 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;
DwarfVersion = 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;
}
MCSymbol *DwarfUnits::getStringPoolSym() {
return Asm->GetTempSymbol(StringPref);
}
MCSymbol *DwarfUnits::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 DwarfUnits::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 DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
}
unsigned DwarfUnits::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 DwarfUnits::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(CompileUnit *TheCU, DISubprogram SP,
DIE* Die) {
if (!SP.isDefinition()) return;
TheCU->addAccelName(SP.getName(), Die);
if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
TheCU->addAccelName(SP.getLinkageName(), Die);
if (isObjCClass(SP.getName())) {
StringRef Class, Category;
getObjCClassCategory(SP.getName(), Class, Category);
TheCU->addAccelObjC(Class, Die);
if (Category != "")
TheCU->addAccelObjC(Category, Die);
TheCU->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(CompileUnit *SPCU,
const MDNode *SPNode) {
DIE *SPDie = SPCU->getDIE(SPNode);
assert(SPDie && "Unable to find subprogram DIE!");
DISubprogram SP(SPNode);
if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getCUDie());
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.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)
for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
DIE *Arg =
SPCU->createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
DIType ATy = DIType(Args.getElement(i));
SPCU->addType(Arg, ATy);
if (ATy.isArtificial())
SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
if (ATy.isObjectPointer())
SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
}
DIE *SPDeclDie = SPDie;
SPDie =
SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getCUDie());
SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
}
}
}
SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc,
Asm->GetTempSymbol("func_begin",
Asm->getFunctionNumber()));
SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc,
Asm->GetTempSymbol("func_end",
Asm->getFunctionNumber()));
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;
}
DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *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> &Ranges = Scope->getRanges();
if (Ranges.size() > 1) {
TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
DebugRangeSymbols.size()
* Asm->getDataLayout().getPointerSize());
for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end(); RI != RE; ++RI) {
DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
}
DebugRangeSymbols.push_back(NULL);
DebugRangeSymbols.push_back(NULL);
return ScopeDIE;
}
SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.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(CompileUnit *TheCU,
LexicalScope *Scope) {
const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
assert(Ranges.empty() == false &&
"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 (Ranges.size() > 1) {
TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
DebugRangeSymbols.size()
* Asm->getDataLayout().getPointerSize());
for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
RE = Ranges.end(); RI != RE; ++RI) {
DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
}
DebugRangeSymbols.push_back(NULL);
DebugRangeSymbols.push_back(NULL);
} else {
SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.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(CompileUnit *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;
}
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(CompileUnit *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, 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);
if (DS.isSubprogram())
TheCU->addPubTypes(DISubprogram(DS));
return ScopeDIE;
}
unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
StringRef DirName, unsigned CUID) {
if (Asm->TM.hasMCUseLoc() && 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;
}
CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
DICompileUnit DIUnit(N);
StringRef FN = DIUnit.getFilename();
CompilationDir = DIUnit.getDirectory();
DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
CompileUnit *NewCU =
new CompileUnit(GlobalCUIndexCount++, Die, N, Asm, this, &InfoHolder);
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);
if (!useSplitDwarf())
NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
MCSymbol *LineTableStartSym = Asm->GetTempSymbol("line_table_start",
NewCU->getUniqueID());
Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
NewCU->getUniqueID());
bool UseTheFirstCU =
(Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
(NewCU->getUniqueID() == 0);
if (!useSplitDwarf()) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
UseTheFirstCU ? Asm->GetTempSymbol("section_line")
: LineTableStartSym);
else if (UseTheFirstCU)
NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
else
NewCU->addDelta(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
LineTableStartSym, DwarfLineSectionSym);
if (!CompilationDir.empty())
NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
if (GenerateGnuPubSections) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubnames,
dwarf::DW_FORM_sec_offset,
Asm->GetTempSymbol("gnu_pubnames",
NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_data4,
Asm->GetTempSymbol("gnu_pubnames",
NewCU->getUniqueID()),
DwarfGnuPubNamesSectionSym);
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubtypes,
dwarf::DW_FORM_sec_offset,
Asm->GetTempSymbol("gnu_pubtypes",
NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
Asm->GetTempSymbol("gnu_pubtypes",
NewCU->getUniqueID()),
DwarfGnuPubTypesSectionSym);
}
}
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;
InfoHolder.addUnit(NewCU);
CUMap.insert(std::make_pair(N, NewCU));
CUDieMap.insert(std::make_pair(Die, NewCU));
return NewCU;
}
void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
CompileUnit *&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(CompileUnit *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(CompileUnit *TheCU, const MDNode *N,
DIE *Context) {
DIImportedEntity Module(N);
if (!Module.Verify())
return;
return constructImportedEntityDIE(TheCU, Module, Context);
}
void DwarfDebug::constructImportedEntityDIE(CompileUnit *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));
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));
CompileUnit *CU = constructCompileUnit(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(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;
CompileUnit *SPCU = 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);
}
}
}
}
}
static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
DIEValue *V = Die->findAttribute(Attr);
if (DIEString *S = dyn_cast_or_null<DIEString>(V))
return S->getString();
return StringRef("");
}
static bool isContainedInAnonNamespace(DIE *Die) {
DIE *Parent = Die->getParent();
while (Parent) {
if (Parent->getTag() == dwarf::DW_TAG_namespace &&
getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
return true;
Parent = Parent->getParent();
}
return false;
}
static bool shouldAddODRHash(CompileUnit *CU, DIE *Die) {
return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
!isContainedInAnonNamespace(Die);
}
void DwarfDebug::finalizeModuleInfo() {
collectDeadVariables();
computeInlinedDIEs();
for (unsigned i = 0, e = TypeUnits.size(); i != e; ++i) {
DIE *Die = TypeUnits[i];
DIEHash Hash;
if (GenerateODRHash && shouldAddODRHash(CUMap.begin()->second, Die))
CUMap.begin()->second->addUInt(Die, dwarf::DW_AT_GNU_odr_signature,
dwarf::DW_FORM_data8,
Hash.computeDIEODRSignature(*Die));
}
for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
CUE = CUMap.end();
CUI != CUE; ++CUI) {
CompileUnit *TheCU = CUI->second;
TheCU->constructContainingTypeDIEs();
if (useSplitDwarf()) {
uint64_t ID = 0;
if (GenerateCUHash) {
DIEHash CUHash;
ID = CUHash.computeCUSignature(*TheCU->getCUDie());
}
TheCU->addUInt(TheCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
dwarf::DW_FORM_data8, ID);
CompileUnit *SkCU = constructSkeletonCU(TheCU);
SkCU->addUInt(SkCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
dwarf::DW_FORM_data8, ID);
}
}
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() {
if (!FirstCU) return;
endSections();
finalizeModuleInfo();
if (!useSplitDwarf()) {
emitDebugStr();
emitDebugInfo();
emitAbbreviations();
emitDebugLoc();
emitDebugARanges();
emitDebugRanges();
emitDebugMacInfo();
} else {
emitDebugStr();
if (useSplitDwarf())
emitDebugStrDWO();
emitDebugInfo();
emitDebugInfoDWO();
emitAbbreviations();
emitDebugAbbrevDWO();
emitDebugLoc();
emitDebugARanges();
emitDebugRanges();
emitDebugMacInfo();
InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
}
if (useDwarfAccelTables()) {
emitAccelNames();
emitAccelObjC();
emitAccelNamespaces();
emitAccelTypes();
}
if (HasDwarfPubSections) {
emitDebugPubNames(GenerateGnuPubSections);
emitDebugPubTypes(GenerateGnuPubSections);
}
SPMap.clear();
for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end(); I != E; ++I)
delete I->second;
for (SmallVectorImpl<CompileUnit *>::iterator I = SkeletonCUs.begin(),
E = SkeletonCUs.end(); I != E; ++I)
delete *I;
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(const MachineFunction *MF,
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(MF->getFunction()->arg_size());
if (ArgNo > Size)
CurrentFnArguments.resize(ArgNo * 2);
CurrentFnArguments[ArgNo - 1] = Var;
return true;
}
void
DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
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(MF, 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 DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
const MCSymbol *FLabel,
const MCSymbol *SLabel,
const MachineInstr *MI) {
const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
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 DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
}
if (MI->getOperand(0).isImm())
return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
if (MI->getOperand(0).isFPImm())
return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
if (MI->getOperand(0).isCImm())
return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
}
void
DwarfDebug::collectVariableInfo(const MachineFunction *MF,
SmallPtrSet<const MDNode *, 16> &Processed) {
collectVariableInfoFromMMITable(MF, 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(MF->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(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(MF, RegVar, Scope))
addScopeVariable(Scope, RegVar);
if (AbsVar)
AbsVar->setMInsn(MInsn);
if (History.size() <= 1 || (History.size() == 2 &&
MInsn->isIdenticalTo(History.back()))) {
RegVar->setMInsn(MInsn);
continue;
}
RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
for (SmallVectorImpl<const MachineInstr*>::const_iterator
HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
const MachineInstr *Begin = *HI;
assert(Begin->isDebugValue() && "Invalid History entry");
if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
&& !Begin->getOperand(0).getReg())
continue;
const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
const MCSymbol *SLabel = 0;
if (HI + 1 == HE)
SLabel = FunctionEndSym;
else {
const MachineInstr *End = HI[1];
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");
++HI;
}
}
DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
Begin));
}
DotDebugLocEntries.push_back(DotDebugLocEntry());
}
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
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) {
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(const MachineInstr *MI) {
if (!MI->isDebugValue())
PrevLabel = 0;
DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
LabelsAfterInsn.find(MI);
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();
}
void DwarfDebug::beginFunction(const MachineFunction *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();
CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
assert(TheCU && "Unable to find compile unit!");
if (Asm->TM.hasMCUseLoc() && 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());
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!");
const MDNode *Var =
MI->getOperand(MI->getNumOperands() - 1).getMetadata();
if (isDbgValueInDefinedReg(MI))
LiveUserVar[MI->getOperand(0).getReg()] = Var;
SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
if (History.empty()) {
UserVariables.push_back(Var);
DIVariable DV(Var);
if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
DISubprogram(getDISubprogram(DV.getContext()))
.describes(MF->getFunction()))
LabelsBeforeInsn[MI] = FunctionBeginSym;
} 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;
DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
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())
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);
}
}
for (unsigned i = 0, e = History.size(); i != e; ++i) {
const MachineInstr *MI = History[i];
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 (!MMI->hasDebugInfo() || LScopes.empty()) return;
FunctionEndSym = Asm->GetTempSymbol("func_end",
Asm->getFunctionNumber());
Asm->OutStreamer.EmitLabel(FunctionEndSym);
Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
SmallPtrSet<const MDNode *, 16> ProcessedVars;
collectVariableInfo(MF, ProcessedVars);
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
CompileUnit *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 (!MF->getTarget().Options.DisableFramePointerElim(*MF))
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;
}
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
DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
const std::vector<DIE *> &Children = Die->getChildren();
assignAbbrevNumber(Die->getAbbrev());
unsigned AbbrevNumber = Die->getAbbrevNumber();
const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Die->setOffset(Offset);
Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
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 DwarfUnits::computeSizeAndOffsets() {
unsigned SecOffset = 0;
for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
E = CUs.end(); I != E; ++I) {
(*I)->setDebugInfoOffset(SecOffset);
unsigned Offset = sizeof(int32_t) + (*I)->getHeaderSize();
unsigned EndOffset = computeSizeAndOffset((*I)->getCUDie(), Offset);
SecOffset += EndOffset;
}
}
void DwarfDebug::emitSectionLabels() {
const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
DwarfInfoSectionSym =
emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
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");
TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
emitSectionSym(Asm, TLOF.getDataSection());
}
void DwarfDebug::emitDIE(DIE *Die, ArrayRef<DIEAbbrev *> Abbrevs) {
unsigned AbbrevNumber = Die->getAbbrevNumber();
const DIEAbbrev *Abbrev = Abbrevs[AbbrevNumber - 1];
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
Twine::utohexstr(Die->getOffset()) + ":0x" +
Twine::utohexstr(Die->getSize()) + " " +
dwarf::TagString(Abbrev->getTag()));
Asm->EmitULEB128(AbbrevNumber);
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.");
CompileUnit *CU = CUDieMap.lookup(Origin->getCompileUnit());
assert(CU && "CUDie should belong to a CU.");
Addr += CU->getDebugInfoOffset();
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Asm->EmitLabelPlusOffset(DwarfInfoSectionSym, Addr,
DIEEntry::getRefAddrSize(Asm));
else
Asm->EmitLabelOffsetDifference(DwarfInfoSectionSym, Addr,
DwarfInfoSectionSym,
DIEEntry::getRefAddrSize(Asm));
} else {
assert(Die->getCompileUnit() == Origin->getCompileUnit() &&
"The referenced DIE should belong to the same CU in ref4");
Asm->EmitInt32(Addr);
}
break;
}
case dwarf::DW_AT_ranges: {
DIEInteger *V = cast<DIEInteger>(Values[i]);
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
V->getValue(),
4);
} else {
Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
V->getValue(),
DwarfDebugRangeSectionSym,
4);
}
break;
}
case dwarf::DW_AT_location: {
if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Asm->EmitLabelReference(L->getValue(), 4);
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], Abbrevs);
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("End Of Children Mark");
Asm->EmitInt8(0);
}
}
void DwarfUnits::emitUnits(DwarfDebug *DD,
const MCSection *USection,
const MCSection *ASection,
const MCSymbol *ASectionSym) {
Asm->OutStreamer.SwitchSection(USection);
for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
E = CUs.end(); I != E; ++I) {
CompileUnit *TheCU = *I;
DIE *Die = TheCU->getCUDie();
Asm->OutStreamer
.EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
TheCU->getUniqueID()));
Asm->OutStreamer.AddComment("Length of Unit");
Asm->EmitInt32(TheCU->getHeaderSize() + Die->getSize());
TheCU->emitHeader(ASection, ASectionSym);
DD->emitDIE(Die, Abbreviations);
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
TheCU->getUniqueID()));
}
}
void DwarfDebug::emitDebugInfo() {
DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
Asm->getObjFileLowering().getDwarfAbbrevSection(),
DwarfAbbrevSectionSym);
}
void DwarfDebug::emitAbbreviations() {
if (!useSplitDwarf())
emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
&Abbreviations);
else
emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
}
void DwarfDebug::emitAbbrevs(const MCSection *Section,
std::vector<DIEAbbrev *> *Abbrevs) {
if (!Abbrevs->empty()) {
Asm->OutStreamer.SwitchSection(Section);
MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Asm->OutStreamer.EmitLabel(Begin);
for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
const DIEAbbrev *Abbrev = Abbrevs->at(i);
Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Abbrev->Emit(Asm);
}
Asm->EmitULEB128(0, "EOM(3)");
MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Asm->OutStreamer.EmitLabel(End);
}
}
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 (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end(); I != E; ++I) {
CompileUnit *TheCU = I->second;
const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
for (StringMap<std::vector<DIE*> >::const_iterator
GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<DIE *> &Entities = GI->second;
for (std::vector<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 (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end(); I != E; ++I) {
CompileUnit *TheCU = I->second;
const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
for (StringMap<std::vector<DIE*> >::const_iterator
GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<DIE *> &Entities = GI->second;
for (std::vector<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 (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end(); I != E; ++I) {
CompileUnit *TheCU = I->second;
const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
for (StringMap<std::vector<DIE*> >::const_iterator
GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<DIE *> &Entities = GI->second;
for (std::vector<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 (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end(); I != E; ++I) {
CompileUnit *TheCU = I->second;
const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
= TheCU->getAccelTypes();
for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
StringRef Name = GI->getKey();
const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
for (std::vector<std::pair<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(CompileUnit *CU,
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 *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
const MCSection *PSec =
GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
: Asm->getObjFileLowering().getDwarfPubNamesSection();
typedef DenseMap<const MDNode*, CompileUnit*> CUMapType;
for (CUMapType::iterator I = CUMap.begin(), E = CUMap.end(); I != E; ++I) {
CompileUnit *TheCU = I->second;
unsigned ID = TheCU->getUniqueID();
Asm->OutStreamer.SwitchSection(PSec);
if (GnuStyle)
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames",
TheCU->getUniqueID()));
Asm->OutStreamer.AddComment("Length of Public Names Info");
Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
Asm->GetTempSymbol("pubnames_begin", ID), 4);
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
Asm->OutStreamer.AddComment("DWARF Version");
Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
DwarfInfoSectionSym);
Asm->OutStreamer.AddComment("Compilation Unit Length");
Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
4);
const StringMap<DIE*> &Globals = TheCU->getGlobalNames();
for (StringMap<DIE*>::const_iterator
GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
const char *Name = GI->getKeyData();
DIE *Entity = GI->second;
Asm->OutStreamer.AddComment("DIE offset");
Asm->EmitInt32(Entity->getOffset());
if (GnuStyle) {
dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
Asm->OutStreamer.AddComment(
Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
Asm->EmitInt8(Desc.toBits());
}
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("External Name");
Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
}
Asm->OutStreamer.AddComment("End Mark");
Asm->EmitInt32(0);
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
}
}
void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
const MCSection *PSec =
GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
: Asm->getObjFileLowering().getDwarfPubTypesSection();
for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
E = CUMap.end();
I != E; ++I) {
CompileUnit *TheCU = I->second;
Asm->OutStreamer.SwitchSection(PSec);
if (GnuStyle)
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes",
TheCU->getUniqueID()));
Asm->OutStreamer.AddComment("Length of Public Types Info");
Asm->EmitLabelDifference(
Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Asm->OutStreamer.EmitLabel(
Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()));
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("DWARF Version");
Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Asm->EmitSectionOffset(
Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()),
DwarfInfoSectionSym);
Asm->OutStreamer.AddComment("Compilation Unit Length");
Asm->EmitLabelDifference(
Asm->GetTempSymbol(ISec->getLabelEndName(), TheCU->getUniqueID()),
Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()), 4);
const StringMap<DIE *> &Globals = TheCU->getGlobalTypes();
for (StringMap<DIE *>::const_iterator GI = Globals.begin(),
GE = Globals.end();
GI != GE; ++GI) {
const char *Name = GI->getKeyData();
DIE *Entity = GI->second;
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("DIE offset");
Asm->EmitInt32(Entity->getOffset());
if (GnuStyle) {
dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
Asm->OutStreamer.AddComment(
Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
Asm->EmitInt8(Desc.toBits());
}
if (Asm->isVerbose())
Asm->OutStreamer.AddComment("External Name");
Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
}
Asm->OutStreamer.AddComment("End Mark");
Asm->EmitInt32(0);
Asm->OutStreamer.EmitLabel(
Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()));
}
}
void DwarfUnits::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 DwarfUnits::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() {
DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
}
void DwarfDebug::emitDebugLoc() {
if (DotDebugLocEntries.empty())
return;
for (SmallVectorImpl<DotDebugLocEntry>::iterator
I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
I != E; ++I) {
DotDebugLocEntry &Entry = *I;
if (I + 1 != DotDebugLocEntries.end())
Entry.Merge(I+1);
}
Asm->OutStreamer.SwitchSection(
Asm->getObjFileLowering().getDwarfLocSection());
unsigned char Size = Asm->getDataLayout().getPointerSize();
Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
unsigned index = 1;
for (SmallVectorImpl<DotDebugLocEntry>::iterator
I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
I != E; ++I, ++index) {
DotDebugLocEntry &Entry = *I;
if (Entry.isMerged()) continue;
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);
DIVariable DV(Entry.getVariable());
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);
if (Entry.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(Entry.getInt());
} else {
Asm->OutStreamer.AddComment("DW_OP_constu");
Asm->EmitInt8(dwarf::DW_OP_constu);
Asm->EmitULEB128(Entry.getInt());
}
} else if (Entry.isLocation()) {
MachineLocation Loc = Entry.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->EmitInt8(dwarf::DW_OP_plus_uconst);
Asm->EmitULEB128(DV.getAddrElement(++i));
} else if (Element == DIBuilder::OpDeref) {
if (!Loc.isReg())
Asm->EmitInt8(dwarf::DW_OP_deref);
} else
llvm_unreachable("unknown Opcode found in complex address");
}
}
}
Asm->OutStreamer.EmitLabel(end);
}
}
}
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 CompileUnit *A, const CompileUnit *B) {
return (A->getUniqueID() < B->getUniqueID());
}
struct ArangeSpan {
const MCSymbol *Start, *End;
};
void DwarfDebug::emitDebugARanges() {
Asm->OutStreamer
.SwitchSection(Asm->getObjFileLowering().getDwarfARangesSection());
typedef DenseMap<CompileUnit *, 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;
}
}
}
}
const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
unsigned PtrSize = Asm->getDataLayout().getPointerSize();
std::vector<CompileUnit *> CUs;
for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
CompileUnit *CU = it->first;
CUs.push_back(CU);
}
std::sort(CUs.begin(), CUs.end(), CUSort);
for (size_t CUIdx=0;CUIdx<CUs.size();CUIdx++) {
CompileUnit *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 = 0;
while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
Padding++;
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(
Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
DwarfInfoSectionSym);
Asm->OutStreamer.AddComment("Address Size (in bytes)");
Asm->EmitInt8(PtrSize);
Asm->OutStreamer.AddComment("Segment Size (in bytes)");
Asm->EmitInt8(0);
for (unsigned n = 0; n < Padding; n++)
Asm->EmitInt8(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 (SmallVectorImpl<const MCSymbol *>::iterator
I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
I != E; ++I) {
if (*I)
Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size);
else
Asm->OutStreamer.EmitIntValue(0, Size);
}
}
void DwarfDebug::emitDebugMacInfo() {
if (const MCSection *LineInfo =
Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Asm->OutStreamer.SwitchSection(LineInfo);
}
}
CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
Asm, this, &SkeletonHolder);
NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
DICompileUnit(CU->getNode()).getSplitDebugFilename());
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_addr_base, dwarf::DW_FORM_sec_offset,
DwarfAddrSectionSym);
else
NewCU->addUInt(Die, dwarf::DW_AT_GNU_addr_base,
dwarf::DW_FORM_sec_offset, 0);
NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
DwarfLineSectionSym);
else
NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset, 0);
if (!CompilationDir.empty())
NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
if (GenerateGnuPubSections) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_sec_offset,
Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_data4,
Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
DwarfGnuPubNamesSectionSym);
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_sec_offset,
Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
DwarfGnuPubTypesSectionSym);
}
if (DebugRangeSymbols.size()) {
if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
NewCU->addLabel(Die, dwarf::DW_AT_GNU_ranges_base,
dwarf::DW_FORM_sec_offset, DwarfDebugRangeSectionSym);
else
NewCU->addUInt(Die, dwarf::DW_AT_GNU_ranges_base, dwarf::DW_FORM_data4,
0);
}
SkeletonHolder.addUnit(NewCU);
SkeletonCUs.push_back(NewCU);
return NewCU;
}
void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
assert(useSplitDwarf() && "No split dwarf debug info?");
emitAbbrevs(Section, &SkeletonAbbrevs);
}
void DwarfDebug::emitDebugInfoDWO() {
assert(useSplitDwarf() && "No split dwarf debug info?");
InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
DwarfAbbrevDWOSectionSym);
}
void DwarfDebug::emitDebugAbbrevDWO() {
assert(useSplitDwarf() && "No split dwarf?");
emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
&Abbreviations);
}
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);
}