blob: bcc7c45afc01bc70a149a149376ad61ee4753116 [file] [log] [blame]
Kevin Enderbye5930f12010-07-28 20:55:35 +00001//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Kevin Enderbye5930f12010-07-28 20:55:35 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000010#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/Hashing.h"
Scott Linder16c7bda2018-02-23 23:01:06 +000013#include "llvm/ADT/Optional.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000016#include "llvm/ADT/SmallVector.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000019#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000021#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000024#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000025#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000026#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000027#include "llvm/MC/MCSection.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000028#include "llvm/MC/MCStreamer.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000029#include "llvm/MC/MCSymbol.h"
Paul Robinson1f900292018-02-06 20:29:21 +000030#include "llvm/MC/StringTableBuilder.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/Endian.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000033#include "llvm/Support/EndianStream.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000035#include "llvm/Support/LEB128.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000036#include "llvm/Support/MathExtras.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000037#include "llvm/Support/Path.h"
38#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include <cassert>
41#include <cstdint>
42#include <string>
43#include <utility>
44#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000045
Kevin Enderbye5930f12010-07-28 20:55:35 +000046using namespace llvm;
47
Paul Robinson1f900292018-02-06 20:29:21 +000048/// Manage the .debug_line_str section contents, if we use it.
49class llvm::MCDwarfLineStr {
50 MCSymbol *LineStrLabel = nullptr;
51 StringTableBuilder LineStrings{StringTableBuilder::DWARF};
52 bool UseRelocs = false;
53
54public:
55 /// Construct an instance that can emit .debug_line_str (for use in a normal
56 /// v5 line table).
57 explicit MCDwarfLineStr(MCContext &Ctx) {
58 UseRelocs = Ctx.getAsmInfo()->doesDwarfUseRelocationsAcrossSections();
59 if (UseRelocs)
60 LineStrLabel =
61 Ctx.getObjectFileInfo()->getDwarfLineStrSection()->getBeginSymbol();
62 }
63
64 /// Emit a reference to the string.
65 void emitRef(MCStreamer *MCOS, StringRef Path);
66
67 /// Emit the .debug_line_str section if appropriate.
68 void emitSection(MCStreamer *MCOS);
69};
70
Ulrich Weigand32d725b2013-06-12 14:46:54 +000071static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000072 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000073 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000074 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000075 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000076 // TODO: report this error, but really only once.
77 ;
78 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000079 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000080}
81
82//
83// This is called when an instruction is assembled into the specified section
84// and if there is information from the last .loc directive that has yet to have
85// a line entry made for it is made.
86//
David Majnemer8cc30782016-01-21 01:59:03 +000087void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000088 if (!MCOS->getContext().getDwarfLocSeen())
89 return;
90
91 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000092 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000093 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000094 MCOS->EmitLabel(LineSym);
95
96 // Get the current .loc info saved in the context.
97 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
98
99 // Create a (local) line entry with the symbol and the current .loc info.
David Majnemer8cc30782016-01-21 01:59:03 +0000100 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000101
102 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +0000103 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000104
Kevin Enderbye46564a2010-09-30 16:52:03 +0000105 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +0000106 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +0000107 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +0000108 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +0000109 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110}
111
112//
113// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000114//
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000115static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
116 const MCSymbol &Start,
117 const MCSymbol &End,
118 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000119 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
120 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000121 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000122 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000123 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000124 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000125 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000126 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000127 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000128 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000129 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000130 return Res3;
131}
132
Kevin Enderbye46564a2010-09-30 16:52:03 +0000133//
Paul Robinson1f900292018-02-06 20:29:21 +0000134// This helper routine returns an expression of Start + IntVal .
135//
136static inline const MCExpr *
137makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal) {
138 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
139 const MCExpr *LHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
140 const MCExpr *RHS = MCConstantExpr::create(IntVal, Ctx);
141 const MCExpr *Res = MCBinaryExpr::create(MCBinaryExpr::Add, LHS, RHS, Ctx);
142 return Res;
143}
144
145//
Kevin Enderbye46564a2010-09-30 16:52:03 +0000146// This emits the Dwarf line table for the specified section from the entries
147// in the LineSection.
148//
David Blaikief4ad6982014-03-12 22:35:23 +0000149static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000150EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000151 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000152 unsigned FileNum = 1;
153 unsigned LastLine = 1;
154 unsigned Column = 0;
155 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
156 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000157 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000158 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000159
David Majnemer8cc30782016-01-21 01:59:03 +0000160 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000161 for (const MCDwarfLineEntry &LineEntry : LineEntries) {
162 int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000163
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000164 if (FileNum != LineEntry.getFileNum()) {
165 FileNum = LineEntry.getFileNum();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000166 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000167 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000168 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000169 if (Column != LineEntry.getColumn()) {
170 Column = LineEntry.getColumn();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000171 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000172 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000173 }
Dehao Chen6e0c8442016-10-07 15:21:31 +0000174 if (Discriminator != LineEntry.getDiscriminator() &&
175 MCOS->getContext().getDwarfVersion() >= 4) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000176 Discriminator = LineEntry.getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000177 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000178 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
179 MCOS->EmitULEB128IntValue(Size + 1);
180 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
181 MCOS->EmitULEB128IntValue(Discriminator);
182 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000183 if (Isa != LineEntry.getIsa()) {
184 Isa = LineEntry.getIsa();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000185 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000186 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000187 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000188 if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
189 Flags = LineEntry.getFlags();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000190 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
191 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000192 if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000193 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000194 if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000195 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000196 if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000197 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
198
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000199 MCSymbol *Label = LineEntry.getLabel();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000200
201 // At this point we want to emit/create the sequence to encode the delta in
202 // line numbers and the increment of the address from the previous Label
203 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000204 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000205 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000206 asmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000207
Dehao Chen1b54fce2016-04-28 22:09:37 +0000208 Discriminator = 0;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000209 LastLine = LineEntry.getLine();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000210 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000211 }
212
213 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000214 // Use the section end label to compute the address delta and use INT64_MAX
215 // as the line delta which is the signal that this is actually a
216 // DW_LNE_end_sequence.
217 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000218
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000219 // Switch back the dwarf line section, in case endSection had to switch the
220 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000221 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000222 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000223
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000224 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000225 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000226 AsmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000227}
228
229//
230// This emits the Dwarf file and the line tables.
231//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000232void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
233 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000234 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000235
David Blaikie8bf66c42014-04-01 07:35:52 +0000236 auto &LineTables = context.getMCDwarfLineTables();
237
238 // Bail out early so we don't switch to the debug_line section needlessly and
239 // in doing so create an unnecessary (if empty) section.
240 if (LineTables.empty())
241 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000242
Paul Robinson1f900292018-02-06 20:29:21 +0000243 // In a v5 non-split line table, put the strings in a separate section.
244 Optional<MCDwarfLineStr> LineStr;
245 if (context.getDwarfVersion() >= 5)
246 LineStr = MCDwarfLineStr(context);
247
David Blaikie11765bc2014-03-13 17:55:28 +0000248 // Switch to the section where the table will be emitted into.
249 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
250
Manman Ren4e042a62013-02-05 21:52:47 +0000251 // Handle the rest of the Compile Units.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000252 for (const auto &CUIDTablePair : LineTables) {
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000253 CUIDTablePair.second.EmitCU(MCOS, Params, LineStr);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000254 }
Paul Robinson1f900292018-02-06 20:29:21 +0000255
256 if (LineStr)
257 LineStr->emitSection(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000258}
259
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000260void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params,
261 MCSection *Section) const {
Ali Tamur783d84b2019-04-19 02:26:56 +0000262 if (!HasSplitLineTable)
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000263 return;
Paul Robinson1f900292018-02-06 20:29:21 +0000264 Optional<MCDwarfLineStr> NoLineStr(None);
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000265 MCOS.SwitchSection(Section);
Paul Robinson1f900292018-02-06 20:29:21 +0000266 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None, NoLineStr).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000267}
268
Frederic Rissa5ab8442015-08-07 15:14:08 +0000269std::pair<MCSymbol *, MCSymbol *>
Paul Robinson1f900292018-02-06 20:29:21 +0000270MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
271 Optional<MCDwarfLineStr> &LineStr) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000272 static const char StandardOpcodeLengths[] = {
273 0, // length of DW_LNS_copy
274 1, // length of DW_LNS_advance_pc
275 1, // length of DW_LNS_advance_line
276 1, // length of DW_LNS_set_file
277 1, // length of DW_LNS_set_column
278 0, // length of DW_LNS_negate_stmt
279 0, // length of DW_LNS_set_basic_block
280 0, // length of DW_LNS_const_add_pc
281 1, // length of DW_LNS_fixed_advance_pc
282 0, // length of DW_LNS_set_prologue_end
283 0, // length of DW_LNS_set_epilogue_begin
284 1 // DW_LNS_set_isa
285 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000286 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000287 (Params.DWARF2LineOpcodeBase - 1U));
Paul Robinson1f900292018-02-06 20:29:21 +0000288 return Emit(
289 MCOS, Params,
290 makeArrayRef(StandardOpcodeLengths, Params.DWARF2LineOpcodeBase - 1),
291 LineStr);
David Blaikie8287aff2014-03-18 02:13:23 +0000292}
293
Rafael Espindolac23174b2014-08-15 15:12:13 +0000294static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
295 MCContext &Context = OS.getContext();
296 assert(!isa<MCSymbolRefExpr>(Expr));
297 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
298 return Expr;
299
Jim Grosbach6f482002015-05-18 18:43:14 +0000300 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000301 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000302 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000303}
304
305static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
306 const MCExpr *ABS = forceExpAbs(OS, Value);
307 OS.EmitValue(ABS, Size);
308}
309
Paul Robinson1f900292018-02-06 20:29:21 +0000310void MCDwarfLineStr::emitSection(MCStreamer *MCOS) {
311 // Switch to the .debug_line_str section.
312 MCOS->SwitchSection(
313 MCOS->getContext().getObjectFileInfo()->getDwarfLineStrSection());
314 // Emit the strings without perturbing the offsets we used.
315 LineStrings.finalizeInOrder();
316 SmallString<0> Data;
317 Data.resize(LineStrings.getSize());
318 LineStrings.write((uint8_t *)Data.data());
319 MCOS->EmitBinaryData(Data.str());
320}
321
322void MCDwarfLineStr::emitRef(MCStreamer *MCOS, StringRef Path) {
323 int RefSize = 4; // FIXME: Support DWARF-64
324 size_t Offset = LineStrings.add(Path);
325 if (UseRelocs) {
326 MCContext &Ctx = MCOS->getContext();
327 MCOS->EmitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
328 } else
329 MCOS->EmitIntValue(Offset, RefSize);
330}
331
Paul Robinson2bf8f492018-01-30 21:39:28 +0000332void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000333 // First the directory table.
Paul Robinson2bf8f492018-01-30 21:39:28 +0000334 for (auto &Dir : MCDwarfDirs) {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000335 MCOS->EmitBytes(Dir); // The DirectoryName, and...
336 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
337 }
338 MCOS->EmitIntValue(0, 1); // Terminate the directory list.
339
340 // Second the file table.
341 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
342 assert(!MCDwarfFiles[i].Name.empty());
343 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName and...
344 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
345 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
346 MCOS->EmitIntValue(0, 1); // Last modification timestamp (always 0).
347 MCOS->EmitIntValue(0, 1); // File size (always 0).
348 }
349 MCOS->EmitIntValue(0, 1); // Terminate the file list.
350}
351
Paul Robinsonb271f312018-03-29 17:16:41 +0000352static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000353 bool EmitMD5, bool HasSource,
Paul Robinsonb271f312018-03-29 17:16:41 +0000354 Optional<MCDwarfLineStr> &LineStr) {
355 assert(!DwarfFile.Name.empty());
356 if (LineStr)
357 LineStr->emitRef(MCOS, DwarfFile.Name);
358 else {
359 MCOS->EmitBytes(DwarfFile.Name); // FileName and...
360 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
361 }
362 MCOS->EmitULEB128IntValue(DwarfFile.DirIndex); // Directory number.
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000363 if (EmitMD5) {
Eric Christopher798e83b2019-04-04 23:34:38 +0000364 const MD5::MD5Result &Cksum = *DwarfFile.Checksum;
Paul Robinsonb271f312018-03-29 17:16:41 +0000365 MCOS->EmitBinaryData(
Eric Christopher798e83b2019-04-04 23:34:38 +0000366 StringRef(reinterpret_cast<const char *>(Cksum.Bytes.data()),
367 Cksum.Bytes.size()));
Paul Robinsonb271f312018-03-29 17:16:41 +0000368 }
369 if (HasSource) {
370 if (LineStr)
371 LineStr->emitRef(MCOS, DwarfFile.Source.getValueOr(StringRef()));
372 else {
373 MCOS->EmitBytes(
374 DwarfFile.Source.getValueOr(StringRef())); // Source and...
375 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
376 }
377 }
378}
379
Paul Robinson1f900292018-02-06 20:29:21 +0000380void MCDwarfLineTableHeader::emitV5FileDirTables(
Eric Christopher492cad52019-04-12 01:02:02 +0000381 MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr) const {
Paul Robinson1f900292018-02-06 20:29:21 +0000382 // The directory format, which is just a list of the directory paths. In a
383 // non-split object, these are references to .debug_line_str; in a split
384 // object, they are inline strings.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000385 MCOS->EmitIntValue(1, 1);
386 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
Paul Robinson1f900292018-02-06 20:29:21 +0000387 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
388 : dwarf::DW_FORM_string);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000389 MCOS->EmitULEB128IntValue(MCDwarfDirs.size() + 1);
Paul Robinsonf69316c2018-06-12 16:09:03 +0000390 // Try not to emit an empty compilation directory.
Eric Christopher492cad52019-04-12 01:02:02 +0000391 const StringRef CompDir = CompilationDir.empty()
392 ? MCOS->getContext().getCompilationDir()
393 : StringRef(CompilationDir);
Paul Robinson1f900292018-02-06 20:29:21 +0000394 if (LineStr) {
395 // Record path strings, emit references here.
Paul Robinsonf69316c2018-06-12 16:09:03 +0000396 LineStr->emitRef(MCOS, CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000397 for (const auto &Dir : MCDwarfDirs)
Paul Robinson1f900292018-02-06 20:29:21 +0000398 LineStr->emitRef(MCOS, Dir);
399 } else {
Paul Robinsonf69316c2018-06-12 16:09:03 +0000400 // The list of directory paths. Compilation directory comes first.
401 MCOS->EmitBytes(CompDir);
Paul Robinson1f900292018-02-06 20:29:21 +0000402 MCOS->EmitBytes(StringRef("\0", 1));
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000403 for (const auto &Dir : MCDwarfDirs) {
Paul Robinson1f900292018-02-06 20:29:21 +0000404 MCOS->EmitBytes(Dir); // The DirectoryName, and...
405 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
406 }
Paul Robinson795ab0d2017-12-05 20:35:00 +0000407 }
408
409 // The file format, which is the inline null-terminated filename and a
410 // directory index. We don't track file size/timestamp so don't emit them
Scott Linder16c7bda2018-02-23 23:01:06 +0000411 // in the v5 table. Emit MD5 checksums and source if we have them.
412 uint64_t Entries = 2;
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000413 if (HasAllMD5)
Scott Linder16c7bda2018-02-23 23:01:06 +0000414 Entries += 1;
415 if (HasSource)
416 Entries += 1;
417 MCOS->EmitIntValue(Entries, 1);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000418 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
Paul Robinson1f900292018-02-06 20:29:21 +0000419 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
420 : dwarf::DW_FORM_string);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000421 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_directory_index);
422 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_udata);
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000423 if (HasAllMD5) {
Paul Robinson29f5f982018-01-09 23:31:48 +0000424 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_MD5);
425 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_data16);
426 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000427 if (HasSource) {
428 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
429 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
430 : dwarf::DW_FORM_string);
431 }
Paul Robinsonb271f312018-03-29 17:16:41 +0000432 // Then the counted list of files. The root file is file #0, then emit the
Paul Robinson1ca25762019-03-01 20:58:04 +0000433 // files as provide by .file directives.
434 // MCDwarfFiles has an unused element [0] so use size() not size()+1.
435 // But sometimes MCDwarfFiles is empty, in which case we still emit one file.
436 MCOS->EmitULEB128IntValue(MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size());
437 // To accommodate assembler source written for DWARF v4 but trying to emit
438 // v5: If we didn't see a root file explicitly, replicate file #1.
439 assert((!RootFile.Name.empty() || MCDwarfFiles.size() >= 1) &&
440 "No root file and no .file directives");
Paul Robinsonf69316c2018-06-12 16:09:03 +0000441 emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000442 HasAllMD5, HasSource, LineStr);
Paul Robinsonb271f312018-03-29 17:16:41 +0000443 for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000444 emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000445}
446
David Blaikie8287aff2014-03-18 02:13:23 +0000447std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000448MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
Paul Robinson1f900292018-02-06 20:29:21 +0000449 ArrayRef<char> StandardOpcodeLengths,
450 Optional<MCDwarfLineStr> &LineStr) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000451 MCContext &context = MCOS->getContext();
452
453 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000454 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000455 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000456 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000457 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000458 MCOS->EmitLabel(LineStartSym);
459
460 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000461 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000462
463 // The first 4 bytes is the total length of the information for this
464 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000465 emitAbsValue(*MCOS,
466 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000467
Jonas Devlieghere49ff4d92018-08-08 21:16:50 +0000468 // Next 2 bytes is the Version.
Jonas Devlieghere64c901d2018-09-13 13:13:50 +0000469 unsigned LineTableVersion = context.getDwarfVersion();
Paul Robinson68ba7722017-12-04 21:27:46 +0000470 MCOS->EmitIntValue(LineTableVersion, 2);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000471
Paul Robinson795ab0d2017-12-05 20:35:00 +0000472 // Keep track of the bytes between the very start and where the header length
473 // comes out.
474 unsigned PreHeaderLengthBytes = 4 + 2;
475
476 // In v5, we get address info next.
477 if (LineTableVersion >= 5) {
478 MCOS->EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
479 MCOS->EmitIntValue(0, 1); // Segment selector; same as EmitGenDwarfAranges.
480 PreHeaderLengthBytes += 2;
481 }
482
Kevin Enderbye46564a2010-09-30 16:52:03 +0000483 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000484 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000485
Paul Robinson795ab0d2017-12-05 20:35:00 +0000486 // Length of the prologue, is the next 4 bytes. This is actually the length
487 // from after the length word, to the end of the prologue.
488 emitAbsValue(*MCOS,
489 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
490 (PreHeaderLengthBytes + 4)),
491 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000492
493 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000494 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Fangrui Songf78650a2018-07-30 19:41:25 +0000495 // maximum_operations_per_instruction
Paul Robinson68ba7722017-12-04 21:27:46 +0000496 // For non-VLIW architectures this field is always 1.
497 // FIXME: VLIW architectures need to update this field accordingly.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000498 if (LineTableVersion >= 4)
Paul Robinson68ba7722017-12-04 21:27:46 +0000499 MCOS->EmitIntValue(1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000500 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000501 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
502 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000503 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000504
505 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000506 for (char Length : StandardOpcodeLengths)
507 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000508
Paul Robinson795ab0d2017-12-05 20:35:00 +0000509 // Put out the directory and file tables. The formats vary depending on
510 // the version.
511 if (LineTableVersion >= 5)
Eric Christopher492cad52019-04-12 01:02:02 +0000512 emitV5FileDirTables(MCOS, LineStr);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000513 else
Paul Robinson2bf8f492018-01-30 21:39:28 +0000514 emitV2FileDirTables(MCOS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000515
516 // This is the end of the prologue, so set the value of the symbol at the
517 // end of the prologue (that was used in a previous expression).
518 MCOS->EmitLabel(ProEndSym);
519
David Blaikie5cff0e62014-03-13 21:47:12 +0000520 return std::make_pair(LineStartSym, LineEndSym);
521}
522
Frederic Rissa5ab8442015-08-07 15:14:08 +0000523void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
Paul Robinson1f900292018-02-06 20:29:21 +0000524 MCDwarfLineTableParams Params,
525 Optional<MCDwarfLineStr> &LineStr) const {
526 MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000527
Kevin Enderbye46564a2010-09-30 16:52:03 +0000528 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000529 for (const auto &LineSec : MCLineSections.getMCLineEntries())
530 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000531
Kevin Enderbye46564a2010-09-30 16:52:03 +0000532 // This is the end of the section, so set the value of the symbol at the end
533 // of this section (that was used in a previous expression).
534 MCOS->EmitLabel(LineEndSym);
535}
536
Paul Robinson70def122018-02-22 21:03:33 +0000537Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory,
538 StringRef &FileName,
Eric Christopher798e83b2019-04-04 23:34:38 +0000539 Optional<MD5::MD5Result> Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000540 Optional<StringRef> Source,
Ali Tamur783d84b2019-04-19 02:26:56 +0000541 uint16_t DwarfVersion,
Paul Robinson70def122018-02-22 21:03:33 +0000542 unsigned FileNumber) {
Ali Tamur783d84b2019-04-19 02:26:56 +0000543 return Header.tryGetFile(Directory, FileName, Checksum, Source, DwarfVersion,
544 FileNumber);
545}
546
Benjamin Kramerdc5f8052019-08-23 19:59:23 +0000547static bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
548 StringRef &FileName, Optional<MD5::MD5Result> Checksum) {
Ali Tamur783d84b2019-04-19 02:26:56 +0000549 if (RootFile.Name.empty() || RootFile.Name != FileName.data())
550 return false;
551 return RootFile.Checksum == Checksum;
David Blaikie5cff0e62014-03-13 21:47:12 +0000552}
553
Scott Linder16c7bda2018-02-23 23:01:06 +0000554Expected<unsigned>
555MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
556 StringRef &FileName,
Eric Christopher798e83b2019-04-04 23:34:38 +0000557 Optional<MD5::MD5Result> Checksum,
Eric Christopher06bfe352019-04-12 03:49:13 +0000558 Optional<StringRef> Source,
Ali Tamur783d84b2019-04-19 02:26:56 +0000559 uint16_t DwarfVersion,
Scott Linder16c7bda2018-02-23 23:01:06 +0000560 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000561 if (Directory == CompilationDir)
562 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000563 if (FileName.empty()) {
564 FileName = "<stdin>";
565 Directory = "";
566 }
567 assert(!FileName.empty());
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000568 // Keep track of whether any or all files have an MD5 checksum.
569 // If any files have embedded source, they all must.
Scott Linder16c7bda2018-02-23 23:01:06 +0000570 if (MCDwarfFiles.empty()) {
Eric Christopher798e83b2019-04-04 23:34:38 +0000571 trackMD5Usage(Checksum.hasValue());
Scott Linder16c7bda2018-02-23 23:01:06 +0000572 HasSource = (Source != None);
573 }
Ali Tamur783d84b2019-04-19 02:26:56 +0000574 if (isRootFile(RootFile, Directory, FileName, Checksum) && DwarfVersion >= 5)
575 return 0;
David Blaikiec714ef42014-03-17 01:52:11 +0000576 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000577 // File numbers start with 1 and/or after any file numbers
578 // allocated by inline-assembler .file directives.
579 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000580 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000581 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000582 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
583 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000584 if (!IterBool.second)
585 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000586 }
David Blaikie498589c2014-03-13 19:15:04 +0000587 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
Paul Robinsona1cedd62017-12-14 18:46:43 +0000588 if (FileNumber >= MCDwarfFiles.size())
589 MCDwarfFiles.resize(FileNumber + 1);
David Blaikie498589c2014-03-13 19:15:04 +0000590
591 // Get the new MCDwarfFile slot for this FileNumber.
592 MCDwarfFile &File = MCDwarfFiles[FileNumber];
593
Paul Robinson70def122018-02-22 21:03:33 +0000594 // It is an error to see the same number more than once.
David Blaikie498589c2014-03-13 19:15:04 +0000595 if (!File.Name.empty())
Paul Robinson70def122018-02-22 21:03:33 +0000596 return make_error<StringError>("file number already allocated",
597 inconvertibleErrorCode());
David Blaikie498589c2014-03-13 19:15:04 +0000598
Scott Linder16c7bda2018-02-23 23:01:06 +0000599 // If any files have embedded source, they all must.
600 if (HasSource != (Source != None))
601 return make_error<StringError>("inconsistent use of embedded source",
602 inconvertibleErrorCode());
Paul Robinson29f5f982018-01-09 23:31:48 +0000603
David Blaikie498589c2014-03-13 19:15:04 +0000604 if (Directory.empty()) {
605 // Separate the directory part from the basename of the FileName.
606 StringRef tFileName = sys::path::filename(FileName);
607 if (!tFileName.empty()) {
608 Directory = sys::path::parent_path(FileName);
609 if (!Directory.empty())
610 FileName = tFileName;
611 }
612 }
613
614 // Find or make an entry in the MCDwarfDirs vector for this Directory.
615 // Capture directory name.
616 unsigned DirIndex;
617 if (Directory.empty()) {
618 // For FileNames with no directories a DirIndex of 0 is used.
619 DirIndex = 0;
620 } else {
Fangrui Song21229572019-04-12 04:55:10 +0000621 DirIndex = llvm::find(MCDwarfDirs, Directory) - MCDwarfDirs.begin();
David Blaikie498589c2014-03-13 19:15:04 +0000622 if (DirIndex >= MCDwarfDirs.size())
623 MCDwarfDirs.push_back(Directory);
624 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
625 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
626 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
627 // are stored at MCDwarfFiles[FileNumber].Name .
628 DirIndex++;
629 }
630
631 File.Name = FileName;
632 File.DirIndex = DirIndex;
Paul Robinson29f5f982018-01-09 23:31:48 +0000633 File.Checksum = Checksum;
Eric Christopher798e83b2019-04-04 23:34:38 +0000634 trackMD5Usage(Checksum.hasValue());
Scott Linder16c7bda2018-02-23 23:01:06 +0000635 File.Source = Source;
636 if (Source)
637 HasSource = true;
David Blaikie498589c2014-03-13 19:15:04 +0000638
639 // return the allocated FileNumber.
640 return FileNumber;
641}
642
Kevin Enderbye46564a2010-09-30 16:52:03 +0000643/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000644void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
645 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000646 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000647 SmallString<256> Tmp;
648 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000649 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000650 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000651}
652
Frederic Rissa5ab8442015-08-07 15:14:08 +0000653/// Given a special op, return the address skip amount (in units of
654/// DWARF2_LINE_MIN_INSN_LENGTH).
655static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
656 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
657}
658
Kevin Enderbye46564a2010-09-30 16:52:03 +0000659/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000660void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
661 int64_t LineDelta, uint64_t AddrDelta,
662 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000663 uint64_t Temp, Opcode;
664 bool NeedCopy = false;
665
Frederic Rissa5ab8442015-08-07 15:14:08 +0000666 // The maximum address skip amount that can be encoded with a special op.
667 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
668
Kevin Enderbye46564a2010-09-30 16:52:03 +0000669 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000670 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000671
672 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000673 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000674 // end_sequence to emit the matrix entry.
675 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000676 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000677 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000678 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000679 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000680 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000681 }
682 OS << char(dwarf::DW_LNS_extended_op);
683 OS << char(1);
684 OS << char(dwarf::DW_LNE_end_sequence);
685 return;
686 }
687
688 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000689 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000690
691 // If the line increment is out of range of a special opcode, we must encode
692 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000693 if (Temp >= Params.DWARF2LineRange ||
694 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000695 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000696 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000697
698 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000699 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000700 NeedCopy = true;
701 }
702
703 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
704 if (LineDelta == 0 && AddrDelta == 0) {
705 OS << char(dwarf::DW_LNS_copy);
706 return;
707 }
708
709 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000710 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000711
712 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000713 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000714 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000715 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000716 if (Opcode <= 255) {
717 OS << char(Opcode);
718 return;
719 }
720
721 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000722 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000723 if (Opcode <= 255) {
724 OS << char(dwarf::DW_LNS_const_add_pc);
725 OS << char(Opcode);
726 return;
727 }
728 }
729
730 // Otherwise use DW_LNS_advance_pc.
731 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000732 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000733
734 if (NeedCopy)
735 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000736 else {
737 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000738 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000739 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000740}
741
Hsiangkai Wang5c63af02018-08-01 02:18:06 +0000742bool MCDwarfLineAddr::FixedEncode(MCContext &Context,
743 MCDwarfLineTableParams Params,
744 int64_t LineDelta, uint64_t AddrDelta,
745 raw_ostream &OS,
746 uint32_t *Offset, uint32_t *Size) {
747 if (LineDelta != INT64_MAX) {
748 OS << char(dwarf::DW_LNS_advance_line);
749 encodeSLEB128(LineDelta, OS);
750 }
751
752 // Use address delta to adjust address or use absolute address to adjust
753 // address.
754 bool SetDelta;
755 // According to DWARF spec., the DW_LNS_fixed_advance_pc opcode takes a
756 // single uhalf (unencoded) operand. So, the maximum value of AddrDelta
757 // is 65535. We set a conservative upper bound for it for relaxation.
758 if (AddrDelta > 60000) {
759 const MCAsmInfo *asmInfo = Context.getAsmInfo();
760 unsigned AddrSize = asmInfo->getCodePointerSize();
761
762 OS << char(dwarf::DW_LNS_extended_op);
763 encodeULEB128(1 + AddrSize, OS);
764 OS << char(dwarf::DW_LNE_set_address);
765 // Generate fixup for the address.
766 *Offset = OS.tell();
767 *Size = AddrSize;
768 SetDelta = false;
Fangrui Songac590672019-04-10 09:41:48 +0000769 OS.write_zeros(AddrSize);
Hsiangkai Wang5c63af02018-08-01 02:18:06 +0000770 } else {
771 OS << char(dwarf::DW_LNS_fixed_advance_pc);
772 // Generate fixup for 2-bytes address delta.
773 *Offset = OS.tell();
774 *Size = 2;
775 SetDelta = true;
776 OS << char(0);
777 OS << char(0);
778 }
779
780 if (LineDelta == INT64_MAX) {
781 OS << char(dwarf::DW_LNS_extended_op);
782 OS << char(1);
783 OS << char(dwarf::DW_LNE_end_sequence);
784 } else {
785 OS << char(dwarf::DW_LNS_copy);
786 }
787
788 return SetDelta;
789}
790
Kevin Enderbye7739d42011-12-09 18:09:40 +0000791// Utility function to write a tuple for .debug_abbrev.
792static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
793 MCOS->EmitULEB128IntValue(Name);
794 MCOS->EmitULEB128IntValue(Form);
795}
796
797// When generating dwarf for assembly source files this emits
798// the data for .debug_abbrev section which contains three DIEs.
799static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
800 MCContext &context = MCOS->getContext();
801 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
802
803 // DW_TAG_compile_unit DIE abbrev (1).
804 MCOS->EmitULEB128IntValue(1);
805 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
806 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000807 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
808 ? dwarf::DW_FORM_sec_offset
809 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000810 if (context.getGenDwarfSectionSyms().size() > 1 &&
811 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000812 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
813 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000814 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000815 } else {
816 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
817 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
818 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000819 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000820 if (!context.getCompilationDir().empty())
821 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000822 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
823 if (!DwarfDebugFlags.empty())
824 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
825 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
826 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
827 EmitAbbrev(MCOS, 0, 0);
828
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000829 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000830 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000831 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000832 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
833 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
834 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
835 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
836 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000837 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
838 EmitAbbrev(MCOS, 0, 0);
839
840 // DW_TAG_unspecified_parameters DIE abbrev (3).
841 MCOS->EmitULEB128IntValue(3);
842 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
843 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
844 EmitAbbrev(MCOS, 0, 0);
845
846 // Terminate the abbreviations for this compilation unit.
847 MCOS->EmitIntValue(0, 1);
848}
849
850// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000851// .debug_aranges section. This section contains a header and a table of pairs
852// of PointerSize'ed values for the address and size of section(s) with line
853// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000854static void EmitGenDwarfAranges(MCStreamer *MCOS,
855 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000856 MCContext &context = MCOS->getContext();
857
Oliver Stannard8b273082014-06-19 15:52:37 +0000858 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000859
860 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
861
862 // This will be the length of the .debug_aranges section, first account for
863 // the size of each item in the header (see below where we emit these items).
864 int Length = 4 + 2 + 4 + 1 + 1;
865
866 // Figure the padding after the header before the table of address and size
867 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000868 const MCAsmInfo *asmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000869 int AddrSize = asmInfo->getCodePointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000870 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
871 if (Pad == 2 * AddrSize)
872 Pad = 0;
873 Length += Pad;
874
875 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000876 // of each section we have in the table.
877 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000878 // And the pair of terminating zeros.
879 Length += 2 * AddrSize;
880
Kevin Enderbye7739d42011-12-09 18:09:40 +0000881 // Emit the header for this section.
882 // The 4 byte length not including the 4 byte value for the length.
883 MCOS->EmitIntValue(Length - 4, 4);
884 // The 2 byte version, which is 2.
885 MCOS->EmitIntValue(2, 2);
886 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000887 // of the .debug_info.
888 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000889 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
890 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000891 else
892 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000893 // The 1 byte size of an address.
894 MCOS->EmitIntValue(AddrSize, 1);
895 // The 1 byte size of a segment descriptor, we use a value of zero.
896 MCOS->EmitIntValue(0, 1);
897 // Align the header with the padding if needed, before we put out the table.
898 for(int i = 0; i < Pad; i++)
899 MCOS->EmitIntValue(0, 1);
900
Oliver Stannard8b273082014-06-19 15:52:37 +0000901 // Now emit the table of pairs of PointerSize'ed values for the section
902 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000903 for (MCSection *Sec : Sections) {
904 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000905 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000906 assert(StartSymbol && "StartSymbol must not be NULL");
907 assert(EndSymbol && "EndSymbol must not be NULL");
908
Jim Grosbach13760bd2015-05-30 01:25:56 +0000909 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000910 StartSymbol, MCSymbolRefExpr::VK_None, context);
911 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
912 *StartSymbol, *EndSymbol, 0);
913 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000914 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000915 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000916
917 // And finally the pair of terminating zeros.
918 MCOS->EmitIntValue(0, AddrSize);
919 MCOS->EmitIntValue(0, AddrSize);
920}
921
922// When generating dwarf for assembly source files this emits the data for
923// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000924// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000925static void EmitGenDwarfInfo(MCStreamer *MCOS,
926 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000927 const MCSymbol *LineSectionSymbol,
928 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000929 MCContext &context = MCOS->getContext();
930
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000931 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000932
933 // Create a symbol at the start and end of this section used in here for the
934 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000935 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000936 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000937 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000938
939 // First part: the header.
940
941 // The 4 byte total length of the information for this compilation unit, not
942 // including these 4 bytes.
943 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000944 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000945
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000946 // The 2 byte DWARF version.
947 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000948
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000949 // The DWARF v5 header has unit type, address size, abbrev offset.
950 // Earlier versions have abbrev offset, address size.
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000951 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000952 int AddrSize = AsmInfo.getCodePointerSize();
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000953 if (context.getDwarfVersion() >= 5) {
954 MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
955 MCOS->EmitIntValue(AddrSize, 1);
956 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000957 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
958 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000959 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000960 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000961 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000962 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
963 AsmInfo.needsDwarfSectionOffsetDirective());
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000964 if (context.getDwarfVersion() <= 4)
965 MCOS->EmitIntValue(AddrSize, 1);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000966
967 // Second part: the compile_unit DIE.
968
969 // The DW_TAG_compile_unit DIE abbrev (1).
970 MCOS->EmitULEB128IntValue(1);
971
972 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
973 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000974 if (LineSectionSymbol)
975 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
976 AsmInfo.needsDwarfSectionOffsetDirective());
977 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000978 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000979
Oliver Stannard8b273082014-06-19 15:52:37 +0000980 if (RangesSectionSymbol) {
981 // There are multiple sections containing code, so we must use the
982 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000983
Oliver Stannard8b273082014-06-19 15:52:37 +0000984 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
985 // to the address range list for this compilation unit.
986 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
987 } else {
988 // If we only have one non-empty code section, we can use the simpler
989 // AT_low_pc and AT_high_pc attributes.
990
991 // Find the first (and only) non-empty text section
992 auto &Sections = context.getGenDwarfSectionSyms();
993 const auto TextSection = Sections.begin();
994 assert(TextSection != Sections.end() && "No text section found");
995
Rafael Espindolae0746792015-05-21 16:52:32 +0000996 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
997 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000998 assert(StartSymbol && "StartSymbol must not be NULL");
999 assert(EndSymbol && "EndSymbol must not be NULL");
1000
1001 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001002 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001003 StartSymbol, MCSymbolRefExpr::VK_None, context);
1004 MCOS->EmitValue(Start, AddrSize);
1005
1006 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001007 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001008 EndSymbol, MCSymbolRefExpr::VK_None, context);
1009 MCOS->EmitValue(End, AddrSize);
1010 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001011
1012 // AT_name, the name of the source file. Reconstruct from the first directory
1013 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +00001014 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +00001015 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +00001016 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +00001017 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001018 }
Paul Robinson1ca25762019-03-01 20:58:04 +00001019 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = context.getMCDwarfFiles();
1020 // MCDwarfFiles might be empty if we have an empty source file.
1021 // If it's not empty, [0] is unused and [1] is the first actual file.
1022 assert(MCDwarfFiles.empty() || MCDwarfFiles.size() >= 2);
1023 const MCDwarfFile &RootFile =
1024 MCDwarfFiles.empty()
1025 ? context.getMCDwarfLineTable(/*CUID=*/0).getRootFile()
1026 : MCDwarfFiles[1];
1027 MCOS->EmitBytes(RootFile.Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001028 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1029
1030 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +00001031 if (!context.getCompilationDir().empty()) {
1032 MCOS->EmitBytes(context.getCompilationDir());
1033 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1034 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001035
1036 // AT_APPLE_flags, the command line arguments of the assembler tool.
1037 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
1038 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +00001039 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001040 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1041 }
1042
1043 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +00001044 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +00001045 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +00001046 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +00001047 else
1048 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +00001049 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1050
1051 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
1052 // draft has no standard code for assembler.
1053 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
1054
Kevin Enderby8d4a2202012-01-10 17:52:29 +00001055 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001056
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001057 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001058 const std::vector<MCGenDwarfLabelEntry> &Entries =
1059 MCOS->getContext().getMCGenDwarfLabelEntries();
1060 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +00001061 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +00001062 MCOS->EmitULEB128IntValue(2);
1063
1064 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001065 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001066 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1067
1068 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001069 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001070
1071 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001072 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001073
1074 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001075 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +00001076 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +00001077 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001078
Kevin Enderbye7739d42011-12-09 18:09:40 +00001079 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
1080 MCOS->EmitIntValue(0, 1);
1081
1082 // The DW_TAG_unspecified_parameters DIE abbrev (3).
1083 MCOS->EmitULEB128IntValue(3);
1084
1085 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
1086 MCOS->EmitIntValue(0, 1);
1087 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001088
1089 // Add the NULL DIE terminating the Compile Unit DIE's.
1090 MCOS->EmitIntValue(0, 1);
1091
1092 // Now set the value of the symbol at the end of the info section.
1093 MCOS->EmitLabel(InfoEnd);
1094}
1095
Oliver Stannard8b273082014-06-19 15:52:37 +00001096// When generating dwarf for assembly source files this emits the data for
1097// .debug_ranges section. We only emit one range list, which spans all of the
1098// executable sections of this file.
1099static void EmitGenDwarfRanges(MCStreamer *MCOS) {
1100 MCContext &context = MCOS->getContext();
1101 auto &Sections = context.getGenDwarfSectionSyms();
1102
1103 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001104 int AddrSize = AsmInfo->getCodePointerSize();
Oliver Stannard8b273082014-06-19 15:52:37 +00001105
1106 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
1107
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001108 for (MCSection *Sec : Sections) {
1109 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +00001110 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +00001111 assert(StartSymbol && "StartSymbol must not be NULL");
1112 assert(EndSymbol && "EndSymbol must not be NULL");
1113
1114 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +00001115 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001116 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +00001117 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +00001118 MCOS->EmitValue(SectionStartAddr, AddrSize);
1119
1120 // Emit a range list entry spanning this section
1121 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
1122 *StartSymbol, *EndSymbol, 0);
1123 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001124 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +00001125 }
1126
1127 // Emit end of list entry
1128 MCOS->EmitIntValue(0, AddrSize);
1129 MCOS->EmitIntValue(0, AddrSize);
1130}
1131
Kevin Enderbye7739d42011-12-09 18:09:40 +00001132//
1133// When generating dwarf for assembly source files this emits the Dwarf
1134// sections.
1135//
David Blaikie8bf66c42014-04-01 07:35:52 +00001136void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +00001137 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +00001138
1139 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +00001140 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001141 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +00001142 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +00001143 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +00001144 if (CreateDwarfSectionSymbols)
1145 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +00001146 MCSymbol *AbbrevSectionSymbol = nullptr;
1147 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001148 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +00001149
1150 // Create end symbols for each section, and remove empty sections
1151 MCOS->getContext().finalizeDwarfSections(*MCOS);
1152
1153 // If there are no sections to generate debug info for, we don't need
1154 // to do anything
1155 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
1156 return;
1157
Oliver Stannard14f97d02014-09-22 10:45:16 +00001158 // We only use the .debug_ranges section if we have multiple code sections,
1159 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +00001160 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +00001161 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
1162 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +00001163 CreateDwarfSectionSymbols |= UseRangesSection;
1164
Kevin Enderbye7739d42011-12-09 18:09:40 +00001165 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001166 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001167 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001168 MCOS->EmitLabel(InfoSectionSymbol);
1169 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001170 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001171 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001172 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001173 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001174 }
Oliver Stannard8b273082014-06-19 15:52:37 +00001175 if (UseRangesSection) {
1176 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
1177 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001178 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +00001179 MCOS->EmitLabel(RangesSectionSymbol);
1180 }
1181 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001182
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001183 assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
Oliver Stannard8b273082014-06-19 15:52:37 +00001184
1185 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001186
1187 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001188 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001189
Oliver Stannard8b273082014-06-19 15:52:37 +00001190 if (UseRangesSection)
1191 EmitGenDwarfRanges(MCOS);
1192
Kevin Enderbye7739d42011-12-09 18:09:40 +00001193 // Output the data for .debug_abbrev section.
1194 EmitGenDwarfAbbrev(MCOS);
1195
1196 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +00001197 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
1198 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001199}
1200
1201//
1202// When generating dwarf for assembly source files this is called when symbol
1203// for a label is created. If this symbol is not a temporary and is in the
1204// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001205// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001206//
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001207void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +00001208 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +00001209 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001210 if (Symbol->isTemporary())
1211 return;
1212 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +00001213 // We won't create dwarf labels for symbols in sections that we are not
1214 // generating debug info for.
Eric Christopher445c9522016-10-14 05:47:37 +00001215 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
Kevin Enderbye7739d42011-12-09 18:09:40 +00001216 return;
1217
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001218 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +00001219 // underbar if any.
1220 StringRef Name = Symbol->getName();
1221 if (Name.startswith("_"))
1222 Name = Name.substr(1, Name.size()-1);
1223
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001224 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001225 unsigned FileNumber = context.getGenDwarfFileNumber();
1226
1227 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001228 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +00001229 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001230 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
1231
1232 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
1233 // values so that they don't have things like an ARM thumb bit from the
1234 // original symbol. So when used they won't get a low bit set after
1235 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +00001236 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +00001237 MCOS->EmitLabel(Label);
1238
1239 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001240 MCOS->getContext().addMCGenDwarfLabelEntry(
1241 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +00001242}
1243
Rafael Espindola0a017a62010-12-10 07:39:47 +00001244static int getDataAlignmentFactor(MCStreamer &streamer) {
1245 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001246 const MCAsmInfo *asmInfo = context.getAsmInfo();
1247 int size = asmInfo->getCalleeSaveStackSlotSize();
1248 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +00001249 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +00001250 else
1251 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001252}
1253
Rafael Espindola5395f442011-04-22 00:08:43 +00001254static unsigned getSizeForEncoding(MCStreamer &streamer,
1255 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001256 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001257 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001258 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +00001259 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001260 case dwarf::DW_EH_PE_absptr:
1261 case dwarf::DW_EH_PE_signed:
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001262 return context.getAsmInfo()->getCodePointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001263 case dwarf::DW_EH_PE_udata2:
1264 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +00001265 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001266 case dwarf::DW_EH_PE_udata4:
1267 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +00001268 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001269 case dwarf::DW_EH_PE_udata8:
1270 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +00001271 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001272 }
Rafael Espindola5395f442011-04-22 00:08:43 +00001273}
1274
Rafael Espindolafe963b12014-08-15 03:07:13 +00001275static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
1276 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +00001277 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001278 const MCAsmInfo *asmInfo = context.getAsmInfo();
1279 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1280 symbolEncoding,
1281 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +00001282 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001283 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +00001284 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +00001285 else
1286 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001287}
1288
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001289static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1290 unsigned symbolEncoding) {
1291 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001292 const MCAsmInfo *asmInfo = context.getAsmInfo();
1293 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1294 symbolEncoding,
1295 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001296 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001297 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001298}
1299
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001300namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001301
Rafael Espindola68c21652015-11-05 23:54:18 +00001302class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001303 int CFAOffset = 0;
1304 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001305 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001306 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001307
Rafael Espindola68c21652015-11-05 23:54:18 +00001308public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001309 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1310 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001311
Rafael Espindola68c21652015-11-05 23:54:18 +00001312 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001313 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001314
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001315 const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
Rafael Espindola46be4352015-11-06 03:02:51 +00001316 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001317 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001318 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001319 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001320 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001321};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001322
1323} // end anonymous namespace
1324
Rafael Espindolafe963b12014-08-15 03:07:13 +00001325static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001326 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001327}
1328
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001329void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001330 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001331 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001332
1333 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001334 case MCCFIInstruction::OpRegister: {
1335 unsigned Reg1 = Instr.getRegister();
1336 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001337 if (!IsEH) {
Jake Ehrlich3da79822017-12-01 21:44:27 +00001338 Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
1339 Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001340 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001341 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1342 Streamer.EmitULEB128IntValue(Reg1);
1343 Streamer.EmitULEB128IntValue(Reg2);
1344 return;
1345 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001346 case MCCFIInstruction::OpWindowSave:
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001347 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1348 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001349
Luke Cheesemanf57d7d82018-12-18 10:37:42 +00001350 case MCCFIInstruction::OpNegateRAState:
1351 Streamer.EmitIntValue(dwarf::DW_CFA_AARCH64_negate_ra_state, 1);
1352 return;
1353
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001354 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001355 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001356 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1357 Streamer.EmitULEB128IntValue(Reg);
1358 return;
1359 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001360 case MCCFIInstruction::OpAdjustCfaOffset:
1361 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001362 const bool IsRelative =
1363 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1364
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001365 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1366
1367 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001368 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001369 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001370 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001371
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001372 Streamer.EmitULEB128IntValue(CFAOffset);
1373
1374 return;
1375 }
1376 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001377 unsigned Reg = Instr.getRegister();
1378 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001379 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001380 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001381 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001382 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001383 Streamer.EmitULEB128IntValue(CFAOffset);
1384
1385 return;
1386 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001387 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001388 unsigned Reg = Instr.getRegister();
1389 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001390 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001391 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001392 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001393
1394 return;
1395 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001396 case MCCFIInstruction::OpOffset:
1397 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001398 const bool IsRelative =
1399 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001400
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001401 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001402 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001403 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001404
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001405 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001406 if (IsRelative)
1407 Offset -= CFAOffset;
1408 Offset = Offset / dataAlignmentFactor;
1409
1410 if (Offset < 0) {
1411 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1412 Streamer.EmitULEB128IntValue(Reg);
1413 Streamer.EmitSLEB128IntValue(Offset);
1414 } else if (Reg < 64) {
1415 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001416 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001417 } else {
1418 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001419 Streamer.EmitULEB128IntValue(Reg);
1420 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001421 }
1422 return;
1423 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001424 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001425 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1426 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001427 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001428 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1429 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001430 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001431 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001432 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001433 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001434 return;
1435 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001436 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001437 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001438 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001439 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Alexander Richardsondbfa6e62018-11-13 10:54:49 +00001440 if (Reg < 64) {
1441 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1442 } else {
1443 Streamer.EmitIntValue(dwarf::DW_CFA_restore_extended, 1);
1444 Streamer.EmitULEB128IntValue(Reg);
1445 }
Rafael Espindola4ea99812011-12-29 21:43:03 +00001446 return;
1447 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001448 case MCCFIInstruction::OpGnuArgsSize:
Michael Kuperstein259f1502015-10-07 07:01:31 +00001449 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1450 Streamer.EmitULEB128IntValue(Instr.getOffset());
1451 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001452
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001453 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001454 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001455 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001456 }
1457 llvm_unreachable("Unhandled case in switch");
1458}
1459
Rafael Espindolafe963b12014-08-15 03:07:13 +00001460/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001461void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001462 MCSymbol *BaseLabel) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001463 for (const MCCFIInstruction &Instr : Instrs) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001464 MCSymbol *Label = Instr.getLabel();
1465 // Throw out move if the label is invalid.
1466 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1467
1468 // Advance row if new location.
1469 if (BaseLabel && Label) {
1470 MCSymbol *ThisSym = Label;
1471 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001472 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001473 BaseLabel = ThisSym;
1474 }
1475 }
1476
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001477 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001478 }
1479}
1480
Rafael Espindolafe963b12014-08-15 03:07:13 +00001481/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001482void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001483 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001484 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001485
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001486 // range-start range-length compact-unwind-enc personality-func lsda
1487 // _foo LfooEnd-_foo 0x00000023 0 0
1488 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1489 //
1490 // .section __LD,__compact_unwind,regular,debug
1491 //
1492 // # compact unwind for _foo
1493 // .quad _foo
1494 // .set L1,LfooEnd-_foo
1495 // .long L1
1496 // .long 0x01010001
1497 // .quad 0
1498 // .quad 0
1499 //
1500 // # compact unwind for _bar
1501 // .quad _bar
1502 // .set L2,LbarEnd-_bar
1503 // .long L2
1504 // .long 0x01020011
1505 // .quad __gxx_personality
1506 // .quad except_tab1
1507
Bill Wendling49bc6802011-07-19 00:06:12 +00001508 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001509 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001510 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001511
Bill Wendlingdafd5982011-07-14 23:34:45 +00001512 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001513 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001514 Encoding |= 0x40000000;
1515
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001516 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001517 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001518 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001519 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001520
1521 // Range Length
1522 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1523 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001524 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001525
Bill Wendling75174662011-06-30 00:30:52 +00001526 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001527 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1528 Streamer.EmitIntValue(Encoding, Size);
1529
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001530 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001531 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001532 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001533 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001534 else
1535 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001536
1537 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001538 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001539 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001540 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001541 else
1542 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001543}
1544
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001545static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1546 if (IsEH)
1547 return 1;
1548 switch (DwarfVersion) {
1549 case 2:
1550 return 1;
1551 case 3:
1552 return 3;
1553 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001554 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001555 return 4;
1556 }
1557 llvm_unreachable("Unknown version");
1558}
1559
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001560const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001561 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001562 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001563 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001564
Jim Grosbach6f482002015-05-18 18:43:14 +00001565 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001566 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001567
Jim Grosbach6f482002015-05-18 18:43:14 +00001568 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001569
1570 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001571 const MCExpr *Length =
1572 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1573 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001574
1575 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001576 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001577 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001578
1579 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001580 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001581 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001582
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001583 if (IsEH) {
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001584 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001585 Augmentation += "z";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001586 if (Frame.Personality)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001587 Augmentation += "P";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001588 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001589 Augmentation += "L";
1590 Augmentation += "R";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001591 if (Frame.IsSignalFrame)
Rafael Espindola3c47e372012-01-23 21:51:52 +00001592 Augmentation += "S";
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001593 if (Frame.IsBKeyFrame)
1594 Augmentation += "B";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001595 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001596 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001597 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001598
Keith Walkerea9483f2015-05-12 15:25:08 +00001599 if (CIEVersion >= 4) {
1600 // Address Size
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001601 Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001602
1603 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001604 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001605 }
1606
Rafael Espindola0a017a62010-12-10 07:39:47 +00001607 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001608 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001609
1610 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001611 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001612
1613 // Return Address Register
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001614 unsigned RAReg = Frame.RAReg;
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001615 if (RAReg == static_cast<unsigned>(INT_MAX))
1616 RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1617
Oliver Stannardf7693f42014-06-19 15:39:33 +00001618 if (CIEVersion == 1) {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001619 assert(RAReg <= 255 &&
Oliver Stannardf7693f42014-06-19 15:39:33 +00001620 "DWARF 2 encodes return_address_register in one byte");
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001621 Streamer.EmitIntValue(RAReg, 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001622 } else {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001623 Streamer.EmitULEB128IntValue(RAReg);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001624 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001625
1626 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001627 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001628 if (IsEH) {
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001629 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001630 // Personality Encoding
1631 augmentationLength += 1;
1632 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001633 augmentationLength +=
1634 getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001635 }
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001636 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001637 augmentationLength += 1;
1638 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001639 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001640
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001641 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001642
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001643 // Augmentation Data (optional)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001644 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001645 // Personality Encoding
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001646 emitEncodingByte(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001647 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001648 EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001649 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001650
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001651 if (Frame.Lsda)
1652 emitEncodingByte(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001653
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001654 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001655 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001656 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001657
1658 // Initial Instructions
1659
Bill Wendlingbc07a892013-06-18 07:20:20 +00001660 const MCAsmInfo *MAI = context.getAsmInfo();
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001661 if (!Frame.IsSimple) {
David Majnemere035cf92014-01-27 17:20:25 +00001662 const std::vector<MCCFIInstruction> &Instructions =
1663 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001664 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001665 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001666
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001667 InitialCFAOffset = CFAOffset;
1668
Rafael Espindola0a017a62010-12-10 07:39:47 +00001669 // Padding
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001670 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001671
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001672 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001673 return *sectionStart;
1674}
1675
Rafael Espindola46be4352015-11-06 03:02:51 +00001676void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1677 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001678 bool LastInSection,
1679 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001680 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001681 MCSymbol *fdeStart = context.createTempSymbol();
1682 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001683 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001684
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001685 CFAOffset = InitialCFAOffset;
1686
Rafael Espindola0a017a62010-12-10 07:39:47 +00001687 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001688 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1689 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001690
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001691 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001692
Rafael Espindola0a017a62010-12-10 07:39:47 +00001693 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001694 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001695 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001696 const MCExpr *offset =
1697 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1698 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001699 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001700 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001701 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001702 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001703 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001704 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001705 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001706
Rafael Espindola0a017a62010-12-10 07:39:47 +00001707 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001708 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001709 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001710 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1711 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001712
1713 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001714 const MCExpr *Range =
1715 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1716 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001717
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001718 if (IsEH) {
1719 // Augmentation Data Length
1720 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001721
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001722 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001723 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001724
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001725 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001726
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001727 // Augmentation Data
1728 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001729 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001730 }
Rafael Espindola68067662011-04-29 03:06:29 +00001731
Rafael Espindola0a017a62010-12-10 07:39:47 +00001732 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001733 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001734
Rafael Espindola0a017a62010-12-10 07:39:47 +00001735 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001736 // The size of a .eh_frame section has to be a multiple of the alignment
1737 // since a null CIE is interpreted as the end. Old systems overaligned
1738 // .eh_frame, so we do too and account for it in the last FDE.
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001739 unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
Rafael Espindola46be4352015-11-06 03:02:51 +00001740 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001741
Rafael Espindola46be4352015-11-06 03:02:51 +00001742 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001743}
1744
Benjamin Kramer570dd782010-12-30 22:34:44 +00001745namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001746
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001747struct CIEKey {
1748 static const CIEKey getEmptyKey() {
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001749 return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX),
1750 false);
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001751 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001752
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001753 static const CIEKey getTombstoneKey() {
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001754 return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX),
1755 false);
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001756 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001757
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001758 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001759 unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001760 unsigned RAReg, bool IsBKeyFrame)
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001761 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001762 LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001763 IsSimple(IsSimple), RAReg(RAReg), IsBKeyFrame(IsBKeyFrame) {}
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001764
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001765 explicit CIEKey(const MCDwarfFrameInfo &Frame)
1766 : Personality(Frame.Personality),
1767 PersonalityEncoding(Frame.PersonalityEncoding),
1768 LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001769 IsSimple(Frame.IsSimple), RAReg(Frame.RAReg),
1770 IsBKeyFrame(Frame.IsBKeyFrame) {}
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001771
Eli Friedmanfb26c322019-03-14 23:08:19 +00001772 StringRef PersonalityName() const {
1773 if (!Personality)
1774 return StringRef();
1775 return Personality->getName();
1776 }
1777
1778 bool operator<(const CIEKey &Other) const {
1779 return std::make_tuple(PersonalityName(), PersonalityEncoding, LsdaEncoding,
1780 IsSignalFrame, IsSimple, RAReg) <
1781 std::make_tuple(Other.PersonalityName(), Other.PersonalityEncoding,
1782 Other.LsdaEncoding, Other.IsSignalFrame,
1783 Other.IsSimple, Other.RAReg);
1784 }
1785
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001786 const MCSymbol *Personality;
1787 unsigned PersonalityEncoding;
1788 unsigned LsdaEncoding;
1789 bool IsSignalFrame;
1790 bool IsSimple;
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001791 unsigned RAReg;
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001792 bool IsBKeyFrame;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001793};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001794
1795} // end anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001796
1797namespace llvm {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001798
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001799template <> struct DenseMapInfo<CIEKey> {
1800 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1801 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001802
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001803 static unsigned getHashValue(const CIEKey &Key) {
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001804 return static_cast<unsigned>(hash_combine(
1805 Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1806 Key.IsSignalFrame, Key.IsSimple, Key.RAReg, Key.IsBKeyFrame));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001807 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001808
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001809 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1810 return LHS.Personality == RHS.Personality &&
1811 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1812 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1813 LHS.IsSignalFrame == RHS.IsSignalFrame &&
Luke Cheeseman41a9e532018-12-21 10:45:08 +00001814 LHS.IsSimple == RHS.IsSimple && LHS.RAReg == RHS.RAReg &&
1815 LHS.IsBKeyFrame == RHS.IsBKeyFrame;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001816 }
1817};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001818
1819} // end namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001820
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001821void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001822 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001823 Streamer.generateCompactUnwindEncodings(MAB);
1824
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001825 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001826 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001827 const MCAsmInfo *AsmInfo = Context.getAsmInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001828 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001829 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001830
Bill Wendling9803abb2011-09-06 18:37:11 +00001831 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001832 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001833 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001834 bool SectionEmitted = false;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001835 for (const MCDwarfFrameInfo &Frame : FrameArray) {
Bob Wilson0e180f22013-05-07 20:56:33 +00001836 if (Frame.CompactUnwindEncoding == 0) continue;
1837 if (!SectionEmitted) {
1838 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001839 Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001840 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001841 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001842 NeedsEHFrameSection |=
1843 Frame.CompactUnwindEncoding ==
1844 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001845 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001846 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001847 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001848
Tim Northoverd1c6f512014-03-29 09:03:13 +00001849 if (!NeedsEHFrameSection) return;
1850
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001851 MCSection &Section =
1852 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1853 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001854
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001855 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001856 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001857 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001858
Eric Christopherd29430d2014-06-19 20:00:09 +00001859 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001860
Craig Topperbb694de2014-04-13 04:57:38 +00001861 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001862 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Eli Friedmanfb26c322019-03-14 23:08:19 +00001863 // Sort the FDEs by their corresponding CIE before we emit them.
1864 // This isn't technically necessary according to the DWARF standard,
1865 // but the Android libunwindstack rejects eh_frame sections where
1866 // an FDE refers to a CIE other than the closest previous CIE.
1867 std::vector<MCDwarfFrameInfo> FrameArrayX(FrameArray.begin(), FrameArray.end());
Fangrui Songefd94c52019-04-23 14:51:27 +00001868 llvm::stable_sort(FrameArrayX,
1869 [](const MCDwarfFrameInfo &X, const MCDwarfFrameInfo &Y) {
1870 return CIEKey(X) < CIEKey(Y);
1871 });
Eli Friedmanfb26c322019-03-14 23:08:19 +00001872 for (auto I = FrameArrayX.begin(), E = FrameArrayX.end(); I != E;) {
Rafael Espindola46be4352015-11-06 03:02:51 +00001873 const MCDwarfFrameInfo &Frame = *I;
1874 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001875 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001876 MOFI->getCompactUnwindDwarfEHFrameOnly())
1877 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1878 // of by the compact unwind encoding.
1879 continue;
1880
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001881 CIEKey Key(Frame);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001882 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1883 if (!CIEStart)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001884 CIEStart = &Emitter.EmitCIE(Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001885
Rafael Espindola97588e12015-11-06 13:14:59 +00001886 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001887 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001888}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001889
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001890void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001891 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001892 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001893 SmallString<256> Tmp;
1894 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001895 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001896 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001897}
1898
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001899void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001900 uint64_t AddrDelta, raw_ostream &OS,
1901 uint32_t *Offset, uint32_t *Size) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001902 // Scale the address delta by the minimum instruction length.
1903 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1904
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001905 bool WithFixups = false;
1906 if (Offset && Size)
1907 WithFixups = true;
1908
Peter Collingbournee3f65292018-05-18 19:46:24 +00001909 support::endianness E =
1910 Context.getAsmInfo()->isLittleEndian() ? support::little : support::big;
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001911 if (AddrDelta == 0) {
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001912 if (WithFixups) {
1913 *Offset = 0;
1914 *Size = 0;
1915 }
Rafael Espindolaab39c632011-05-08 14:35:21 +00001916 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001917 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001918 if (WithFixups) {
1919 *Offset = OS.tell();
1920 *Size = 6;
1921 OS << uint8_t(dwarf::DW_CFA_advance_loc);
1922 } else
1923 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001924 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001925 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001926 if (WithFixups) {
1927 *Offset = OS.tell();
1928 *Size = 8;
1929 OS.write_zeros(1);
1930 } else
1931 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001932 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001933 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001934 if (WithFixups) {
1935 *Offset = OS.tell();
1936 *Size = 16;
1937 OS.write_zeros(2);
1938 } else
1939 support::endian::write<uint16_t>(OS, AddrDelta, E);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001940 } else {
1941 assert(isUInt<32>(AddrDelta));
1942 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +00001943 if (WithFixups) {
1944 *Offset = OS.tell();
1945 *Size = 32;
1946 OS.write_zeros(4);
1947 } else
1948 support::endian::write<uint32_t>(OS, AddrDelta, E);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001949 }
1950}