namespace llvm {
+/// resolve - Look in the DwarfDebug map for the MDNode that
+/// corresponds to the reference.
+template <typename T>
+T DbgVariable::resolve(DIRef<T> Ref) const {
+ return DD->resolve(Ref);
+}
+
DIType DbgVariable::getType() const {
DIType Ty = Var.getType();
// FIXME: isBlockByrefVariable should be reformulated in terms of complex
uint16_t tag = Ty.getTag();
if (tag == dwarf::DW_TAG_pointer_type)
- subType = DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom());
+ 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 (DD->resolve(DT.getTypeDerivedFrom()));
+ return (resolve(DT.getTypeDerivedFrom()));
}
}
return Ty;
DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
FunctionBeginSym = FunctionEndSym = 0;
- // Turn on accelerator tables and older gdb compatibility
- // for Darwin by default, pubnames by default for non-Darwin,
- // and handle split dwarf.
+ // Turn on accelerator tables for Darwin by default, pubnames by
+ // default for non-Darwin, and handle split dwarf.
bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
if (DwarfAccelTables == Default)
beginModule();
}
}
-DwarfDebug::~DwarfDebug() {
-}
// Switch to the specified MCSection and emit an assembler
// temporary label to it if SymbolStem is specified.
return In.slice(In.find(' ') + 1, In.find(']'));
}
+// Helper for sorting sections into a stable output order.
+static bool SectionSort(const MCSection *A, const MCSection *B) {
+ std::string LA = (A ? A->getLabelBeginName() : "");
+ std::string LB = (B ? B->getLabelBeginName() : "");
+ return LA < LB;
+}
+
// Add the various names to the Dwarf accelerator table names.
// TODO: Determine whether or not we should add names for programs
// that do not have a DW_AT_name or DW_AT_linkage_name field - this
// scope then create and insert DIEs for these variables.
DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
const MDNode *SPNode) {
- DIE *SPDie = getSPDIE(SPNode);
+ DIE *SPDie = SPCU->getDIE(SPNode);
assert(SPDie && "Unable to find subprogram DIE!");
DISubprogram SP(SPNode);
// If we're updating an abstract DIE, then we will be adding the children and
// object pointer later on. But what we don't want to do is process the
// concrete DIE twice.
- DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode);
- if (AbsSPDIE) {
- bool InSameCU = (AbsSPDIE->getCompileUnit() == SPCU->getCUDie());
+ if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
// Pick up abstract subprogram DIE.
SPDie = new DIE(dwarf::DW_TAG_subprogram);
- // If AbsSPDIE belongs to a different CU, use DW_FORM_ref_addr instead of
- // DW_FORM_ref4.
- SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
- InSameCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
- AbsSPDIE);
+ SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
SPCU->addDie(SPDie);
} else {
DISubprogram SPDecl = SP.getFunctionDeclaration();
// function then gdb prefers the definition at top level and but does not
// expect specification DIE in parent function. So avoid creating
// specification DIE for a function defined inside a function.
- if (SP.isDefinition() && !SP.getContext().isCompileUnit() &&
- !SP.getContext().isFile() &&
- !isSubprogramContext(SP.getContext())) {
+ DIScope SPContext = resolve(SP.getContext());
+ if (SP.isDefinition() && !SPContext.isCompileUnit() &&
+ !SPContext.isFile() &&
+ !isSubprogramContext(SPContext)) {
SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
// Add arguments.
if (ATy.isArtificial())
SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
if (ATy.isObjectPointer())
- SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
- dwarf::DW_FORM_ref4, Arg);
+ SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
SPDie->addChild(Arg);
}
DIE *SPDeclDie = SPDie;
SPDie = new DIE(dwarf::DW_TAG_subprogram);
- SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
- dwarf::DW_FORM_ref4, SPDeclDie);
+ SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
SPCU->addDie(SPDie);
}
}
return NULL;
DIScope DS(Scope->getScopeNode());
DISubprogram InlinedSP = getDISubprogram(DS);
- DIE *OriginDIE = getSPDIE(InlinedSP);
+ 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,
- dwarf::DW_FORM_ref4, OriginDIE);
+ TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
if (Ranges.size() > 1) {
// .debug_range section has not been laid out yet. Emit offset in
else if (DS.isSubprogram()) {
ProcessedSPNodes.insert(DS);
if (Scope->isAbstractScope()) {
- ScopeDIE = getSPDIE(DS);
+ ScopeDIE = TheCU->getDIE(DS);
// Note down abstract DIE.
if (ScopeDIE)
AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
ScopeDIE->addChild(*I);
if (DS.isSubprogram() && ObjectPointer != NULL)
- TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
- dwarf::DW_FORM_ref4, ObjectPointer);
+ TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
if (DS.isSubprogram())
TheCU->addPubTypes(DISubprogram(DS));
StringRef DirName, unsigned CUID) {
// If we use .loc in assembly, we can't separate .file entries according to
// compile units. Thus all files will belong to the default compile unit.
- if (Asm->TM.hasMCUseLoc() &&
- Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer)
+
+ // FIXME: add a better feature test than hasRawTextSupport. Even better,
+ // extend .file to support this.
+ if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
CUID = 0;
// If FE did not provide a file name, then assume stdin.
// Use a single line table if we are using .loc and generating assembly.
bool UseTheFirstCU =
- (Asm->TM.hasMCUseLoc() &&
- Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer) ||
- (NewCU->getUniqueID() == 0);
+ (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
+ (NewCU->getUniqueID() == 0);
if (!useSplitDwarf()) {
// DW_AT_stmt_list is a offset of line number information for this
NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
- Asm->GetTempSymbol("gnu_pubnames",
+ Asm->GetTempSymbol("gnu_pubtypes",
NewCU->getUniqueID()),
DwarfGnuPubTypesSectionSym);
}
}
// Construct subprogram DIE.
-void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
- const MDNode *N) {
+void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
+ // FIXME: We should only call this routine once, however, during LTO if a
+ // program is defined in multiple CUs we could end up calling it out of
+ // beginModule as we walk the CUs.
+
CompileUnit *&CURef = SPMap[N];
if (CURef)
return;
DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
- // Add to map.
- TheCU->insertDIE(N, SubprogramDie);
-
- // Add to context owner.
- TheCU->addToContextOwner(SubprogramDie, SP.getContext());
-
// Expose as a global name.
- TheCU->addGlobalName(SP.getName(), SubprogramDie);
+ TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
}
void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
TheCU->getUniqueID());
TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, 0, FileID);
TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, 0, Module.getLineNumber());
- TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, dwarf::DW_FORM_ref4,
- EntityDie);
+ TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
StringRef Name = Module.getName();
if (!Name.empty())
TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
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;
+ if (ProcessedSPNodes.count(SP) != 0)
+ continue;
+ if (!SP.isSubprogram())
+ continue;
+ if (!SP.isDefinition())
+ continue;
DIArray Variables = SP.getVariables();
- if (Variables.getNumElements() == 0) continue;
+ if (Variables.getNumElements() == 0)
+ continue;
LexicalScope *Scope =
- new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
+ new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
DeadFnScopeMap[SP] = Scope;
// Construct subprogram DIE and add variables DIEs.
CompileUnit *SPCU = CUMap.lookup(TheCU);
assert(SPCU && "Unable to find Compile Unit!");
- constructSubprogramDIE(SPCU, SP);
- DIE *ScopeDIE = getSPDIE(SP);
+ DIE *SPDIE = SPCU->getDIE(SP);
+ assert(SPDIE && "Subprogram wasn't created?");
for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
DIVariable DV(Variables.getElement(vi));
- if (!DV.isVariable()) continue;
+ if (!DV.isVariable())
+ continue;
DbgVariable NewVar(DV, NULL, this);
if (DIE *VariableDIE =
- SPCU->constructVariableDIE(&NewVar, Scope->isAbstractScope()))
- ScopeDIE->addChild(VariableDIE);
+ SPCU->constructVariableDIE(&NewVar, Scope->isAbstractScope()))
+ SPDIE->addChild(VariableDIE);
}
}
}
Hash.computeDIEODRSignature(Die));
}
- // Process the worklist to add attributes with the correct form (ref_addr or
- // ref4).
- for (unsigned I = 0, E = DIEEntryWorklist.size(); I < E; I++) {
- addDIEEntry(DIEEntryWorklist[I].Die, DIEEntryWorklist[I].Attribute,
- dwarf::DW_FORM_ref4, DIEEntryWorklist[I].Entry);
- assert(E == DIEEntryWorklist.size() &&
- "We should not add to the worklist during finalization.");
- }
-
// Handle anything that needs to be done on a per-cu basis.
for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
CUE = CUMap.end();
}
}
- // Add terminating symbols for each section.
+ // Build a list of sections used.
+ std::vector<const MCSection *> Sections;
for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
it++) {
const MCSection *Section = it->first;
+ Sections.push_back(Section);
+ }
+
+ // Sort the sections into order.
+ // This is only done to ensure consistent output order across different runs.
+ std::sort(Sections.begin(), Sections.end(), SectionSort);
+
+ // Add terminating symbols for each section.
+ for (unsigned ID=0;ID<Sections.size();ID++) {
+ const MCSection *Section = Sections[ID];
MCSymbol *Sym = NULL;
if (Section) {
- Sym = Asm->GetTempSymbol(Section->getLabelEndName());
+ // We can't call MCSection::getLabelEndName, as it's only safe to do so
+ // if we know the section name up-front. For user-created sections, the resulting
+ // label may not be valid to use as a label. (section names can use a greater
+ // set of characters on some systems)
+ Sym = Asm->GetTempSymbol("debug_end", ID);
Asm->OutStreamer.SwitchSection(Section);
Asm->OutStreamer.EmitLabel(Sym);
}
LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
assert(TheCU && "Unable to find compile unit!");
- if (Asm->TM.hasMCUseLoc() &&
- Asm->OutStreamer.getKind() == MCStreamer::SK_AsmStreamer)
+ if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
// Use a single line table if we are using .loc and generating assembly.
Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
else
// Compute the size and offset of all the DIEs.
void DwarfUnits::computeSizeAndOffsets() {
// Offset from the beginning of debug info section.
- unsigned SecOffset = 0;
for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
E = CUs.end(); I != E; ++I) {
- (*I)->setDebugInfoOffset(SecOffset);
unsigned Offset =
sizeof(int32_t) + // Length of Compilation Unit Info
sizeof(int16_t) + // DWARF version number
sizeof(int32_t) + // Offset Into Abbrev. Section
sizeof(int8_t); // Pointer Size (in bytes)
-
- unsigned EndOffset = computeSizeAndOffset((*I)->getCUDie(), Offset);
- SecOffset += EndOffset;
+ computeSizeAndOffset((*I)->getCUDie(), Offset);
}
}
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_containing_type: {
+ case dwarf::DW_AT_abstract_origin: {
DIEEntry *E = cast<DIEEntry>(Values[i]);
DIE *Origin = E->getEntry();
unsigned Addr = Origin->getOffset();
- if (Form == dwarf::DW_FORM_ref_addr) {
- // For DW_FORM_ref_addr, output the offset from beginning of debug info
- // section. Origin->getOffset() returns the offset from start of the
- // compile unit.
- DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
- Addr += Holder.getCUOffset(Origin->getCompileUnit());
- }
- Asm->OutStreamer.EmitIntValue(Addr,
- Form == dwarf::DW_FORM_ref_addr ? DIEEntry::getRefAddrSize(Asm) : 4);
+ Asm->EmitInt32(Addr);
break;
}
case dwarf::DW_AT_ranges: {
}
}
-/// For a given compile unit DIE, returns offset from beginning of debug info.
-unsigned DwarfUnits::getCUOffset(DIE *Die) {
- assert(Die->getTag() == dwarf::DW_TAG_compile_unit &&
- "Input DIE should be compile unit in getCUOffset.");
- for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(), E = CUs.end();
- I != E; ++I) {
- CompileUnit *TheCU = *I;
- if (TheCU->getCUDie() == Die)
- return TheCU->getDebugInfoOffset();
- }
- llvm_unreachable("The compile unit DIE should belong to CUs in DwarfUnits.");
-}
-
// Emit the debug info section.
void DwarfDebug::emitDebugInfo() {
DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
/// computeIndexValue - Compute the gdb index value for the DIE and CU.
static dwarf::PubIndexEntryDescriptor computeIndexValue(CompileUnit *CU,
DIE *Die) {
- dwarf::GDBIndexEntryLinkage Linkage =
- Die->findAttribute(dwarf::DW_AT_external) ? dwarf::GIEL_EXTERNAL
- : dwarf::GIEL_STATIC;
+ dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
+
+ // We could have a specification DIE that has our most of our knowledge,
+ // look for that now.
+ 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:
CompileUnit *TheCU = I->second;
unsigned ID = TheCU->getUniqueID();
- if (TheCU->getGlobalNames().empty())
- continue;
-
// Start the dwarf pubnames section.
Asm->OutStreamer.SwitchSection(PSec);
}
};
-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 bool CUSort(const CompileUnit *A, const CompileUnit *B) {
return (A->getUniqueID() < B->getUniqueID());
}
Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
else
NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
- Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
+ Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
DwarfGnuPubTypesSectionSym);
}
InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
OffSec, StrSym);
}
-
-/// When we don't know whether the correct form is ref4 or ref_addr, we create
-/// a worklist item and insert it to DIEEntryWorklist.
-void DwarfDebug::addDIEEntry(DIE *Die, uint16_t Attribute, uint16_t Form,
- DIEEntry *Entry) {
- /// Early exit when we only have a single CU.
- if (GlobalCUIndexCount == 1 || Form != dwarf::DW_FORM_ref4) {
- Die->addValue(Attribute, Form, Entry);
- return;
- }
- DIE *DieCU = Die->checkCompileUnit();
- DIE *EntryCU = Entry->getEntry()->checkCompileUnit();
- if (!DieCU || !EntryCU) {
- // Die or Entry is not added to an owner yet.
- insertDIEEntryWorklist(Die, Attribute, Entry);
- return;
- }
- Die->addValue(Attribute,
- EntryCU == DieCU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
- Entry);
-}