blob: 9574379d78d2736039b76e2fed3eaa0f7014d451 [file] [log] [blame]
Kevin Enderbye5930f12010-07-28 20:55:35 +00001//===- lib/MC/MCDwarf.cpp - MCDwarf implementation ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCDwarf.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/DenseMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/Hashing.h"
Scott Linder16c7bda2018-02-23 23:01:06 +000014#include "llvm/ADT/Optional.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallString.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include "llvm/ADT/SmallVector.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000018#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000020#include "llvm/BinaryFormat/Dwarf.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000022#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000025#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000026#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000027#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000028#include "llvm/MC/MCSection.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000029#include "llvm/MC/MCStreamer.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000030#include "llvm/MC/MCSymbol.h"
Paul Robinson1f900292018-02-06 20:29:21 +000031#include "llvm/MC/StringTableBuilder.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000033#include "llvm/Support/Endian.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000034#include "llvm/Support/EndianStream.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000035#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000036#include "llvm/Support/LEB128.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000037#include "llvm/Support/MathExtras.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000038#include "llvm/Support/Path.h"
39#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000040#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000041#include <cassert>
42#include <cstdint>
43#include <string>
44#include <utility>
45#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000046
Kevin Enderbye5930f12010-07-28 20:55:35 +000047using namespace llvm;
48
Paul Robinson1f900292018-02-06 20:29:21 +000049/// Manage the .debug_line_str section contents, if we use it.
50class llvm::MCDwarfLineStr {
51 MCSymbol *LineStrLabel = nullptr;
52 StringTableBuilder LineStrings{StringTableBuilder::DWARF};
53 bool UseRelocs = false;
54
55public:
56 /// Construct an instance that can emit .debug_line_str (for use in a normal
57 /// v5 line table).
58 explicit MCDwarfLineStr(MCContext &Ctx) {
59 UseRelocs = Ctx.getAsmInfo()->doesDwarfUseRelocationsAcrossSections();
60 if (UseRelocs)
61 LineStrLabel =
62 Ctx.getObjectFileInfo()->getDwarfLineStrSection()->getBeginSymbol();
63 }
64
65 /// Emit a reference to the string.
66 void emitRef(MCStreamer *MCOS, StringRef Path);
67
68 /// Emit the .debug_line_str section if appropriate.
69 void emitSection(MCStreamer *MCOS);
70};
71
Ulrich Weigand32d725b2013-06-12 14:46:54 +000072static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000073 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000074 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000075 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000076 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000077 // TODO: report this error, but really only once.
78 ;
79 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000080 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000081}
82
83//
84// This is called when an instruction is assembled into the specified section
85// and if there is information from the last .loc directive that has yet to have
86// a line entry made for it is made.
87//
David Majnemer8cc30782016-01-21 01:59:03 +000088void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000089 if (!MCOS->getContext().getDwarfLocSeen())
90 return;
91
92 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000093 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000094 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000095 MCOS->EmitLabel(LineSym);
96
97 // Get the current .loc info saved in the context.
98 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
99
100 // Create a (local) line entry with the symbol and the current .loc info.
David Majnemer8cc30782016-01-21 01:59:03 +0000101 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102
103 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +0000104 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000105
Kevin Enderbye46564a2010-09-30 16:52:03 +0000106 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +0000107 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +0000108 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +0000109 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +0000110 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000111}
112
113//
114// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000115//
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000116static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
117 const MCSymbol &Start,
118 const MCSymbol &End,
119 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000120 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
121 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000122 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000123 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000124 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000125 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000126 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000127 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000128 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000129 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000130 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000131 return Res3;
132}
133
Kevin Enderbye46564a2010-09-30 16:52:03 +0000134//
Paul Robinson1f900292018-02-06 20:29:21 +0000135// This helper routine returns an expression of Start + IntVal .
136//
137static inline const MCExpr *
138makeStartPlusIntExpr(MCContext &Ctx, const MCSymbol &Start, int IntVal) {
139 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
140 const MCExpr *LHS = MCSymbolRefExpr::create(&Start, Variant, Ctx);
141 const MCExpr *RHS = MCConstantExpr::create(IntVal, Ctx);
142 const MCExpr *Res = MCBinaryExpr::create(MCBinaryExpr::Add, LHS, RHS, Ctx);
143 return Res;
144}
145
146//
Kevin Enderbye46564a2010-09-30 16:52:03 +0000147// This emits the Dwarf line table for the specified section from the entries
148// in the LineSection.
149//
David Blaikief4ad6982014-03-12 22:35:23 +0000150static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000151EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000152 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000153 unsigned FileNum = 1;
154 unsigned LastLine = 1;
155 unsigned Column = 0;
156 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
157 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000158 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000159 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000160
David Majnemer8cc30782016-01-21 01:59:03 +0000161 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000162 for (const MCDwarfLineEntry &LineEntry : LineEntries) {
163 int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000164
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000165 if (FileNum != LineEntry.getFileNum()) {
166 FileNum = LineEntry.getFileNum();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000167 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000168 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000169 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000170 if (Column != LineEntry.getColumn()) {
171 Column = LineEntry.getColumn();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000172 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000173 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000174 }
Dehao Chen6e0c8442016-10-07 15:21:31 +0000175 if (Discriminator != LineEntry.getDiscriminator() &&
176 MCOS->getContext().getDwarfVersion() >= 4) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000177 Discriminator = LineEntry.getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000178 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000179 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
180 MCOS->EmitULEB128IntValue(Size + 1);
181 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
182 MCOS->EmitULEB128IntValue(Discriminator);
183 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000184 if (Isa != LineEntry.getIsa()) {
185 Isa = LineEntry.getIsa();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000186 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000187 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000188 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000189 if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
190 Flags = LineEntry.getFlags();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000191 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
192 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000193 if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000194 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000195 if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000196 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000197 if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000198 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
199
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000200 MCSymbol *Label = LineEntry.getLabel();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000201
202 // At this point we want to emit/create the sequence to encode the delta in
203 // line numbers and the increment of the address from the previous Label
204 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000205 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000206 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000207 asmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000208
Dehao Chen1b54fce2016-04-28 22:09:37 +0000209 Discriminator = 0;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000210 LastLine = LineEntry.getLine();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000211 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000212 }
213
214 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000215 // Use the section end label to compute the address delta and use INT64_MAX
216 // as the line delta which is the signal that this is actually a
217 // DW_LNE_end_sequence.
218 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000219
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000220 // Switch back the dwarf line section, in case endSection had to switch the
221 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000222 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000223 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000224
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000225 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000226 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000227 AsmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000228}
229
230//
231// This emits the Dwarf file and the line tables.
232//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000233void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
234 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000235 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000236
David Blaikie8bf66c42014-04-01 07:35:52 +0000237 auto &LineTables = context.getMCDwarfLineTables();
238
239 // Bail out early so we don't switch to the debug_line section needlessly and
240 // in doing so create an unnecessary (if empty) section.
241 if (LineTables.empty())
242 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000243
Paul Robinson1f900292018-02-06 20:29:21 +0000244 // In a v5 non-split line table, put the strings in a separate section.
245 Optional<MCDwarfLineStr> LineStr;
246 if (context.getDwarfVersion() >= 5)
247 LineStr = MCDwarfLineStr(context);
248
David Blaikie11765bc2014-03-13 17:55:28 +0000249 // Switch to the section where the table will be emitted into.
250 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
251
Manman Ren4e042a62013-02-05 21:52:47 +0000252 // Handle the rest of the Compile Units.
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000253 for (const auto &CUIDTablePair : LineTables) {
Jonas Devlieghere26ddf272018-07-11 12:30:35 +0000254 CUIDTablePair.second.EmitCU(MCOS, Params, LineStr);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000255 }
Paul Robinson1f900292018-02-06 20:29:21 +0000256
257 if (LineStr)
258 LineStr->emitSection(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000259}
260
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000261void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS, MCDwarfLineTableParams Params,
262 MCSection *Section) const {
263 if (Header.MCDwarfFiles.empty())
264 return;
Paul Robinson1f900292018-02-06 20:29:21 +0000265 Optional<MCDwarfLineStr> NoLineStr(None);
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000266 MCOS.SwitchSection(Section);
Paul Robinson1f900292018-02-06 20:29:21 +0000267 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None, NoLineStr).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000268}
269
Frederic Rissa5ab8442015-08-07 15:14:08 +0000270std::pair<MCSymbol *, MCSymbol *>
Paul Robinson1f900292018-02-06 20:29:21 +0000271MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
272 Optional<MCDwarfLineStr> &LineStr) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000273 static const char StandardOpcodeLengths[] = {
274 0, // length of DW_LNS_copy
275 1, // length of DW_LNS_advance_pc
276 1, // length of DW_LNS_advance_line
277 1, // length of DW_LNS_set_file
278 1, // length of DW_LNS_set_column
279 0, // length of DW_LNS_negate_stmt
280 0, // length of DW_LNS_set_basic_block
281 0, // length of DW_LNS_const_add_pc
282 1, // length of DW_LNS_fixed_advance_pc
283 0, // length of DW_LNS_set_prologue_end
284 0, // length of DW_LNS_set_epilogue_begin
285 1 // DW_LNS_set_isa
286 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000287 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000288 (Params.DWARF2LineOpcodeBase - 1U));
Paul Robinson1f900292018-02-06 20:29:21 +0000289 return Emit(
290 MCOS, Params,
291 makeArrayRef(StandardOpcodeLengths, Params.DWARF2LineOpcodeBase - 1),
292 LineStr);
David Blaikie8287aff2014-03-18 02:13:23 +0000293}
294
Rafael Espindolac23174b2014-08-15 15:12:13 +0000295static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
296 MCContext &Context = OS.getContext();
297 assert(!isa<MCSymbolRefExpr>(Expr));
298 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
299 return Expr;
300
Jim Grosbach6f482002015-05-18 18:43:14 +0000301 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000302 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000303 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000304}
305
306static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
307 const MCExpr *ABS = forceExpAbs(OS, Value);
308 OS.EmitValue(ABS, Size);
309}
310
Paul Robinson1f900292018-02-06 20:29:21 +0000311void MCDwarfLineStr::emitSection(MCStreamer *MCOS) {
312 // Switch to the .debug_line_str section.
313 MCOS->SwitchSection(
314 MCOS->getContext().getObjectFileInfo()->getDwarfLineStrSection());
315 // Emit the strings without perturbing the offsets we used.
316 LineStrings.finalizeInOrder();
317 SmallString<0> Data;
318 Data.resize(LineStrings.getSize());
319 LineStrings.write((uint8_t *)Data.data());
320 MCOS->EmitBinaryData(Data.str());
321}
322
323void MCDwarfLineStr::emitRef(MCStreamer *MCOS, StringRef Path) {
324 int RefSize = 4; // FIXME: Support DWARF-64
325 size_t Offset = LineStrings.add(Path);
326 if (UseRelocs) {
327 MCContext &Ctx = MCOS->getContext();
328 MCOS->EmitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
329 } else
330 MCOS->EmitIntValue(Offset, RefSize);
331}
332
Paul Robinson2bf8f492018-01-30 21:39:28 +0000333void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000334 // First the directory table.
Paul Robinson2bf8f492018-01-30 21:39:28 +0000335 for (auto &Dir : MCDwarfDirs) {
Paul Robinson795ab0d2017-12-05 20:35:00 +0000336 MCOS->EmitBytes(Dir); // The DirectoryName, and...
337 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
338 }
339 MCOS->EmitIntValue(0, 1); // Terminate the directory list.
340
341 // Second the file table.
342 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
343 assert(!MCDwarfFiles[i].Name.empty());
344 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName and...
345 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
346 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex); // Directory number.
347 MCOS->EmitIntValue(0, 1); // Last modification timestamp (always 0).
348 MCOS->EmitIntValue(0, 1); // File size (always 0).
349 }
350 MCOS->EmitIntValue(0, 1); // Terminate the file list.
351}
352
Paul Robinsonb271f312018-03-29 17:16:41 +0000353static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000354 bool EmitMD5, bool HasSource,
Paul Robinsonb271f312018-03-29 17:16:41 +0000355 Optional<MCDwarfLineStr> &LineStr) {
356 assert(!DwarfFile.Name.empty());
357 if (LineStr)
358 LineStr->emitRef(MCOS, DwarfFile.Name);
359 else {
360 MCOS->EmitBytes(DwarfFile.Name); // FileName and...
361 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
362 }
363 MCOS->EmitULEB128IntValue(DwarfFile.DirIndex); // Directory number.
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000364 if (EmitMD5) {
Paul Robinsonb271f312018-03-29 17:16:41 +0000365 MD5::MD5Result *Cksum = DwarfFile.Checksum;
366 MCOS->EmitBinaryData(
367 StringRef(reinterpret_cast<const char *>(Cksum->Bytes.data()),
368 Cksum->Bytes.size()));
369 }
370 if (HasSource) {
371 if (LineStr)
372 LineStr->emitRef(MCOS, DwarfFile.Source.getValueOr(StringRef()));
373 else {
374 MCOS->EmitBytes(
375 DwarfFile.Source.getValueOr(StringRef())); // Source and...
376 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
377 }
378 }
379}
380
Paul Robinson1f900292018-02-06 20:29:21 +0000381void MCDwarfLineTableHeader::emitV5FileDirTables(
Paul Robinsonf69316c2018-06-12 16:09:03 +0000382 MCStreamer *MCOS, Optional<MCDwarfLineStr> &LineStr,
383 StringRef CtxCompilationDir) const {
Paul Robinson1f900292018-02-06 20:29:21 +0000384 // The directory format, which is just a list of the directory paths. In a
385 // non-split object, these are references to .debug_line_str; in a split
386 // object, they are inline strings.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000387 MCOS->EmitIntValue(1, 1);
388 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
Paul Robinson1f900292018-02-06 20:29:21 +0000389 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
390 : dwarf::DW_FORM_string);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000391 MCOS->EmitULEB128IntValue(MCDwarfDirs.size() + 1);
Paul Robinsonf69316c2018-06-12 16:09:03 +0000392 // Try not to emit an empty compilation directory.
Paul Robinson8e3e3742018-06-20 17:08:46 +0000393 const StringRef CompDir =
394 CompilationDir.empty() ? CtxCompilationDir : StringRef(CompilationDir);
Paul Robinson1f900292018-02-06 20:29:21 +0000395 if (LineStr) {
396 // Record path strings, emit references here.
Paul Robinsonf69316c2018-06-12 16:09:03 +0000397 LineStr->emitRef(MCOS, CompDir);
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000398 for (const auto &Dir : MCDwarfDirs)
Paul Robinson1f900292018-02-06 20:29:21 +0000399 LineStr->emitRef(MCOS, Dir);
400 } else {
Paul Robinsonf69316c2018-06-12 16:09:03 +0000401 // The list of directory paths. Compilation directory comes first.
402 MCOS->EmitBytes(CompDir);
Paul Robinson1f900292018-02-06 20:29:21 +0000403 MCOS->EmitBytes(StringRef("\0", 1));
Paul Robinsonc17c8bf2018-07-10 14:41:54 +0000404 for (const auto &Dir : MCDwarfDirs) {
Paul Robinson1f900292018-02-06 20:29:21 +0000405 MCOS->EmitBytes(Dir); // The DirectoryName, and...
406 MCOS->EmitBytes(StringRef("\0", 1)); // its null terminator.
407 }
Paul Robinson795ab0d2017-12-05 20:35:00 +0000408 }
409
410 // The file format, which is the inline null-terminated filename and a
411 // directory index. We don't track file size/timestamp so don't emit them
Scott Linder16c7bda2018-02-23 23:01:06 +0000412 // in the v5 table. Emit MD5 checksums and source if we have them.
413 uint64_t Entries = 2;
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000414 if (HasAllMD5)
Scott Linder16c7bda2018-02-23 23:01:06 +0000415 Entries += 1;
416 if (HasSource)
417 Entries += 1;
418 MCOS->EmitIntValue(Entries, 1);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000419 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_path);
Paul Robinson1f900292018-02-06 20:29:21 +0000420 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
421 : dwarf::DW_FORM_string);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000422 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_directory_index);
423 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_udata);
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000424 if (HasAllMD5) {
Paul Robinson29f5f982018-01-09 23:31:48 +0000425 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_MD5);
426 MCOS->EmitULEB128IntValue(dwarf::DW_FORM_data16);
427 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000428 if (HasSource) {
429 MCOS->EmitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
430 MCOS->EmitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
431 : dwarf::DW_FORM_string);
432 }
Paul Robinsonb271f312018-03-29 17:16:41 +0000433 // Then the counted list of files. The root file is file #0, then emit the
Paul Robinsonf69316c2018-06-12 16:09:03 +0000434 // files as provide by .file directives. To accommodate assembler source
435 // written for DWARF v4 but trying to emit v5, if we didn't see a root file
436 // explicitly, replicate file #1.
Paul Robinsonb271f312018-03-29 17:16:41 +0000437 MCOS->EmitULEB128IntValue(MCDwarfFiles.size());
Paul Robinsonf69316c2018-06-12 16:09:03 +0000438 emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000439 HasAllMD5, HasSource, LineStr);
Paul Robinsonb271f312018-03-29 17:16:41 +0000440 for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000441 emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
Paul Robinson795ab0d2017-12-05 20:35:00 +0000442}
443
David Blaikie8287aff2014-03-18 02:13:23 +0000444std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000445MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
Paul Robinson1f900292018-02-06 20:29:21 +0000446 ArrayRef<char> StandardOpcodeLengths,
447 Optional<MCDwarfLineStr> &LineStr) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000448 MCContext &context = MCOS->getContext();
449
450 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000451 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000452 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000453 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000454 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000455 MCOS->EmitLabel(LineStartSym);
456
457 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000458 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000459
460 // The first 4 bytes is the total length of the information for this
461 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000462 emitAbsValue(*MCOS,
463 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000464
Paul Robinson68ba7722017-12-04 21:27:46 +0000465 unsigned LineTableVersion = context.getDwarfVersion();
Jonas Devlieghere49ff4d92018-08-08 21:16:50 +0000466
467 // On Darwin we default to v2 for anything before DWARF v5.
468 if (context.getObjectFileInfo()->getTargetTriple().isOSDarwin() &&
469 LineTableVersion < 5)
Davide Italiano23b3f6d2017-12-07 00:57:25 +0000470 LineTableVersion = 2;
Jonas Devlieghere49ff4d92018-08-08 21:16:50 +0000471
472 // Next 2 bytes is the Version.
Paul Robinson68ba7722017-12-04 21:27:46 +0000473 MCOS->EmitIntValue(LineTableVersion, 2);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000474
Paul Robinson795ab0d2017-12-05 20:35:00 +0000475 // Keep track of the bytes between the very start and where the header length
476 // comes out.
477 unsigned PreHeaderLengthBytes = 4 + 2;
478
479 // In v5, we get address info next.
480 if (LineTableVersion >= 5) {
481 MCOS->EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
482 MCOS->EmitIntValue(0, 1); // Segment selector; same as EmitGenDwarfAranges.
483 PreHeaderLengthBytes += 2;
484 }
485
Kevin Enderbye46564a2010-09-30 16:52:03 +0000486 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000487 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000488
Paul Robinson795ab0d2017-12-05 20:35:00 +0000489 // Length of the prologue, is the next 4 bytes. This is actually the length
490 // from after the length word, to the end of the prologue.
491 emitAbsValue(*MCOS,
492 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
493 (PreHeaderLengthBytes + 4)),
494 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000495
496 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000497 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Fangrui Songf78650a2018-07-30 19:41:25 +0000498 // maximum_operations_per_instruction
Paul Robinson68ba7722017-12-04 21:27:46 +0000499 // For non-VLIW architectures this field is always 1.
500 // FIXME: VLIW architectures need to update this field accordingly.
Paul Robinson795ab0d2017-12-05 20:35:00 +0000501 if (LineTableVersion >= 4)
Paul Robinson68ba7722017-12-04 21:27:46 +0000502 MCOS->EmitIntValue(1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000503 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000504 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
505 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000506 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000507
508 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000509 for (char Length : StandardOpcodeLengths)
510 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000511
Paul Robinson795ab0d2017-12-05 20:35:00 +0000512 // Put out the directory and file tables. The formats vary depending on
513 // the version.
514 if (LineTableVersion >= 5)
Paul Robinsonf69316c2018-06-12 16:09:03 +0000515 emitV5FileDirTables(MCOS, LineStr, context.getCompilationDir());
Paul Robinson795ab0d2017-12-05 20:35:00 +0000516 else
Paul Robinson2bf8f492018-01-30 21:39:28 +0000517 emitV2FileDirTables(MCOS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000518
519 // This is the end of the prologue, so set the value of the symbol at the
520 // end of the prologue (that was used in a previous expression).
521 MCOS->EmitLabel(ProEndSym);
522
David Blaikie5cff0e62014-03-13 21:47:12 +0000523 return std::make_pair(LineStartSym, LineEndSym);
524}
525
Frederic Rissa5ab8442015-08-07 15:14:08 +0000526void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
Paul Robinson1f900292018-02-06 20:29:21 +0000527 MCDwarfLineTableParams Params,
528 Optional<MCDwarfLineStr> &LineStr) const {
529 MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000530
Kevin Enderbye46564a2010-09-30 16:52:03 +0000531 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000532 for (const auto &LineSec : MCLineSections.getMCLineEntries())
533 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000534
Kevin Enderbye46564a2010-09-30 16:52:03 +0000535 // This is the end of the section, so set the value of the symbol at the end
536 // of this section (that was used in a previous expression).
537 MCOS->EmitLabel(LineEndSym);
538}
539
Paul Robinson70def122018-02-22 21:03:33 +0000540Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory,
541 StringRef &FileName,
542 MD5::MD5Result *Checksum,
Scott Linder16c7bda2018-02-23 23:01:06 +0000543 Optional<StringRef> Source,
Paul Robinson70def122018-02-22 21:03:33 +0000544 unsigned FileNumber) {
Scott Linder16c7bda2018-02-23 23:01:06 +0000545 return Header.tryGetFile(Directory, FileName, Checksum, Source, FileNumber);
David Blaikie5cff0e62014-03-13 21:47:12 +0000546}
547
Scott Linder16c7bda2018-02-23 23:01:06 +0000548Expected<unsigned>
549MCDwarfLineTableHeader::tryGetFile(StringRef &Directory,
550 StringRef &FileName,
551 MD5::MD5Result *Checksum,
552 Optional<StringRef> &Source,
553 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000554 if (Directory == CompilationDir)
555 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000556 if (FileName.empty()) {
557 FileName = "<stdin>";
558 Directory = "";
559 }
560 assert(!FileName.empty());
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000561 // Keep track of whether any or all files have an MD5 checksum.
562 // If any files have embedded source, they all must.
Scott Linder16c7bda2018-02-23 23:01:06 +0000563 if (MCDwarfFiles.empty()) {
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000564 trackMD5Usage(Checksum);
Scott Linder16c7bda2018-02-23 23:01:06 +0000565 HasSource = (Source != None);
566 }
David Blaikiec714ef42014-03-17 01:52:11 +0000567 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000568 // File numbers start with 1 and/or after any file numbers
569 // allocated by inline-assembler .file directives.
570 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000571 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000572 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000573 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
574 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000575 if (!IterBool.second)
576 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000577 }
David Blaikie498589c2014-03-13 19:15:04 +0000578 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
Paul Robinsona1cedd62017-12-14 18:46:43 +0000579 if (FileNumber >= MCDwarfFiles.size())
580 MCDwarfFiles.resize(FileNumber + 1);
David Blaikie498589c2014-03-13 19:15:04 +0000581
582 // Get the new MCDwarfFile slot for this FileNumber.
583 MCDwarfFile &File = MCDwarfFiles[FileNumber];
584
Paul Robinson70def122018-02-22 21:03:33 +0000585 // It is an error to see the same number more than once.
David Blaikie498589c2014-03-13 19:15:04 +0000586 if (!File.Name.empty())
Paul Robinson70def122018-02-22 21:03:33 +0000587 return make_error<StringError>("file number already allocated",
588 inconvertibleErrorCode());
David Blaikie498589c2014-03-13 19:15:04 +0000589
Scott Linder16c7bda2018-02-23 23:01:06 +0000590 // If any files have embedded source, they all must.
591 if (HasSource != (Source != None))
592 return make_error<StringError>("inconsistent use of embedded source",
593 inconvertibleErrorCode());
Paul Robinson29f5f982018-01-09 23:31:48 +0000594
David Blaikie498589c2014-03-13 19:15:04 +0000595 if (Directory.empty()) {
596 // Separate the directory part from the basename of the FileName.
597 StringRef tFileName = sys::path::filename(FileName);
598 if (!tFileName.empty()) {
599 Directory = sys::path::parent_path(FileName);
600 if (!Directory.empty())
601 FileName = tFileName;
602 }
603 }
604
605 // Find or make an entry in the MCDwarfDirs vector for this Directory.
606 // Capture directory name.
607 unsigned DirIndex;
608 if (Directory.empty()) {
609 // For FileNames with no directories a DirIndex of 0 is used.
610 DirIndex = 0;
611 } else {
612 DirIndex = 0;
613 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
614 if (Directory == MCDwarfDirs[DirIndex])
615 break;
616 }
617 if (DirIndex >= MCDwarfDirs.size())
618 MCDwarfDirs.push_back(Directory);
619 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
620 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
621 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
622 // are stored at MCDwarfFiles[FileNumber].Name .
623 DirIndex++;
624 }
625
626 File.Name = FileName;
627 File.DirIndex = DirIndex;
Paul Robinson29f5f982018-01-09 23:31:48 +0000628 File.Checksum = Checksum;
Paul Robinsoncc7344a2018-06-14 13:38:20 +0000629 trackMD5Usage(Checksum);
Scott Linder16c7bda2018-02-23 23:01:06 +0000630 File.Source = Source;
631 if (Source)
632 HasSource = true;
David Blaikie498589c2014-03-13 19:15:04 +0000633
634 // return the allocated FileNumber.
635 return FileNumber;
636}
637
Kevin Enderbye46564a2010-09-30 16:52:03 +0000638/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000639void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
640 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000641 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000642 SmallString<256> Tmp;
643 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000644 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000645 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000646}
647
Frederic Rissa5ab8442015-08-07 15:14:08 +0000648/// Given a special op, return the address skip amount (in units of
649/// DWARF2_LINE_MIN_INSN_LENGTH).
650static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
651 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
652}
653
Kevin Enderbye46564a2010-09-30 16:52:03 +0000654/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000655void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
656 int64_t LineDelta, uint64_t AddrDelta,
657 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000658 uint64_t Temp, Opcode;
659 bool NeedCopy = false;
660
Frederic Rissa5ab8442015-08-07 15:14:08 +0000661 // The maximum address skip amount that can be encoded with a special op.
662 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
663
Kevin Enderbye46564a2010-09-30 16:52:03 +0000664 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000665 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000666
667 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000668 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000669 // end_sequence to emit the matrix entry.
670 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000671 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000672 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000673 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000674 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000675 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000676 }
677 OS << char(dwarf::DW_LNS_extended_op);
678 OS << char(1);
679 OS << char(dwarf::DW_LNE_end_sequence);
680 return;
681 }
682
683 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000684 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000685
686 // If the line increment is out of range of a special opcode, we must encode
687 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000688 if (Temp >= Params.DWARF2LineRange ||
689 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000690 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000691 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000692
693 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000694 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000695 NeedCopy = true;
696 }
697
698 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
699 if (LineDelta == 0 && AddrDelta == 0) {
700 OS << char(dwarf::DW_LNS_copy);
701 return;
702 }
703
704 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000705 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000706
707 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000708 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000709 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000710 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000711 if (Opcode <= 255) {
712 OS << char(Opcode);
713 return;
714 }
715
716 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000717 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000718 if (Opcode <= 255) {
719 OS << char(dwarf::DW_LNS_const_add_pc);
720 OS << char(Opcode);
721 return;
722 }
723 }
724
725 // Otherwise use DW_LNS_advance_pc.
726 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000727 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000728
729 if (NeedCopy)
730 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000731 else {
732 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000733 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000734 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000735}
736
Hsiangkai Wang5c63af02018-08-01 02:18:06 +0000737bool MCDwarfLineAddr::FixedEncode(MCContext &Context,
738 MCDwarfLineTableParams Params,
739 int64_t LineDelta, uint64_t AddrDelta,
740 raw_ostream &OS,
741 uint32_t *Offset, uint32_t *Size) {
742 if (LineDelta != INT64_MAX) {
743 OS << char(dwarf::DW_LNS_advance_line);
744 encodeSLEB128(LineDelta, OS);
745 }
746
747 // Use address delta to adjust address or use absolute address to adjust
748 // address.
749 bool SetDelta;
750 // According to DWARF spec., the DW_LNS_fixed_advance_pc opcode takes a
751 // single uhalf (unencoded) operand. So, the maximum value of AddrDelta
752 // is 65535. We set a conservative upper bound for it for relaxation.
753 if (AddrDelta > 60000) {
754 const MCAsmInfo *asmInfo = Context.getAsmInfo();
755 unsigned AddrSize = asmInfo->getCodePointerSize();
756
757 OS << char(dwarf::DW_LNS_extended_op);
758 encodeULEB128(1 + AddrSize, OS);
759 OS << char(dwarf::DW_LNE_set_address);
760 // Generate fixup for the address.
761 *Offset = OS.tell();
762 *Size = AddrSize;
763 SetDelta = false;
764 std::vector<uint8_t> FillData;
765 FillData.insert(FillData.begin(), AddrSize, 0);
766 OS.write(reinterpret_cast<char *>(FillData.data()), AddrSize);
767 } else {
768 OS << char(dwarf::DW_LNS_fixed_advance_pc);
769 // Generate fixup for 2-bytes address delta.
770 *Offset = OS.tell();
771 *Size = 2;
772 SetDelta = true;
773 OS << char(0);
774 OS << char(0);
775 }
776
777 if (LineDelta == INT64_MAX) {
778 OS << char(dwarf::DW_LNS_extended_op);
779 OS << char(1);
780 OS << char(dwarf::DW_LNE_end_sequence);
781 } else {
782 OS << char(dwarf::DW_LNS_copy);
783 }
784
785 return SetDelta;
786}
787
Kevin Enderbye7739d42011-12-09 18:09:40 +0000788// Utility function to write a tuple for .debug_abbrev.
789static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
790 MCOS->EmitULEB128IntValue(Name);
791 MCOS->EmitULEB128IntValue(Form);
792}
793
794// When generating dwarf for assembly source files this emits
795// the data for .debug_abbrev section which contains three DIEs.
796static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
797 MCContext &context = MCOS->getContext();
798 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
799
800 // DW_TAG_compile_unit DIE abbrev (1).
801 MCOS->EmitULEB128IntValue(1);
802 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
803 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000804 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
805 ? dwarf::DW_FORM_sec_offset
806 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000807 if (context.getGenDwarfSectionSyms().size() > 1 &&
808 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000809 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
810 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000811 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000812 } else {
813 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
814 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
815 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000816 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000817 if (!context.getCompilationDir().empty())
818 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000819 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
820 if (!DwarfDebugFlags.empty())
821 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
822 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
823 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
824 EmitAbbrev(MCOS, 0, 0);
825
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000826 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000827 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000828 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000829 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
830 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
831 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
832 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
833 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000834 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
835 EmitAbbrev(MCOS, 0, 0);
836
837 // DW_TAG_unspecified_parameters DIE abbrev (3).
838 MCOS->EmitULEB128IntValue(3);
839 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
840 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
841 EmitAbbrev(MCOS, 0, 0);
842
843 // Terminate the abbreviations for this compilation unit.
844 MCOS->EmitIntValue(0, 1);
845}
846
847// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000848// .debug_aranges section. This section contains a header and a table of pairs
849// of PointerSize'ed values for the address and size of section(s) with line
850// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000851static void EmitGenDwarfAranges(MCStreamer *MCOS,
852 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000853 MCContext &context = MCOS->getContext();
854
Oliver Stannard8b273082014-06-19 15:52:37 +0000855 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000856
857 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
858
859 // This will be the length of the .debug_aranges section, first account for
860 // the size of each item in the header (see below where we emit these items).
861 int Length = 4 + 2 + 4 + 1 + 1;
862
863 // Figure the padding after the header before the table of address and size
864 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000865 const MCAsmInfo *asmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000866 int AddrSize = asmInfo->getCodePointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000867 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
868 if (Pad == 2 * AddrSize)
869 Pad = 0;
870 Length += Pad;
871
872 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000873 // of each section we have in the table.
874 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000875 // And the pair of terminating zeros.
876 Length += 2 * AddrSize;
877
Kevin Enderbye7739d42011-12-09 18:09:40 +0000878 // Emit the header for this section.
879 // The 4 byte length not including the 4 byte value for the length.
880 MCOS->EmitIntValue(Length - 4, 4);
881 // The 2 byte version, which is 2.
882 MCOS->EmitIntValue(2, 2);
883 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000884 // of the .debug_info.
885 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000886 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
887 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000888 else
889 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000890 // The 1 byte size of an address.
891 MCOS->EmitIntValue(AddrSize, 1);
892 // The 1 byte size of a segment descriptor, we use a value of zero.
893 MCOS->EmitIntValue(0, 1);
894 // Align the header with the padding if needed, before we put out the table.
895 for(int i = 0; i < Pad; i++)
896 MCOS->EmitIntValue(0, 1);
897
Oliver Stannard8b273082014-06-19 15:52:37 +0000898 // Now emit the table of pairs of PointerSize'ed values for the section
899 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000900 for (MCSection *Sec : Sections) {
901 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000902 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000903 assert(StartSymbol && "StartSymbol must not be NULL");
904 assert(EndSymbol && "EndSymbol must not be NULL");
905
Jim Grosbach13760bd2015-05-30 01:25:56 +0000906 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000907 StartSymbol, MCSymbolRefExpr::VK_None, context);
908 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
909 *StartSymbol, *EndSymbol, 0);
910 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000911 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000912 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000913
914 // And finally the pair of terminating zeros.
915 MCOS->EmitIntValue(0, AddrSize);
916 MCOS->EmitIntValue(0, AddrSize);
917}
918
919// When generating dwarf for assembly source files this emits the data for
920// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000921// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000922static void EmitGenDwarfInfo(MCStreamer *MCOS,
923 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000924 const MCSymbol *LineSectionSymbol,
925 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000926 MCContext &context = MCOS->getContext();
927
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000928 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000929
930 // Create a symbol at the start and end of this section used in here for the
931 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000932 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000933 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000934 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000935
936 // First part: the header.
937
938 // The 4 byte total length of the information for this compilation unit, not
939 // including these 4 bytes.
940 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000941 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000942
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000943 // The 2 byte DWARF version.
944 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000945
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000946 // The DWARF v5 header has unit type, address size, abbrev offset.
947 // Earlier versions have abbrev offset, address size.
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000948 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000949 int AddrSize = AsmInfo.getCodePointerSize();
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000950 if (context.getDwarfVersion() >= 5) {
951 MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
952 MCOS->EmitIntValue(AddrSize, 1);
953 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000954 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
955 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000956 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000957 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000958 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000959 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
960 AsmInfo.needsDwarfSectionOffsetDirective());
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000961 if (context.getDwarfVersion() <= 4)
962 MCOS->EmitIntValue(AddrSize, 1);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000963
964 // Second part: the compile_unit DIE.
965
966 // The DW_TAG_compile_unit DIE abbrev (1).
967 MCOS->EmitULEB128IntValue(1);
968
969 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
970 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000971 if (LineSectionSymbol)
972 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
973 AsmInfo.needsDwarfSectionOffsetDirective());
974 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000975 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000976
Oliver Stannard8b273082014-06-19 15:52:37 +0000977 if (RangesSectionSymbol) {
978 // There are multiple sections containing code, so we must use the
979 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000980
Oliver Stannard8b273082014-06-19 15:52:37 +0000981 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
982 // to the address range list for this compilation unit.
983 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
984 } else {
985 // If we only have one non-empty code section, we can use the simpler
986 // AT_low_pc and AT_high_pc attributes.
987
988 // Find the first (and only) non-empty text section
989 auto &Sections = context.getGenDwarfSectionSyms();
990 const auto TextSection = Sections.begin();
991 assert(TextSection != Sections.end() && "No text section found");
992
Rafael Espindolae0746792015-05-21 16:52:32 +0000993 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
994 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000995 assert(StartSymbol && "StartSymbol must not be NULL");
996 assert(EndSymbol && "EndSymbol must not be NULL");
997
998 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000999 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001000 StartSymbol, MCSymbolRefExpr::VK_None, context);
1001 MCOS->EmitValue(Start, AddrSize);
1002
1003 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001004 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001005 EndSymbol, MCSymbolRefExpr::VK_None, context);
1006 MCOS->EmitValue(End, AddrSize);
1007 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001008
1009 // AT_name, the name of the source file. Reconstruct from the first directory
1010 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +00001011 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +00001012 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +00001013 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +00001014 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001015 }
David Blaikiea55ddad2014-03-13 18:55:04 +00001016 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +00001017 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +00001018 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001019 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1020
1021 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +00001022 if (!context.getCompilationDir().empty()) {
1023 MCOS->EmitBytes(context.getCompilationDir());
1024 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1025 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001026
1027 // AT_APPLE_flags, the command line arguments of the assembler tool.
1028 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
1029 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +00001030 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001031 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1032 }
1033
1034 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +00001035 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +00001036 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +00001037 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +00001038 else
1039 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +00001040 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1041
1042 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
1043 // draft has no standard code for assembler.
1044 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
1045
Kevin Enderby8d4a2202012-01-10 17:52:29 +00001046 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001047
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001048 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001049 const std::vector<MCGenDwarfLabelEntry> &Entries =
1050 MCOS->getContext().getMCGenDwarfLabelEntries();
1051 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +00001052 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +00001053 MCOS->EmitULEB128IntValue(2);
1054
1055 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001056 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001057 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
1058
1059 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001060 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001061
1062 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001063 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001064
1065 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +00001066 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +00001067 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +00001068 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001069
Kevin Enderbye7739d42011-12-09 18:09:40 +00001070 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
1071 MCOS->EmitIntValue(0, 1);
1072
1073 // The DW_TAG_unspecified_parameters DIE abbrev (3).
1074 MCOS->EmitULEB128IntValue(3);
1075
1076 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
1077 MCOS->EmitIntValue(0, 1);
1078 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001079
1080 // Add the NULL DIE terminating the Compile Unit DIE's.
1081 MCOS->EmitIntValue(0, 1);
1082
1083 // Now set the value of the symbol at the end of the info section.
1084 MCOS->EmitLabel(InfoEnd);
1085}
1086
Oliver Stannard8b273082014-06-19 15:52:37 +00001087// When generating dwarf for assembly source files this emits the data for
1088// .debug_ranges section. We only emit one range list, which spans all of the
1089// executable sections of this file.
1090static void EmitGenDwarfRanges(MCStreamer *MCOS) {
1091 MCContext &context = MCOS->getContext();
1092 auto &Sections = context.getGenDwarfSectionSyms();
1093
1094 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001095 int AddrSize = AsmInfo->getCodePointerSize();
Oliver Stannard8b273082014-06-19 15:52:37 +00001096
1097 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
1098
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001099 for (MCSection *Sec : Sections) {
1100 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +00001101 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +00001102 assert(StartSymbol && "StartSymbol must not be NULL");
1103 assert(EndSymbol && "EndSymbol must not be NULL");
1104
1105 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +00001106 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +00001107 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +00001108 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +00001109 MCOS->EmitValue(SectionStartAddr, AddrSize);
1110
1111 // Emit a range list entry spanning this section
1112 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
1113 *StartSymbol, *EndSymbol, 0);
1114 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001115 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +00001116 }
1117
1118 // Emit end of list entry
1119 MCOS->EmitIntValue(0, AddrSize);
1120 MCOS->EmitIntValue(0, AddrSize);
1121}
1122
Kevin Enderbye7739d42011-12-09 18:09:40 +00001123//
1124// When generating dwarf for assembly source files this emits the Dwarf
1125// sections.
1126//
David Blaikie8bf66c42014-04-01 07:35:52 +00001127void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +00001128 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +00001129
1130 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +00001131 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001132 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +00001133 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +00001134 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +00001135 if (CreateDwarfSectionSymbols)
1136 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +00001137 MCSymbol *AbbrevSectionSymbol = nullptr;
1138 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001139 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +00001140
1141 // Create end symbols for each section, and remove empty sections
1142 MCOS->getContext().finalizeDwarfSections(*MCOS);
1143
1144 // If there are no sections to generate debug info for, we don't need
1145 // to do anything
1146 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
1147 return;
1148
Oliver Stannard14f97d02014-09-22 10:45:16 +00001149 // We only use the .debug_ranges section if we have multiple code sections,
1150 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +00001151 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +00001152 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
1153 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +00001154 CreateDwarfSectionSymbols |= UseRangesSection;
1155
Kevin Enderbye7739d42011-12-09 18:09:40 +00001156 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001157 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001158 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001159 MCOS->EmitLabel(InfoSectionSymbol);
1160 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001161 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001162 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001163 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001164 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +00001165 }
Oliver Stannard8b273082014-06-19 15:52:37 +00001166 if (UseRangesSection) {
1167 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
1168 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001169 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +00001170 MCOS->EmitLabel(RangesSectionSymbol);
1171 }
1172 }
Kevin Enderbye7739d42011-12-09 18:09:40 +00001173
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001174 assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
Oliver Stannard8b273082014-06-19 15:52:37 +00001175
1176 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +00001177
1178 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +00001179 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001180
Oliver Stannard8b273082014-06-19 15:52:37 +00001181 if (UseRangesSection)
1182 EmitGenDwarfRanges(MCOS);
1183
Kevin Enderbye7739d42011-12-09 18:09:40 +00001184 // Output the data for .debug_abbrev section.
1185 EmitGenDwarfAbbrev(MCOS);
1186
1187 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +00001188 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
1189 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001190}
1191
1192//
1193// When generating dwarf for assembly source files this is called when symbol
1194// for a label is created. If this symbol is not a temporary and is in the
1195// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001196// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001197//
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001198void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +00001199 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +00001200 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001201 if (Symbol->isTemporary())
1202 return;
1203 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +00001204 // We won't create dwarf labels for symbols in sections that we are not
1205 // generating debug info for.
Eric Christopher445c9522016-10-14 05:47:37 +00001206 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
Kevin Enderbye7739d42011-12-09 18:09:40 +00001207 return;
1208
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001209 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +00001210 // underbar if any.
1211 StringRef Name = Symbol->getName();
1212 if (Name.startswith("_"))
1213 Name = Name.substr(1, Name.size()-1);
1214
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001215 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +00001216 unsigned FileNumber = context.getGenDwarfFileNumber();
1217
1218 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +00001219 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +00001220 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +00001221 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
1222
1223 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
1224 // values so that they don't have things like an ARM thumb bit from the
1225 // original symbol. So when used they won't get a low bit set after
1226 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +00001227 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +00001228 MCOS->EmitLabel(Label);
1229
1230 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +00001231 MCOS->getContext().addMCGenDwarfLabelEntry(
1232 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +00001233}
1234
Rafael Espindola0a017a62010-12-10 07:39:47 +00001235static int getDataAlignmentFactor(MCStreamer &streamer) {
1236 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001237 const MCAsmInfo *asmInfo = context.getAsmInfo();
1238 int size = asmInfo->getCalleeSaveStackSlotSize();
1239 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +00001240 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +00001241 else
1242 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001243}
1244
Rafael Espindola5395f442011-04-22 00:08:43 +00001245static unsigned getSizeForEncoding(MCStreamer &streamer,
1246 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001247 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001248 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001249 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +00001250 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001251 case dwarf::DW_EH_PE_absptr:
1252 case dwarf::DW_EH_PE_signed:
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001253 return context.getAsmInfo()->getCodePointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001254 case dwarf::DW_EH_PE_udata2:
1255 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +00001256 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001257 case dwarf::DW_EH_PE_udata4:
1258 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +00001259 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001260 case dwarf::DW_EH_PE_udata8:
1261 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +00001262 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001263 }
Rafael Espindola5395f442011-04-22 00:08:43 +00001264}
1265
Rafael Espindolafe963b12014-08-15 03:07:13 +00001266static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
1267 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +00001268 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001269 const MCAsmInfo *asmInfo = context.getAsmInfo();
1270 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1271 symbolEncoding,
1272 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +00001273 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001274 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +00001275 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +00001276 else
1277 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001278}
1279
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001280static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1281 unsigned symbolEncoding) {
1282 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001283 const MCAsmInfo *asmInfo = context.getAsmInfo();
1284 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1285 symbolEncoding,
1286 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001287 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001288 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001289}
1290
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001291namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001292
Rafael Espindola68c21652015-11-05 23:54:18 +00001293class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001294 int CFAOffset = 0;
1295 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001296 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001297 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001298
Rafael Espindola68c21652015-11-05 23:54:18 +00001299public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001300 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1301 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001302
Rafael Espindola68c21652015-11-05 23:54:18 +00001303 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001304 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001305
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001306 const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
Rafael Espindola46be4352015-11-06 03:02:51 +00001307 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001308 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001309 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001310 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001311 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001312};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001313
1314} // end anonymous namespace
1315
Rafael Espindolafe963b12014-08-15 03:07:13 +00001316static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001317 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001318}
1319
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001320void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001321 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001322 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001323
1324 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001325 case MCCFIInstruction::OpRegister: {
1326 unsigned Reg1 = Instr.getRegister();
1327 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001328 if (!IsEH) {
Jake Ehrlich3da79822017-12-01 21:44:27 +00001329 Reg1 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg1);
1330 Reg2 = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg2);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001331 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001332 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1333 Streamer.EmitULEB128IntValue(Reg1);
1334 Streamer.EmitULEB128IntValue(Reg2);
1335 return;
1336 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001337 case MCCFIInstruction::OpWindowSave:
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001338 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1339 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001340
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001341 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001342 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001343 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1344 Streamer.EmitULEB128IntValue(Reg);
1345 return;
1346 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001347 case MCCFIInstruction::OpAdjustCfaOffset:
1348 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001349 const bool IsRelative =
1350 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1351
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001352 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1353
1354 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001355 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001356 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001357 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001358
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001359 Streamer.EmitULEB128IntValue(CFAOffset);
1360
1361 return;
1362 }
1363 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001364 unsigned Reg = Instr.getRegister();
1365 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001366 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001367 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001368 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001369 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001370 Streamer.EmitULEB128IntValue(CFAOffset);
1371
1372 return;
1373 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001374 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001375 unsigned Reg = Instr.getRegister();
1376 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001377 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001378 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001379 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001380
1381 return;
1382 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001383 case MCCFIInstruction::OpOffset:
1384 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001385 const bool IsRelative =
1386 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001387
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001388 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001389 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001390 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001391
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001392 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001393 if (IsRelative)
1394 Offset -= CFAOffset;
1395 Offset = Offset / dataAlignmentFactor;
1396
1397 if (Offset < 0) {
1398 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1399 Streamer.EmitULEB128IntValue(Reg);
1400 Streamer.EmitSLEB128IntValue(Offset);
1401 } else if (Reg < 64) {
1402 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001403 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001404 } else {
1405 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001406 Streamer.EmitULEB128IntValue(Reg);
1407 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001408 }
1409 return;
1410 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001411 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001412 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1413 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001414 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001415 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1416 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001417 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001418 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001419 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001420 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001421 return;
1422 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001423 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001424 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001425 if (!IsEH)
Jake Ehrlich3da79822017-12-01 21:44:27 +00001426 Reg = MRI->getDwarfRegNumFromDwarfEHRegNum(Reg);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001427 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1428 return;
1429 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001430 case MCCFIInstruction::OpGnuArgsSize:
Michael Kuperstein259f1502015-10-07 07:01:31 +00001431 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1432 Streamer.EmitULEB128IntValue(Instr.getOffset());
1433 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001434
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001435 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001436 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001437 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001438 }
1439 llvm_unreachable("Unhandled case in switch");
1440}
1441
Rafael Espindolafe963b12014-08-15 03:07:13 +00001442/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001443void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001444 MCSymbol *BaseLabel) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001445 for (const MCCFIInstruction &Instr : Instrs) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001446 MCSymbol *Label = Instr.getLabel();
1447 // Throw out move if the label is invalid.
1448 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1449
1450 // Advance row if new location.
1451 if (BaseLabel && Label) {
1452 MCSymbol *ThisSym = Label;
1453 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001454 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001455 BaseLabel = ThisSym;
1456 }
1457 }
1458
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001459 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001460 }
1461}
1462
Rafael Espindolafe963b12014-08-15 03:07:13 +00001463/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001464void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001465 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001466 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001467
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001468 // range-start range-length compact-unwind-enc personality-func lsda
1469 // _foo LfooEnd-_foo 0x00000023 0 0
1470 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1471 //
1472 // .section __LD,__compact_unwind,regular,debug
1473 //
1474 // # compact unwind for _foo
1475 // .quad _foo
1476 // .set L1,LfooEnd-_foo
1477 // .long L1
1478 // .long 0x01010001
1479 // .quad 0
1480 // .quad 0
1481 //
1482 // # compact unwind for _bar
1483 // .quad _bar
1484 // .set L2,LbarEnd-_bar
1485 // .long L2
1486 // .long 0x01020011
1487 // .quad __gxx_personality
1488 // .quad except_tab1
1489
Bill Wendling49bc6802011-07-19 00:06:12 +00001490 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001491 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001492 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001493
Bill Wendlingdafd5982011-07-14 23:34:45 +00001494 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001495 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001496 Encoding |= 0x40000000;
1497
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001498 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001499 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001500 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001501 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001502
1503 // Range Length
1504 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1505 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001506 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001507
Bill Wendling75174662011-06-30 00:30:52 +00001508 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001509 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1510 Streamer.EmitIntValue(Encoding, Size);
1511
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001512 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001513 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001514 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001515 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001516 else
1517 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001518
1519 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001520 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001521 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001522 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001523 else
1524 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001525}
1526
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001527static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1528 if (IsEH)
1529 return 1;
1530 switch (DwarfVersion) {
1531 case 2:
1532 return 1;
1533 case 3:
1534 return 3;
1535 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001536 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001537 return 4;
1538 }
1539 llvm_unreachable("Unknown version");
1540}
1541
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001542const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001543 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001544 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001545 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001546
Jim Grosbach6f482002015-05-18 18:43:14 +00001547 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001548 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001549
Jim Grosbach6f482002015-05-18 18:43:14 +00001550 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001551
1552 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001553 const MCExpr *Length =
1554 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1555 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001556
1557 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001558 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001559 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001560
1561 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001562 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001563 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001564
1565 // Augmentation String
1566 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001567 if (IsEH) {
1568 Augmentation += "z";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001569 if (Frame.Personality)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001570 Augmentation += "P";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001571 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001572 Augmentation += "L";
1573 Augmentation += "R";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001574 if (Frame.IsSignalFrame)
Rafael Espindola3c47e372012-01-23 21:51:52 +00001575 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001576 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001577 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001578 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001579
Keith Walkerea9483f2015-05-12 15:25:08 +00001580 if (CIEVersion >= 4) {
1581 // Address Size
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001582 Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001583
1584 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001585 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001586 }
1587
Rafael Espindola0a017a62010-12-10 07:39:47 +00001588 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001589 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001590
1591 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001592 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001593
1594 // Return Address Register
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001595 unsigned RAReg = Frame.RAReg;
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001596 if (RAReg == static_cast<unsigned>(INT_MAX))
1597 RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1598
Oliver Stannardf7693f42014-06-19 15:39:33 +00001599 if (CIEVersion == 1) {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001600 assert(RAReg <= 255 &&
Oliver Stannardf7693f42014-06-19 15:39:33 +00001601 "DWARF 2 encodes return_address_register in one byte");
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001602 Streamer.EmitIntValue(RAReg, 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001603 } else {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001604 Streamer.EmitULEB128IntValue(RAReg);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001605 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001606
1607 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001608 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001609 if (IsEH) {
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001610 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001611 // Personality Encoding
1612 augmentationLength += 1;
1613 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001614 augmentationLength +=
1615 getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001616 }
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001617 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001618 augmentationLength += 1;
1619 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001620 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001621
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001622 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001623
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001624 // Augmentation Data (optional)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001625 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001626 // Personality Encoding
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001627 emitEncodingByte(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001628 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001629 EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001630 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001631
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001632 if (Frame.Lsda)
1633 emitEncodingByte(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001634
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001635 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001636 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001637 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001638
1639 // Initial Instructions
1640
Bill Wendlingbc07a892013-06-18 07:20:20 +00001641 const MCAsmInfo *MAI = context.getAsmInfo();
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001642 if (!Frame.IsSimple) {
David Majnemere035cf92014-01-27 17:20:25 +00001643 const std::vector<MCCFIInstruction> &Instructions =
1644 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001645 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001646 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001647
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001648 InitialCFAOffset = CFAOffset;
1649
Rafael Espindola0a017a62010-12-10 07:39:47 +00001650 // Padding
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001651 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001652
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001653 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001654 return *sectionStart;
1655}
1656
Rafael Espindola46be4352015-11-06 03:02:51 +00001657void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1658 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001659 bool LastInSection,
1660 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001661 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001662 MCSymbol *fdeStart = context.createTempSymbol();
1663 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001664 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001665
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001666 CFAOffset = InitialCFAOffset;
1667
Rafael Espindola0a017a62010-12-10 07:39:47 +00001668 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001669 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1670 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001671
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001672 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001673
Rafael Espindola0a017a62010-12-10 07:39:47 +00001674 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001675 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001676 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001677 const MCExpr *offset =
1678 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1679 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001680 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001681 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001682 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001683 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001684 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001685 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001686 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001687
Rafael Espindola0a017a62010-12-10 07:39:47 +00001688 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001689 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001690 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001691 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1692 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001693
1694 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001695 const MCExpr *Range =
1696 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1697 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001698
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001699 if (IsEH) {
1700 // Augmentation Data Length
1701 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001702
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001703 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001704 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001705
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001706 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001707
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001708 // Augmentation Data
1709 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001710 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001711 }
Rafael Espindola68067662011-04-29 03:06:29 +00001712
Rafael Espindola0a017a62010-12-10 07:39:47 +00001713 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001714 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001715
Rafael Espindola0a017a62010-12-10 07:39:47 +00001716 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001717 // The size of a .eh_frame section has to be a multiple of the alignment
1718 // since a null CIE is interpreted as the end. Old systems overaligned
1719 // .eh_frame, so we do too and account for it in the last FDE.
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001720 unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
Rafael Espindola46be4352015-11-06 03:02:51 +00001721 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001722
Rafael Espindola46be4352015-11-06 03:02:51 +00001723 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001724}
1725
Benjamin Kramer570dd782010-12-30 22:34:44 +00001726namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001727
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001728struct CIEKey {
1729 static const CIEKey getEmptyKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001730 return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001731 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001732
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001733 static const CIEKey getTombstoneKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001734 return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001735 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001736
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001737 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001738 unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001739 unsigned RAReg)
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001740 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001741 LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001742 IsSimple(IsSimple), RAReg(RAReg) {}
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001743
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001744 explicit CIEKey(const MCDwarfFrameInfo &Frame)
1745 : Personality(Frame.Personality),
1746 PersonalityEncoding(Frame.PersonalityEncoding),
1747 LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
1748 IsSimple(Frame.IsSimple), RAReg(Frame.RAReg) {}
1749
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001750 const MCSymbol *Personality;
1751 unsigned PersonalityEncoding;
1752 unsigned LsdaEncoding;
1753 bool IsSignalFrame;
1754 bool IsSimple;
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001755 unsigned RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001756};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001757
1758} // end anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001759
1760namespace llvm {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001761
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001762template <> struct DenseMapInfo<CIEKey> {
1763 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1764 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001765
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001766 static unsigned getHashValue(const CIEKey &Key) {
1767 return static_cast<unsigned>(
1768 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001769 Key.IsSignalFrame, Key.IsSimple, Key.RAReg));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001770 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001771
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001772 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1773 return LHS.Personality == RHS.Personality &&
1774 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1775 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1776 LHS.IsSignalFrame == RHS.IsSignalFrame &&
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001777 LHS.IsSimple == RHS.IsSimple &&
1778 LHS.RAReg == RHS.RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001779 }
1780};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001781
1782} // end namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001783
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001784void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001785 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001786 Streamer.generateCompactUnwindEncodings(MAB);
1787
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001788 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001789 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001790 const MCAsmInfo *AsmInfo = Context.getAsmInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001791 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001792 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001793
Bill Wendling9803abb2011-09-06 18:37:11 +00001794 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001795 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001796 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001797 bool SectionEmitted = false;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001798 for (const MCDwarfFrameInfo &Frame : FrameArray) {
Bob Wilson0e180f22013-05-07 20:56:33 +00001799 if (Frame.CompactUnwindEncoding == 0) continue;
1800 if (!SectionEmitted) {
1801 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001802 Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001803 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001804 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001805 NeedsEHFrameSection |=
1806 Frame.CompactUnwindEncoding ==
1807 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001808 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001809 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001810 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001811
Tim Northoverd1c6f512014-03-29 09:03:13 +00001812 if (!NeedsEHFrameSection) return;
1813
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001814 MCSection &Section =
1815 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1816 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001817
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001818 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001819 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001820 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001821
Eric Christopherd29430d2014-06-19 20:00:09 +00001822 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001823
Craig Topperbb694de2014-04-13 04:57:38 +00001824 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001825 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001826 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1827 const MCDwarfFrameInfo &Frame = *I;
1828 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001829 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001830 MOFI->getCompactUnwindDwarfEHFrameOnly())
1831 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1832 // of by the compact unwind encoding.
1833 continue;
1834
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001835 CIEKey Key(Frame);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001836 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1837 if (!CIEStart)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001838 CIEStart = &Emitter.EmitCIE(Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001839
Rafael Espindola97588e12015-11-06 13:14:59 +00001840 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001841 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001842}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001843
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001844void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001845 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001846 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001847 SmallString<256> Tmp;
1848 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001849 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001850 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001851}
1852
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001853void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1854 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001855 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001856 // Scale the address delta by the minimum instruction length.
1857 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1858
Peter Collingbournee3f65292018-05-18 19:46:24 +00001859 support::endianness E =
1860 Context.getAsmInfo()->isLittleEndian() ? support::little : support::big;
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001861 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001862 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001863 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1864 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001865 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001866 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1867 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001868 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001869 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Peter Collingbournee3f65292018-05-18 19:46:24 +00001870 support::endian::write<uint16_t>(OS, AddrDelta, E);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001871 } else {
1872 assert(isUInt<32>(AddrDelta));
1873 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Peter Collingbournee3f65292018-05-18 19:46:24 +00001874 support::endian::write<uint32_t>(OS, AddrDelta, E);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001875 }
1876}