From: Frederic Riss Date: Fri, 7 Aug 2015 15:14:08 +0000 (+0000) Subject: [MC/Dwarf] Allow to specify custom parameters for linetable emission. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=796478f4f5379df5806eab4a8307e8aa8965d565;p=oota-llvm.git [MC/Dwarf] Allow to specify custom parameters for linetable emission. NFC patch for current users, but llvm-dsymutil will use the new functionality to adapt to the input linetable. Based on a patch by Adrian Prantl. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244318 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index 0642af837e7..66b5cb9addb 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -18,6 +18,7 @@ #include "llvm/ADT/ilist_node.h" #include "llvm/ADT/iterator.h" #include "llvm/MC/MCDirectives.h" +#include "llvm/MC/MCDwarf.h" #include "llvm/MC/MCFixup.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCLinkerOptimizationHint.h" @@ -591,6 +592,8 @@ private: /// List of declared file names std::vector FileNames; + MCDwarfLineTableParams LTParams; + /// The set of function symbols for which a .thumb_func directive has /// been seen. // @@ -736,6 +739,9 @@ public: MCObjectWriter &getWriter() const { return Writer; } + MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; } + void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; } + /// Finish - Do final processing and write the object to the output stream. /// \p Writer is used for custom object writer (as the MCJIT does), /// if not specified it is automatically created from backend. diff --git a/include/llvm/MC/MCDwarf.h b/include/llvm/MC/MCDwarf.h index 1e72dfee4ad..3e9d84344b9 100644 --- a/include/llvm/MC/MCDwarf.h +++ b/include/llvm/MC/MCDwarf.h @@ -182,6 +182,19 @@ public: } }; +struct MCDwarfLineTableParams { + /// First special line opcode - leave room for the standard opcodes. + /// Note: If you want to change this, you'll have to update the + /// "StandardOpcodeLengths" table that is emitted in + /// \c Emit(). + unsigned char DWARF2LineOpcodeBase = 13; + /// Minimum line offset in a special line info. opcode. The value + /// -5 was chosen to give a reasonable range of values. + char DWARF2LineBase = -5; + /// Range of line offsets in a special line info. opcode. + unsigned char DWARF2LineRange = 14; +}; + struct MCDwarfLineTableHeader { MCSymbol *Label; SmallVector MCDwarfDirs; @@ -192,9 +205,11 @@ struct MCDwarfLineTableHeader { MCDwarfLineTableHeader() : Label(nullptr) {} unsigned getFile(StringRef &Directory, StringRef &FileName, unsigned FileNumber = 0); - std::pair Emit(MCStreamer *MCOS) const; + std::pair Emit(MCStreamer *MCOS, + MCDwarfLineTableParams Params) const; std::pair - Emit(MCStreamer *MCOS, ArrayRef SpecialOpcodeLengths) const; + Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, + ArrayRef SpecialOpcodeLengths) const; }; class MCDwarfDwoLineTable { @@ -206,7 +221,7 @@ public: unsigned getFile(StringRef Directory, StringRef FileName) { return Header.getFile(Directory, FileName); } - void Emit(MCStreamer &MCOS) const; + void Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params) const; }; class MCDwarfLineTable { @@ -215,10 +230,10 @@ class MCDwarfLineTable { public: // This emits the Dwarf file and the line tables for all Compile Units. - static void Emit(MCObjectStreamer *MCOS); + static void Emit(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params); // This emits the Dwarf file and the line tables for a given Compile Unit. - void EmitCU(MCObjectStreamer *MCOS) const; + void EmitCU(MCObjectStreamer *MCOS, MCDwarfLineTableParams Params) const; unsigned getFile(StringRef &Directory, StringRef &FileName, unsigned FileNumber = 0); @@ -262,11 +277,12 @@ public: class MCDwarfLineAddr { public: /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. - static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta, - raw_ostream &OS); + static void Encode(MCContext &Context, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS); /// Utility function to emit the encoding to a streamer. - static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta); + static void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta); }; class MCGenDwarfInfo { diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index e731542d419..e09a90fbd0b 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -33,6 +33,7 @@ #include "llvm/IR/Module.h" #include "llvm/IR/ValueHandle.h" #include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCDwarf.h" #include "llvm/MC/MCSection.h" #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSymbol.h" @@ -1894,7 +1895,7 @@ void DwarfDebug::emitDebugLineDWO() { assert(useSplitDwarf() && "No split dwarf?"); Asm->OutStreamer->SwitchSection( Asm->getObjFileLowering().getDwarfLineDWOSection()); - SplitTypeUnitFileTable.Emit(*Asm->OutStreamer); + SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams()); } // Emit the .debug_str.dwo section for separated dwarf. This contains the diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index ffb1479444c..38e126191c1 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -1026,7 +1026,8 @@ bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout, SmallString<8> &Data = DF.getContents(); Data.clear(); raw_svector_ostream OSE(Data); - MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OSE); + MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta, + AddrDelta, OSE); OSE.flush(); return OldSize != Data.size(); } diff --git a/lib/MC/MCDwarf.cpp b/lib/MC/MCDwarf.cpp index c84c4865f51..cfe58dae343 100644 --- a/lib/MC/MCDwarf.cpp +++ b/lib/MC/MCDwarf.cpp @@ -29,25 +29,6 @@ #include "llvm/Support/raw_ostream.h" using namespace llvm; -// Given a special op, return the address skip amount (in units of -// DWARF2_LINE_MIN_INSN_LENGTH. -#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE) - -// The maximum address skip amount that can be encoded with a special op. -#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255) - -// First special line opcode - leave room for the standard opcodes. -// Note: If you want to change this, you'll have to update the -// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit(). -#define DWARF2_LINE_OPCODE_BASE 13 - -// Minimum line offset in a special line info. opcode. This value -// was chosen to give a reasonable range of values. -#define DWARF2_LINE_BASE -5 - -// Range of line offsets in a special line info. opcode. -#define DWARF2_LINE_RANGE 14 - static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) { unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment(); if (MinInsnLength == 1) @@ -197,7 +178,8 @@ EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section, // // This emits the Dwarf file and the line tables. // -void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) { +void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS, + MCDwarfLineTableParams Params) { MCContext &context = MCOS->getContext(); auto &LineTables = context.getMCDwarfLineTables(); @@ -212,14 +194,17 @@ void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) { // Handle the rest of the Compile Units. for (const auto &CUIDTablePair : LineTables) - CUIDTablePair.second.EmitCU(MCOS); + CUIDTablePair.second.EmitCU(MCOS, Params); } -void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS) const { - MCOS.EmitLabel(Header.Emit(&MCOS, None).second); +void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, + MCDwarfLineTableParams Params) const { + MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second); } -std::pair MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const { +std::pair +MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, + MCDwarfLineTableParams Params) const { static const char StandardOpcodeLengths[] = { 0, // length of DW_LNS_copy 1, // length of DW_LNS_advance_pc @@ -234,9 +219,10 @@ std::pair MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) 0, // length of DW_LNS_set_epilogue_begin 1 // DW_LNS_set_isa }; - assert(array_lengthof(StandardOpcodeLengths) == - (DWARF2_LINE_OPCODE_BASE - 1)); - return Emit(MCOS, StandardOpcodeLengths); + assert(array_lengthof(StandardOpcodeLengths) >= + (Params.DWARF2LineOpcodeBase - 1)); + return Emit(MCOS, Params, ArrayRef(StandardOpcodeLengths, + Params.DWARF2LineOpcodeBase - 1)); } static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) { @@ -256,7 +242,7 @@ static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) { } std::pair -MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, +MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, ArrayRef StandardOpcodeLengths) const { MCContext &context = MCOS->getContext(); @@ -293,8 +279,8 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, // Parameters of the state machine, are next. MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1); MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1); - MCOS->EmitIntValue(DWARF2_LINE_BASE, 1); - MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1); + MCOS->EmitIntValue(Params.DWARF2LineBase, 1); + MCOS->EmitIntValue(Params.DWARF2LineRange, 1); MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1); // Standard opcode lengths @@ -329,8 +315,9 @@ MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, return std::make_pair(LineStartSym, LineEndSym); } -void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS) const { - MCSymbol *LineEndSym = Header.Emit(MCOS).second; +void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS, + MCDwarfLineTableParams Params) const { + MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second; // Put out the line tables. for (const auto &LineSec : MCLineSections.getMCLineEntries()) @@ -416,21 +403,31 @@ unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory, } /// Utility function to emit the encoding to a streamer. -void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta, - uint64_t AddrDelta) { +void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta) { MCContext &Context = MCOS->getContext(); SmallString<256> Tmp; raw_svector_ostream OS(Tmp); - MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS); + MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS); MCOS->EmitBytes(OS.str()); } +/// Given a special op, return the address skip amount (in units of +/// DWARF2_LINE_MIN_INSN_LENGTH). +static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) { + return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange; +} + /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas. -void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, - uint64_t AddrDelta, raw_ostream &OS) { +void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params, + int64_t LineDelta, uint64_t AddrDelta, + raw_ostream &OS) { uint64_t Temp, Opcode; bool NeedCopy = false; + // The maximum address skip amount that can be encoded with a special op. + uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255); + // Scale the address delta by the minimum instruction length. AddrDelta = ScaleAddrDelta(Context, AddrDelta); @@ -438,7 +435,7 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the // end_sequence to emit the matrix entry. if (LineDelta == INT64_MAX) { - if (AddrDelta == MAX_SPECIAL_ADDR_DELTA) + if (AddrDelta == MaxSpecialAddrDelta) OS << char(dwarf::DW_LNS_const_add_pc); else if (AddrDelta) { OS << char(dwarf::DW_LNS_advance_pc); @@ -451,16 +448,16 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, } // Bias the line delta by the base. - Temp = LineDelta - DWARF2_LINE_BASE; + Temp = LineDelta - Params.DWARF2LineBase; // If the line increment is out of range of a special opcode, we must encode // it with DW_LNS_advance_line. - if (Temp >= DWARF2_LINE_RANGE) { + if (Temp >= Params.DWARF2LineRange) { OS << char(dwarf::DW_LNS_advance_line); encodeSLEB128(LineDelta, OS); LineDelta = 0; - Temp = 0 - DWARF2_LINE_BASE; + Temp = 0 - Params.DWARF2LineBase; NeedCopy = true; } @@ -471,19 +468,19 @@ void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta, } // Bias the opcode by the special opcode base. - Temp += DWARF2_LINE_OPCODE_BASE; + Temp += Params.DWARF2LineOpcodeBase; // Avoid overflow when addr_delta is large. - if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) { + if (AddrDelta < 256 + MaxSpecialAddrDelta) { // Try using a special opcode. - Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE; + Opcode = Temp + AddrDelta * Params.DWARF2LineRange; if (Opcode <= 255) { OS << char(Opcode); return; } // Try using DW_LNS_const_add_pc followed by special op. - Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE; + Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange; if (Opcode <= 255) { OS << char(dwarf::DW_LNS_const_add_pc); OS << char(Opcode); diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 0a637775d4e..3906464ed8b 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -321,8 +321,10 @@ static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, return AddrDelta; } -static void emitDwarfSetLineAddr(MCObjectStreamer &OS, int64_t LineDelta, - const MCSymbol *Label, int PointerSize) { +static void emitDwarfSetLineAddr(MCObjectStreamer &OS, + MCDwarfLineTableParams Params, + int64_t LineDelta, const MCSymbol *Label, + int PointerSize) { // emit the sequence to set the address OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1); OS.EmitULEB128IntValue(PointerSize + 1); @@ -330,7 +332,7 @@ static void emitDwarfSetLineAddr(MCObjectStreamer &OS, int64_t LineDelta, OS.EmitSymbolValue(Label, PointerSize); // emit the sequence for the LineDelta (from 1) and a zero address delta. - MCDwarfLineAddr::Emit(&OS, LineDelta, 0); + MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0); } void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, @@ -338,13 +340,15 @@ void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *Label, unsigned PointerSize) { if (!LastLabel) { - emitDwarfSetLineAddr(*this, LineDelta, Label, PointerSize); + emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta, + Label, PointerSize); return; } const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel); int64_t Res; if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) { - MCDwarfLineAddr::Emit(this, LineDelta, Res); + MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta, + Res); return; } insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta)); @@ -451,7 +455,7 @@ void MCObjectStreamer::FinishImpl() { MCGenDwarfInfo::Emit(this); // Dump out the dwarf file & directory tables and line tables. - MCDwarfLineTable::Emit(this); + MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams()); flushPendingLabels(nullptr); getAssembler().Finish();