diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h --- a/llvm/include/llvm/CodeGen/AsmPrinter.h +++ b/llvm/include/llvm/CodeGen/AsmPrinter.h @@ -645,7 +645,7 @@ /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen /// according to the settings. - void emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo, + void emitDwarfUnitLength(MCSymbol *Hi, MCSymbol *Lo, const Twine &Comment) const; /// Emit reference to a call site with a specified encoding diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h --- a/llvm/include/llvm/MC/MCStreamer.h +++ b/llvm/include/llvm/MC/MCStreamer.h @@ -1102,7 +1102,7 @@ /// Emit a unit length field. The actual format, DWARF32 or DWARF64, is chosen /// according to the settings. - virtual void emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo, + virtual void emitDwarfUnitLength(MCSymbol *Hi, MCSymbol *Lo, const Twine &Comment); }; diff --git a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp @@ -368,7 +368,6 @@ AsmPrinter *Asm = Ctx.Asm; Asm->emitDwarfUnitLength(Ctx.ContributionEnd, Ctx.ContributionStart, "Header: unit length"); - Asm->OutStreamer->emitLabel(Ctx.ContributionStart); Asm->OutStreamer->AddComment("Header: version"); Asm->emitInt16(Version); Asm->OutStreamer->AddComment("Header: padding"); diff --git a/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp b/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AddressPool.cpp @@ -30,7 +30,6 @@ MCSymbol *EndLabel = Asm.createTempSymbol(Prefix + "end"); Asm.emitDwarfUnitLength(EndLabel, BeginLabel, "Length of contribution"); - Asm.OutStreamer->emitLabel(BeginLabel); Asm.OutStreamer->AddComment("DWARF version number"); Asm.emitInt16(Asm.getDwarfVersion()); Asm.OutStreamer->AddComment("Address size"); diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp @@ -203,7 +203,7 @@ OutStreamer->emitDwarfUnitLength(Length, Comment); } -void AsmPrinter::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo, +void AsmPrinter::emitDwarfUnitLength(MCSymbol *Hi, MCSymbol *Lo, const Twine &Comment) const { OutStreamer->emitDwarfUnitLength(Hi, Lo, Comment); } diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -2365,8 +2365,6 @@ Asm->emitDwarfUnitLength(EndLabel, BeginLabel, "Length of Public " + Name + " Info"); - Asm->OutStreamer->emitLabel(BeginLabel); - Asm->OutStreamer->AddComment("DWARF Version"); Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION); diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp --- a/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -1680,7 +1680,6 @@ MCSymbol *BeginLabel = Asm->createTempSymbol(Prefix + "start"); EndLabel = Asm->createTempSymbol(Prefix + "end"); Asm->emitDwarfUnitLength(EndLabel, BeginLabel, "Length of Unit"); - Asm->OutStreamer->emitLabel(BeginLabel); } else Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(), "Length of Unit"); diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp --- a/llvm/lib/MC/MCDwarf.cpp +++ b/llvm/lib/MC/MCDwarf.cpp @@ -476,21 +476,10 @@ // Create a symbol for the end of the section (to be set when we get there). MCSymbol *LineEndSym = context.createTempSymbol(); - - unsigned UnitLengthBytes = - dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat()); unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat()); - if (context.getDwarfFormat() == dwarf::DWARF64) - // Emit DWARF64 mark. - MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64); - - // The length field does not include itself and, in case of the 64-bit DWARF - // format, the DWARF64 mark. - emitAbsValue(*MCOS, - makeEndMinusStartExpr(context, *LineStartSym, *LineEndSym, - UnitLengthBytes), - OffsetSize); + MCSymbol *SymWithoutUnitLength = context.createTempSymbol("debug_line_"); + MCOS->emitDwarfUnitLength(LineEndSym, SymWithoutUnitLength, "unit length"); // Next 2 bytes is the Version. unsigned LineTableVersion = context.getDwarfVersion(); @@ -498,7 +487,7 @@ // Keep track of the bytes between the very start and where the header length // comes out. - unsigned PreHeaderLengthBytes = UnitLengthBytes + 2; + unsigned PreHeaderLengthBytes = 2; // In v5, we get address info next. if (LineTableVersion >= 5) { @@ -513,7 +502,7 @@ // Length of the prologue, is the next 4 bytes (8 bytes for DWARF64). This is // actually the length from after the length word, to the end of the prologue. emitAbsValue(*MCOS, - makeEndMinusStartExpr(context, *LineStartSym, *ProEndSym, + makeEndMinusStartExpr(context, *SymWithoutUnitLength, *ProEndSym, (PreHeaderLengthBytes + OffsetSize)), OffsetSize); diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp --- a/llvm/lib/MC/MCStreamer.cpp +++ b/llvm/lib/MC/MCStreamer.cpp @@ -1006,12 +1006,15 @@ emitIntValue(Length, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat())); } -void MCStreamer::emitDwarfUnitLength(const MCSymbol *Hi, const MCSymbol *Lo, +void MCStreamer::emitDwarfUnitLength(MCSymbol *Hi, MCSymbol *Lo, const Twine &Comment) { maybeEmitDwarf64Mark(); AddComment(Comment); emitAbsoluteSymbolDiff( Hi, Lo, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat())); + // emit the begin symbol after we generate the length field. + if (Lo->isUndefined()) + emitLabel(Lo); } void MCStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) { diff --git a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp --- a/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp +++ b/llvm/unittests/CodeGen/AsmPrinterDwarfTest.cpp @@ -328,11 +328,15 @@ Hi = TestPrinter->getCtx().createTempSymbol(); Lo = TestPrinter->getCtx().createTempSymbol(); + + Sec = TestPrinter->getCtx().getELFSection(".tst", ELF::SHT_PROGBITS, 0); + TestPrinter->getMS().SwitchSection(Sec); return true; } MCSymbol *Hi = nullptr; MCSymbol *Lo = nullptr; + MCSection *Sec = nullptr; }; TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF32) {