xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCDwarf.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/MC/MCDwarf.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
120b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h"
130b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
140b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
160b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
170b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
190b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
200b57cec5SDimitry Andric #include "llvm/Config/config.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCObjectStreamer.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
300b57cec5SDimitry Andric #include "llvm/MC/StringTableBuilder.h"
310b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
320b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
330b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h"
340b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
350b57cec5SDimitry Andric #include "llvm/Support/LEB128.h"
360b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
370b57cec5SDimitry Andric #include "llvm/Support/Path.h"
380b57cec5SDimitry Andric #include "llvm/Support/SourceMgr.h"
390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
400b57cec5SDimitry Andric #include <cassert>
410b57cec5SDimitry Andric #include <cstdint>
420b57cec5SDimitry Andric #include <string>
430b57cec5SDimitry Andric #include <utility>
440b57cec5SDimitry Andric #include <vector>
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric using namespace llvm;
470b57cec5SDimitry Andric 
48*5ffd83dbSDimitry Andric MCSymbol *mcdwarf::emitListsTableHeaderStart(MCStreamer &S) {
49*5ffd83dbSDimitry Andric   MCSymbol *Start =
50*5ffd83dbSDimitry Andric       S.getContext().createTempSymbol("debug_list_header_start", true, true);
51*5ffd83dbSDimitry Andric   MCSymbol *End =
52*5ffd83dbSDimitry Andric       S.getContext().createTempSymbol("debug_list_header_end", true, true);
53*5ffd83dbSDimitry Andric   auto DwarfFormat = S.getContext().getDwarfFormat();
54*5ffd83dbSDimitry Andric   if (DwarfFormat == dwarf::DWARF64) {
55*5ffd83dbSDimitry Andric     S.AddComment("DWARF64 mark");
56*5ffd83dbSDimitry Andric     S.emitInt32(dwarf::DW_LENGTH_DWARF64);
57*5ffd83dbSDimitry Andric   }
58*5ffd83dbSDimitry Andric   S.AddComment("Length");
59*5ffd83dbSDimitry Andric   S.emitAbsoluteSymbolDiff(End, Start,
60*5ffd83dbSDimitry Andric                            dwarf::getDwarfOffsetByteSize(DwarfFormat));
61*5ffd83dbSDimitry Andric   S.emitLabel(Start);
62*5ffd83dbSDimitry Andric   S.AddComment("Version");
63*5ffd83dbSDimitry Andric   S.emitInt16(S.getContext().getDwarfVersion());
64*5ffd83dbSDimitry Andric   S.AddComment("Address size");
65*5ffd83dbSDimitry Andric   S.emitInt8(S.getContext().getAsmInfo()->getCodePointerSize());
66*5ffd83dbSDimitry Andric   S.AddComment("Segment selector size");
67*5ffd83dbSDimitry Andric   S.emitInt8(0);
68*5ffd83dbSDimitry Andric   return End;
69*5ffd83dbSDimitry Andric }
70*5ffd83dbSDimitry Andric 
710b57cec5SDimitry Andric /// Manage the .debug_line_str section contents, if we use it.
720b57cec5SDimitry Andric class llvm::MCDwarfLineStr {
730b57cec5SDimitry Andric   MCSymbol *LineStrLabel = nullptr;
740b57cec5SDimitry Andric   StringTableBuilder LineStrings{StringTableBuilder::DWARF};
750b57cec5SDimitry Andric   bool UseRelocs = false;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric public:
780b57cec5SDimitry Andric   /// Construct an instance that can emit .debug_line_str (for use in a normal
790b57cec5SDimitry Andric   /// v5 line table).
800b57cec5SDimitry Andric   explicit MCDwarfLineStr(MCContext &Ctx) {
810b57cec5SDimitry Andric     UseRelocs = Ctx.getAsmInfo()->doesDwarfUseRelocationsAcrossSections();
820b57cec5SDimitry Andric     if (UseRelocs)
830b57cec5SDimitry Andric       LineStrLabel =
840b57cec5SDimitry Andric           Ctx.getObjectFileInfo()->getDwarfLineStrSection()->getBeginSymbol();
850b57cec5SDimitry Andric   }
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   /// Emit a reference to the string.
880b57cec5SDimitry Andric   void emitRef(MCStreamer *MCOS, StringRef Path);
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   /// Emit the .debug_line_str section if appropriate.
910b57cec5SDimitry Andric   void emitSection(MCStreamer *MCOS);
920b57cec5SDimitry Andric };
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
950b57cec5SDimitry Andric   unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
960b57cec5SDimitry Andric   if (MinInsnLength == 1)
970b57cec5SDimitry Andric     return AddrDelta;
980b57cec5SDimitry Andric   if (AddrDelta % MinInsnLength != 0) {
990b57cec5SDimitry Andric     // TODO: report this error, but really only once.
1000b57cec5SDimitry Andric     ;
1010b57cec5SDimitry Andric   }
1020b57cec5SDimitry Andric   return AddrDelta / MinInsnLength;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric //
1060b57cec5SDimitry Andric // This is called when an instruction is assembled into the specified section
1070b57cec5SDimitry Andric // and if there is information from the last .loc directive that has yet to have
1080b57cec5SDimitry Andric // a line entry made for it is made.
1090b57cec5SDimitry Andric //
1100b57cec5SDimitry Andric void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
1110b57cec5SDimitry Andric   if (!MCOS->getContext().getDwarfLocSeen())
1120b57cec5SDimitry Andric     return;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // Create a symbol at in the current section for use in the line entry.
1150b57cec5SDimitry Andric   MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
1160b57cec5SDimitry Andric   // Set the value of the symbol to use for the MCDwarfLineEntry.
117*5ffd83dbSDimitry Andric   MCOS->emitLabel(LineSym);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // Get the current .loc info saved in the context.
1200b57cec5SDimitry Andric   const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   // Create a (local) line entry with the symbol and the current .loc info.
1230b57cec5SDimitry Andric   MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   // clear DwarfLocSeen saying the current .loc info is now used.
1260b57cec5SDimitry Andric   MCOS->getContext().clearDwarfLocSeen();
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   // Add the line entry to this section's entries.
1290b57cec5SDimitry Andric   MCOS->getContext()
1300b57cec5SDimitry Andric       .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
1310b57cec5SDimitry Andric       .getMCLineSections()
1320b57cec5SDimitry Andric       .addLineEntry(LineEntry, Section);
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric //
1360b57cec5SDimitry Andric // This helper routine returns an expression of End - Start + IntVal .
1370b57cec5SDimitry Andric //
138*5ffd83dbSDimitry Andric static inline const MCExpr *makeEndMinusStartExpr(MCContext &Ctx,
1390b57cec5SDimitry Andric                                                   const MCSymbol &Start,
1400b57cec5SDimitry Andric                                                   const MCSymbol &End,
1410b57cec5SDimitry Andric                                                   int IntVal) {
1420b57cec5SDimitry Andric   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
143*5ffd83dbSDimitry Andric   const MCExpr *Res = MCSymbolRefExpr::create(&End, Variant, Ctx);
144*5ffd83dbSDimitry Andric   const MCExpr *RHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
145*5ffd83dbSDimitry Andric   const MCExpr *Res1 = MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, Ctx);
146*5ffd83dbSDimitry Andric   const MCExpr *Res2 = MCConstantExpr::create(IntVal, Ctx);
147*5ffd83dbSDimitry Andric   const MCExpr *Res3 = MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, Ctx);
1480b57cec5SDimitry Andric   return Res3;
1490b57cec5SDimitry Andric }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric //
1520b57cec5SDimitry Andric // This helper routine returns an expression of Start + IntVal .
1530b57cec5SDimitry Andric //
1540b57cec5SDimitry Andric static inline const MCExpr *
1550b57cec5SDimitry Andric makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal) {
1560b57cec5SDimitry Andric   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
1570b57cec5SDimitry Andric   const MCExpr *LHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
1580b57cec5SDimitry Andric   const MCExpr *RHS = MCConstantExpr::create(IntVal, Ctx);
1590b57cec5SDimitry Andric   const MCExpr *Res = MCBinaryExpr::create(MCBinaryExpr::Add, LHS, RHS, Ctx);
1600b57cec5SDimitry Andric   return Res;
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric //
1640b57cec5SDimitry Andric // This emits the Dwarf line table for the specified section from the entries
1650b57cec5SDimitry Andric // in the LineSection.
1660b57cec5SDimitry Andric //
167*5ffd83dbSDimitry Andric static inline void emitDwarfLineTable(
168*5ffd83dbSDimitry Andric     MCObjectStreamer *MCOS, MCSection *Section,
1690b57cec5SDimitry Andric     const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
1700b57cec5SDimitry Andric   unsigned FileNum = 1;
1710b57cec5SDimitry Andric   unsigned LastLine = 1;
1720b57cec5SDimitry Andric   unsigned Column = 0;
1730b57cec5SDimitry Andric   unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
1740b57cec5SDimitry Andric   unsigned Isa = 0;
1750b57cec5SDimitry Andric   unsigned Discriminator = 0;
1760b57cec5SDimitry Andric   MCSymbol *LastLabel = nullptr;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
1790b57cec5SDimitry Andric   for (const MCDwarfLineEntry &LineEntry : LineEntries) {
1800b57cec5SDimitry Andric     int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric     if (FileNum != LineEntry.getFileNum()) {
1830b57cec5SDimitry Andric       FileNum = LineEntry.getFileNum();
184*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_file);
185*5ffd83dbSDimitry Andric       MCOS->emitULEB128IntValue(FileNum);
1860b57cec5SDimitry Andric     }
1870b57cec5SDimitry Andric     if (Column != LineEntry.getColumn()) {
1880b57cec5SDimitry Andric       Column = LineEntry.getColumn();
189*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_column);
190*5ffd83dbSDimitry Andric       MCOS->emitULEB128IntValue(Column);
1910b57cec5SDimitry Andric     }
1920b57cec5SDimitry Andric     if (Discriminator != LineEntry.getDiscriminator() &&
1930b57cec5SDimitry Andric         MCOS->getContext().getDwarfVersion() >= 4) {
1940b57cec5SDimitry Andric       Discriminator = LineEntry.getDiscriminator();
1950b57cec5SDimitry Andric       unsigned Size = getULEB128Size(Discriminator);
196*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_extended_op);
197*5ffd83dbSDimitry Andric       MCOS->emitULEB128IntValue(Size + 1);
198*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNE_set_discriminator);
199*5ffd83dbSDimitry Andric       MCOS->emitULEB128IntValue(Discriminator);
2000b57cec5SDimitry Andric     }
2010b57cec5SDimitry Andric     if (Isa != LineEntry.getIsa()) {
2020b57cec5SDimitry Andric       Isa = LineEntry.getIsa();
203*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_isa);
204*5ffd83dbSDimitry Andric       MCOS->emitULEB128IntValue(Isa);
2050b57cec5SDimitry Andric     }
2060b57cec5SDimitry Andric     if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
2070b57cec5SDimitry Andric       Flags = LineEntry.getFlags();
208*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_negate_stmt);
2090b57cec5SDimitry Andric     }
2100b57cec5SDimitry Andric     if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
211*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_basic_block);
2120b57cec5SDimitry Andric     if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
213*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end);
2140b57cec5SDimitry Andric     if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
215*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin);
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     MCSymbol *Label = LineEntry.getLabel();
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric     // At this point we want to emit/create the sequence to encode the delta in
2200b57cec5SDimitry Andric     // line numbers and the increment of the address from the previous Label
2210b57cec5SDimitry Andric     // and the current Label.
2220b57cec5SDimitry Andric     const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
223*5ffd83dbSDimitry Andric     MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
2240b57cec5SDimitry Andric                                    asmInfo->getCodePointerSize());
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric     Discriminator = 0;
2270b57cec5SDimitry Andric     LastLine = LineEntry.getLine();
2280b57cec5SDimitry Andric     LastLabel = Label;
2290b57cec5SDimitry Andric   }
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric   // Emit a DW_LNE_end_sequence for the end of the section.
2320b57cec5SDimitry Andric   // Use the section end label to compute the address delta and use INT64_MAX
2330b57cec5SDimitry Andric   // as the line delta which is the signal that this is actually a
2340b57cec5SDimitry Andric   // DW_LNE_end_sequence.
2350b57cec5SDimitry Andric   MCSymbol *SectionEnd = MCOS->endSection(Section);
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   // Switch back the dwarf line section, in case endSection had to switch the
2380b57cec5SDimitry Andric   // section.
2390b57cec5SDimitry Andric   MCContext &Ctx = MCOS->getContext();
2400b57cec5SDimitry Andric   MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
243*5ffd83dbSDimitry Andric   MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
2440b57cec5SDimitry Andric                                  AsmInfo->getCodePointerSize());
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric //
2480b57cec5SDimitry Andric // This emits the Dwarf file and the line tables.
2490b57cec5SDimitry Andric //
2500b57cec5SDimitry Andric void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
2510b57cec5SDimitry Andric                             MCDwarfLineTableParams Params) {
2520b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric   auto &LineTables = context.getMCDwarfLineTables();
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   // Bail out early so we don't switch to the debug_line section needlessly and
2570b57cec5SDimitry Andric   // in doing so create an unnecessary (if empty) section.
2580b57cec5SDimitry Andric   if (LineTables.empty())
2590b57cec5SDimitry Andric     return;
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric   // In a v5 non-split line table, put the strings in a separate section.
2620b57cec5SDimitry Andric   Optional<MCDwarfLineStr> LineStr;
2630b57cec5SDimitry Andric   if (context.getDwarfVersion() >= 5)
2640b57cec5SDimitry Andric     LineStr = MCDwarfLineStr(context);
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // Switch to the section where the table will be emitted into.
2670b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   // Handle the rest of the Compile Units.
2700b57cec5SDimitry Andric   for (const auto &CUIDTablePair : LineTables) {
2710b57cec5SDimitry Andric     CUIDTablePair.second.EmitCU(MCOS, Params, LineStr);
2720b57cec5SDimitry Andric   }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   if (LineStr)
2750b57cec5SDimitry Andric     LineStr->emitSection(MCOS);
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params,
2790b57cec5SDimitry Andric                                MCSection *Section) const {
2800b57cec5SDimitry Andric   if (!HasSplitLineTable)
2810b57cec5SDimitry Andric     return;
2820b57cec5SDimitry Andric   Optional<MCDwarfLineStr> NoLineStr(None);
2830b57cec5SDimitry Andric   MCOS.SwitchSection(Section);
284*5ffd83dbSDimitry Andric   MCOS.emitLabel(Header.Emit(&MCOS, Params, None, NoLineStr).second);
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric std::pair<MCSymbol *, MCSymbol *>
2880b57cec5SDimitry Andric MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
2890b57cec5SDimitry Andric                              Optional<MCDwarfLineStr> &LineStr) const {
2900b57cec5SDimitry Andric   static const char StandardOpcodeLengths[] = {
2910b57cec5SDimitry Andric       0, // length of DW_LNS_copy
2920b57cec5SDimitry Andric       1, // length of DW_LNS_advance_pc
2930b57cec5SDimitry Andric       1, // length of DW_LNS_advance_line
2940b57cec5SDimitry Andric       1, // length of DW_LNS_set_file
2950b57cec5SDimitry Andric       1, // length of DW_LNS_set_column
2960b57cec5SDimitry Andric       0, // length of DW_LNS_negate_stmt
2970b57cec5SDimitry Andric       0, // length of DW_LNS_set_basic_block
2980b57cec5SDimitry Andric       0, // length of DW_LNS_const_add_pc
2990b57cec5SDimitry Andric       1, // length of DW_LNS_fixed_advance_pc
3000b57cec5SDimitry Andric       0, // length of DW_LNS_set_prologue_end
3010b57cec5SDimitry Andric       0, // length of DW_LNS_set_epilogue_begin
3020b57cec5SDimitry Andric       1  // DW_LNS_set_isa
3030b57cec5SDimitry Andric   };
3040b57cec5SDimitry Andric   assert(array_lengthof(StandardOpcodeLengths) >=
3050b57cec5SDimitry Andric          (Params.DWARF2LineOpcodeBase - 1U));
3060b57cec5SDimitry Andric   return Emit(
3070b57cec5SDimitry Andric       MCOS, Params,
3080b57cec5SDimitry Andric       makeArrayRef(StandardOpcodeLengths, Params.DWARF2LineOpcodeBase - 1),
3090b57cec5SDimitry Andric       LineStr);
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
3130b57cec5SDimitry Andric   MCContext &Context = OS.getContext();
3140b57cec5SDimitry Andric   assert(!isa<MCSymbolRefExpr>(Expr));
3150b57cec5SDimitry Andric   if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
3160b57cec5SDimitry Andric     return Expr;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   MCSymbol *ABS = Context.createTempSymbol();
319*5ffd83dbSDimitry Andric   OS.emitAssignment(ABS, Expr);
3200b57cec5SDimitry Andric   return MCSymbolRefExpr::create(ABS, Context);
3210b57cec5SDimitry Andric }
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
3240b57cec5SDimitry Andric   const MCExpr *ABS = forceExpAbs(OS, Value);
325*5ffd83dbSDimitry Andric   OS.emitValue(ABS, Size);
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric 
3280b57cec5SDimitry Andric void MCDwarfLineStr::emitSection(MCStreamer *MCOS) {
3290b57cec5SDimitry Andric   // Switch to the .debug_line_str section.
3300b57cec5SDimitry Andric   MCOS->SwitchSection(
3310b57cec5SDimitry Andric       MCOS->getContext().getObjectFileInfo()->getDwarfLineStrSection());
3320b57cec5SDimitry Andric   // Emit the strings without perturbing the offsets we used.
3330b57cec5SDimitry Andric   LineStrings.finalizeInOrder();
3340b57cec5SDimitry Andric   SmallString<0> Data;
3350b57cec5SDimitry Andric   Data.resize(LineStrings.getSize());
3360b57cec5SDimitry Andric   LineStrings.write((uint8_t *)Data.data());
337*5ffd83dbSDimitry Andric   MCOS->emitBinaryData(Data.str());
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric void MCDwarfLineStr::emitRef(MCStreamer *MCOS, StringRef Path) {
341*5ffd83dbSDimitry Andric   int RefSize =
342*5ffd83dbSDimitry Andric       dwarf::getDwarfOffsetByteSize(MCOS->getContext().getDwarfFormat());
3430b57cec5SDimitry Andric   size_t Offset = LineStrings.add(Path);
3440b57cec5SDimitry Andric   if (UseRelocs) {
3450b57cec5SDimitry Andric     MCContext &Ctx = MCOS->getContext();
346*5ffd83dbSDimitry Andric     MCOS->emitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
3470b57cec5SDimitry Andric   } else
348*5ffd83dbSDimitry Andric     MCOS->emitIntValue(Offset, RefSize);
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
3520b57cec5SDimitry Andric   // First the directory table.
3530b57cec5SDimitry Andric   for (auto &Dir : MCDwarfDirs) {
354*5ffd83dbSDimitry Andric     MCOS->emitBytes(Dir);                // The DirectoryName, and...
355*5ffd83dbSDimitry Andric     MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
3560b57cec5SDimitry Andric   }
357*5ffd83dbSDimitry Andric   MCOS->emitInt8(0); // Terminate the directory list.
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   // Second the file table.
3600b57cec5SDimitry Andric   for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
3610b57cec5SDimitry Andric     assert(!MCDwarfFiles[i].Name.empty());
362*5ffd83dbSDimitry Andric     MCOS->emitBytes(MCDwarfFiles[i].Name); // FileName and...
363*5ffd83dbSDimitry Andric     MCOS->emitBytes(StringRef("\0", 1));   // its null terminator.
364*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
365*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // Last modification timestamp (always 0).
366*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // File size (always 0).
3670b57cec5SDimitry Andric   }
368*5ffd83dbSDimitry Andric   MCOS->emitInt8(0); // Terminate the file list.
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
3720b57cec5SDimitry Andric                                bool EmitMD5, bool HasSource,
3730b57cec5SDimitry Andric                                Optional<MCDwarfLineStr> &LineStr) {
3740b57cec5SDimitry Andric   assert(!DwarfFile.Name.empty());
3750b57cec5SDimitry Andric   if (LineStr)
3760b57cec5SDimitry Andric     LineStr->emitRef(MCOS, DwarfFile.Name);
3770b57cec5SDimitry Andric   else {
378*5ffd83dbSDimitry Andric     MCOS->emitBytes(DwarfFile.Name);     // FileName and...
379*5ffd83dbSDimitry Andric     MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
3800b57cec5SDimitry Andric   }
381*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(DwarfFile.DirIndex); // Directory number.
3820b57cec5SDimitry Andric   if (EmitMD5) {
3830b57cec5SDimitry Andric     const MD5::MD5Result &Cksum = *DwarfFile.Checksum;
384*5ffd83dbSDimitry Andric     MCOS->emitBinaryData(
3850b57cec5SDimitry Andric         StringRef(reinterpret_cast<const char *>(Cksum.Bytes.data()),
3860b57cec5SDimitry Andric                   Cksum.Bytes.size()));
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric   if (HasSource) {
3890b57cec5SDimitry Andric     if (LineStr)
3900b57cec5SDimitry Andric       LineStr->emitRef(MCOS, DwarfFile.Source.getValueOr(StringRef()));
3910b57cec5SDimitry Andric     else {
392*5ffd83dbSDimitry Andric       MCOS->emitBytes(
3930b57cec5SDimitry Andric           DwarfFile.Source.getValueOr(StringRef())); // Source and...
394*5ffd83dbSDimitry Andric       MCOS->emitBytes(StringRef("\0", 1));           // its null terminator.
3950b57cec5SDimitry Andric     }
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric void MCDwarfLineTableHeader::emitV5FileDirTables(
4000b57cec5SDimitry Andric     MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr) const {
4010b57cec5SDimitry Andric   // The directory format, which is just a list of the directory paths.  In a
4020b57cec5SDimitry Andric   // non-split object, these are references to .debug_line_str; in a split
4030b57cec5SDimitry Andric   // object, they are inline strings.
404*5ffd83dbSDimitry Andric   MCOS->emitInt8(1);
405*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_LNCT_path);
406*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
4070b57cec5SDimitry Andric                                     : dwarf::DW_FORM_string);
408*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(MCDwarfDirs.size() + 1);
4090b57cec5SDimitry Andric   // Try not to emit an empty compilation directory.
4100b57cec5SDimitry Andric   const StringRef CompDir = CompilationDir.empty()
4110b57cec5SDimitry Andric                                 ? MCOS->getContext().getCompilationDir()
4120b57cec5SDimitry Andric                                 : StringRef(CompilationDir);
4130b57cec5SDimitry Andric   if (LineStr) {
4140b57cec5SDimitry Andric     // Record path strings, emit references here.
4150b57cec5SDimitry Andric     LineStr->emitRef(MCOS, CompDir);
4160b57cec5SDimitry Andric     for (const auto &Dir : MCDwarfDirs)
4170b57cec5SDimitry Andric       LineStr->emitRef(MCOS, Dir);
4180b57cec5SDimitry Andric   } else {
4190b57cec5SDimitry Andric     // The list of directory paths.  Compilation directory comes first.
420*5ffd83dbSDimitry Andric     MCOS->emitBytes(CompDir);
421*5ffd83dbSDimitry Andric     MCOS->emitBytes(StringRef("\0", 1));
4220b57cec5SDimitry Andric     for (const auto &Dir : MCDwarfDirs) {
423*5ffd83dbSDimitry Andric       MCOS->emitBytes(Dir);                // The DirectoryName, and...
424*5ffd83dbSDimitry Andric       MCOS->emitBytes(StringRef("\0", 1)); // its null terminator.
4250b57cec5SDimitry Andric     }
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric 
4280b57cec5SDimitry Andric   // The file format, which is the inline null-terminated filename and a
4290b57cec5SDimitry Andric   // directory index.  We don't track file size/timestamp so don't emit them
4300b57cec5SDimitry Andric   // in the v5 table.  Emit MD5 checksums and source if we have them.
4310b57cec5SDimitry Andric   uint64_t Entries = 2;
4320b57cec5SDimitry Andric   if (HasAllMD5)
4330b57cec5SDimitry Andric     Entries += 1;
4340b57cec5SDimitry Andric   if (HasSource)
4350b57cec5SDimitry Andric     Entries += 1;
436*5ffd83dbSDimitry Andric   MCOS->emitInt8(Entries);
437*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_LNCT_path);
438*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
4390b57cec5SDimitry Andric                                     : dwarf::DW_FORM_string);
440*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_LNCT_directory_index);
441*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_FORM_udata);
4420b57cec5SDimitry Andric   if (HasAllMD5) {
443*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(dwarf::DW_LNCT_MD5);
444*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(dwarf::DW_FORM_data16);
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric   if (HasSource) {
447*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
448*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
4490b57cec5SDimitry Andric                                       : dwarf::DW_FORM_string);
4500b57cec5SDimitry Andric   }
4510b57cec5SDimitry Andric   // Then the counted list of files. The root file is file #0, then emit the
4520b57cec5SDimitry Andric   // files as provide by .file directives.
4530b57cec5SDimitry Andric   // MCDwarfFiles has an unused element [0] so use size() not size()+1.
4540b57cec5SDimitry Andric   // But sometimes MCDwarfFiles is empty, in which case we still emit one file.
455*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size());
4560b57cec5SDimitry Andric   // To accommodate assembler source written for DWARF v4 but trying to emit
4570b57cec5SDimitry Andric   // v5: If we didn't see a root file explicitly, replicate file #1.
4580b57cec5SDimitry Andric   assert((!RootFile.Name.empty() || MCDwarfFiles.size() >= 1) &&
4590b57cec5SDimitry Andric          "No root file and no .file directives");
4600b57cec5SDimitry Andric   emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
4610b57cec5SDimitry Andric                      HasAllMD5, HasSource, LineStr);
4620b57cec5SDimitry Andric   for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
4630b57cec5SDimitry Andric     emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric std::pair<MCSymbol *, MCSymbol *>
4670b57cec5SDimitry Andric MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
4680b57cec5SDimitry Andric                              ArrayRef<char> StandardOpcodeLengths,
4690b57cec5SDimitry Andric                              Optional<MCDwarfLineStr> &LineStr) const {
4700b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   // Create a symbol at the beginning of the line table.
4730b57cec5SDimitry Andric   MCSymbol *LineStartSym = Label;
4740b57cec5SDimitry Andric   if (!LineStartSym)
4750b57cec5SDimitry Andric     LineStartSym = context.createTempSymbol();
4760b57cec5SDimitry Andric   // Set the value of the symbol, as we are at the start of the line table.
477*5ffd83dbSDimitry Andric   MCOS->emitLabel(LineStartSym);
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   // Create a symbol for the end of the section (to be set when we get there).
4800b57cec5SDimitry Andric   MCSymbol *LineEndSym = context.createTempSymbol();
4810b57cec5SDimitry Andric 
482*5ffd83dbSDimitry Andric   unsigned UnitLengthBytes =
483*5ffd83dbSDimitry Andric       dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
484*5ffd83dbSDimitry Andric   unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
485*5ffd83dbSDimitry Andric 
486*5ffd83dbSDimitry Andric   if (context.getDwarfFormat() == dwarf::DWARF64)
487*5ffd83dbSDimitry Andric     // Emit DWARF64 mark.
488*5ffd83dbSDimitry Andric     MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
489*5ffd83dbSDimitry Andric 
490*5ffd83dbSDimitry Andric   // The length field does not include itself and, in case of the 64-bit DWARF
491*5ffd83dbSDimitry Andric   // format, the DWARF64 mark.
4920b57cec5SDimitry Andric   emitAbsValue(*MCOS,
493*5ffd83dbSDimitry Andric                makeEndMinusStartExpr(context, *LineStartSym, *LineEndSym,
494*5ffd83dbSDimitry Andric                                      UnitLengthBytes),
495*5ffd83dbSDimitry Andric                OffsetSize);
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric   // Next 2 bytes is the Version.
4980b57cec5SDimitry Andric   unsigned LineTableVersion = context.getDwarfVersion();
499*5ffd83dbSDimitry Andric   MCOS->emitInt16(LineTableVersion);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   // Keep track of the bytes between the very start and where the header length
5020b57cec5SDimitry Andric   // comes out.
503*5ffd83dbSDimitry Andric   unsigned PreHeaderLengthBytes = UnitLengthBytes + 2;
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric   // In v5, we get address info next.
5060b57cec5SDimitry Andric   if (LineTableVersion >= 5) {
507*5ffd83dbSDimitry Andric     MCOS->emitInt8(context.getAsmInfo()->getCodePointerSize());
508*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // Segment selector; same as EmitGenDwarfAranges.
5090b57cec5SDimitry Andric     PreHeaderLengthBytes += 2;
5100b57cec5SDimitry Andric   }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   // Create a symbol for the end of the prologue (to be set when we get there).
5130b57cec5SDimitry Andric   MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
5140b57cec5SDimitry Andric 
515*5ffd83dbSDimitry Andric   // Length of the prologue, is the next 4 bytes (8 bytes for DWARF64). This is
516*5ffd83dbSDimitry Andric   // actually the length from after the length word, to the end of the prologue.
5170b57cec5SDimitry Andric   emitAbsValue(*MCOS,
518*5ffd83dbSDimitry Andric                makeEndMinusStartExpr(context, *LineStartSym, *ProEndSym,
519*5ffd83dbSDimitry Andric                                      (PreHeaderLengthBytes + OffsetSize)),
520*5ffd83dbSDimitry Andric                OffsetSize);
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   // Parameters of the state machine, are next.
523*5ffd83dbSDimitry Andric   MCOS->emitInt8(context.getAsmInfo()->getMinInstAlignment());
5240b57cec5SDimitry Andric   // maximum_operations_per_instruction
5250b57cec5SDimitry Andric   // For non-VLIW architectures this field is always 1.
5260b57cec5SDimitry Andric   // FIXME: VLIW architectures need to update this field accordingly.
5270b57cec5SDimitry Andric   if (LineTableVersion >= 4)
528*5ffd83dbSDimitry Andric     MCOS->emitInt8(1);
529*5ffd83dbSDimitry Andric   MCOS->emitInt8(DWARF2_LINE_DEFAULT_IS_STMT);
530*5ffd83dbSDimitry Andric   MCOS->emitInt8(Params.DWARF2LineBase);
531*5ffd83dbSDimitry Andric   MCOS->emitInt8(Params.DWARF2LineRange);
532*5ffd83dbSDimitry Andric   MCOS->emitInt8(StandardOpcodeLengths.size() + 1);
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric   // Standard opcode lengths
5350b57cec5SDimitry Andric   for (char Length : StandardOpcodeLengths)
536*5ffd83dbSDimitry Andric     MCOS->emitInt8(Length);
5370b57cec5SDimitry Andric 
5380b57cec5SDimitry Andric   // Put out the directory and file tables.  The formats vary depending on
5390b57cec5SDimitry Andric   // the version.
5400b57cec5SDimitry Andric   if (LineTableVersion >= 5)
5410b57cec5SDimitry Andric     emitV5FileDirTables(MCOS, LineStr);
5420b57cec5SDimitry Andric   else
5430b57cec5SDimitry Andric     emitV2FileDirTables(MCOS);
5440b57cec5SDimitry Andric 
5450b57cec5SDimitry Andric   // This is the end of the prologue, so set the value of the symbol at the
5460b57cec5SDimitry Andric   // end of the prologue (that was used in a previous expression).
547*5ffd83dbSDimitry Andric   MCOS->emitLabel(ProEndSym);
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric   return std::make_pair(LineStartSym, LineEndSym);
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
5530b57cec5SDimitry Andric                               MCDwarfLineTableParams Params,
5540b57cec5SDimitry Andric                               Optional<MCDwarfLineStr> &LineStr) const {
5550b57cec5SDimitry Andric   MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   // Put out the line tables.
5580b57cec5SDimitry Andric   for (const auto &LineSec : MCLineSections.getMCLineEntries())
559*5ffd83dbSDimitry Andric     emitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric   // This is the end of the section, so set the value of the symbol at the end
5620b57cec5SDimitry Andric   // of this section (that was used in a previous expression).
563*5ffd83dbSDimitry Andric   MCOS->emitLabel(LineEndSym);
5640b57cec5SDimitry Andric }
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory,
5670b57cec5SDimitry Andric                                                 StringRef &FileName,
5680b57cec5SDimitry Andric                                                 Optional<MD5::MD5Result> Checksum,
5690b57cec5SDimitry Andric                                                 Optional<StringRef> Source,
5700b57cec5SDimitry Andric                                                 uint16_t DwarfVersion,
5710b57cec5SDimitry Andric                                                 unsigned FileNumber) {
5720b57cec5SDimitry Andric   return Header.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
5730b57cec5SDimitry Andric                            FileNumber);
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
5768bcb0991SDimitry Andric static bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
5770b57cec5SDimitry Andric                        StringRef &FileName, Optional<MD5::MD5Result> Checksum) {
5780b57cec5SDimitry Andric   if (RootFile.Name.empty() || RootFile.Name != FileName.data())
5790b57cec5SDimitry Andric     return false;
5800b57cec5SDimitry Andric   return RootFile.Checksum == Checksum;
5810b57cec5SDimitry Andric }
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric Expected<unsigned>
5840b57cec5SDimitry Andric MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
5850b57cec5SDimitry Andric                                    StringRef &FileName,
5860b57cec5SDimitry Andric                                    Optional<MD5::MD5Result> Checksum,
5870b57cec5SDimitry Andric                                    Optional<StringRef> Source,
5880b57cec5SDimitry Andric                                    uint16_t DwarfVersion,
5890b57cec5SDimitry Andric                                    unsigned FileNumber) {
5900b57cec5SDimitry Andric   if (Directory == CompilationDir)
5910b57cec5SDimitry Andric     Directory = "";
5920b57cec5SDimitry Andric   if (FileName.empty()) {
5930b57cec5SDimitry Andric     FileName = "<stdin>";
5940b57cec5SDimitry Andric     Directory = "";
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric   assert(!FileName.empty());
5970b57cec5SDimitry Andric   // Keep track of whether any or all files have an MD5 checksum.
5980b57cec5SDimitry Andric   // If any files have embedded source, they all must.
5990b57cec5SDimitry Andric   if (MCDwarfFiles.empty()) {
6000b57cec5SDimitry Andric     trackMD5Usage(Checksum.hasValue());
6010b57cec5SDimitry Andric     HasSource = (Source != None);
6020b57cec5SDimitry Andric   }
6030b57cec5SDimitry Andric   if (isRootFile(RootFile, Directory, FileName, Checksum) && DwarfVersion >= 5)
6040b57cec5SDimitry Andric     return 0;
6050b57cec5SDimitry Andric   if (FileNumber == 0) {
6060b57cec5SDimitry Andric     // File numbers start with 1 and/or after any file numbers
6070b57cec5SDimitry Andric     // allocated by inline-assembler .file directives.
6080b57cec5SDimitry Andric     FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
6090b57cec5SDimitry Andric     SmallString<256> Buffer;
6100b57cec5SDimitry Andric     auto IterBool = SourceIdMap.insert(
6110b57cec5SDimitry Andric         std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
6120b57cec5SDimitry Andric                        FileNumber));
6130b57cec5SDimitry Andric     if (!IterBool.second)
6140b57cec5SDimitry Andric       return IterBool.first->second;
6150b57cec5SDimitry Andric   }
6160b57cec5SDimitry Andric   // Make space for this FileNumber in the MCDwarfFiles vector if needed.
6170b57cec5SDimitry Andric   if (FileNumber >= MCDwarfFiles.size())
6180b57cec5SDimitry Andric     MCDwarfFiles.resize(FileNumber + 1);
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric   // Get the new MCDwarfFile slot for this FileNumber.
6210b57cec5SDimitry Andric   MCDwarfFile &File = MCDwarfFiles[FileNumber];
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric   // It is an error to see the same number more than once.
6240b57cec5SDimitry Andric   if (!File.Name.empty())
6250b57cec5SDimitry Andric     return make_error<StringError>("file number already allocated",
6260b57cec5SDimitry Andric                                    inconvertibleErrorCode());
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   // If any files have embedded source, they all must.
6290b57cec5SDimitry Andric   if (HasSource != (Source != None))
6300b57cec5SDimitry Andric     return make_error<StringError>("inconsistent use of embedded source",
6310b57cec5SDimitry Andric                                    inconvertibleErrorCode());
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   if (Directory.empty()) {
6340b57cec5SDimitry Andric     // Separate the directory part from the basename of the FileName.
6350b57cec5SDimitry Andric     StringRef tFileName = sys::path::filename(FileName);
6360b57cec5SDimitry Andric     if (!tFileName.empty()) {
6370b57cec5SDimitry Andric       Directory = sys::path::parent_path(FileName);
6380b57cec5SDimitry Andric       if (!Directory.empty())
6390b57cec5SDimitry Andric         FileName = tFileName;
6400b57cec5SDimitry Andric     }
6410b57cec5SDimitry Andric   }
6420b57cec5SDimitry Andric 
6430b57cec5SDimitry Andric   // Find or make an entry in the MCDwarfDirs vector for this Directory.
6440b57cec5SDimitry Andric   // Capture directory name.
6450b57cec5SDimitry Andric   unsigned DirIndex;
6460b57cec5SDimitry Andric   if (Directory.empty()) {
6470b57cec5SDimitry Andric     // For FileNames with no directories a DirIndex of 0 is used.
6480b57cec5SDimitry Andric     DirIndex = 0;
6490b57cec5SDimitry Andric   } else {
6500b57cec5SDimitry Andric     DirIndex = llvm::find(MCDwarfDirs, Directory) - MCDwarfDirs.begin();
6510b57cec5SDimitry Andric     if (DirIndex >= MCDwarfDirs.size())
652*5ffd83dbSDimitry Andric       MCDwarfDirs.push_back(std::string(Directory));
6530b57cec5SDimitry Andric     // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
6540b57cec5SDimitry Andric     // no directories.  MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
6550b57cec5SDimitry Andric     // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
6560b57cec5SDimitry Andric     // are stored at MCDwarfFiles[FileNumber].Name .
6570b57cec5SDimitry Andric     DirIndex++;
6580b57cec5SDimitry Andric   }
6590b57cec5SDimitry Andric 
660*5ffd83dbSDimitry Andric   File.Name = std::string(FileName);
6610b57cec5SDimitry Andric   File.DirIndex = DirIndex;
6620b57cec5SDimitry Andric   File.Checksum = Checksum;
6630b57cec5SDimitry Andric   trackMD5Usage(Checksum.hasValue());
6640b57cec5SDimitry Andric   File.Source = Source;
6650b57cec5SDimitry Andric   if (Source)
6660b57cec5SDimitry Andric     HasSource = true;
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   // return the allocated FileNumber.
6690b57cec5SDimitry Andric   return FileNumber;
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric /// Utility function to emit the encoding to a streamer.
6730b57cec5SDimitry Andric void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
6740b57cec5SDimitry Andric                            int64_t LineDelta, uint64_t AddrDelta) {
6750b57cec5SDimitry Andric   MCContext &Context = MCOS->getContext();
6760b57cec5SDimitry Andric   SmallString<256> Tmp;
6770b57cec5SDimitry Andric   raw_svector_ostream OS(Tmp);
6780b57cec5SDimitry Andric   MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
679*5ffd83dbSDimitry Andric   MCOS->emitBytes(OS.str());
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric /// Given a special op, return the address skip amount (in units of
6830b57cec5SDimitry Andric /// DWARF2_LINE_MIN_INSN_LENGTH).
6840b57cec5SDimitry Andric static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
6850b57cec5SDimitry Andric   return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
6860b57cec5SDimitry Andric }
6870b57cec5SDimitry Andric 
6880b57cec5SDimitry Andric /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
6890b57cec5SDimitry Andric void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
6900b57cec5SDimitry Andric                              int64_t LineDelta, uint64_t AddrDelta,
6910b57cec5SDimitry Andric                              raw_ostream &OS) {
6920b57cec5SDimitry Andric   uint64_t Temp, Opcode;
6930b57cec5SDimitry Andric   bool NeedCopy = false;
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric   // The maximum address skip amount that can be encoded with a special op.
6960b57cec5SDimitry Andric   uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   // Scale the address delta by the minimum instruction length.
6990b57cec5SDimitry Andric   AddrDelta = ScaleAddrDelta(Context, AddrDelta);
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric   // A LineDelta of INT64_MAX is a signal that this is actually a
7020b57cec5SDimitry Andric   // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
7030b57cec5SDimitry Andric   // end_sequence to emit the matrix entry.
7040b57cec5SDimitry Andric   if (LineDelta == INT64_MAX) {
7050b57cec5SDimitry Andric     if (AddrDelta == MaxSpecialAddrDelta)
7060b57cec5SDimitry Andric       OS << char(dwarf::DW_LNS_const_add_pc);
7070b57cec5SDimitry Andric     else if (AddrDelta) {
7080b57cec5SDimitry Andric       OS << char(dwarf::DW_LNS_advance_pc);
7090b57cec5SDimitry Andric       encodeULEB128(AddrDelta, OS);
7100b57cec5SDimitry Andric     }
7110b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_extended_op);
7120b57cec5SDimitry Andric     OS << char(1);
7130b57cec5SDimitry Andric     OS << char(dwarf::DW_LNE_end_sequence);
7140b57cec5SDimitry Andric     return;
7150b57cec5SDimitry Andric   }
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   // Bias the line delta by the base.
7180b57cec5SDimitry Andric   Temp = LineDelta - Params.DWARF2LineBase;
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric   // If the line increment is out of range of a special opcode, we must encode
7210b57cec5SDimitry Andric   // it with DW_LNS_advance_line.
7220b57cec5SDimitry Andric   if (Temp >= Params.DWARF2LineRange ||
7230b57cec5SDimitry Andric       Temp + Params.DWARF2LineOpcodeBase > 255) {
7240b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_advance_line);
7250b57cec5SDimitry Andric     encodeSLEB128(LineDelta, OS);
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric     LineDelta = 0;
7280b57cec5SDimitry Andric     Temp = 0 - Params.DWARF2LineBase;
7290b57cec5SDimitry Andric     NeedCopy = true;
7300b57cec5SDimitry Andric   }
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric   // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
7330b57cec5SDimitry Andric   if (LineDelta == 0 && AddrDelta == 0) {
7340b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_copy);
7350b57cec5SDimitry Andric     return;
7360b57cec5SDimitry Andric   }
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric   // Bias the opcode by the special opcode base.
7390b57cec5SDimitry Andric   Temp += Params.DWARF2LineOpcodeBase;
7400b57cec5SDimitry Andric 
7410b57cec5SDimitry Andric   // Avoid overflow when addr_delta is large.
7420b57cec5SDimitry Andric   if (AddrDelta < 256 + MaxSpecialAddrDelta) {
7430b57cec5SDimitry Andric     // Try using a special opcode.
7440b57cec5SDimitry Andric     Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
7450b57cec5SDimitry Andric     if (Opcode <= 255) {
7460b57cec5SDimitry Andric       OS << char(Opcode);
7470b57cec5SDimitry Andric       return;
7480b57cec5SDimitry Andric     }
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric     // Try using DW_LNS_const_add_pc followed by special op.
7510b57cec5SDimitry Andric     Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
7520b57cec5SDimitry Andric     if (Opcode <= 255) {
7530b57cec5SDimitry Andric       OS << char(dwarf::DW_LNS_const_add_pc);
7540b57cec5SDimitry Andric       OS << char(Opcode);
7550b57cec5SDimitry Andric       return;
7560b57cec5SDimitry Andric     }
7570b57cec5SDimitry Andric   }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   // Otherwise use DW_LNS_advance_pc.
7600b57cec5SDimitry Andric   OS << char(dwarf::DW_LNS_advance_pc);
7610b57cec5SDimitry Andric   encodeULEB128(AddrDelta, OS);
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric   if (NeedCopy)
7640b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_copy);
7650b57cec5SDimitry Andric   else {
7660b57cec5SDimitry Andric     assert(Temp <= 255 && "Buggy special opcode encoding.");
7670b57cec5SDimitry Andric     OS << char(Temp);
7680b57cec5SDimitry Andric   }
7690b57cec5SDimitry Andric }
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric bool MCDwarfLineAddr::FixedEncode(MCContext &Context,
7720b57cec5SDimitry Andric                                   MCDwarfLineTableParams Params,
7730b57cec5SDimitry Andric                                   int64_t LineDelta, uint64_t AddrDelta,
7740b57cec5SDimitry Andric                                   raw_ostream &OS,
7750b57cec5SDimitry Andric                                   uint32_t *Offset, uint32_t *Size) {
7760b57cec5SDimitry Andric   if (LineDelta != INT64_MAX) {
7770b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_advance_line);
7780b57cec5SDimitry Andric     encodeSLEB128(LineDelta, OS);
7790b57cec5SDimitry Andric   }
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric   // Use address delta to adjust address or use absolute address to adjust
7820b57cec5SDimitry Andric   // address.
7830b57cec5SDimitry Andric   bool SetDelta;
7840b57cec5SDimitry Andric   // According to DWARF spec., the DW_LNS_fixed_advance_pc opcode takes a
7850b57cec5SDimitry Andric   // single uhalf (unencoded) operand. So, the maximum value of AddrDelta
7860b57cec5SDimitry Andric   // is 65535. We set a conservative upper bound for it for relaxation.
7870b57cec5SDimitry Andric   if (AddrDelta > 60000) {
7880b57cec5SDimitry Andric     const MCAsmInfo *asmInfo = Context.getAsmInfo();
7890b57cec5SDimitry Andric     unsigned AddrSize = asmInfo->getCodePointerSize();
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_extended_op);
7920b57cec5SDimitry Andric     encodeULEB128(1 + AddrSize, OS);
7930b57cec5SDimitry Andric     OS << char(dwarf::DW_LNE_set_address);
7940b57cec5SDimitry Andric     // Generate fixup for the address.
7950b57cec5SDimitry Andric     *Offset = OS.tell();
7960b57cec5SDimitry Andric     *Size = AddrSize;
7970b57cec5SDimitry Andric     SetDelta = false;
7980b57cec5SDimitry Andric     OS.write_zeros(AddrSize);
7990b57cec5SDimitry Andric   } else {
8000b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_fixed_advance_pc);
8010b57cec5SDimitry Andric     // Generate fixup for 2-bytes address delta.
8020b57cec5SDimitry Andric     *Offset = OS.tell();
8030b57cec5SDimitry Andric     *Size = 2;
8040b57cec5SDimitry Andric     SetDelta = true;
8050b57cec5SDimitry Andric     OS << char(0);
8060b57cec5SDimitry Andric     OS << char(0);
8070b57cec5SDimitry Andric   }
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric   if (LineDelta == INT64_MAX) {
8100b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_extended_op);
8110b57cec5SDimitry Andric     OS << char(1);
8120b57cec5SDimitry Andric     OS << char(dwarf::DW_LNE_end_sequence);
8130b57cec5SDimitry Andric   } else {
8140b57cec5SDimitry Andric     OS << char(dwarf::DW_LNS_copy);
8150b57cec5SDimitry Andric   }
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   return SetDelta;
8180b57cec5SDimitry Andric }
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric // Utility function to write a tuple for .debug_abbrev.
8210b57cec5SDimitry Andric static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
822*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(Name);
823*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(Form);
8240b57cec5SDimitry Andric }
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric // When generating dwarf for assembly source files this emits
8270b57cec5SDimitry Andric // the data for .debug_abbrev section which contains three DIEs.
8280b57cec5SDimitry Andric static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
8290b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
8300b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric   // DW_TAG_compile_unit DIE abbrev (1).
833*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(1);
834*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_TAG_compile_unit);
835*5ffd83dbSDimitry Andric   MCOS->emitInt8(dwarf::DW_CHILDREN_yes);
836*5ffd83dbSDimitry Andric   dwarf::Form SecOffsetForm =
837*5ffd83dbSDimitry Andric       context.getDwarfVersion() >= 4
8380b57cec5SDimitry Andric           ? dwarf::DW_FORM_sec_offset
839*5ffd83dbSDimitry Andric           : (context.getDwarfFormat() == dwarf::DWARF64 ? dwarf::DW_FORM_data8
8400b57cec5SDimitry Andric                                                         : dwarf::DW_FORM_data4);
841*5ffd83dbSDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, SecOffsetForm);
8420b57cec5SDimitry Andric   if (context.getGenDwarfSectionSyms().size() > 1 &&
8430b57cec5SDimitry Andric       context.getDwarfVersion() >= 3) {
844*5ffd83dbSDimitry Andric     EmitAbbrev(MCOS, dwarf::DW_AT_ranges, SecOffsetForm);
8450b57cec5SDimitry Andric   } else {
8460b57cec5SDimitry Andric     EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
8470b57cec5SDimitry Andric     EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
8480b57cec5SDimitry Andric   }
8490b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
8500b57cec5SDimitry Andric   if (!context.getCompilationDir().empty())
8510b57cec5SDimitry Andric     EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
8520b57cec5SDimitry Andric   StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
8530b57cec5SDimitry Andric   if (!DwarfDebugFlags.empty())
8540b57cec5SDimitry Andric     EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
8550b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
8560b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
8570b57cec5SDimitry Andric   EmitAbbrev(MCOS, 0, 0);
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric   // DW_TAG_label DIE abbrev (2).
860*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(2);
861*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(dwarf::DW_TAG_label);
862*5ffd83dbSDimitry Andric   MCOS->emitInt8(dwarf::DW_CHILDREN_no);
8630b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
8640b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
8650b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
8660b57cec5SDimitry Andric   EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
8670b57cec5SDimitry Andric   EmitAbbrev(MCOS, 0, 0);
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // Terminate the abbreviations for this compilation unit.
870*5ffd83dbSDimitry Andric   MCOS->emitInt8(0);
8710b57cec5SDimitry Andric }
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric // When generating dwarf for assembly source files this emits the data for
8740b57cec5SDimitry Andric // .debug_aranges section. This section contains a header and a table of pairs
8750b57cec5SDimitry Andric // of PointerSize'ed values for the address and size of section(s) with line
8760b57cec5SDimitry Andric // table entries.
8770b57cec5SDimitry Andric static void EmitGenDwarfAranges(MCStreamer *MCOS,
8780b57cec5SDimitry Andric                                 const MCSymbol *InfoSectionSymbol) {
8790b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   auto &Sections = context.getGenDwarfSectionSyms();
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
8840b57cec5SDimitry Andric 
885*5ffd83dbSDimitry Andric   unsigned UnitLengthBytes =
886*5ffd83dbSDimitry Andric       dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
887*5ffd83dbSDimitry Andric   unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
888*5ffd83dbSDimitry Andric 
8890b57cec5SDimitry Andric   // This will be the length of the .debug_aranges section, first account for
8900b57cec5SDimitry Andric   // the size of each item in the header (see below where we emit these items).
891*5ffd83dbSDimitry Andric   int Length = UnitLengthBytes + 2 + OffsetSize + 1 + 1;
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   // Figure the padding after the header before the table of address and size
8940b57cec5SDimitry Andric   // pairs who's values are PointerSize'ed.
8950b57cec5SDimitry Andric   const MCAsmInfo *asmInfo = context.getAsmInfo();
8960b57cec5SDimitry Andric   int AddrSize = asmInfo->getCodePointerSize();
8970b57cec5SDimitry Andric   int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
8980b57cec5SDimitry Andric   if (Pad == 2 * AddrSize)
8990b57cec5SDimitry Andric     Pad = 0;
9000b57cec5SDimitry Andric   Length += Pad;
9010b57cec5SDimitry Andric 
9020b57cec5SDimitry Andric   // Add the size of the pair of PointerSize'ed values for the address and size
9030b57cec5SDimitry Andric   // of each section we have in the table.
9040b57cec5SDimitry Andric   Length += 2 * AddrSize * Sections.size();
9050b57cec5SDimitry Andric   // And the pair of terminating zeros.
9060b57cec5SDimitry Andric   Length += 2 * AddrSize;
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric   // Emit the header for this section.
909*5ffd83dbSDimitry Andric   if (context.getDwarfFormat() == dwarf::DWARF64)
910*5ffd83dbSDimitry Andric     // The DWARF64 mark.
911*5ffd83dbSDimitry Andric     MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
912*5ffd83dbSDimitry Andric   // The 4 (8 for DWARF64) byte length not including the length of the unit
913*5ffd83dbSDimitry Andric   // length field itself.
914*5ffd83dbSDimitry Andric   MCOS->emitIntValue(Length - UnitLengthBytes, OffsetSize);
9150b57cec5SDimitry Andric   // The 2 byte version, which is 2.
916*5ffd83dbSDimitry Andric   MCOS->emitInt16(2);
917*5ffd83dbSDimitry Andric   // The 4 (8 for DWARF64) byte offset to the compile unit in the .debug_info
918*5ffd83dbSDimitry Andric   // from the start of the .debug_info.
9190b57cec5SDimitry Andric   if (InfoSectionSymbol)
920*5ffd83dbSDimitry Andric     MCOS->emitSymbolValue(InfoSectionSymbol, OffsetSize,
9210b57cec5SDimitry Andric                           asmInfo->needsDwarfSectionOffsetDirective());
9220b57cec5SDimitry Andric   else
923*5ffd83dbSDimitry Andric     MCOS->emitIntValue(0, OffsetSize);
9240b57cec5SDimitry Andric   // The 1 byte size of an address.
925*5ffd83dbSDimitry Andric   MCOS->emitInt8(AddrSize);
9260b57cec5SDimitry Andric   // The 1 byte size of a segment descriptor, we use a value of zero.
927*5ffd83dbSDimitry Andric   MCOS->emitInt8(0);
9280b57cec5SDimitry Andric   // Align the header with the padding if needed, before we put out the table.
9290b57cec5SDimitry Andric   for(int i = 0; i < Pad; i++)
930*5ffd83dbSDimitry Andric     MCOS->emitInt8(0);
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   // Now emit the table of pairs of PointerSize'ed values for the section
9330b57cec5SDimitry Andric   // addresses and sizes.
9340b57cec5SDimitry Andric   for (MCSection *Sec : Sections) {
9350b57cec5SDimitry Andric     const MCSymbol *StartSymbol = Sec->getBeginSymbol();
9360b57cec5SDimitry Andric     MCSymbol *EndSymbol = Sec->getEndSymbol(context);
9370b57cec5SDimitry Andric     assert(StartSymbol && "StartSymbol must not be NULL");
9380b57cec5SDimitry Andric     assert(EndSymbol && "EndSymbol must not be NULL");
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric     const MCExpr *Addr = MCSymbolRefExpr::create(
9410b57cec5SDimitry Andric       StartSymbol, MCSymbolRefExpr::VK_None, context);
942*5ffd83dbSDimitry Andric     const MCExpr *Size =
943*5ffd83dbSDimitry Andric         makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
944*5ffd83dbSDimitry Andric     MCOS->emitValue(Addr, AddrSize);
9450b57cec5SDimitry Andric     emitAbsValue(*MCOS, Size, AddrSize);
9460b57cec5SDimitry Andric   }
9470b57cec5SDimitry Andric 
9480b57cec5SDimitry Andric   // And finally the pair of terminating zeros.
949*5ffd83dbSDimitry Andric   MCOS->emitIntValue(0, AddrSize);
950*5ffd83dbSDimitry Andric   MCOS->emitIntValue(0, AddrSize);
9510b57cec5SDimitry Andric }
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric // When generating dwarf for assembly source files this emits the data for
9540b57cec5SDimitry Andric // .debug_info section which contains three parts.  The header, the compile_unit
9550b57cec5SDimitry Andric // DIE and a list of label DIEs.
9560b57cec5SDimitry Andric static void EmitGenDwarfInfo(MCStreamer *MCOS,
9570b57cec5SDimitry Andric                              const MCSymbol *AbbrevSectionSymbol,
9580b57cec5SDimitry Andric                              const MCSymbol *LineSectionSymbol,
959*5ffd83dbSDimitry Andric                              const MCSymbol *RangesSymbol) {
9600b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric   // Create a symbol at the start and end of this section used in here for the
9650b57cec5SDimitry Andric   // expression to calculate the length in the header.
9660b57cec5SDimitry Andric   MCSymbol *InfoStart = context.createTempSymbol();
967*5ffd83dbSDimitry Andric   MCOS->emitLabel(InfoStart);
9680b57cec5SDimitry Andric   MCSymbol *InfoEnd = context.createTempSymbol();
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   // First part: the header.
9710b57cec5SDimitry Andric 
972*5ffd83dbSDimitry Andric   unsigned UnitLengthBytes =
973*5ffd83dbSDimitry Andric       dwarf::getUnitLengthFieldByteSize(context.getDwarfFormat());
974*5ffd83dbSDimitry Andric   unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(context.getDwarfFormat());
975*5ffd83dbSDimitry Andric 
976*5ffd83dbSDimitry Andric   if (context.getDwarfFormat() == dwarf::DWARF64)
977*5ffd83dbSDimitry Andric     // Emit DWARF64 mark.
978*5ffd83dbSDimitry Andric     MCOS->emitInt32(dwarf::DW_LENGTH_DWARF64);
979*5ffd83dbSDimitry Andric 
980*5ffd83dbSDimitry Andric   // The 4 (8 for DWARF64) byte total length of the information for this
981*5ffd83dbSDimitry Andric   // compilation unit, not including the unit length field itself.
982*5ffd83dbSDimitry Andric   const MCExpr *Length =
983*5ffd83dbSDimitry Andric       makeEndMinusStartExpr(context, *InfoStart, *InfoEnd, UnitLengthBytes);
984*5ffd83dbSDimitry Andric   emitAbsValue(*MCOS, Length, OffsetSize);
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   // The 2 byte DWARF version.
987*5ffd83dbSDimitry Andric   MCOS->emitInt16(context.getDwarfVersion());
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   // The DWARF v5 header has unit type, address size, abbrev offset.
9900b57cec5SDimitry Andric   // Earlier versions have abbrev offset, address size.
9910b57cec5SDimitry Andric   const MCAsmInfo &AsmInfo = *context.getAsmInfo();
9920b57cec5SDimitry Andric   int AddrSize = AsmInfo.getCodePointerSize();
9930b57cec5SDimitry Andric   if (context.getDwarfVersion() >= 5) {
994*5ffd83dbSDimitry Andric     MCOS->emitInt8(dwarf::DW_UT_compile);
995*5ffd83dbSDimitry Andric     MCOS->emitInt8(AddrSize);
9960b57cec5SDimitry Andric   }
997*5ffd83dbSDimitry Andric   // The 4 (8 for DWARF64) byte offset to the debug abbrevs from the start of
998*5ffd83dbSDimitry Andric   // the .debug_abbrev.
999*5ffd83dbSDimitry Andric   if (AbbrevSectionSymbol)
1000*5ffd83dbSDimitry Andric     MCOS->emitSymbolValue(AbbrevSectionSymbol, OffsetSize,
10010b57cec5SDimitry Andric                           AsmInfo.needsDwarfSectionOffsetDirective());
1002*5ffd83dbSDimitry Andric   else
1003*5ffd83dbSDimitry Andric     // Since the abbrevs are at the start of the section, the offset is zero.
1004*5ffd83dbSDimitry Andric     MCOS->emitIntValue(0, OffsetSize);
10050b57cec5SDimitry Andric   if (context.getDwarfVersion() <= 4)
1006*5ffd83dbSDimitry Andric     MCOS->emitInt8(AddrSize);
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   // Second part: the compile_unit DIE.
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric   // The DW_TAG_compile_unit DIE abbrev (1).
1011*5ffd83dbSDimitry Andric   MCOS->emitULEB128IntValue(1);
10120b57cec5SDimitry Andric 
1013*5ffd83dbSDimitry Andric   // DW_AT_stmt_list, a 4 (8 for DWARF64) byte offset from the start of the
1014*5ffd83dbSDimitry Andric   // .debug_line section.
10150b57cec5SDimitry Andric   if (LineSectionSymbol)
1016*5ffd83dbSDimitry Andric     MCOS->emitSymbolValue(LineSectionSymbol, OffsetSize,
10170b57cec5SDimitry Andric                           AsmInfo.needsDwarfSectionOffsetDirective());
10180b57cec5SDimitry Andric   else
1019*5ffd83dbSDimitry Andric     // The line table is at the start of the section, so the offset is zero.
1020*5ffd83dbSDimitry Andric     MCOS->emitIntValue(0, OffsetSize);
10210b57cec5SDimitry Andric 
1022*5ffd83dbSDimitry Andric   if (RangesSymbol) {
1023*5ffd83dbSDimitry Andric     // There are multiple sections containing code, so we must use
1024*5ffd83dbSDimitry Andric     // .debug_ranges/.debug_rnglists. AT_ranges, the 4/8 byte offset from the
1025*5ffd83dbSDimitry Andric     // start of the .debug_ranges/.debug_rnglists.
1026*5ffd83dbSDimitry Andric     MCOS->emitSymbolValue(RangesSymbol, OffsetSize);
10270b57cec5SDimitry Andric   } else {
10280b57cec5SDimitry Andric     // If we only have one non-empty code section, we can use the simpler
10290b57cec5SDimitry Andric     // AT_low_pc and AT_high_pc attributes.
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric     // Find the first (and only) non-empty text section
10320b57cec5SDimitry Andric     auto &Sections = context.getGenDwarfSectionSyms();
10330b57cec5SDimitry Andric     const auto TextSection = Sections.begin();
10340b57cec5SDimitry Andric     assert(TextSection != Sections.end() && "No text section found");
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric     MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
10370b57cec5SDimitry Andric     MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
10380b57cec5SDimitry Andric     assert(StartSymbol && "StartSymbol must not be NULL");
10390b57cec5SDimitry Andric     assert(EndSymbol && "EndSymbol must not be NULL");
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric     // AT_low_pc, the first address of the default .text section.
10420b57cec5SDimitry Andric     const MCExpr *Start = MCSymbolRefExpr::create(
10430b57cec5SDimitry Andric         StartSymbol, MCSymbolRefExpr::VK_None, context);
1044*5ffd83dbSDimitry Andric     MCOS->emitValue(Start, AddrSize);
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric     // AT_high_pc, the last address of the default .text section.
10470b57cec5SDimitry Andric     const MCExpr *End = MCSymbolRefExpr::create(
10480b57cec5SDimitry Andric       EndSymbol, MCSymbolRefExpr::VK_None, context);
1049*5ffd83dbSDimitry Andric     MCOS->emitValue(End, AddrSize);
10500b57cec5SDimitry Andric   }
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric   // AT_name, the name of the source file.  Reconstruct from the first directory
10530b57cec5SDimitry Andric   // and file table entries.
10540b57cec5SDimitry Andric   const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
10550b57cec5SDimitry Andric   if (MCDwarfDirs.size() > 0) {
1056*5ffd83dbSDimitry Andric     MCOS->emitBytes(MCDwarfDirs[0]);
1057*5ffd83dbSDimitry Andric     MCOS->emitBytes(sys::path::get_separator());
10580b57cec5SDimitry Andric   }
10590b57cec5SDimitry Andric   const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = context.getMCDwarfFiles();
10600b57cec5SDimitry Andric   // MCDwarfFiles might be empty if we have an empty source file.
10610b57cec5SDimitry Andric   // If it's not empty, [0] is unused and [1] is the first actual file.
10620b57cec5SDimitry Andric   assert(MCDwarfFiles.empty() || MCDwarfFiles.size() >= 2);
10630b57cec5SDimitry Andric   const MCDwarfFile &RootFile =
10640b57cec5SDimitry Andric       MCDwarfFiles.empty()
10650b57cec5SDimitry Andric           ? context.getMCDwarfLineTable(/*CUID=*/0).getRootFile()
10660b57cec5SDimitry Andric           : MCDwarfFiles[1];
1067*5ffd83dbSDimitry Andric   MCOS->emitBytes(RootFile.Name);
1068*5ffd83dbSDimitry Andric   MCOS->emitInt8(0); // NULL byte to terminate the string.
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric   // AT_comp_dir, the working directory the assembly was done in.
10710b57cec5SDimitry Andric   if (!context.getCompilationDir().empty()) {
1072*5ffd83dbSDimitry Andric     MCOS->emitBytes(context.getCompilationDir());
1073*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // NULL byte to terminate the string.
10740b57cec5SDimitry Andric   }
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric   // AT_APPLE_flags, the command line arguments of the assembler tool.
10770b57cec5SDimitry Andric   StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
10780b57cec5SDimitry Andric   if (!DwarfDebugFlags.empty()){
1079*5ffd83dbSDimitry Andric     MCOS->emitBytes(DwarfDebugFlags);
1080*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // NULL byte to terminate the string.
10810b57cec5SDimitry Andric   }
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric   // AT_producer, the version of the assembler tool.
10840b57cec5SDimitry Andric   StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
10850b57cec5SDimitry Andric   if (!DwarfDebugProducer.empty())
1086*5ffd83dbSDimitry Andric     MCOS->emitBytes(DwarfDebugProducer);
10870b57cec5SDimitry Andric   else
1088*5ffd83dbSDimitry Andric     MCOS->emitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
1089*5ffd83dbSDimitry Andric   MCOS->emitInt8(0); // NULL byte to terminate the string.
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric   // AT_language, a 4 byte value.  We use DW_LANG_Mips_Assembler as the dwarf2
10920b57cec5SDimitry Andric   // draft has no standard code for assembler.
1093*5ffd83dbSDimitry Andric   MCOS->emitInt16(dwarf::DW_LANG_Mips_Assembler);
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric   // Third part: the list of label DIEs.
10960b57cec5SDimitry Andric 
10970b57cec5SDimitry Andric   // Loop on saved info for dwarf labels and create the DIEs for them.
10980b57cec5SDimitry Andric   const std::vector<MCGenDwarfLabelEntry> &Entries =
10990b57cec5SDimitry Andric       MCOS->getContext().getMCGenDwarfLabelEntries();
11000b57cec5SDimitry Andric   for (const auto &Entry : Entries) {
11010b57cec5SDimitry Andric     // The DW_TAG_label DIE abbrev (2).
1102*5ffd83dbSDimitry Andric     MCOS->emitULEB128IntValue(2);
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric     // AT_name, of the label without any leading underbar.
1105*5ffd83dbSDimitry Andric     MCOS->emitBytes(Entry.getName());
1106*5ffd83dbSDimitry Andric     MCOS->emitInt8(0); // NULL byte to terminate the string.
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric     // AT_decl_file, index into the file table.
1109*5ffd83dbSDimitry Andric     MCOS->emitInt32(Entry.getFileNumber());
11100b57cec5SDimitry Andric 
11110b57cec5SDimitry Andric     // AT_decl_line, source line number.
1112*5ffd83dbSDimitry Andric     MCOS->emitInt32(Entry.getLineNumber());
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric     // AT_low_pc, start address of the label.
11150b57cec5SDimitry Andric     const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
11160b57cec5SDimitry Andric                                              MCSymbolRefExpr::VK_None, context);
1117*5ffd83dbSDimitry Andric     MCOS->emitValue(AT_low_pc, AddrSize);
11180b57cec5SDimitry Andric   }
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric   // Add the NULL DIE terminating the Compile Unit DIE's.
1121*5ffd83dbSDimitry Andric   MCOS->emitInt8(0);
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric   // Now set the value of the symbol at the end of the info section.
1124*5ffd83dbSDimitry Andric   MCOS->emitLabel(InfoEnd);
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric // When generating dwarf for assembly source files this emits the data for
11280b57cec5SDimitry Andric // .debug_ranges section. We only emit one range list, which spans all of the
11290b57cec5SDimitry Andric // executable sections of this file.
1130*5ffd83dbSDimitry Andric static MCSymbol *emitGenDwarfRanges(MCStreamer *MCOS) {
11310b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
11320b57cec5SDimitry Andric   auto &Sections = context.getGenDwarfSectionSyms();
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric   const MCAsmInfo *AsmInfo = context.getAsmInfo();
11350b57cec5SDimitry Andric   int AddrSize = AsmInfo->getCodePointerSize();
1136*5ffd83dbSDimitry Andric   MCSymbol *RangesSymbol;
11370b57cec5SDimitry Andric 
1138*5ffd83dbSDimitry Andric   if (MCOS->getContext().getDwarfVersion() >= 5) {
1139*5ffd83dbSDimitry Andric     MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRnglistsSection());
1140*5ffd83dbSDimitry Andric     MCSymbol *EndSymbol = mcdwarf::emitListsTableHeaderStart(*MCOS);
1141*5ffd83dbSDimitry Andric     MCOS->AddComment("Offset entry count");
1142*5ffd83dbSDimitry Andric     MCOS->emitInt32(0);
1143*5ffd83dbSDimitry Andric     RangesSymbol = context.createTempSymbol("debug_rnglist0_start", true, true);
1144*5ffd83dbSDimitry Andric     MCOS->emitLabel(RangesSymbol);
11450b57cec5SDimitry Andric     for (MCSection *Sec : Sections) {
11460b57cec5SDimitry Andric       const MCSymbol *StartSymbol = Sec->getBeginSymbol();
1147*5ffd83dbSDimitry Andric       const MCSymbol *EndSymbol = Sec->getEndSymbol(context);
1148*5ffd83dbSDimitry Andric       const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
1149*5ffd83dbSDimitry Andric           StartSymbol, MCSymbolRefExpr::VK_None, context);
1150*5ffd83dbSDimitry Andric       const MCExpr *SectionSize =
1151*5ffd83dbSDimitry Andric           makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
1152*5ffd83dbSDimitry Andric       MCOS->emitInt8(dwarf::DW_RLE_start_length);
1153*5ffd83dbSDimitry Andric       MCOS->emitValue(SectionStartAddr, AddrSize);
1154*5ffd83dbSDimitry Andric       MCOS->emitULEB128Value(SectionSize);
1155*5ffd83dbSDimitry Andric     }
1156*5ffd83dbSDimitry Andric     MCOS->emitInt8(dwarf::DW_RLE_end_of_list);
1157*5ffd83dbSDimitry Andric     MCOS->emitLabel(EndSymbol);
1158*5ffd83dbSDimitry Andric   } else {
1159*5ffd83dbSDimitry Andric     MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
1160*5ffd83dbSDimitry Andric     RangesSymbol = context.createTempSymbol("debug_ranges_start", true, true);
1161*5ffd83dbSDimitry Andric     MCOS->emitLabel(RangesSymbol);
1162*5ffd83dbSDimitry Andric     for (MCSection *Sec : Sections) {
1163*5ffd83dbSDimitry Andric       const MCSymbol *StartSymbol = Sec->getBeginSymbol();
1164*5ffd83dbSDimitry Andric       const MCSymbol *EndSymbol = Sec->getEndSymbol(context);
11650b57cec5SDimitry Andric 
1166*5ffd83dbSDimitry Andric       // Emit a base address selection entry for the section start.
11670b57cec5SDimitry Andric       const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
11680b57cec5SDimitry Andric           StartSymbol, MCSymbolRefExpr::VK_None, context);
11690b57cec5SDimitry Andric       MCOS->emitFill(AddrSize, 0xFF);
1170*5ffd83dbSDimitry Andric       MCOS->emitValue(SectionStartAddr, AddrSize);
11710b57cec5SDimitry Andric 
1172*5ffd83dbSDimitry Andric       // Emit a range list entry spanning this section.
1173*5ffd83dbSDimitry Andric       const MCExpr *SectionSize =
1174*5ffd83dbSDimitry Andric           makeEndMinusStartExpr(context, *StartSymbol, *EndSymbol, 0);
1175*5ffd83dbSDimitry Andric       MCOS->emitIntValue(0, AddrSize);
11760b57cec5SDimitry Andric       emitAbsValue(*MCOS, SectionSize, AddrSize);
11770b57cec5SDimitry Andric     }
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric     // Emit end of list entry
1180*5ffd83dbSDimitry Andric     MCOS->emitIntValue(0, AddrSize);
1181*5ffd83dbSDimitry Andric     MCOS->emitIntValue(0, AddrSize);
1182*5ffd83dbSDimitry Andric   }
1183*5ffd83dbSDimitry Andric 
1184*5ffd83dbSDimitry Andric   return RangesSymbol;
11850b57cec5SDimitry Andric }
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric //
11880b57cec5SDimitry Andric // When generating dwarf for assembly source files this emits the Dwarf
11890b57cec5SDimitry Andric // sections.
11900b57cec5SDimitry Andric //
11910b57cec5SDimitry Andric void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
11920b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric   // Create the dwarf sections in this order (.debug_line already created).
11950b57cec5SDimitry Andric   const MCAsmInfo *AsmInfo = context.getAsmInfo();
11960b57cec5SDimitry Andric   bool CreateDwarfSectionSymbols =
11970b57cec5SDimitry Andric       AsmInfo->doesDwarfUseRelocationsAcrossSections();
11980b57cec5SDimitry Andric   MCSymbol *LineSectionSymbol = nullptr;
11990b57cec5SDimitry Andric   if (CreateDwarfSectionSymbols)
12000b57cec5SDimitry Andric     LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
12010b57cec5SDimitry Andric   MCSymbol *AbbrevSectionSymbol = nullptr;
12020b57cec5SDimitry Andric   MCSymbol *InfoSectionSymbol = nullptr;
1203*5ffd83dbSDimitry Andric   MCSymbol *RangesSymbol = nullptr;
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric   // Create end symbols for each section, and remove empty sections
12060b57cec5SDimitry Andric   MCOS->getContext().finalizeDwarfSections(*MCOS);
12070b57cec5SDimitry Andric 
12080b57cec5SDimitry Andric   // If there are no sections to generate debug info for, we don't need
12090b57cec5SDimitry Andric   // to do anything
12100b57cec5SDimitry Andric   if (MCOS->getContext().getGenDwarfSectionSyms().empty())
12110b57cec5SDimitry Andric     return;
12120b57cec5SDimitry Andric 
12130b57cec5SDimitry Andric   // We only use the .debug_ranges section if we have multiple code sections,
12140b57cec5SDimitry Andric   // and we are emitting a DWARF version which supports it.
12150b57cec5SDimitry Andric   const bool UseRangesSection =
12160b57cec5SDimitry Andric       MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
12170b57cec5SDimitry Andric       MCOS->getContext().getDwarfVersion() >= 3;
12180b57cec5SDimitry Andric   CreateDwarfSectionSymbols |= UseRangesSection;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
12210b57cec5SDimitry Andric   if (CreateDwarfSectionSymbols) {
12220b57cec5SDimitry Andric     InfoSectionSymbol = context.createTempSymbol();
1223*5ffd83dbSDimitry Andric     MCOS->emitLabel(InfoSectionSymbol);
12240b57cec5SDimitry Andric   }
12250b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
12260b57cec5SDimitry Andric   if (CreateDwarfSectionSymbols) {
12270b57cec5SDimitry Andric     AbbrevSectionSymbol = context.createTempSymbol();
1228*5ffd83dbSDimitry Andric     MCOS->emitLabel(AbbrevSectionSymbol);
12290b57cec5SDimitry Andric   }
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric   MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric   // Output the data for .debug_aranges section.
12340b57cec5SDimitry Andric   EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
12350b57cec5SDimitry Andric 
1236*5ffd83dbSDimitry Andric   if (UseRangesSection) {
1237*5ffd83dbSDimitry Andric     RangesSymbol = emitGenDwarfRanges(MCOS);
1238*5ffd83dbSDimitry Andric     assert(RangesSymbol);
1239*5ffd83dbSDimitry Andric   }
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric   // Output the data for .debug_abbrev section.
12420b57cec5SDimitry Andric   EmitGenDwarfAbbrev(MCOS);
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric   // Output the data for .debug_info section.
1245*5ffd83dbSDimitry Andric   EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol, RangesSymbol);
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric //
12490b57cec5SDimitry Andric // When generating dwarf for assembly source files this is called when symbol
12500b57cec5SDimitry Andric // for a label is created.  If this symbol is not a temporary and is in the
12510b57cec5SDimitry Andric // section that dwarf is being generated for, save the needed info to create
12520b57cec5SDimitry Andric // a dwarf label.
12530b57cec5SDimitry Andric //
12540b57cec5SDimitry Andric void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
12550b57cec5SDimitry Andric                                      SourceMgr &SrcMgr, SMLoc &Loc) {
12560b57cec5SDimitry Andric   // We won't create dwarf labels for temporary symbols.
12570b57cec5SDimitry Andric   if (Symbol->isTemporary())
12580b57cec5SDimitry Andric     return;
12590b57cec5SDimitry Andric   MCContext &context = MCOS->getContext();
12600b57cec5SDimitry Andric   // We won't create dwarf labels for symbols in sections that we are not
12610b57cec5SDimitry Andric   // generating debug info for.
12620b57cec5SDimitry Andric   if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
12630b57cec5SDimitry Andric     return;
12640b57cec5SDimitry Andric 
12650b57cec5SDimitry Andric   // The dwarf label's name does not have the symbol name's leading
12660b57cec5SDimitry Andric   // underbar if any.
12670b57cec5SDimitry Andric   StringRef Name = Symbol->getName();
12680b57cec5SDimitry Andric   if (Name.startswith("_"))
12690b57cec5SDimitry Andric     Name = Name.substr(1, Name.size()-1);
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric   // Get the dwarf file number to be used for the dwarf label.
12720b57cec5SDimitry Andric   unsigned FileNumber = context.getGenDwarfFileNumber();
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric   // Finding the line number is the expensive part which is why we just don't
12750b57cec5SDimitry Andric   // pass it in as for some symbols we won't create a dwarf label.
12760b57cec5SDimitry Andric   unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
12770b57cec5SDimitry Andric   unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
12800b57cec5SDimitry Andric   // values so that they don't have things like an ARM thumb bit from the
12810b57cec5SDimitry Andric   // original symbol. So when used they won't get a low bit set after
12820b57cec5SDimitry Andric   // relocation.
12830b57cec5SDimitry Andric   MCSymbol *Label = context.createTempSymbol();
1284*5ffd83dbSDimitry Andric   MCOS->emitLabel(Label);
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric   // Create and entry for the info and add it to the other entries.
12870b57cec5SDimitry Andric   MCOS->getContext().addMCGenDwarfLabelEntry(
12880b57cec5SDimitry Andric       MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
12890b57cec5SDimitry Andric }
12900b57cec5SDimitry Andric 
12910b57cec5SDimitry Andric static int getDataAlignmentFactor(MCStreamer &streamer) {
12920b57cec5SDimitry Andric   MCContext &context = streamer.getContext();
12930b57cec5SDimitry Andric   const MCAsmInfo *asmInfo = context.getAsmInfo();
12940b57cec5SDimitry Andric   int size = asmInfo->getCalleeSaveStackSlotSize();
12950b57cec5SDimitry Andric   if (asmInfo->isStackGrowthDirectionUp())
12960b57cec5SDimitry Andric     return size;
12970b57cec5SDimitry Andric   else
12980b57cec5SDimitry Andric     return -size;
12990b57cec5SDimitry Andric }
13000b57cec5SDimitry Andric 
13010b57cec5SDimitry Andric static unsigned getSizeForEncoding(MCStreamer &streamer,
13020b57cec5SDimitry Andric                                    unsigned symbolEncoding) {
13030b57cec5SDimitry Andric   MCContext &context = streamer.getContext();
13040b57cec5SDimitry Andric   unsigned format = symbolEncoding & 0x0f;
13050b57cec5SDimitry Andric   switch (format) {
13060b57cec5SDimitry Andric   default: llvm_unreachable("Unknown Encoding");
13070b57cec5SDimitry Andric   case dwarf::DW_EH_PE_absptr:
13080b57cec5SDimitry Andric   case dwarf::DW_EH_PE_signed:
13090b57cec5SDimitry Andric     return context.getAsmInfo()->getCodePointerSize();
13100b57cec5SDimitry Andric   case dwarf::DW_EH_PE_udata2:
13110b57cec5SDimitry Andric   case dwarf::DW_EH_PE_sdata2:
13120b57cec5SDimitry Andric     return 2;
13130b57cec5SDimitry Andric   case dwarf::DW_EH_PE_udata4:
13140b57cec5SDimitry Andric   case dwarf::DW_EH_PE_sdata4:
13150b57cec5SDimitry Andric     return 4;
13160b57cec5SDimitry Andric   case dwarf::DW_EH_PE_udata8:
13170b57cec5SDimitry Andric   case dwarf::DW_EH_PE_sdata8:
13180b57cec5SDimitry Andric     return 8;
13190b57cec5SDimitry Andric   }
13200b57cec5SDimitry Andric }
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
13230b57cec5SDimitry Andric                        unsigned symbolEncoding, bool isEH) {
13240b57cec5SDimitry Andric   MCContext &context = streamer.getContext();
13250b57cec5SDimitry Andric   const MCAsmInfo *asmInfo = context.getAsmInfo();
13260b57cec5SDimitry Andric   const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
13270b57cec5SDimitry Andric                                                  symbolEncoding,
13280b57cec5SDimitry Andric                                                  streamer);
13290b57cec5SDimitry Andric   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
13300b57cec5SDimitry Andric   if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
13310b57cec5SDimitry Andric     emitAbsValue(streamer, v, size);
13320b57cec5SDimitry Andric   else
1333*5ffd83dbSDimitry Andric     streamer.emitValue(v, size);
13340b57cec5SDimitry Andric }
13350b57cec5SDimitry Andric 
13360b57cec5SDimitry Andric static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
13370b57cec5SDimitry Andric                             unsigned symbolEncoding) {
13380b57cec5SDimitry Andric   MCContext &context = streamer.getContext();
13390b57cec5SDimitry Andric   const MCAsmInfo *asmInfo = context.getAsmInfo();
13400b57cec5SDimitry Andric   const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
13410b57cec5SDimitry Andric                                                          symbolEncoding,
13420b57cec5SDimitry Andric                                                          streamer);
13430b57cec5SDimitry Andric   unsigned size = getSizeForEncoding(streamer, symbolEncoding);
1344*5ffd83dbSDimitry Andric   streamer.emitValue(v, size);
13450b57cec5SDimitry Andric }
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric namespace {
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric class FrameEmitterImpl {
13500b57cec5SDimitry Andric   int CFAOffset = 0;
13510b57cec5SDimitry Andric   int InitialCFAOffset = 0;
13520b57cec5SDimitry Andric   bool IsEH;
13530b57cec5SDimitry Andric   MCObjectStreamer &Streamer;
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric public:
13560b57cec5SDimitry Andric   FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
13570b57cec5SDimitry Andric       : IsEH(IsEH), Streamer(Streamer) {}
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric   /// Emit the unwind information in a compact way.
13600b57cec5SDimitry Andric   void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric   const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
13630b57cec5SDimitry Andric   void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
13640b57cec5SDimitry Andric                bool LastInSection, const MCSymbol &SectionStart);
1365*5ffd83dbSDimitry Andric   void emitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
13660b57cec5SDimitry Andric                            MCSymbol *BaseLabel);
1367*5ffd83dbSDimitry Andric   void emitCFIInstruction(const MCCFIInstruction &Instr);
13680b57cec5SDimitry Andric };
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric } // end anonymous namespace
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
1373*5ffd83dbSDimitry Andric   Streamer.emitInt8(Encoding);
13740b57cec5SDimitry Andric }
13750b57cec5SDimitry Andric 
1376*5ffd83dbSDimitry Andric void FrameEmitterImpl::emitCFIInstruction(const MCCFIInstruction &Instr) {
13770b57cec5SDimitry Andric   int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
13780b57cec5SDimitry Andric   auto *MRI = Streamer.getContext().getRegisterInfo();
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   switch (Instr.getOperation()) {
13810b57cec5SDimitry Andric   case MCCFIInstruction::OpRegister: {
13820b57cec5SDimitry Andric     unsigned Reg1 = Instr.getRegister();
13830b57cec5SDimitry Andric     unsigned Reg2 = Instr.getRegister2();
13840b57cec5SDimitry Andric     if (!IsEH) {
13850b57cec5SDimitry Andric       Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
13860b57cec5SDimitry Andric       Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
13870b57cec5SDimitry Andric     }
1388*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_register);
1389*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg1);
1390*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg2);
13910b57cec5SDimitry Andric     return;
13920b57cec5SDimitry Andric   }
13930b57cec5SDimitry Andric   case MCCFIInstruction::OpWindowSave:
1394*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_GNU_window_save);
13950b57cec5SDimitry Andric     return;
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   case MCCFIInstruction::OpNegateRAState:
1398*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_AARCH64_negate_ra_state);
13990b57cec5SDimitry Andric     return;
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   case MCCFIInstruction::OpUndefined: {
14020b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
1403*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_undefined);
1404*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg);
14050b57cec5SDimitry Andric     return;
14060b57cec5SDimitry Andric   }
14070b57cec5SDimitry Andric   case MCCFIInstruction::OpAdjustCfaOffset:
14080b57cec5SDimitry Andric   case MCCFIInstruction::OpDefCfaOffset: {
14090b57cec5SDimitry Andric     const bool IsRelative =
14100b57cec5SDimitry Andric       Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
14110b57cec5SDimitry Andric 
1412*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_def_cfa_offset);
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric     if (IsRelative)
14150b57cec5SDimitry Andric       CFAOffset += Instr.getOffset();
14160b57cec5SDimitry Andric     else
1417*5ffd83dbSDimitry Andric       CFAOffset = Instr.getOffset();
14180b57cec5SDimitry Andric 
1419*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(CFAOffset);
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric     return;
14220b57cec5SDimitry Andric   }
14230b57cec5SDimitry Andric   case MCCFIInstruction::OpDefCfa: {
14240b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
14250b57cec5SDimitry Andric     if (!IsEH)
14260b57cec5SDimitry Andric       Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1427*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_def_cfa);
1428*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg);
1429*5ffd83dbSDimitry Andric     CFAOffset = Instr.getOffset();
1430*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(CFAOffset);
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric     return;
14330b57cec5SDimitry Andric   }
14340b57cec5SDimitry Andric   case MCCFIInstruction::OpDefCfaRegister: {
14350b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
14360b57cec5SDimitry Andric     if (!IsEH)
14370b57cec5SDimitry Andric       Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
1438*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_def_cfa_register);
1439*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg);
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric     return;
14420b57cec5SDimitry Andric   }
14430b57cec5SDimitry Andric   case MCCFIInstruction::OpOffset:
14440b57cec5SDimitry Andric   case MCCFIInstruction::OpRelOffset: {
14450b57cec5SDimitry Andric     const bool IsRelative =
14460b57cec5SDimitry Andric       Instr.getOperation() == MCCFIInstruction::OpRelOffset;
14470b57cec5SDimitry Andric 
14480b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
14490b57cec5SDimitry Andric     if (!IsEH)
14500b57cec5SDimitry Andric       Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
14510b57cec5SDimitry Andric 
14520b57cec5SDimitry Andric     int Offset = Instr.getOffset();
14530b57cec5SDimitry Andric     if (IsRelative)
14540b57cec5SDimitry Andric       Offset -= CFAOffset;
14550b57cec5SDimitry Andric     Offset = Offset / dataAlignmentFactor;
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric     if (Offset < 0) {
1458*5ffd83dbSDimitry Andric       Streamer.emitInt8(dwarf::DW_CFA_offset_extended_sf);
1459*5ffd83dbSDimitry Andric       Streamer.emitULEB128IntValue(Reg);
1460*5ffd83dbSDimitry Andric       Streamer.emitSLEB128IntValue(Offset);
14610b57cec5SDimitry Andric     } else if (Reg < 64) {
1462*5ffd83dbSDimitry Andric       Streamer.emitInt8(dwarf::DW_CFA_offset + Reg);
1463*5ffd83dbSDimitry Andric       Streamer.emitULEB128IntValue(Offset);
14640b57cec5SDimitry Andric     } else {
1465*5ffd83dbSDimitry Andric       Streamer.emitInt8(dwarf::DW_CFA_offset_extended);
1466*5ffd83dbSDimitry Andric       Streamer.emitULEB128IntValue(Reg);
1467*5ffd83dbSDimitry Andric       Streamer.emitULEB128IntValue(Offset);
14680b57cec5SDimitry Andric     }
14690b57cec5SDimitry Andric     return;
14700b57cec5SDimitry Andric   }
14710b57cec5SDimitry Andric   case MCCFIInstruction::OpRememberState:
1472*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_remember_state);
14730b57cec5SDimitry Andric     return;
14740b57cec5SDimitry Andric   case MCCFIInstruction::OpRestoreState:
1475*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_restore_state);
14760b57cec5SDimitry Andric     return;
14770b57cec5SDimitry Andric   case MCCFIInstruction::OpSameValue: {
14780b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
1479*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_same_value);
1480*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Reg);
14810b57cec5SDimitry Andric     return;
14820b57cec5SDimitry Andric   }
14830b57cec5SDimitry Andric   case MCCFIInstruction::OpRestore: {
14840b57cec5SDimitry Andric     unsigned Reg = Instr.getRegister();
14850b57cec5SDimitry Andric     if (!IsEH)
14860b57cec5SDimitry Andric       Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
14870b57cec5SDimitry Andric     if (Reg < 64) {
1488*5ffd83dbSDimitry Andric       Streamer.emitInt8(dwarf::DW_CFA_restore | Reg);
14890b57cec5SDimitry Andric     } else {
1490*5ffd83dbSDimitry Andric       Streamer.emitInt8(dwarf::DW_CFA_restore_extended);
1491*5ffd83dbSDimitry Andric       Streamer.emitULEB128IntValue(Reg);
14920b57cec5SDimitry Andric     }
14930b57cec5SDimitry Andric     return;
14940b57cec5SDimitry Andric   }
14950b57cec5SDimitry Andric   case MCCFIInstruction::OpGnuArgsSize:
1496*5ffd83dbSDimitry Andric     Streamer.emitInt8(dwarf::DW_CFA_GNU_args_size);
1497*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(Instr.getOffset());
14980b57cec5SDimitry Andric     return;
14990b57cec5SDimitry Andric 
15000b57cec5SDimitry Andric   case MCCFIInstruction::OpEscape:
1501*5ffd83dbSDimitry Andric     Streamer.emitBytes(Instr.getValues());
15020b57cec5SDimitry Andric     return;
15030b57cec5SDimitry Andric   }
15040b57cec5SDimitry Andric   llvm_unreachable("Unhandled case in switch");
15050b57cec5SDimitry Andric }
15060b57cec5SDimitry Andric 
15070b57cec5SDimitry Andric /// Emit frame instructions to describe the layout of the frame.
1508*5ffd83dbSDimitry Andric void FrameEmitterImpl::emitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
15090b57cec5SDimitry Andric                                            MCSymbol *BaseLabel) {
15100b57cec5SDimitry Andric   for (const MCCFIInstruction &Instr : Instrs) {
15110b57cec5SDimitry Andric     MCSymbol *Label = Instr.getLabel();
15120b57cec5SDimitry Andric     // Throw out move if the label is invalid.
15130b57cec5SDimitry Andric     if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
15140b57cec5SDimitry Andric 
15150b57cec5SDimitry Andric     // Advance row if new location.
15160b57cec5SDimitry Andric     if (BaseLabel && Label) {
15170b57cec5SDimitry Andric       MCSymbol *ThisSym = Label;
15180b57cec5SDimitry Andric       if (ThisSym != BaseLabel) {
1519*5ffd83dbSDimitry Andric         Streamer.emitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
15200b57cec5SDimitry Andric         BaseLabel = ThisSym;
15210b57cec5SDimitry Andric       }
15220b57cec5SDimitry Andric     }
15230b57cec5SDimitry Andric 
1524*5ffd83dbSDimitry Andric     emitCFIInstruction(Instr);
15250b57cec5SDimitry Andric   }
15260b57cec5SDimitry Andric }
15270b57cec5SDimitry Andric 
15280b57cec5SDimitry Andric /// Emit the unwind information in a compact way.
15290b57cec5SDimitry Andric void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
15300b57cec5SDimitry Andric   MCContext &Context = Streamer.getContext();
15310b57cec5SDimitry Andric   const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
15320b57cec5SDimitry Andric 
15330b57cec5SDimitry Andric   // range-start range-length  compact-unwind-enc personality-func   lsda
15340b57cec5SDimitry Andric   //  _foo       LfooEnd-_foo  0x00000023          0                 0
15350b57cec5SDimitry Andric   //  _bar       LbarEnd-_bar  0x00000025         __gxx_personality  except_tab1
15360b57cec5SDimitry Andric   //
15370b57cec5SDimitry Andric   //   .section __LD,__compact_unwind,regular,debug
15380b57cec5SDimitry Andric   //
15390b57cec5SDimitry Andric   //   # compact unwind for _foo
15400b57cec5SDimitry Andric   //   .quad _foo
15410b57cec5SDimitry Andric   //   .set L1,LfooEnd-_foo
15420b57cec5SDimitry Andric   //   .long L1
15430b57cec5SDimitry Andric   //   .long 0x01010001
15440b57cec5SDimitry Andric   //   .quad 0
15450b57cec5SDimitry Andric   //   .quad 0
15460b57cec5SDimitry Andric   //
15470b57cec5SDimitry Andric   //   # compact unwind for _bar
15480b57cec5SDimitry Andric   //   .quad _bar
15490b57cec5SDimitry Andric   //   .set L2,LbarEnd-_bar
15500b57cec5SDimitry Andric   //   .long L2
15510b57cec5SDimitry Andric   //   .long 0x01020011
15520b57cec5SDimitry Andric   //   .quad __gxx_personality
15530b57cec5SDimitry Andric   //   .quad except_tab1
15540b57cec5SDimitry Andric 
15550b57cec5SDimitry Andric   uint32_t Encoding = Frame.CompactUnwindEncoding;
15560b57cec5SDimitry Andric   if (!Encoding) return;
15570b57cec5SDimitry Andric   bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
15580b57cec5SDimitry Andric 
15590b57cec5SDimitry Andric   // The encoding needs to know we have an LSDA.
15600b57cec5SDimitry Andric   if (!DwarfEHFrameOnly && Frame.Lsda)
15610b57cec5SDimitry Andric     Encoding |= 0x40000000;
15620b57cec5SDimitry Andric 
15630b57cec5SDimitry Andric   // Range Start
15640b57cec5SDimitry Andric   unsigned FDEEncoding = MOFI->getFDEEncoding();
15650b57cec5SDimitry Andric   unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
1566*5ffd83dbSDimitry Andric   Streamer.emitSymbolValue(Frame.Begin, Size);
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   // Range Length
1569*5ffd83dbSDimitry Andric   const MCExpr *Range =
1570*5ffd83dbSDimitry Andric       makeEndMinusStartExpr(Context, *Frame.Begin, *Frame.End, 0);
15710b57cec5SDimitry Andric   emitAbsValue(Streamer, Range, 4);
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric   // Compact Encoding
15740b57cec5SDimitry Andric   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1575*5ffd83dbSDimitry Andric   Streamer.emitIntValue(Encoding, Size);
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric   // Personality Function
15780b57cec5SDimitry Andric   Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
15790b57cec5SDimitry Andric   if (!DwarfEHFrameOnly && Frame.Personality)
1580*5ffd83dbSDimitry Andric     Streamer.emitSymbolValue(Frame.Personality, Size);
15810b57cec5SDimitry Andric   else
1582*5ffd83dbSDimitry Andric     Streamer.emitIntValue(0, Size); // No personality fn
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric   // LSDA
15850b57cec5SDimitry Andric   Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
15860b57cec5SDimitry Andric   if (!DwarfEHFrameOnly && Frame.Lsda)
1587*5ffd83dbSDimitry Andric     Streamer.emitSymbolValue(Frame.Lsda, Size);
15880b57cec5SDimitry Andric   else
1589*5ffd83dbSDimitry Andric     Streamer.emitIntValue(0, Size); // No LSDA
15900b57cec5SDimitry Andric }
15910b57cec5SDimitry Andric 
15920b57cec5SDimitry Andric static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
15930b57cec5SDimitry Andric   if (IsEH)
15940b57cec5SDimitry Andric     return 1;
15950b57cec5SDimitry Andric   switch (DwarfVersion) {
15960b57cec5SDimitry Andric   case 2:
15970b57cec5SDimitry Andric     return 1;
15980b57cec5SDimitry Andric   case 3:
15990b57cec5SDimitry Andric     return 3;
16000b57cec5SDimitry Andric   case 4:
16010b57cec5SDimitry Andric   case 5:
16020b57cec5SDimitry Andric     return 4;
16030b57cec5SDimitry Andric   }
16040b57cec5SDimitry Andric   llvm_unreachable("Unknown version");
16050b57cec5SDimitry Andric }
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
16080b57cec5SDimitry Andric   MCContext &context = Streamer.getContext();
16090b57cec5SDimitry Andric   const MCRegisterInfo *MRI = context.getRegisterInfo();
16100b57cec5SDimitry Andric   const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric   MCSymbol *sectionStart = context.createTempSymbol();
1613*5ffd83dbSDimitry Andric   Streamer.emitLabel(sectionStart);
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   MCSymbol *sectionEnd = context.createTempSymbol();
16160b57cec5SDimitry Andric 
1617*5ffd83dbSDimitry Andric   dwarf::DwarfFormat Format = IsEH ? dwarf::DWARF32 : context.getDwarfFormat();
1618*5ffd83dbSDimitry Andric   unsigned UnitLengthBytes = dwarf::getUnitLengthFieldByteSize(Format);
1619*5ffd83dbSDimitry Andric   unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format);
1620*5ffd83dbSDimitry Andric   bool IsDwarf64 = Format == dwarf::DWARF64;
1621*5ffd83dbSDimitry Andric 
1622*5ffd83dbSDimitry Andric   if (IsDwarf64)
1623*5ffd83dbSDimitry Andric     // DWARF64 mark
1624*5ffd83dbSDimitry Andric     Streamer.emitInt32(dwarf::DW_LENGTH_DWARF64);
1625*5ffd83dbSDimitry Andric 
16260b57cec5SDimitry Andric   // Length
1627*5ffd83dbSDimitry Andric   const MCExpr *Length = makeEndMinusStartExpr(context, *sectionStart,
1628*5ffd83dbSDimitry Andric                                                *sectionEnd, UnitLengthBytes);
1629*5ffd83dbSDimitry Andric   emitAbsValue(Streamer, Length, OffsetSize);
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric   // CIE ID
1632*5ffd83dbSDimitry Andric   uint64_t CIE_ID =
1633*5ffd83dbSDimitry Andric       IsEH ? 0 : (IsDwarf64 ? dwarf::DW64_CIE_ID : dwarf::DW_CIE_ID);
1634*5ffd83dbSDimitry Andric   Streamer.emitIntValue(CIE_ID, OffsetSize);
16350b57cec5SDimitry Andric 
16360b57cec5SDimitry Andric   // Version
16370b57cec5SDimitry Andric   uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
1638*5ffd83dbSDimitry Andric   Streamer.emitInt8(CIEVersion);
16390b57cec5SDimitry Andric 
16400b57cec5SDimitry Andric   if (IsEH) {
16410b57cec5SDimitry Andric     SmallString<8> Augmentation;
16420b57cec5SDimitry Andric     Augmentation += "z";
16430b57cec5SDimitry Andric     if (Frame.Personality)
16440b57cec5SDimitry Andric       Augmentation += "P";
16450b57cec5SDimitry Andric     if (Frame.Lsda)
16460b57cec5SDimitry Andric       Augmentation += "L";
16470b57cec5SDimitry Andric     Augmentation += "R";
16480b57cec5SDimitry Andric     if (Frame.IsSignalFrame)
16490b57cec5SDimitry Andric       Augmentation += "S";
16500b57cec5SDimitry Andric     if (Frame.IsBKeyFrame)
16510b57cec5SDimitry Andric       Augmentation += "B";
1652*5ffd83dbSDimitry Andric     Streamer.emitBytes(Augmentation);
16530b57cec5SDimitry Andric   }
1654*5ffd83dbSDimitry Andric   Streamer.emitInt8(0);
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   if (CIEVersion >= 4) {
16570b57cec5SDimitry Andric     // Address Size
1658*5ffd83dbSDimitry Andric     Streamer.emitInt8(context.getAsmInfo()->getCodePointerSize());
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric     // Segment Descriptor Size
1661*5ffd83dbSDimitry Andric     Streamer.emitInt8(0);
16620b57cec5SDimitry Andric   }
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric   // Code Alignment Factor
1665*5ffd83dbSDimitry Andric   Streamer.emitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
16660b57cec5SDimitry Andric 
16670b57cec5SDimitry Andric   // Data Alignment Factor
1668*5ffd83dbSDimitry Andric   Streamer.emitSLEB128IntValue(getDataAlignmentFactor(Streamer));
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   // Return Address Register
16710b57cec5SDimitry Andric   unsigned RAReg = Frame.RAReg;
16720b57cec5SDimitry Andric   if (RAReg == static_cast<unsigned>(INT_MAX))
16730b57cec5SDimitry Andric     RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
16740b57cec5SDimitry Andric 
16750b57cec5SDimitry Andric   if (CIEVersion == 1) {
16760b57cec5SDimitry Andric     assert(RAReg <= 255 &&
16770b57cec5SDimitry Andric            "DWARF 2 encodes return_address_register in one byte");
1678*5ffd83dbSDimitry Andric     Streamer.emitInt8(RAReg);
16790b57cec5SDimitry Andric   } else {
1680*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(RAReg);
16810b57cec5SDimitry Andric   }
16820b57cec5SDimitry Andric 
16830b57cec5SDimitry Andric   // Augmentation Data Length (optional)
16840b57cec5SDimitry Andric   unsigned augmentationLength = 0;
16850b57cec5SDimitry Andric   if (IsEH) {
16860b57cec5SDimitry Andric     if (Frame.Personality) {
16870b57cec5SDimitry Andric       // Personality Encoding
16880b57cec5SDimitry Andric       augmentationLength += 1;
16890b57cec5SDimitry Andric       // Personality
16900b57cec5SDimitry Andric       augmentationLength +=
16910b57cec5SDimitry Andric           getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
16920b57cec5SDimitry Andric     }
16930b57cec5SDimitry Andric     if (Frame.Lsda)
16940b57cec5SDimitry Andric       augmentationLength += 1;
16950b57cec5SDimitry Andric     // Encoding of the FDE pointers
16960b57cec5SDimitry Andric     augmentationLength += 1;
16970b57cec5SDimitry Andric 
1698*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(augmentationLength);
16990b57cec5SDimitry Andric 
17000b57cec5SDimitry Andric     // Augmentation Data (optional)
17010b57cec5SDimitry Andric     if (Frame.Personality) {
17020b57cec5SDimitry Andric       // Personality Encoding
17030b57cec5SDimitry Andric       emitEncodingByte(Streamer, Frame.PersonalityEncoding);
17040b57cec5SDimitry Andric       // Personality
17050b57cec5SDimitry Andric       EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
17060b57cec5SDimitry Andric     }
17070b57cec5SDimitry Andric 
17080b57cec5SDimitry Andric     if (Frame.Lsda)
17090b57cec5SDimitry Andric       emitEncodingByte(Streamer, Frame.LsdaEncoding);
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric     // Encoding of the FDE pointers
17120b57cec5SDimitry Andric     emitEncodingByte(Streamer, MOFI->getFDEEncoding());
17130b57cec5SDimitry Andric   }
17140b57cec5SDimitry Andric 
17150b57cec5SDimitry Andric   // Initial Instructions
17160b57cec5SDimitry Andric 
17170b57cec5SDimitry Andric   const MCAsmInfo *MAI = context.getAsmInfo();
17180b57cec5SDimitry Andric   if (!Frame.IsSimple) {
17190b57cec5SDimitry Andric     const std::vector<MCCFIInstruction> &Instructions =
17200b57cec5SDimitry Andric         MAI->getInitialFrameState();
1721*5ffd83dbSDimitry Andric     emitCFIInstructions(Instructions, nullptr);
17220b57cec5SDimitry Andric   }
17230b57cec5SDimitry Andric 
17240b57cec5SDimitry Andric   InitialCFAOffset = CFAOffset;
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric   // Padding
1727*5ffd83dbSDimitry Andric   Streamer.emitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
17280b57cec5SDimitry Andric 
1729*5ffd83dbSDimitry Andric   Streamer.emitLabel(sectionEnd);
17300b57cec5SDimitry Andric   return *sectionStart;
17310b57cec5SDimitry Andric }
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
17340b57cec5SDimitry Andric                                const MCDwarfFrameInfo &frame,
17350b57cec5SDimitry Andric                                bool LastInSection,
17360b57cec5SDimitry Andric                                const MCSymbol &SectionStart) {
17370b57cec5SDimitry Andric   MCContext &context = Streamer.getContext();
17380b57cec5SDimitry Andric   MCSymbol *fdeStart = context.createTempSymbol();
17390b57cec5SDimitry Andric   MCSymbol *fdeEnd = context.createTempSymbol();
17400b57cec5SDimitry Andric   const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
17410b57cec5SDimitry Andric 
17420b57cec5SDimitry Andric   CFAOffset = InitialCFAOffset;
17430b57cec5SDimitry Andric 
1744*5ffd83dbSDimitry Andric   dwarf::DwarfFormat Format = IsEH ? dwarf::DWARF32 : context.getDwarfFormat();
1745*5ffd83dbSDimitry Andric   unsigned OffsetSize = dwarf::getDwarfOffsetByteSize(Format);
17460b57cec5SDimitry Andric 
1747*5ffd83dbSDimitry Andric   if (Format == dwarf::DWARF64)
1748*5ffd83dbSDimitry Andric     // DWARF64 mark
1749*5ffd83dbSDimitry Andric     Streamer.emitInt32(dwarf::DW_LENGTH_DWARF64);
1750*5ffd83dbSDimitry Andric 
1751*5ffd83dbSDimitry Andric   // Length
1752*5ffd83dbSDimitry Andric   const MCExpr *Length = makeEndMinusStartExpr(context, *fdeStart, *fdeEnd, 0);
1753*5ffd83dbSDimitry Andric   emitAbsValue(Streamer, Length, OffsetSize);
1754*5ffd83dbSDimitry Andric 
1755*5ffd83dbSDimitry Andric   Streamer.emitLabel(fdeStart);
17560b57cec5SDimitry Andric 
17570b57cec5SDimitry Andric   // CIE Pointer
17580b57cec5SDimitry Andric   const MCAsmInfo *asmInfo = context.getAsmInfo();
17590b57cec5SDimitry Andric   if (IsEH) {
17600b57cec5SDimitry Andric     const MCExpr *offset =
1761*5ffd83dbSDimitry Andric         makeEndMinusStartExpr(context, cieStart, *fdeStart, 0);
1762*5ffd83dbSDimitry Andric     emitAbsValue(Streamer, offset, OffsetSize);
17630b57cec5SDimitry Andric   } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
17640b57cec5SDimitry Andric     const MCExpr *offset =
1765*5ffd83dbSDimitry Andric         makeEndMinusStartExpr(context, SectionStart, cieStart, 0);
1766*5ffd83dbSDimitry Andric     emitAbsValue(Streamer, offset, OffsetSize);
17670b57cec5SDimitry Andric   } else {
1768*5ffd83dbSDimitry Andric     Streamer.emitSymbolValue(&cieStart, OffsetSize,
1769480093f4SDimitry Andric                              asmInfo->needsDwarfSectionOffsetDirective());
17700b57cec5SDimitry Andric   }
17710b57cec5SDimitry Andric 
17720b57cec5SDimitry Andric   // PC Begin
17730b57cec5SDimitry Andric   unsigned PCEncoding =
17740b57cec5SDimitry Andric       IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
17750b57cec5SDimitry Andric   unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
17760b57cec5SDimitry Andric   emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric   // PC Range
17790b57cec5SDimitry Andric   const MCExpr *Range =
1780*5ffd83dbSDimitry Andric       makeEndMinusStartExpr(context, *frame.Begin, *frame.End, 0);
17810b57cec5SDimitry Andric   emitAbsValue(Streamer, Range, PCSize);
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric   if (IsEH) {
17840b57cec5SDimitry Andric     // Augmentation Data Length
17850b57cec5SDimitry Andric     unsigned augmentationLength = 0;
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric     if (frame.Lsda)
17880b57cec5SDimitry Andric       augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
17890b57cec5SDimitry Andric 
1790*5ffd83dbSDimitry Andric     Streamer.emitULEB128IntValue(augmentationLength);
17910b57cec5SDimitry Andric 
17920b57cec5SDimitry Andric     // Augmentation Data
17930b57cec5SDimitry Andric     if (frame.Lsda)
17940b57cec5SDimitry Andric       emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
17950b57cec5SDimitry Andric   }
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric   // Call Frame Instructions
1798*5ffd83dbSDimitry Andric   emitCFIInstructions(frame.Instructions, frame.Begin);
17990b57cec5SDimitry Andric 
18000b57cec5SDimitry Andric   // Padding
18010b57cec5SDimitry Andric   // The size of a .eh_frame section has to be a multiple of the alignment
18020b57cec5SDimitry Andric   // since a null CIE is interpreted as the end. Old systems overaligned
18030b57cec5SDimitry Andric   // .eh_frame, so we do too and account for it in the last FDE.
18040b57cec5SDimitry Andric   unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
1805*5ffd83dbSDimitry Andric   Streamer.emitValueToAlignment(Align);
18060b57cec5SDimitry Andric 
1807*5ffd83dbSDimitry Andric   Streamer.emitLabel(fdeEnd);
18080b57cec5SDimitry Andric }
18090b57cec5SDimitry Andric 
18100b57cec5SDimitry Andric namespace {
18110b57cec5SDimitry Andric 
18120b57cec5SDimitry Andric struct CIEKey {
18130b57cec5SDimitry Andric   static const CIEKey getEmptyKey() {
18140b57cec5SDimitry Andric     return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX),
18150b57cec5SDimitry Andric                   false);
18160b57cec5SDimitry Andric   }
18170b57cec5SDimitry Andric 
18180b57cec5SDimitry Andric   static const CIEKey getTombstoneKey() {
18190b57cec5SDimitry Andric     return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX),
18200b57cec5SDimitry Andric                   false);
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
18240b57cec5SDimitry Andric          unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
18250b57cec5SDimitry Andric          unsigned RAReg, bool IsBKeyFrame)
18260b57cec5SDimitry Andric       : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
18270b57cec5SDimitry Andric         LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
18280b57cec5SDimitry Andric         IsSimple(IsSimple), RAReg(RAReg), IsBKeyFrame(IsBKeyFrame) {}
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric   explicit CIEKey(const MCDwarfFrameInfo &Frame)
18310b57cec5SDimitry Andric       : Personality(Frame.Personality),
18320b57cec5SDimitry Andric         PersonalityEncoding(Frame.PersonalityEncoding),
18330b57cec5SDimitry Andric         LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
18340b57cec5SDimitry Andric         IsSimple(Frame.IsSimple), RAReg(Frame.RAReg),
18350b57cec5SDimitry Andric         IsBKeyFrame(Frame.IsBKeyFrame) {}
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric   StringRef PersonalityName() const {
18380b57cec5SDimitry Andric     if (!Personality)
18390b57cec5SDimitry Andric       return StringRef();
18400b57cec5SDimitry Andric     return Personality->getName();
18410b57cec5SDimitry Andric   }
18420b57cec5SDimitry Andric 
18430b57cec5SDimitry Andric   bool operator<(const CIEKey &Other) const {
18440b57cec5SDimitry Andric     return std::make_tuple(PersonalityName(), PersonalityEncoding, LsdaEncoding,
18450b57cec5SDimitry Andric                            IsSignalFrame, IsSimple, RAReg) <
18460b57cec5SDimitry Andric            std::make_tuple(Other.PersonalityName(), Other.PersonalityEncoding,
18470b57cec5SDimitry Andric                            Other.LsdaEncoding, Other.IsSignalFrame,
18480b57cec5SDimitry Andric                            Other.IsSimple, Other.RAReg);
18490b57cec5SDimitry Andric   }
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric   const MCSymbol *Personality;
18520b57cec5SDimitry Andric   unsigned PersonalityEncoding;
18530b57cec5SDimitry Andric   unsigned LsdaEncoding;
18540b57cec5SDimitry Andric   bool IsSignalFrame;
18550b57cec5SDimitry Andric   bool IsSimple;
18560b57cec5SDimitry Andric   unsigned RAReg;
18570b57cec5SDimitry Andric   bool IsBKeyFrame;
18580b57cec5SDimitry Andric };
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric } // end anonymous namespace
18610b57cec5SDimitry Andric 
18620b57cec5SDimitry Andric namespace llvm {
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric template <> struct DenseMapInfo<CIEKey> {
18650b57cec5SDimitry Andric   static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
18660b57cec5SDimitry Andric   static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric   static unsigned getHashValue(const CIEKey &Key) {
18690b57cec5SDimitry Andric     return static_cast<unsigned>(hash_combine(
18700b57cec5SDimitry Andric         Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
18710b57cec5SDimitry Andric         Key.IsSignalFrame, Key.IsSimple, Key.RAReg, Key.IsBKeyFrame));
18720b57cec5SDimitry Andric   }
18730b57cec5SDimitry Andric 
18740b57cec5SDimitry Andric   static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
18750b57cec5SDimitry Andric     return LHS.Personality == RHS.Personality &&
18760b57cec5SDimitry Andric            LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
18770b57cec5SDimitry Andric            LHS.LsdaEncoding == RHS.LsdaEncoding &&
18780b57cec5SDimitry Andric            LHS.IsSignalFrame == RHS.IsSignalFrame &&
18790b57cec5SDimitry Andric            LHS.IsSimple == RHS.IsSimple && LHS.RAReg == RHS.RAReg &&
18800b57cec5SDimitry Andric            LHS.IsBKeyFrame == RHS.IsBKeyFrame;
18810b57cec5SDimitry Andric   }
18820b57cec5SDimitry Andric };
18830b57cec5SDimitry Andric 
18840b57cec5SDimitry Andric } // end namespace llvm
18850b57cec5SDimitry Andric 
18860b57cec5SDimitry Andric void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
18870b57cec5SDimitry Andric                                bool IsEH) {
18880b57cec5SDimitry Andric   Streamer.generateCompactUnwindEncodings(MAB);
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric   MCContext &Context = Streamer.getContext();
18910b57cec5SDimitry Andric   const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
18920b57cec5SDimitry Andric   const MCAsmInfo *AsmInfo = Context.getAsmInfo();
18930b57cec5SDimitry Andric   FrameEmitterImpl Emitter(IsEH, Streamer);
18940b57cec5SDimitry Andric   ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric   // Emit the compact unwind info if available.
18970b57cec5SDimitry Andric   bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
18980b57cec5SDimitry Andric   if (IsEH && MOFI->getCompactUnwindSection()) {
18990b57cec5SDimitry Andric     bool SectionEmitted = false;
19000b57cec5SDimitry Andric     for (const MCDwarfFrameInfo &Frame : FrameArray) {
19010b57cec5SDimitry Andric       if (Frame.CompactUnwindEncoding == 0) continue;
19020b57cec5SDimitry Andric       if (!SectionEmitted) {
19030b57cec5SDimitry Andric         Streamer.SwitchSection(MOFI->getCompactUnwindSection());
1904*5ffd83dbSDimitry Andric         Streamer.emitValueToAlignment(AsmInfo->getCodePointerSize());
19050b57cec5SDimitry Andric         SectionEmitted = true;
19060b57cec5SDimitry Andric       }
19070b57cec5SDimitry Andric       NeedsEHFrameSection |=
19080b57cec5SDimitry Andric         Frame.CompactUnwindEncoding ==
19090b57cec5SDimitry Andric           MOFI->getCompactUnwindDwarfEHFrameOnly();
19100b57cec5SDimitry Andric       Emitter.EmitCompactUnwind(Frame);
19110b57cec5SDimitry Andric     }
19120b57cec5SDimitry Andric   }
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric   if (!NeedsEHFrameSection) return;
19150b57cec5SDimitry Andric 
19160b57cec5SDimitry Andric   MCSection &Section =
19170b57cec5SDimitry Andric       IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
19180b57cec5SDimitry Andric            : *MOFI->getDwarfFrameSection();
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric   Streamer.SwitchSection(&Section);
19210b57cec5SDimitry Andric   MCSymbol *SectionStart = Context.createTempSymbol();
1922*5ffd83dbSDimitry Andric   Streamer.emitLabel(SectionStart);
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric   DenseMap<CIEKey, const MCSymbol *> CIEStarts;
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric   const MCSymbol *DummyDebugKey = nullptr;
19270b57cec5SDimitry Andric   bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
19280b57cec5SDimitry Andric   // Sort the FDEs by their corresponding CIE before we emit them.
19290b57cec5SDimitry Andric   // This isn't technically necessary according to the DWARF standard,
19300b57cec5SDimitry Andric   // but the Android libunwindstack rejects eh_frame sections where
19310b57cec5SDimitry Andric   // an FDE refers to a CIE other than the closest previous CIE.
19320b57cec5SDimitry Andric   std::vector<MCDwarfFrameInfo> FrameArrayX(FrameArray.begin(), FrameArray.end());
19330b57cec5SDimitry Andric   llvm::stable_sort(FrameArrayX,
19340b57cec5SDimitry Andric                     [](const MCDwarfFrameInfo &X, const MCDwarfFrameInfo &Y) {
19350b57cec5SDimitry Andric                       return CIEKey(X) < CIEKey(Y);
19360b57cec5SDimitry Andric                     });
19370b57cec5SDimitry Andric   for (auto I = FrameArrayX.begin(), E = FrameArrayX.end(); I != E;) {
19380b57cec5SDimitry Andric     const MCDwarfFrameInfo &Frame = *I;
19390b57cec5SDimitry Andric     ++I;
19400b57cec5SDimitry Andric     if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
19410b57cec5SDimitry Andric           MOFI->getCompactUnwindDwarfEHFrameOnly())
19420b57cec5SDimitry Andric       // Don't generate an EH frame if we don't need one. I.e., it's taken care
19430b57cec5SDimitry Andric       // of by the compact unwind encoding.
19440b57cec5SDimitry Andric       continue;
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric     CIEKey Key(Frame);
19470b57cec5SDimitry Andric     const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
19480b57cec5SDimitry Andric     if (!CIEStart)
19490b57cec5SDimitry Andric       CIEStart = &Emitter.EmitCIE(Frame);
19500b57cec5SDimitry Andric 
19510b57cec5SDimitry Andric     Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
19520b57cec5SDimitry Andric   }
19530b57cec5SDimitry Andric }
19540b57cec5SDimitry Andric 
19550b57cec5SDimitry Andric void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
19560b57cec5SDimitry Andric                                          uint64_t AddrDelta) {
19570b57cec5SDimitry Andric   MCContext &Context = Streamer.getContext();
19580b57cec5SDimitry Andric   SmallString<256> Tmp;
19590b57cec5SDimitry Andric   raw_svector_ostream OS(Tmp);
19600b57cec5SDimitry Andric   MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
1961*5ffd83dbSDimitry Andric   Streamer.emitBytes(OS.str());
19620b57cec5SDimitry Andric }
19630b57cec5SDimitry Andric 
19640b57cec5SDimitry Andric void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
19650b57cec5SDimitry Andric                                            uint64_t AddrDelta, raw_ostream &OS,
19660b57cec5SDimitry Andric                                            uint32_t *Offset, uint32_t *Size) {
19670b57cec5SDimitry Andric   // Scale the address delta by the minimum instruction length.
19680b57cec5SDimitry Andric   AddrDelta = ScaleAddrDelta(Context, AddrDelta);
19690b57cec5SDimitry Andric 
19700b57cec5SDimitry Andric   bool WithFixups = false;
19710b57cec5SDimitry Andric   if (Offset && Size)
19720b57cec5SDimitry Andric     WithFixups = true;
19730b57cec5SDimitry Andric 
19740b57cec5SDimitry Andric   support::endianness E =
19750b57cec5SDimitry Andric       Context.getAsmInfo()->isLittleEndian() ? support::little : support::big;
19760b57cec5SDimitry Andric   if (AddrDelta == 0) {
19770b57cec5SDimitry Andric     if (WithFixups) {
19780b57cec5SDimitry Andric       *Offset = 0;
19790b57cec5SDimitry Andric       *Size = 0;
19800b57cec5SDimitry Andric     }
19810b57cec5SDimitry Andric   } else if (isUIntN(6, AddrDelta)) {
19820b57cec5SDimitry Andric     uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
19830b57cec5SDimitry Andric     if (WithFixups) {
19840b57cec5SDimitry Andric       *Offset = OS.tell();
19850b57cec5SDimitry Andric       *Size = 6;
19860b57cec5SDimitry Andric       OS << uint8_t(dwarf::DW_CFA_advance_loc);
19870b57cec5SDimitry Andric     } else
19880b57cec5SDimitry Andric       OS << Opcode;
19890b57cec5SDimitry Andric   } else if (isUInt<8>(AddrDelta)) {
19900b57cec5SDimitry Andric     OS << uint8_t(dwarf::DW_CFA_advance_loc1);
19910b57cec5SDimitry Andric     if (WithFixups) {
19920b57cec5SDimitry Andric       *Offset = OS.tell();
19930b57cec5SDimitry Andric       *Size = 8;
19940b57cec5SDimitry Andric       OS.write_zeros(1);
19950b57cec5SDimitry Andric     } else
19960b57cec5SDimitry Andric       OS << uint8_t(AddrDelta);
19970b57cec5SDimitry Andric   } else if (isUInt<16>(AddrDelta)) {
19980b57cec5SDimitry Andric     OS << uint8_t(dwarf::DW_CFA_advance_loc2);
19990b57cec5SDimitry Andric     if (WithFixups) {
20000b57cec5SDimitry Andric       *Offset = OS.tell();
20010b57cec5SDimitry Andric       *Size = 16;
20020b57cec5SDimitry Andric       OS.write_zeros(2);
20030b57cec5SDimitry Andric     } else
20040b57cec5SDimitry Andric       support::endian::write<uint16_t>(OS, AddrDelta, E);
20050b57cec5SDimitry Andric   } else {
20060b57cec5SDimitry Andric     assert(isUInt<32>(AddrDelta));
20070b57cec5SDimitry Andric     OS << uint8_t(dwarf::DW_CFA_advance_loc4);
20080b57cec5SDimitry Andric     if (WithFixups) {
20090b57cec5SDimitry Andric       *Offset = OS.tell();
20100b57cec5SDimitry Andric       *Size = 32;
20110b57cec5SDimitry Andric       OS.write_zeros(4);
20120b57cec5SDimitry Andric     } else
20130b57cec5SDimitry Andric       support::endian::write<uint32_t>(OS, AddrDelta, E);
20140b57cec5SDimitry Andric   }
20150b57cec5SDimitry Andric }
2016