blob: a36ff4cb907214898b4f0bb335fc2621ed356fcf [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"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000014#include "llvm/ADT/None.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"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000031#include "llvm/Support/Casting.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000032#include "llvm/Support/Endian.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000033#include "llvm/Support/EndianStream.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000034#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000035#include "llvm/Support/LEB128.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000036#include "llvm/Support/MathExtras.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000037#include "llvm/Support/Path.h"
38#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000039#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000040#include <cassert>
41#include <cstdint>
42#include <string>
43#include <utility>
44#include <vector>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000045
Kevin Enderbye5930f12010-07-28 20:55:35 +000046using namespace llvm;
47
Ulrich Weigand32d725b2013-06-12 14:46:54 +000048static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000049 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000050 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000051 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000052 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000053 // TODO: report this error, but really only once.
54 ;
55 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000056 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000057}
58
59//
60// This is called when an instruction is assembled into the specified section
61// and if there is information from the last .loc directive that has yet to have
62// a line entry made for it is made.
63//
David Majnemer8cc30782016-01-21 01:59:03 +000064void MCDwarfLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000065 if (!MCOS->getContext().getDwarfLocSeen())
66 return;
67
68 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000069 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
David Majnemer8cc30782016-01-21 01:59:03 +000070 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000071 MCOS->EmitLabel(LineSym);
72
73 // Get the current .loc info saved in the context.
74 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
75
76 // Create a (local) line entry with the symbol and the current .loc info.
David Majnemer8cc30782016-01-21 01:59:03 +000077 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +000078
79 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +000080 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000081
Kevin Enderbye46564a2010-09-30 16:52:03 +000082 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000083 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000084 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000085 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000086 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000087}
88
89//
90// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000091//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000092static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
93 const MCSymbol &Start,
94 const MCSymbol &End,
95 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000096 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
97 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +000098 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000099 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000100 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000101 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000102 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000103 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000104 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000105 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000106 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000107 return Res3;
108}
109
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110//
111// This emits the Dwarf line table for the specified section from the entries
112// in the LineSection.
113//
David Blaikief4ad6982014-03-12 22:35:23 +0000114static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000115EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Majnemer8cc30782016-01-21 01:59:03 +0000116 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000117 unsigned FileNum = 1;
118 unsigned LastLine = 1;
119 unsigned Column = 0;
120 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
121 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000122 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000123 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000124
David Majnemer8cc30782016-01-21 01:59:03 +0000125 // Loop through each MCDwarfLineEntry and encode the dwarf line number table.
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000126 for (const MCDwarfLineEntry &LineEntry : LineEntries) {
127 int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000128
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000129 if (FileNum != LineEntry.getFileNum()) {
130 FileNum = LineEntry.getFileNum();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000131 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000132 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000133 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000134 if (Column != LineEntry.getColumn()) {
135 Column = LineEntry.getColumn();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000136 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000137 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000138 }
Dehao Chen6e0c8442016-10-07 15:21:31 +0000139 if (Discriminator != LineEntry.getDiscriminator() &&
140 MCOS->getContext().getDwarfVersion() >= 4) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000141 Discriminator = LineEntry.getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000142 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000143 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
144 MCOS->EmitULEB128IntValue(Size + 1);
145 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
146 MCOS->EmitULEB128IntValue(Discriminator);
147 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000148 if (Isa != LineEntry.getIsa()) {
149 Isa = LineEntry.getIsa();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000150 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000151 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000152 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000153 if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
154 Flags = LineEntry.getFlags();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000155 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
156 }
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000157 if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000158 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000159 if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000160 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000161 if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000162 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
163
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000164 MCSymbol *Label = LineEntry.getLabel();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000165
166 // At this point we want to emit/create the sequence to encode the delta in
167 // line numbers and the increment of the address from the previous Label
168 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000169 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000170 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000171 asmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000172
Dehao Chen1b54fce2016-04-28 22:09:37 +0000173 Discriminator = 0;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000174 LastLine = LineEntry.getLine();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000175 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000176 }
177
178 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000179 // Use the section end label to compute the address delta and use INT64_MAX
180 // as the line delta which is the signal that this is actually a
181 // DW_LNE_end_sequence.
182 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000183
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000184 // Switch back the dwarf line section, in case endSection had to switch the
185 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000186 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000187 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000188
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000189 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000190 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000191 AsmInfo->getCodePointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000192}
193
194//
195// This emits the Dwarf file and the line tables.
196//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000197void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
198 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000199 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000200
David Blaikie8bf66c42014-04-01 07:35:52 +0000201 auto &LineTables = context.getMCDwarfLineTables();
202
203 // Bail out early so we don't switch to the debug_line section needlessly and
204 // in doing so create an unnecessary (if empty) section.
205 if (LineTables.empty())
206 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000207
208 // Switch to the section where the table will be emitted into.
209 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
210
Manman Ren4e042a62013-02-05 21:52:47 +0000211 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000212 for (const auto &CUIDTablePair : LineTables)
Frederic Rissa5ab8442015-08-07 15:14:08 +0000213 CUIDTablePair.second.EmitCU(MCOS, Params);
Manman Ren4e042a62013-02-05 21:52:47 +0000214}
215
Frederic Rissa5ab8442015-08-07 15:14:08 +0000216void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS,
217 MCDwarfLineTableParams Params) const {
218 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000219}
220
Frederic Rissa5ab8442015-08-07 15:14:08 +0000221std::pair<MCSymbol *, MCSymbol *>
222MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
223 MCDwarfLineTableParams Params) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000224 static const char StandardOpcodeLengths[] = {
225 0, // length of DW_LNS_copy
226 1, // length of DW_LNS_advance_pc
227 1, // length of DW_LNS_advance_line
228 1, // length of DW_LNS_set_file
229 1, // length of DW_LNS_set_column
230 0, // length of DW_LNS_negate_stmt
231 0, // length of DW_LNS_set_basic_block
232 0, // length of DW_LNS_const_add_pc
233 1, // length of DW_LNS_fixed_advance_pc
234 0, // length of DW_LNS_set_prologue_end
235 0, // length of DW_LNS_set_epilogue_begin
236 1 // DW_LNS_set_isa
237 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000238 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000239 (Params.DWARF2LineOpcodeBase - 1U));
Craig Topper0013be12015-09-21 05:32:41 +0000240 return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
241 Params.DWARF2LineOpcodeBase - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000242}
243
Rafael Espindolac23174b2014-08-15 15:12:13 +0000244static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
245 MCContext &Context = OS.getContext();
246 assert(!isa<MCSymbolRefExpr>(Expr));
247 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
248 return Expr;
249
Jim Grosbach6f482002015-05-18 18:43:14 +0000250 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000251 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000252 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000253}
254
255static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
256 const MCExpr *ABS = forceExpAbs(OS, Value);
257 OS.EmitValue(ABS, Size);
258}
259
David Blaikie8287aff2014-03-18 02:13:23 +0000260std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000261MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
David Blaikie8287aff2014-03-18 02:13:23 +0000262 ArrayRef<char> StandardOpcodeLengths) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000263 MCContext &context = MCOS->getContext();
264
265 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000266 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000267 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000268 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000269 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000270 MCOS->EmitLabel(LineStartSym);
271
272 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000273 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000274
275 // The first 4 bytes is the total length of the information for this
276 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000277 emitAbsValue(*MCOS,
278 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000279
Nico Weberc4d695e2016-12-16 14:02:28 +0000280 // Next 2 bytes is the Version, which is Dwarf 2.
281 MCOS->EmitIntValue(2, 2);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000282
283 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000284 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000285
286 // Length of the prologue, is the next 4 bytes. Which is the start of the
287 // section to the end of the prologue. Not including the 4 bytes for the
288 // total length, the 2 bytes for the version, and these 4 bytes for the
289 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000290 emitAbsValue(
291 *MCOS,
292 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000293
294 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000295 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000296 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000297 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
298 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000299 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000300
301 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000302 for (char Length : StandardOpcodeLengths)
303 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000304
305 // Put out the directory and file tables.
306
307 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000309 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
310 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311 }
312 MCOS->EmitIntValue(0, 1); // Terminate the directory list
313
314 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000315 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000316 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000317 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000318 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000319 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000320 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000321 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
322 MCOS->EmitIntValue(0, 1); // filesize (always 0)
323 }
324 MCOS->EmitIntValue(0, 1); // Terminate the file list
325
326 // This is the end of the prologue, so set the value of the symbol at the
327 // end of the prologue (that was used in a previous expression).
328 MCOS->EmitLabel(ProEndSym);
329
David Blaikie5cff0e62014-03-13 21:47:12 +0000330 return std::make_pair(LineStartSym, LineEndSym);
331}
332
Frederic Rissa5ab8442015-08-07 15:14:08 +0000333void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
334 MCDwarfLineTableParams Params) const {
335 MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000336
Kevin Enderbye46564a2010-09-30 16:52:03 +0000337 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000338 for (const auto &LineSec : MCLineSections.getMCLineEntries())
339 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000340
Kevin Enderbye46564a2010-09-30 16:52:03 +0000341 // This is the end of the section, so set the value of the symbol at the end
342 // of this section (that was used in a previous expression).
343 MCOS->EmitLabel(LineEndSym);
344}
345
David Blaikiec2df16b2014-03-17 18:13:58 +0000346unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000347 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000348 return Header.getFile(Directory, FileName, FileNumber);
349}
350
David Blaikiec2df16b2014-03-17 18:13:58 +0000351unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
352 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000353 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000354 if (Directory == CompilationDir)
355 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000356 if (FileName.empty()) {
357 FileName = "<stdin>";
358 Directory = "";
359 }
360 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000361 if (FileNumber == 0) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000362 // File numbers start with 1 and/or after any file numbers
363 // allocated by inline-assembler .file directives.
364 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
Pete Cooperb7800812015-05-20 19:12:14 +0000365 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000366 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000367 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
368 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000369 if (!IterBool.second)
370 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000371 }
David Blaikie498589c2014-03-13 19:15:04 +0000372 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
373 MCDwarfFiles.resize(FileNumber + 1);
374
375 // Get the new MCDwarfFile slot for this FileNumber.
376 MCDwarfFile &File = MCDwarfFiles[FileNumber];
377
378 // It is an error to use see the same number more than once.
379 if (!File.Name.empty())
380 return 0;
381
382 if (Directory.empty()) {
383 // Separate the directory part from the basename of the FileName.
384 StringRef tFileName = sys::path::filename(FileName);
385 if (!tFileName.empty()) {
386 Directory = sys::path::parent_path(FileName);
387 if (!Directory.empty())
388 FileName = tFileName;
389 }
390 }
391
392 // Find or make an entry in the MCDwarfDirs vector for this Directory.
393 // Capture directory name.
394 unsigned DirIndex;
395 if (Directory.empty()) {
396 // For FileNames with no directories a DirIndex of 0 is used.
397 DirIndex = 0;
398 } else {
399 DirIndex = 0;
400 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
401 if (Directory == MCDwarfDirs[DirIndex])
402 break;
403 }
404 if (DirIndex >= MCDwarfDirs.size())
405 MCDwarfDirs.push_back(Directory);
406 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
407 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
408 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
409 // are stored at MCDwarfFiles[FileNumber].Name .
410 DirIndex++;
411 }
412
413 File.Name = FileName;
414 File.DirIndex = DirIndex;
415
416 // return the allocated FileNumber.
417 return FileNumber;
418}
419
Kevin Enderbye46564a2010-09-30 16:52:03 +0000420/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000421void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
422 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000423 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000424 SmallString<256> Tmp;
425 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000426 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000427 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000428}
429
Frederic Rissa5ab8442015-08-07 15:14:08 +0000430/// Given a special op, return the address skip amount (in units of
431/// DWARF2_LINE_MIN_INSN_LENGTH).
432static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
433 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
434}
435
Kevin Enderbye46564a2010-09-30 16:52:03 +0000436/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000437void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
438 int64_t LineDelta, uint64_t AddrDelta,
439 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000440 uint64_t Temp, Opcode;
441 bool NeedCopy = false;
442
Frederic Rissa5ab8442015-08-07 15:14:08 +0000443 // The maximum address skip amount that can be encoded with a special op.
444 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
445
Kevin Enderbye46564a2010-09-30 16:52:03 +0000446 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000447 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000448
449 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000450 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000451 // end_sequence to emit the matrix entry.
452 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000453 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000454 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000455 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000456 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000457 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000458 }
459 OS << char(dwarf::DW_LNS_extended_op);
460 OS << char(1);
461 OS << char(dwarf::DW_LNE_end_sequence);
462 return;
463 }
464
465 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000466 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000467
468 // If the line increment is out of range of a special opcode, we must encode
469 // it with DW_LNS_advance_line.
Frederic Risse5b78d82016-01-31 22:06:35 +0000470 if (Temp >= Params.DWARF2LineRange ||
471 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000472 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000473 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000474
475 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000476 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000477 NeedCopy = true;
478 }
479
480 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
481 if (LineDelta == 0 && AddrDelta == 0) {
482 OS << char(dwarf::DW_LNS_copy);
483 return;
484 }
485
486 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000487 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000488
489 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000490 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000491 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000492 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000493 if (Opcode <= 255) {
494 OS << char(Opcode);
495 return;
496 }
497
498 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000499 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000500 if (Opcode <= 255) {
501 OS << char(dwarf::DW_LNS_const_add_pc);
502 OS << char(Opcode);
503 return;
504 }
505 }
506
507 // Otherwise use DW_LNS_advance_pc.
508 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000509 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000510
511 if (NeedCopy)
512 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000513 else {
514 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000515 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000516 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000517}
518
Kevin Enderbye7739d42011-12-09 18:09:40 +0000519// Utility function to write a tuple for .debug_abbrev.
520static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
521 MCOS->EmitULEB128IntValue(Name);
522 MCOS->EmitULEB128IntValue(Form);
523}
524
525// When generating dwarf for assembly source files this emits
526// the data for .debug_abbrev section which contains three DIEs.
527static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
528 MCContext &context = MCOS->getContext();
529 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
530
531 // DW_TAG_compile_unit DIE abbrev (1).
532 MCOS->EmitULEB128IntValue(1);
533 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
534 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000535 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
536 ? dwarf::DW_FORM_sec_offset
537 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000538 if (context.getGenDwarfSectionSyms().size() > 1 &&
539 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000540 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
541 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000542 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000543 } else {
544 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
545 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
546 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000547 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000548 if (!context.getCompilationDir().empty())
549 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000550 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
551 if (!DwarfDebugFlags.empty())
552 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
553 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
554 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
555 EmitAbbrev(MCOS, 0, 0);
556
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000557 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000558 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000559 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000560 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
561 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
562 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
563 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
564 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000565 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
566 EmitAbbrev(MCOS, 0, 0);
567
568 // DW_TAG_unspecified_parameters DIE abbrev (3).
569 MCOS->EmitULEB128IntValue(3);
570 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
571 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
572 EmitAbbrev(MCOS, 0, 0);
573
574 // Terminate the abbreviations for this compilation unit.
575 MCOS->EmitIntValue(0, 1);
576}
577
578// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000579// .debug_aranges section. This section contains a header and a table of pairs
580// of PointerSize'ed values for the address and size of section(s) with line
581// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000582static void EmitGenDwarfAranges(MCStreamer *MCOS,
583 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000584 MCContext &context = MCOS->getContext();
585
Oliver Stannard8b273082014-06-19 15:52:37 +0000586 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000587
588 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
589
590 // This will be the length of the .debug_aranges section, first account for
591 // the size of each item in the header (see below where we emit these items).
592 int Length = 4 + 2 + 4 + 1 + 1;
593
594 // Figure the padding after the header before the table of address and size
595 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000596 const MCAsmInfo *asmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000597 int AddrSize = asmInfo->getCodePointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000598 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
599 if (Pad == 2 * AddrSize)
600 Pad = 0;
601 Length += Pad;
602
603 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000604 // of each section we have in the table.
605 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000606 // And the pair of terminating zeros.
607 Length += 2 * AddrSize;
608
Kevin Enderbye7739d42011-12-09 18:09:40 +0000609 // Emit the header for this section.
610 // The 4 byte length not including the 4 byte value for the length.
611 MCOS->EmitIntValue(Length - 4, 4);
612 // The 2 byte version, which is 2.
613 MCOS->EmitIntValue(2, 2);
614 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000615 // of the .debug_info.
616 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000617 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
618 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000619 else
620 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000621 // The 1 byte size of an address.
622 MCOS->EmitIntValue(AddrSize, 1);
623 // The 1 byte size of a segment descriptor, we use a value of zero.
624 MCOS->EmitIntValue(0, 1);
625 // Align the header with the padding if needed, before we put out the table.
626 for(int i = 0; i < Pad; i++)
627 MCOS->EmitIntValue(0, 1);
628
Oliver Stannard8b273082014-06-19 15:52:37 +0000629 // Now emit the table of pairs of PointerSize'ed values for the section
630 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000631 for (MCSection *Sec : Sections) {
632 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000633 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000634 assert(StartSymbol && "StartSymbol must not be NULL");
635 assert(EndSymbol && "EndSymbol must not be NULL");
636
Jim Grosbach13760bd2015-05-30 01:25:56 +0000637 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000638 StartSymbol, MCSymbolRefExpr::VK_None, context);
639 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
640 *StartSymbol, *EndSymbol, 0);
641 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000642 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000643 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000644
645 // And finally the pair of terminating zeros.
646 MCOS->EmitIntValue(0, AddrSize);
647 MCOS->EmitIntValue(0, AddrSize);
648}
649
650// When generating dwarf for assembly source files this emits the data for
651// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000652// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000653static void EmitGenDwarfInfo(MCStreamer *MCOS,
654 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000655 const MCSymbol *LineSectionSymbol,
656 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000657 MCContext &context = MCOS->getContext();
658
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000659 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000660
661 // Create a symbol at the start and end of this section used in here for the
662 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000663 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000664 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000665 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000666
667 // First part: the header.
668
669 // The 4 byte total length of the information for this compilation unit, not
670 // including these 4 bytes.
671 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000672 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000673
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000674 // The 2 byte DWARF version.
675 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000676
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000677 // The DWARF v5 header has unit type, address size, abbrev offset.
678 // Earlier versions have abbrev offset, address size.
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000679 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000680 int AddrSize = AsmInfo.getCodePointerSize();
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000681 if (context.getDwarfVersion() >= 5) {
682 MCOS->EmitIntValue(dwarf::DW_UT_compile, 1);
683 MCOS->EmitIntValue(AddrSize, 1);
684 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000685 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
686 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000687 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000688 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000689 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000690 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
691 AsmInfo.needsDwarfSectionOffsetDirective());
Paul Robinsondccb4fe2017-02-28 23:40:46 +0000692 if (context.getDwarfVersion() <= 4)
693 MCOS->EmitIntValue(AddrSize, 1);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000694
695 // Second part: the compile_unit DIE.
696
697 // The DW_TAG_compile_unit DIE abbrev (1).
698 MCOS->EmitULEB128IntValue(1);
699
700 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
701 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000702 if (LineSectionSymbol)
703 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
704 AsmInfo.needsDwarfSectionOffsetDirective());
705 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000706 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000707
Oliver Stannard8b273082014-06-19 15:52:37 +0000708 if (RangesSectionSymbol) {
709 // There are multiple sections containing code, so we must use the
710 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000711
Oliver Stannard8b273082014-06-19 15:52:37 +0000712 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
713 // to the address range list for this compilation unit.
714 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
715 } else {
716 // If we only have one non-empty code section, we can use the simpler
717 // AT_low_pc and AT_high_pc attributes.
718
719 // Find the first (and only) non-empty text section
720 auto &Sections = context.getGenDwarfSectionSyms();
721 const auto TextSection = Sections.begin();
722 assert(TextSection != Sections.end() && "No text section found");
723
Rafael Espindolae0746792015-05-21 16:52:32 +0000724 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
725 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000726 assert(StartSymbol && "StartSymbol must not be NULL");
727 assert(EndSymbol && "EndSymbol must not be NULL");
728
729 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000730 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000731 StartSymbol, MCSymbolRefExpr::VK_None, context);
732 MCOS->EmitValue(Start, AddrSize);
733
734 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000735 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000736 EndSymbol, MCSymbolRefExpr::VK_None, context);
737 MCOS->EmitValue(End, AddrSize);
738 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000739
740 // AT_name, the name of the source file. Reconstruct from the first directory
741 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000742 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000743 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000744 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000745 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000746 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000747 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000748 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000749 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000750 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
751
752 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000753 if (!context.getCompilationDir().empty()) {
754 MCOS->EmitBytes(context.getCompilationDir());
755 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
756 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000757
758 // AT_APPLE_flags, the command line arguments of the assembler tool.
759 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
760 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000761 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000762 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
763 }
764
765 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000766 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000767 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000768 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000769 else
770 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000771 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
772
773 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
774 // draft has no standard code for assembler.
775 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
776
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000777 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000778
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000779 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000780 const std::vector<MCGenDwarfLabelEntry> &Entries =
781 MCOS->getContext().getMCGenDwarfLabelEntries();
782 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000783 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000784 MCOS->EmitULEB128IntValue(2);
785
786 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000787 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000788 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
789
790 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000791 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000792
793 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000794 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795
796 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000797 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000798 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000799 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000800
Kevin Enderbye7739d42011-12-09 18:09:40 +0000801 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
802 MCOS->EmitIntValue(0, 1);
803
804 // The DW_TAG_unspecified_parameters DIE abbrev (3).
805 MCOS->EmitULEB128IntValue(3);
806
807 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
808 MCOS->EmitIntValue(0, 1);
809 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000810
811 // Add the NULL DIE terminating the Compile Unit DIE's.
812 MCOS->EmitIntValue(0, 1);
813
814 // Now set the value of the symbol at the end of the info section.
815 MCOS->EmitLabel(InfoEnd);
816}
817
Oliver Stannard8b273082014-06-19 15:52:37 +0000818// When generating dwarf for assembly source files this emits the data for
819// .debug_ranges section. We only emit one range list, which spans all of the
820// executable sections of this file.
821static void EmitGenDwarfRanges(MCStreamer *MCOS) {
822 MCContext &context = MCOS->getContext();
823 auto &Sections = context.getGenDwarfSectionSyms();
824
825 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000826 int AddrSize = AsmInfo->getCodePointerSize();
Oliver Stannard8b273082014-06-19 15:52:37 +0000827
828 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
829
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000830 for (MCSection *Sec : Sections) {
831 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000832 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000833 assert(StartSymbol && "StartSymbol must not be NULL");
834 assert(EndSymbol && "EndSymbol must not be NULL");
835
836 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000837 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000838 StartSymbol, MCSymbolRefExpr::VK_None, context);
Petr Hosekfaef3202016-06-01 01:59:58 +0000839 MCOS->emitFill(AddrSize, 0xFF);
Oliver Stannard8b273082014-06-19 15:52:37 +0000840 MCOS->EmitValue(SectionStartAddr, AddrSize);
841
842 // Emit a range list entry spanning this section
843 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
844 *StartSymbol, *EndSymbol, 0);
845 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000846 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000847 }
848
849 // Emit end of list entry
850 MCOS->EmitIntValue(0, AddrSize);
851 MCOS->EmitIntValue(0, AddrSize);
852}
853
Kevin Enderbye7739d42011-12-09 18:09:40 +0000854//
855// When generating dwarf for assembly source files this emits the Dwarf
856// sections.
857//
David Blaikie8bf66c42014-04-01 07:35:52 +0000858void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000859 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000860
861 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000862 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000863 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000864 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000865 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000866 if (CreateDwarfSectionSymbols)
867 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000868 MCSymbol *AbbrevSectionSymbol = nullptr;
869 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000870 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000871
872 // Create end symbols for each section, and remove empty sections
873 MCOS->getContext().finalizeDwarfSections(*MCOS);
874
875 // If there are no sections to generate debug info for, we don't need
876 // to do anything
877 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
878 return;
879
Oliver Stannard14f97d02014-09-22 10:45:16 +0000880 // We only use the .debug_ranges section if we have multiple code sections,
881 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000882 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000883 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
884 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000885 CreateDwarfSectionSymbols |= UseRangesSection;
886
Kevin Enderbye7739d42011-12-09 18:09:40 +0000887 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000888 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000889 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000890 MCOS->EmitLabel(InfoSectionSymbol);
891 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000892 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000893 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000894 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000895 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000896 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000897 if (UseRangesSection) {
898 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
899 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000900 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000901 MCOS->EmitLabel(RangesSectionSymbol);
902 }
903 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000904
Eugene Zelenkod96089b2017-02-14 00:33:36 +0000905 assert((RangesSectionSymbol != nullptr) || !UseRangesSection);
Oliver Stannard8b273082014-06-19 15:52:37 +0000906
907 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000908
909 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000910 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000911
Oliver Stannard8b273082014-06-19 15:52:37 +0000912 if (UseRangesSection)
913 EmitGenDwarfRanges(MCOS);
914
Kevin Enderbye7739d42011-12-09 18:09:40 +0000915 // Output the data for .debug_abbrev section.
916 EmitGenDwarfAbbrev(MCOS);
917
918 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000919 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
920 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000921}
922
923//
924// When generating dwarf for assembly source files this is called when symbol
925// for a label is created. If this symbol is not a temporary and is in the
926// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000927// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000928//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000929void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000930 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000931 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000932 if (Symbol->isTemporary())
933 return;
934 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000935 // We won't create dwarf labels for symbols in sections that we are not
936 // generating debug info for.
Eric Christopher445c9522016-10-14 05:47:37 +0000937 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSectionOnly()))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000938 return;
939
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000940 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000941 // underbar if any.
942 StringRef Name = Symbol->getName();
943 if (Name.startswith("_"))
944 Name = Name.substr(1, Name.size()-1);
945
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000946 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000947 unsigned FileNumber = context.getGenDwarfFileNumber();
948
949 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000950 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000951 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000952 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
953
954 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
955 // values so that they don't have things like an ARM thumb bit from the
956 // original symbol. So when used they won't get a low bit set after
957 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000958 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000959 MCOS->EmitLabel(Label);
960
961 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000962 MCOS->getContext().addMCGenDwarfLabelEntry(
963 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000964}
965
Rafael Espindola0a017a62010-12-10 07:39:47 +0000966static int getDataAlignmentFactor(MCStreamer &streamer) {
967 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000968 const MCAsmInfo *asmInfo = context.getAsmInfo();
969 int size = asmInfo->getCalleeSaveStackSlotSize();
970 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000971 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000972 else
973 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000974}
975
Rafael Espindola5395f442011-04-22 00:08:43 +0000976static unsigned getSizeForEncoding(MCStreamer &streamer,
977 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000978 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000979 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000980 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000981 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000982 case dwarf::DW_EH_PE_absptr:
983 case dwarf::DW_EH_PE_signed:
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +0000984 return context.getAsmInfo()->getCodePointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000985 case dwarf::DW_EH_PE_udata2:
986 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000987 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000988 case dwarf::DW_EH_PE_udata4:
989 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000990 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000991 case dwarf::DW_EH_PE_udata8:
992 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000993 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000994 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000995}
996
Rafael Espindolafe963b12014-08-15 03:07:13 +0000997static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
998 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000999 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001000 const MCAsmInfo *asmInfo = context.getAsmInfo();
1001 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
1002 symbolEncoding,
1003 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +00001004 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001005 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +00001006 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +00001007 else
1008 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001009}
1010
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001011static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1012 unsigned symbolEncoding) {
1013 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001014 const MCAsmInfo *asmInfo = context.getAsmInfo();
1015 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1016 symbolEncoding,
1017 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001018 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001019 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001020}
1021
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001022namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001023
Rafael Espindola68c21652015-11-05 23:54:18 +00001024class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001025 int CFAOffset = 0;
1026 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001027 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001028 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001029
Rafael Espindola68c21652015-11-05 23:54:18 +00001030public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001031 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1032 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001033
Rafael Espindola68c21652015-11-05 23:54:18 +00001034 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001035 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001036
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001037 const MCSymbol &EmitCIE(const MCDwarfFrameInfo &F);
Rafael Espindola46be4352015-11-06 03:02:51 +00001038 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001039 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001040 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001041 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001042 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001043};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001044
1045} // end anonymous namespace
1046
Rafael Espindolafe963b12014-08-15 03:07:13 +00001047static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001048 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001049}
1050
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001051void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001052 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001053 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001054
1055 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001056 case MCCFIInstruction::OpRegister: {
1057 unsigned Reg1 = Instr.getRegister();
1058 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001059 if (!IsEH) {
1060 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1061 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1062 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001063 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1064 Streamer.EmitULEB128IntValue(Reg1);
1065 Streamer.EmitULEB128IntValue(Reg2);
1066 return;
1067 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001068 case MCCFIInstruction::OpWindowSave:
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001069 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1070 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001071
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001072 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001073 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001074 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1075 Streamer.EmitULEB128IntValue(Reg);
1076 return;
1077 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001078 case MCCFIInstruction::OpAdjustCfaOffset:
1079 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001080 const bool IsRelative =
1081 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1082
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001083 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1084
1085 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001086 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001087 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001088 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001089
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001090 Streamer.EmitULEB128IntValue(CFAOffset);
1091
1092 return;
1093 }
1094 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001095 unsigned Reg = Instr.getRegister();
1096 if (!IsEH)
1097 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001098 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001099 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001100 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001101 Streamer.EmitULEB128IntValue(CFAOffset);
1102
1103 return;
1104 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001105 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001106 unsigned Reg = Instr.getRegister();
1107 if (!IsEH)
1108 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001109 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001110 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001111
1112 return;
1113 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001114 case MCCFIInstruction::OpOffset:
1115 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001116 const bool IsRelative =
1117 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001118
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001119 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001120 if (!IsEH)
1121 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1122
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001123 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001124 if (IsRelative)
1125 Offset -= CFAOffset;
1126 Offset = Offset / dataAlignmentFactor;
1127
1128 if (Offset < 0) {
1129 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1130 Streamer.EmitULEB128IntValue(Reg);
1131 Streamer.EmitSLEB128IntValue(Offset);
1132 } else if (Reg < 64) {
1133 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001134 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001135 } else {
1136 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001137 Streamer.EmitULEB128IntValue(Reg);
1138 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001139 }
1140 return;
1141 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001142 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001143 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1144 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001145 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001146 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1147 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001148 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001149 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001150 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001151 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001152 return;
1153 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001154 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001155 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001156 if (!IsEH)
1157 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001158 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1159 return;
1160 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001161 case MCCFIInstruction::OpGnuArgsSize:
Michael Kuperstein259f1502015-10-07 07:01:31 +00001162 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1163 Streamer.EmitULEB128IntValue(Instr.getOffset());
1164 return;
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001165
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001166 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001167 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001168 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001169 }
1170 llvm_unreachable("Unhandled case in switch");
1171}
1172
Rafael Espindolafe963b12014-08-15 03:07:13 +00001173/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001174void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001175 MCSymbol *BaseLabel) {
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001176 for (const MCCFIInstruction &Instr : Instrs) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001177 MCSymbol *Label = Instr.getLabel();
1178 // Throw out move if the label is invalid.
1179 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1180
1181 // Advance row if new location.
1182 if (BaseLabel && Label) {
1183 MCSymbol *ThisSym = Label;
1184 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001185 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001186 BaseLabel = ThisSym;
1187 }
1188 }
1189
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001190 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001191 }
1192}
1193
Rafael Espindolafe963b12014-08-15 03:07:13 +00001194/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001195void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001196 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001197 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001198
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001199 // range-start range-length compact-unwind-enc personality-func lsda
1200 // _foo LfooEnd-_foo 0x00000023 0 0
1201 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1202 //
1203 // .section __LD,__compact_unwind,regular,debug
1204 //
1205 // # compact unwind for _foo
1206 // .quad _foo
1207 // .set L1,LfooEnd-_foo
1208 // .long L1
1209 // .long 0x01010001
1210 // .quad 0
1211 // .quad 0
1212 //
1213 // # compact unwind for _bar
1214 // .quad _bar
1215 // .set L2,LbarEnd-_bar
1216 // .long L2
1217 // .long 0x01020011
1218 // .quad __gxx_personality
1219 // .quad except_tab1
1220
Bill Wendling49bc6802011-07-19 00:06:12 +00001221 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001222 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001223 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001224
Bill Wendlingdafd5982011-07-14 23:34:45 +00001225 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001226 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001227 Encoding |= 0x40000000;
1228
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001229 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001230 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001231 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001232 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001233
1234 // Range Length
1235 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1236 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001237 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001238
Bill Wendling75174662011-06-30 00:30:52 +00001239 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001240 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1241 Streamer.EmitIntValue(Encoding, Size);
1242
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001243 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001244 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001245 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001246 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001247 else
1248 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001249
1250 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001251 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001252 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001253 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001254 else
1255 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001256}
1257
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001258static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1259 if (IsEH)
1260 return 1;
1261 switch (DwarfVersion) {
1262 case 2:
1263 return 1;
1264 case 3:
1265 return 3;
1266 case 4:
Eric Christopher2ae71802015-12-28 23:02:42 +00001267 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001268 return 4;
1269 }
1270 llvm_unreachable("Unknown version");
1271}
1272
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001273const MCSymbol &FrameEmitterImpl::EmitCIE(const MCDwarfFrameInfo &Frame) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001274 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001275 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001276 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001277
Jim Grosbach6f482002015-05-18 18:43:14 +00001278 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001279 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001280
Jim Grosbach6f482002015-05-18 18:43:14 +00001281 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001282
1283 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001284 const MCExpr *Length =
1285 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1286 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001287
1288 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001289 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001290 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001291
1292 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001293 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001294 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001295
1296 // Augmentation String
1297 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001298 if (IsEH) {
1299 Augmentation += "z";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001300 if (Frame.Personality)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001301 Augmentation += "P";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001302 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001303 Augmentation += "L";
1304 Augmentation += "R";
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001305 if (Frame.IsSignalFrame)
Rafael Espindola3c47e372012-01-23 21:51:52 +00001306 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001307 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001308 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001309 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001310
Keith Walkerea9483f2015-05-12 15:25:08 +00001311 if (CIEVersion >= 4) {
1312 // Address Size
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001313 Streamer.EmitIntValue(context.getAsmInfo()->getCodePointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001314
1315 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001316 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001317 }
1318
Rafael Espindola0a017a62010-12-10 07:39:47 +00001319 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001320 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001321
1322 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001323 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001324
1325 // Return Address Register
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001326 unsigned RAReg = Frame.RAReg;
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001327 if (RAReg == static_cast<unsigned>(INT_MAX))
1328 RAReg = MRI->getDwarfRegNum(MRI->getRARegister(), IsEH);
1329
Oliver Stannardf7693f42014-06-19 15:39:33 +00001330 if (CIEVersion == 1) {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001331 assert(RAReg <= 255 &&
Oliver Stannardf7693f42014-06-19 15:39:33 +00001332 "DWARF 2 encodes return_address_register in one byte");
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001333 Streamer.EmitIntValue(RAReg, 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001334 } else {
Saleem Abdulrasoola219b3d2017-07-28 03:39:19 +00001335 Streamer.EmitULEB128IntValue(RAReg);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001336 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001337
1338 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001339 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001340 if (IsEH) {
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001341 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001342 // Personality Encoding
1343 augmentationLength += 1;
1344 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001345 augmentationLength +=
1346 getSizeForEncoding(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001347 }
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001348 if (Frame.Lsda)
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001349 augmentationLength += 1;
1350 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001351 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001352
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001353 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001354
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001355 // Augmentation Data (optional)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001356 if (Frame.Personality) {
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001357 // Personality Encoding
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001358 emitEncodingByte(Streamer, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001359 // Personality
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001360 EmitPersonality(Streamer, *Frame.Personality, Frame.PersonalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001361 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001362
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001363 if (Frame.Lsda)
1364 emitEncodingByte(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001365
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001366 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001367 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001368 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001369
1370 // Initial Instructions
1371
Bill Wendlingbc07a892013-06-18 07:20:20 +00001372 const MCAsmInfo *MAI = context.getAsmInfo();
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001373 if (!Frame.IsSimple) {
David Majnemere035cf92014-01-27 17:20:25 +00001374 const std::vector<MCCFIInstruction> &Instructions =
1375 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001376 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001377 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001378
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001379 InitialCFAOffset = CFAOffset;
1380
Rafael Espindola0a017a62010-12-10 07:39:47 +00001381 // Padding
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001382 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getCodePointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001383
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001384 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001385 return *sectionStart;
1386}
1387
Rafael Espindola46be4352015-11-06 03:02:51 +00001388void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1389 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001390 bool LastInSection,
1391 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001392 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001393 MCSymbol *fdeStart = context.createTempSymbol();
1394 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001395 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001396
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001397 CFAOffset = InitialCFAOffset;
1398
Rafael Espindola0a017a62010-12-10 07:39:47 +00001399 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001400 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1401 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001402
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001403 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001404
Rafael Espindola0a017a62010-12-10 07:39:47 +00001405 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001406 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001407 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001408 const MCExpr *offset =
1409 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1410 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001411 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001412 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001413 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001414 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001415 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001416 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001417 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001418
Rafael Espindola0a017a62010-12-10 07:39:47 +00001419 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001420 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001421 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001422 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1423 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001424
1425 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001426 const MCExpr *Range =
1427 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1428 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001429
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001430 if (IsEH) {
1431 // Augmentation Data Length
1432 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001433
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001434 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001435 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001436
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001437 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001438
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001439 // Augmentation Data
1440 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001441 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001442 }
Rafael Espindola68067662011-04-29 03:06:29 +00001443
Rafael Espindola0a017a62010-12-10 07:39:47 +00001444 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001445 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001446
Rafael Espindola0a017a62010-12-10 07:39:47 +00001447 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001448 // The size of a .eh_frame section has to be a multiple of the alignment
1449 // since a null CIE is interpreted as the end. Old systems overaligned
1450 // .eh_frame, so we do too and account for it in the last FDE.
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001451 unsigned Align = LastInSection ? asmInfo->getCodePointerSize() : PCSize;
Rafael Espindola46be4352015-11-06 03:02:51 +00001452 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001453
Rafael Espindola46be4352015-11-06 03:02:51 +00001454 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001455}
1456
Benjamin Kramer570dd782010-12-30 22:34:44 +00001457namespace {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001458
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001459struct CIEKey {
1460 static const CIEKey getEmptyKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001461 return CIEKey(nullptr, 0, -1, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001462 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001463
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001464 static const CIEKey getTombstoneKey() {
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001465 return CIEKey(nullptr, -1, 0, false, false, static_cast<unsigned>(INT_MAX));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001466 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001467
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001468 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001469 unsigned LSDAEncoding, bool IsSignalFrame, bool IsSimple,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001470 unsigned RAReg)
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001471 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001472 LsdaEncoding(LSDAEncoding), IsSignalFrame(IsSignalFrame),
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001473 IsSimple(IsSimple), RAReg(RAReg) {}
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001474
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001475 explicit CIEKey(const MCDwarfFrameInfo &Frame)
1476 : Personality(Frame.Personality),
1477 PersonalityEncoding(Frame.PersonalityEncoding),
1478 LsdaEncoding(Frame.LsdaEncoding), IsSignalFrame(Frame.IsSignalFrame),
1479 IsSimple(Frame.IsSimple), RAReg(Frame.RAReg) {}
1480
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001481 const MCSymbol *Personality;
1482 unsigned PersonalityEncoding;
1483 unsigned LsdaEncoding;
1484 bool IsSignalFrame;
1485 bool IsSimple;
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001486 unsigned RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001487};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001488
1489} // end anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001490
1491namespace llvm {
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001492
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001493template <> struct DenseMapInfo<CIEKey> {
1494 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1495 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001496
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001497 static unsigned getHashValue(const CIEKey &Key) {
1498 return static_cast<unsigned>(
1499 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001500 Key.IsSignalFrame, Key.IsSimple, Key.RAReg));
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001501 }
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001502
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001503 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1504 return LHS.Personality == RHS.Personality &&
1505 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1506 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1507 LHS.IsSignalFrame == RHS.IsSignalFrame &&
Saleem Abdulrasoolfc850672017-07-29 20:03:00 +00001508 LHS.IsSimple == RHS.IsSimple &&
1509 LHS.RAReg == RHS.RAReg;
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001510 }
1511};
Eugene Zelenkod96089b2017-02-14 00:33:36 +00001512
1513} // end namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001514
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001515void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001516 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001517 Streamer.generateCompactUnwindEncodings(MAB);
1518
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001519 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001520 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001521 const MCAsmInfo *AsmInfo = Context.getAsmInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001522 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001523 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001524
Bill Wendling9803abb2011-09-06 18:37:11 +00001525 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001526 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001527 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001528 bool SectionEmitted = false;
Benjamin Kramer7b4658f2016-06-26 14:49:00 +00001529 for (const MCDwarfFrameInfo &Frame : FrameArray) {
Bob Wilson0e180f22013-05-07 20:56:33 +00001530 if (Frame.CompactUnwindEncoding == 0) continue;
1531 if (!SectionEmitted) {
1532 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001533 Streamer.EmitValueToAlignment(AsmInfo->getCodePointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001534 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001535 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001536 NeedsEHFrameSection |=
1537 Frame.CompactUnwindEncoding ==
1538 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001539 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001540 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001541 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001542
Tim Northoverd1c6f512014-03-29 09:03:13 +00001543 if (!NeedsEHFrameSection) return;
1544
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001545 MCSection &Section =
1546 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1547 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001548
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001549 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001550 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001551 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001552
Eric Christopherd29430d2014-06-19 20:00:09 +00001553 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001554
Craig Topperbb694de2014-04-13 04:57:38 +00001555 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001556 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001557 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1558 const MCDwarfFrameInfo &Frame = *I;
1559 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001560 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001561 MOFI->getCompactUnwindDwarfEHFrameOnly())
1562 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1563 // of by the compact unwind encoding.
1564 continue;
1565
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001566 CIEKey Key(Frame);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001567 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1568 if (!CIEStart)
Saleem Abdulrasool29199f52017-07-29 20:03:02 +00001569 CIEStart = &Emitter.EmitCIE(Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001570
Rafael Espindola97588e12015-11-06 13:14:59 +00001571 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001572 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001573}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001574
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001575void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001576 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001577 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001578 SmallString<256> Tmp;
1579 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001580 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001581 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001582}
1583
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001584void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1585 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001586 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001587 // Scale the address delta by the minimum instruction length.
1588 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1589
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001590 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001591 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001592 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1593 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001594 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001595 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1596 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001597 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001598 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001599 if (Context.getAsmInfo()->isLittleEndian())
1600 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1601 else
1602 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001603 } else {
1604 assert(isUInt<32>(AddrDelta));
1605 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001606 if (Context.getAsmInfo()->isLittleEndian())
1607 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1608 else
1609 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001610 }
1611}