Index: include/llvm/MC/MCELFStreamer.h =================================================================== --- include/llvm/MC/MCELFStreamer.h +++ include/llvm/MC/MCELFStreamer.h @@ -85,6 +85,8 @@ virtual void EmitValueToAlignment(unsigned, int64_t, unsigned, unsigned); + virtual void Flush(); + virtual void FinishImpl(); /// @} Index: include/llvm/MC/MCStreamer.h =================================================================== --- include/llvm/MC/MCStreamer.h +++ include/llvm/MC/MCStreamer.h @@ -86,6 +86,8 @@ MCSymbol *LastSymbol; + DenseMap SymbolOrdering; + /// SectionStack - This is stack of current and previous section /// values saved by PushSection. SmallVector, 4> SectionStack; @@ -185,6 +187,10 @@ return MCSectionSubPair(); } + /// GetSymbolOrder - Returns an index to represent the order + /// a symbol was emitted in. + unsigned GetSymbolOrder(const MCSymbol *Sym) const { return SymbolOrdering.lookup(Sym); } + /// ChangeSection - Update streamer for a new active section. /// /// This is called by PopSection and SwitchSection, if the current @@ -264,6 +270,13 @@ /// InitToTextSection - Create a text section and switch the streamer to it. virtual void InitToTextSection() = 0; + /// AssignSection - Sets the symbol's section. + /// + /// Each emitted symbol will be tracked in the ordering table, + /// so we can sort on them later. + void AssignSection(MCSymbol *Symbol, const MCSection *Section); + + /// EmitLabel - Emit a label for @p Symbol into the current section. /// /// This corresponds to an assembler statement such as: @@ -620,6 +633,9 @@ /// these methods there. virtual void EmitTCEntry(const MCSymbol &S); + /// Flush - Causes any cached state to be written out. + virtual void Flush() {} + /// FinishImpl - Streamer specific finalization. virtual void FinishImpl() = 0; /// Finish - Finish emission of machine code. Index: include/llvm/Support/Dwarf.h =================================================================== --- include/llvm/Support/Dwarf.h +++ include/llvm/Support/Dwarf.h @@ -58,7 +58,8 @@ DWARF_VERSION = 4, // Default dwarf version we output. DW_CIE_VERSION = 1, // Common frame information version. DW_PUBTYPES_VERSION = 2, // Section version number for .debug_pubtypes. - DW_PUBNAMES_VERSION = 2 // Section version number for .debug_pubnames. + DW_PUBNAMES_VERSION = 2, // Section version number for .debug_pubnames. + DW_ARANGES_VERSION = 2 // Section version number for .debug_aranges. }; // Special ID values that distinguish a CIE from a FDE in DWARF CFI. Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -877,6 +877,9 @@ if (!ModuleFlags.empty()) getObjFileLowering().emitModuleFlags(OutStreamer, ModuleFlags, Mang, TM); + // Make sure we wrote out everything we need. + OutStreamer.Flush(); + // Finalize debug and EH information. if (DE) { { Index: lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp @@ -181,6 +181,12 @@ const MCSymbol *Label) { DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); Die->addValue(Attribute, Form, Value); + + SymbolCU Entry; + Entry.CU = this; + Entry.Sym = Label; + + DD->addLabel(Entry); } /// addLabelAddress - Add a dwarf label attribute data and value using @@ -188,6 +194,15 @@ /// void CompileUnit::addLabelAddress(DIE *Die, uint16_t Attribute, MCSymbol *Label) { + if (Label) + { + SymbolCU Entry; + Entry.CU = this; + Entry.Sym = Label; + + DD->addLabel(Entry); + } + if (!DD->useSplitDwarf()) { if (Label != NULL) { DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); Index: lib/CodeGen/AsmPrinter/DwarfDebug.h =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.h +++ lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -300,6 +300,12 @@ unsigned getCUOffset(DIE *Die); }; +struct SymbolCU +{ + const MCSymbol *Sym; + CompileUnit *CU; +}; + /// \brief Collects and handles dwarf debug information. class DwarfDebug { // Target of Dwarf emission. @@ -332,8 +338,12 @@ // separated by a zero byte, mapped to a unique id. StringMap SourceIdMap; + // List of all labels used in the output. + std::vector Labels; + // Provides a unique id per text section. - SetVector SectionMap; + typedef DenseMap > SectionMapType; + SectionMapType SectionMap; // List of arguments for current function. SmallVector CurrentFnArguments; @@ -657,6 +667,9 @@ /// type units. void addTypeUnitType(DIE *Die) { TypeUnits.push_back(Die); } + /// \brief Add a label so that arange data can be generated for it. + void addLabel(SymbolCU SCU) { Labels.push_back(SCU); } + /// \brief Look up the source id with the given directory and source file /// names. If none currently exists, create a new id and insert it in the /// SourceIds map. Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -872,7 +872,7 @@ MMI->setDebugInfoAvailability(true); // Prime section data. - SectionMap.insert(Asm->getObjFileLowering().getTextSection()); + SectionMap[Asm->getObjFileLowering().getTextSection()]; } // Attach DW_AT_inline attribute with inlined subprogram DIEs. @@ -1031,10 +1031,47 @@ Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getDataSection()); Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("data_end")); - // End text sections. - for (unsigned I = 0, E = SectionMap.size(); I != E; ++I) { - Asm->OutStreamer.SwitchSection(SectionMap[I]); - Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("section_end", I+1)); + // Filter labels by section. + for (size_t n=0;nisInSection()) + { + // Make a note of this symbol and it's section. + const MCSection *Section = &SCU.Sym->getSection(); + if (!Section->getKind().isMetadata()) + SectionMap[Section].push_back(SCU); + } + else + { + // Some symbols (e.g. common/bss on mach-o) can have no section but still + // appear in the output. This sucks as we rely on sections to build + // arange spans. We can do it without, but it's icky. + SectionMap[NULL].push_back(SCU); + } + } + + // Add terminating symbols for each section. + unsigned I = 1; + for (SectionMapType::iterator it = SectionMap.begin(); + it != SectionMap.end(); + it++,I++) + { + const MCSection *Section = it->first; + MCSymbol *Sym = NULL; + + if (Section) + { + Sym = Asm->GetTempSymbol("section_end", I); + Asm->OutStreamer.SwitchSection(Section); + Asm->OutStreamer.EmitLabel(Sym); + } + + // Insert a final terminator. + SymbolCU Entry; + Entry.CU = NULL; + Entry.Sym = Sym; + SectionMap[Section].push_back(Entry); } } @@ -2533,11 +2570,134 @@ } } -// Emit visible names into a debug aranges section. +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; + + // Symbols with no order assigned should be placed at the end. + // (e.g. section end labels) + if (IA == 0) IA = (unsigned)(-1); + if (IB == 0) IB = (unsigned)(-1); + return IA < IB; + } +}; + +struct ArangeSpan +{ + const MCSymbol *Start, *End; +}; + +// Emit a debug aranges section, containing a CU lookup for any +// address we can tie back to a CU. void DwarfDebug::emitDebugARanges() { // Start the dwarf aranges section. Asm->OutStreamer .SwitchSection(Asm->getObjFileLowering().getDwarfARangesSection()); + + typedef DenseMap > SpansType; + + SpansType Spans; + + // Build a set of address spans, sorted by CU. + for (SectionMapType::iterator it = SectionMap.begin(); + it != SectionMap.end(); + it++) + { + const MCSection *Section = it->first; + SmallVector &List = it->second; + if (List.size() < 2) + continue; + + // Sort the symbols by offset within the section. + SymbolCUSorter sorter(Asm->OutStreamer); + std::sort(List.begin(), List.end(), sorter); + + // Build spans between each label. + const MCSymbol *StartSym = List[0].Sym; + for (size_t n=1;ngetObjFileLowering().getDwarfInfoSection(); + unsigned PtrSize = Asm->getDataLayout().getPointerSize(); + + // Emit an arange table for each CU we used. + for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) + { + CompileUnit *CU = it->first; + std::vector &List = it->second; + + // Emit size of content not including length itself + unsigned ContentSize + = sizeof(int16_t) // DWARF ARange version number + + sizeof(int32_t) // Offset of CU in the .debug_info section + + sizeof(int8_t) // Pointer Size (in bytes) + + sizeof(int8_t); // Segment Size (in bytes) + + unsigned TupleSize = PtrSize * 2; + + // 7.20 in the Dwarf specs requires the table to be aligned to a tuple. + unsigned Padding = 0; + while(((sizeof(int32_t)+ContentSize+Padding) % TupleSize) != 0) + Padding++; + + ContentSize += Padding; + ContentSize += (List.size()+1) * TupleSize; + + // For each compile unit, write the list of spans it covers. + 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;nEmitInt8(0xff); + + for (unsigned n=0;nEmitLabelReference(Span.Start, PtrSize); + + // Calculate the size as being from the span start to it's end. + // If we have no valid end symbol, then we just cover the first byte. + // (this sucks, but I can't seem to figure out how to get the size) + if (Span.End) + Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize); + else + Asm->EmitLabelPlusOffset(Span.Start, 1, PtrSize, false); + } + + Asm->OutStreamer.EmitIntValue(0, PtrSize); + Asm->OutStreamer.EmitIntValue(0, PtrSize); + } } // Emit visible names into a debug ranges section. Index: lib/MC/MCAsmStreamer.cpp =================================================================== --- lib/MC/MCAsmStreamer.cpp +++ lib/MC/MCAsmStreamer.cpp @@ -533,6 +533,9 @@ void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { + const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); + AssignSection(Symbol, Section); + OS << "\t.comm\t" << *Symbol << ',' << Size; if (ByteAlignment != 0) { if (MAI->getCOMMDirectiveAlignmentIsInBytes()) @@ -549,6 +552,9 @@ /// @param Size - The size of the common symbol. void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlign) { + const MCSection *Section = getContext().getObjectFileInfo()->getBSSSection(); + AssignSection(Symbol, Section); + OS << "\t.lcomm\t" << *Symbol << ',' << Size; if (ByteAlign > 1) { switch (MAI->getLCOMMDirectiveAlignmentType()) { @@ -568,6 +574,9 @@ void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { + if (Symbol) + AssignSection(Symbol, Section); + // Note: a .zerofill directive does not switch sections. OS << ".zerofill "; @@ -588,6 +597,8 @@ // e.g. _a. void MCAsmStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) { + AssignSection(Symbol, Section); + assert(Symbol != NULL && "Symbol shouldn't be NULL!"); // Instead of using the Section we'll just use the shortcut. // This is a mach-o specific directive and section. Index: lib/MC/MCELFStreamer.cpp =================================================================== --- lib/MC/MCELFStreamer.cpp +++ lib/MC/MCELFStreamer.cpp @@ -272,7 +272,8 @@ ELF::SHF_WRITE | ELF::SHF_ALLOC, SectionKind::getBSS()); - Symbol->setSection(*Section); + + AssignSection(Symbol, Section); struct LocalCommon L = {&SD, Size, ByteAlignment}; LocalCommons.push_back(L); @@ -527,9 +528,7 @@ SD->setBundleLockState(MCSectionData::NotBundleLocked); } -void MCELFStreamer::FinishImpl() { - EmitFrames(NULL, true); - +void MCELFStreamer::Flush() { for (std::vector::const_iterator i = LocalCommons.begin(), e = LocalCommons.end(); i != e; ++i) { @@ -550,8 +549,17 @@ SectData.setAlignment(ByteAlignment); } + LocalCommons.clear(); +} + +void MCELFStreamer::FinishImpl() { + EmitFrames(NULL, true); + + Flush(); + this->MCObjectStreamer::FinishImpl(); } + void MCELFStreamer::EmitTCEntry(const MCSymbol &S) { // Creates a R_PPC64_TOC relocation MCObjectStreamer::EmitSymbolValue(&S, 8); Index: lib/MC/MCMachOStreamer.cpp =================================================================== --- lib/MC/MCMachOStreamer.cpp +++ lib/MC/MCMachOStreamer.cpp @@ -123,7 +123,7 @@ assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); // isSymbolLinkerVisible uses the section. - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); // We have to create a new fragment if this is an atom defining symbol, // fragments cannot span atoms. if (getAssembler().isSymbolLinkerVisible(*Symbol)) @@ -327,6 +327,8 @@ // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself. assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); + AssignSection(Symbol, NULL); + MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); SD.setExternal(true); SD.setCommon(Size, ByteAlignment); @@ -363,7 +365,7 @@ MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); SD.setFragment(F); - Symbol->setSection(*Section); + AssignSection(Symbol, Section); // Update the maximum alignment on the zero fill section if necessary. if (ByteAlignment > SectData.getAlignment()) Index: lib/MC/MCNullStreamer.cpp =================================================================== --- lib/MC/MCNullStreamer.cpp +++ lib/MC/MCNullStreamer.cpp @@ -37,7 +37,7 @@ virtual void EmitLabel(MCSymbol *Symbol) { assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); assert(getCurrentSection().first &&"Cannot emit before setting section!"); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); } virtual void EmitDebugLabel(MCSymbol *Symbol) { EmitLabel(Symbol); Index: lib/MC/MCPureStreamer.cpp =================================================================== --- lib/MC/MCPureStreamer.cpp +++ lib/MC/MCPureStreamer.cpp @@ -121,7 +121,7 @@ assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); assert(getCurrentSection().first && "Cannot emit before setting section!"); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); Index: lib/MC/MCStreamer.cpp =================================================================== --- lib/MC/MCStreamer.cpp +++ lib/MC/MCStreamer.cpp @@ -191,17 +191,27 @@ MCSymbol *EHSymbol) { } +void MCStreamer::AssignSection(MCSymbol *Symbol, const MCSection *Section) +{ + if (Section) + Symbol->setSection(*Section); + else + Symbol->setUndefined(); + + SymbolOrdering[Symbol] = 1 + SymbolOrdering.size(); +} + void MCStreamer::EmitLabel(MCSymbol *Symbol) { assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); assert(getCurrentSection().first && "Cannot emit before setting section!"); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); LastSymbol = Symbol; } void MCStreamer::EmitDebugLabel(MCSymbol *Symbol) { assert(!Symbol->isVariable() && "Cannot emit a variable symbol!"); assert(getCurrentSection().first && "Cannot emit before setting section!"); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); LastSymbol = Symbol; } Index: lib/MC/WinCOFFStreamer.cpp =================================================================== --- lib/MC/WinCOFFStreamer.cpp +++ lib/MC/WinCOFFStreamer.cpp @@ -164,7 +164,7 @@ SymbolData.setExternal(External); - Symbol->setSection(*Section); + AssignSection(Symbol, Section); if (ByteAlignment != 1) new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectionData); Index: lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp =================================================================== --- lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp +++ lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp @@ -129,7 +129,7 @@ MCELF::SetType(SD, ELF::STT_NOTYPE); MCELF::SetBinding(SD, ELF::STB_LOCAL); SD.setExternal(false); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext()); Symbol->setVariableValue(Value); Index: lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp =================================================================== --- lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp +++ lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp @@ -183,7 +183,7 @@ MCELF::SetType(SD, ELF::STT_NOTYPE); MCELF::SetBinding(SD, ELF::STB_LOCAL); SD.setExternal(false); - Symbol->setSection(*getCurrentSection().first); + AssignSection(Symbol, getCurrentSection().first); const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext()); Symbol->setVariableValue(Value);