blob: 95cfe24eaae6f8f647a3564a6d0dcc4d9fe53377 [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
10#include "llvm/MC/MCDwarf.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "llvm/ADT/Hashing.h"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000012#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000019#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000020#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000021#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000022#include "llvm/MC/MCSection.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000023#include "llvm/MC/MCSymbol.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000024#include "llvm/Support/Debug.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000025#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000026#include "llvm/Support/LEB128.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000027#include "llvm/Support/Path.h"
28#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/raw_ostream.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000030using namespace llvm;
31
Kevin Enderbye46564a2010-09-30 16:52:03 +000032// Given a special op, return the address skip amount (in units of
33// DWARF2_LINE_MIN_INSN_LENGTH.
34#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
35
36// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc737ac12011-06-30 23:59:38 +000037#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbye46564a2010-09-30 16:52:03 +000038
39// First special line opcode - leave room for the standard opcodes.
40// Note: If you want to change this, you'll have to update the
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000041// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc737ac12011-06-30 23:59:38 +000042#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbye46564a2010-09-30 16:52:03 +000043
44// Minimum line offset in a special line info. opcode. This value
45// was chosen to give a reasonable range of values.
Bill Wendlingc737ac12011-06-30 23:59:38 +000046#define DWARF2_LINE_BASE -5
Kevin Enderbye46564a2010-09-30 16:52:03 +000047
48// Range of line offsets in a special line info. opcode.
Bill Wendlingc737ac12011-06-30 23:59:38 +000049#define DWARF2_LINE_RANGE 14
Kevin Enderbye46564a2010-09-30 16:52:03 +000050
Ulrich Weigand32d725b2013-06-12 14:46:54 +000051static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000052 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000053 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000054 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000055 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000056 // TODO: report this error, but really only once.
57 ;
58 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000059 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000060}
61
62//
63// This is called when an instruction is assembled into the specified section
64// and if there is information from the last .loc directive that has yet to have
65// a line entry made for it is made.
66//
Rafael Espindola4066e8d2014-05-12 14:28:48 +000067void MCLineEntry::Make(MCObjectStreamer *MCOS, const MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000068 if (!MCOS->getContext().getDwarfLocSeen())
69 return;
70
71 // Create a symbol at in the current section for use in the line entry.
72 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
73 // Set the value of the symbol to use for the MCLineEntry.
74 MCOS->EmitLabel(LineSym);
75
76 // Get the current .loc info saved in the context.
77 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
78
79 // Create a (local) line entry with the symbol and the current .loc info.
80 MCLineEntry LineEntry(LineSym, DwarfLoc);
81
82 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderbya68d0042010-10-04 20:17:24 +000083 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000084
Kevin Enderbye46564a2010-09-30 16:52:03 +000085 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000086 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000087 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000088 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000089 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000090}
91
92//
93// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000094//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000095static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
96 const MCSymbol &Start,
97 const MCSymbol &End,
98 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000099 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
100 const MCExpr *Res =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000101 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102 const MCExpr *RHS =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000103 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000104 const MCExpr *Res1 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000105 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000106 const MCExpr *Res2 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000107 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000108 const MCExpr *Res3 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000109 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000110 return Res3;
111}
112
Kevin Enderbye46564a2010-09-30 16:52:03 +0000113//
114// This emits the Dwarf line table for the specified section from the entries
115// in the LineSection.
116//
David Blaikief4ad6982014-03-12 22:35:23 +0000117static inline void
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +0000118EmitDwarfLineTable(MCObjectStreamer *MCOS, const MCSection *Section,
David Blaikief4ad6982014-03-12 22:35:23 +0000119 const MCLineSection::MCLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000120 unsigned FileNum = 1;
121 unsigned LastLine = 1;
122 unsigned Column = 0;
123 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
124 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000125 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000126 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000127
128 // Loop through each MCLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000129 for (auto it = LineEntries.begin(),
130 ie = LineEntries.end();
131 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000132
133 if (FileNum != it->getFileNum()) {
134 FileNum = it->getFileNum();
135 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000136 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000137 }
138 if (Column != it->getColumn()) {
139 Column = it->getColumn();
140 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000141 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000142 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000143 if (Discriminator != it->getDiscriminator()) {
144 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000145 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000146 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
147 MCOS->EmitULEB128IntValue(Size + 1);
148 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
149 MCOS->EmitULEB128IntValue(Discriminator);
150 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000151 if (Isa != it->getIsa()) {
152 Isa = it->getIsa();
153 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000154 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000155 }
156 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
157 Flags = it->getFlags();
158 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
159 }
160 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
161 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
162 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
163 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
164 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
165 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
166
Rafael Espindola1d37f352010-11-13 01:06:27 +0000167 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000168 MCSymbol *Label = it->getLabel();
169
170 // At this point we want to emit/create the sequence to encode the delta in
171 // line numbers and the increment of the address from the previous Label
172 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000173 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000174 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000175 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000176
177 LastLine = it->getLine();
178 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000179 }
180
181 // Emit a DW_LNE_end_sequence for the end of the section.
182 // Using the pointer Section create a temporary label at the end of the
183 // section and use that and the LastLabel to compute the address delta
184 // and use INT64_MAX as the line delta which is the signal that this is
185 // actually a DW_LNE_end_sequence.
186
187 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000188 // TODO: keep track of the last subsection so that this symbol appears in the
189 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000190 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000191
192 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000193 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000194 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000195 // Set the value of the symbol, as we are at the end of the section.
196 MCOS->EmitLabel(SectionEnd);
197
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000198 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000199 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000200
Bill Wendlingbc07a892013-06-18 07:20:20 +0000201 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000202 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000203 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000204}
205
206//
207// This emits the Dwarf file and the line tables.
208//
Rafael Espindola4066e8d2014-05-12 14:28:48 +0000209void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000210 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000211
David Blaikie8bf66c42014-04-01 07:35:52 +0000212 auto &LineTables = context.getMCDwarfLineTables();
213
214 // Bail out early so we don't switch to the debug_line section needlessly and
215 // in doing so create an unnecessary (if empty) section.
216 if (LineTables.empty())
217 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000218
219 // Switch to the section where the table will be emitted into.
220 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
221
Manman Ren4e042a62013-02-05 21:52:47 +0000222 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000223 for (const auto &CUIDTablePair : LineTables)
224 CUIDTablePair.second.EmitCU(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000225}
226
David Blaikie8287aff2014-03-18 02:13:23 +0000227void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS) const {
228 MCOS.EmitLabel(Header.Emit(&MCOS, None).second);
229}
230
David Blaikie5cff0e62014-03-13 21:47:12 +0000231std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000232 static const char StandardOpcodeLengths[] = {
233 0, // length of DW_LNS_copy
234 1, // length of DW_LNS_advance_pc
235 1, // length of DW_LNS_advance_line
236 1, // length of DW_LNS_set_file
237 1, // length of DW_LNS_set_column
238 0, // length of DW_LNS_negate_stmt
239 0, // length of DW_LNS_set_basic_block
240 0, // length of DW_LNS_const_add_pc
241 1, // length of DW_LNS_fixed_advance_pc
242 0, // length of DW_LNS_set_prologue_end
243 0, // length of DW_LNS_set_epilogue_begin
244 1 // DW_LNS_set_isa
245 };
246 assert(array_lengthof(StandardOpcodeLengths) == (DWARF2_LINE_OPCODE_BASE - 1));
247 return Emit(MCOS, StandardOpcodeLengths);
248}
249
Rafael Espindolac23174b2014-08-15 15:12:13 +0000250static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
251 MCContext &Context = OS.getContext();
252 assert(!isa<MCSymbolRefExpr>(Expr));
253 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
254 return Expr;
255
256 MCSymbol *ABS = Context.CreateTempSymbol();
257 OS.EmitAssignment(ABS, Expr);
258 return MCSymbolRefExpr::Create(ABS, Context);
259}
260
261static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
262 const MCExpr *ABS = forceExpAbs(OS, Value);
263 OS.EmitValue(ABS, Size);
264}
265
David Blaikie8287aff2014-03-18 02:13:23 +0000266std::pair<MCSymbol *, MCSymbol *>
267MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
268 ArrayRef<char> StandardOpcodeLengths) const {
269
Manman Ren4e042a62013-02-05 21:52:47 +0000270 MCContext &context = MCOS->getContext();
271
272 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000273 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000274 if (!LineStartSym)
275 LineStartSym = context.CreateTempSymbol();
276 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000277 MCOS->EmitLabel(LineStartSym);
278
279 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000280 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000281
282 // The first 4 bytes is the total length of the information for this
283 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000284 emitAbsValue(*MCOS,
285 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000286
287 // Next 2 bytes is the Version, which is Dwarf 2.
288 MCOS->EmitIntValue(2, 2);
289
290 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000291 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000292
293 // Length of the prologue, is the next 4 bytes. Which is the start of the
294 // section to the end of the prologue. Not including the 4 bytes for the
295 // total length, the 2 bytes for the version, and these 4 bytes for the
296 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000297 emitAbsValue(
298 *MCOS,
299 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000300
301 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000302 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000303 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
304 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
305 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000306 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000307
308 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000309 for (char Length : StandardOpcodeLengths)
310 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311
312 // Put out the directory and file tables.
313
314 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000315 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000316 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
317 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000318 }
319 MCOS->EmitIntValue(0, 1); // Terminate the directory list
320
321 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000322 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000323 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000324 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000325 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000326 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000327 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000328 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
329 MCOS->EmitIntValue(0, 1); // filesize (always 0)
330 }
331 MCOS->EmitIntValue(0, 1); // Terminate the file list
332
333 // This is the end of the prologue, so set the value of the symbol at the
334 // end of the prologue (that was used in a previous expression).
335 MCOS->EmitLabel(ProEndSym);
336
David Blaikie5cff0e62014-03-13 21:47:12 +0000337 return std::make_pair(LineStartSym, LineEndSym);
338}
339
Rafael Espindola4066e8d2014-05-12 14:28:48 +0000340void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS) const {
David Blaikie8bf66c42014-04-01 07:35:52 +0000341 MCSymbol *LineEndSym = Header.Emit(MCOS).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000342
Kevin Enderbye46564a2010-09-30 16:52:03 +0000343 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000344 for (const auto &LineSec : MCLineSections.getMCLineEntries())
345 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000346
David Blaikiea55e64f2014-03-12 22:28:56 +0000347 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines() &&
David Blaikie11765bc2014-03-13 17:55:28 +0000348 MCLineSections.getMCLineEntries().empty()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000349 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000350 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000351 // total_length >= prologue_length + 10
352 // We are 4 bytes short, since we have total_length = 51 and
353 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000354
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000355 // The regular end_sequence should be sufficient.
356 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000357 }
358
Kevin Enderbye46564a2010-09-30 16:52:03 +0000359 // This is the end of the section, so set the value of the symbol at the end
360 // of this section (that was used in a previous expression).
361 MCOS->EmitLabel(LineEndSym);
362}
363
David Blaikiec2df16b2014-03-17 18:13:58 +0000364unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000365 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000366 return Header.getFile(Directory, FileName, FileNumber);
367}
368
David Blaikiec2df16b2014-03-17 18:13:58 +0000369unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
370 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000371 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000372 if (Directory == CompilationDir)
373 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000374 if (FileName.empty()) {
375 FileName = "<stdin>";
376 Directory = "";
377 }
378 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000379 if (FileNumber == 0) {
380 FileNumber = SourceIdMap.size() + 1;
381 assert((MCDwarfFiles.empty() || FileNumber == MCDwarfFiles.size()) &&
382 "Don't mix autonumbered and explicit numbered line table usage");
383 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(
384 (Directory + Twine('\0') + FileName).str(), FileNumber);
385 if (Ent.getValue() != FileNumber)
386 return Ent.getValue();
387 }
David Blaikie498589c2014-03-13 19:15:04 +0000388 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
389 MCDwarfFiles.resize(FileNumber + 1);
390
391 // Get the new MCDwarfFile slot for this FileNumber.
392 MCDwarfFile &File = MCDwarfFiles[FileNumber];
393
394 // It is an error to use see the same number more than once.
395 if (!File.Name.empty())
396 return 0;
397
398 if (Directory.empty()) {
399 // Separate the directory part from the basename of the FileName.
400 StringRef tFileName = sys::path::filename(FileName);
401 if (!tFileName.empty()) {
402 Directory = sys::path::parent_path(FileName);
403 if (!Directory.empty())
404 FileName = tFileName;
405 }
406 }
407
408 // Find or make an entry in the MCDwarfDirs vector for this Directory.
409 // Capture directory name.
410 unsigned DirIndex;
411 if (Directory.empty()) {
412 // For FileNames with no directories a DirIndex of 0 is used.
413 DirIndex = 0;
414 } else {
415 DirIndex = 0;
416 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
417 if (Directory == MCDwarfDirs[DirIndex])
418 break;
419 }
420 if (DirIndex >= MCDwarfDirs.size())
421 MCDwarfDirs.push_back(Directory);
422 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
423 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
424 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
425 // are stored at MCDwarfFiles[FileNumber].Name .
426 DirIndex++;
427 }
428
429 File.Name = FileName;
430 File.DirIndex = DirIndex;
431
432 // return the allocated FileNumber.
433 return FileNumber;
434}
435
Kevin Enderbye46564a2010-09-30 16:52:03 +0000436/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000437void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000438 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000439 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000440 SmallString<256> Tmp;
441 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000442 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000443 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000444}
445
446/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000447void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
448 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000449 uint64_t Temp, Opcode;
450 bool NeedCopy = false;
451
452 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000453 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000454
455 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000456 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000457 // end_sequence to emit the matrix entry.
458 if (LineDelta == INT64_MAX) {
459 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
460 OS << char(dwarf::DW_LNS_const_add_pc);
461 else {
462 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000463 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000464 }
465 OS << char(dwarf::DW_LNS_extended_op);
466 OS << char(1);
467 OS << char(dwarf::DW_LNE_end_sequence);
468 return;
469 }
470
471 // Bias the line delta by the base.
472 Temp = LineDelta - DWARF2_LINE_BASE;
473
474 // If the line increment is out of range of a special opcode, we must encode
475 // it with DW_LNS_advance_line.
476 if (Temp >= DWARF2_LINE_RANGE) {
477 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000478 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000479
480 LineDelta = 0;
481 Temp = 0 - DWARF2_LINE_BASE;
482 NeedCopy = true;
483 }
484
485 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
486 if (LineDelta == 0 && AddrDelta == 0) {
487 OS << char(dwarf::DW_LNS_copy);
488 return;
489 }
490
491 // Bias the opcode by the special opcode base.
492 Temp += DWARF2_LINE_OPCODE_BASE;
493
494 // Avoid overflow when addr_delta is large.
495 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
496 // Try using a special opcode.
497 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
498 if (Opcode <= 255) {
499 OS << char(Opcode);
500 return;
501 }
502
503 // Try using DW_LNS_const_add_pc followed by special op.
504 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
505 if (Opcode <= 255) {
506 OS << char(dwarf::DW_LNS_const_add_pc);
507 OS << char(Opcode);
508 return;
509 }
510 }
511
512 // Otherwise use DW_LNS_advance_pc.
513 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000514 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000515
516 if (NeedCopy)
517 OS << char(dwarf::DW_LNS_copy);
518 else
519 OS << char(Temp);
520}
521
Kevin Enderbye7739d42011-12-09 18:09:40 +0000522// Utility function to write a tuple for .debug_abbrev.
523static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
524 MCOS->EmitULEB128IntValue(Name);
525 MCOS->EmitULEB128IntValue(Form);
526}
527
528// When generating dwarf for assembly source files this emits
529// the data for .debug_abbrev section which contains three DIEs.
530static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
531 MCContext &context = MCOS->getContext();
532 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
533
534 // DW_TAG_compile_unit DIE abbrev (1).
535 MCOS->EmitULEB128IntValue(1);
536 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
537 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
538 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000539 if (MCOS->getContext().getGenDwarfSectionSyms().size() > 1) {
540 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4);
541 } else {
542 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
543 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
544 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000545 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000546 if (!context.getCompilationDir().empty())
547 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000548 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
549 if (!DwarfDebugFlags.empty())
550 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
551 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
552 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
553 EmitAbbrev(MCOS, 0, 0);
554
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000555 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000556 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000557 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000558 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
559 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
560 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
561 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
562 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000563 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
564 EmitAbbrev(MCOS, 0, 0);
565
566 // DW_TAG_unspecified_parameters DIE abbrev (3).
567 MCOS->EmitULEB128IntValue(3);
568 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
569 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
570 EmitAbbrev(MCOS, 0, 0);
571
572 // Terminate the abbreviations for this compilation unit.
573 MCOS->EmitIntValue(0, 1);
574}
575
576// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000577// .debug_aranges section. This section contains a header and a table of pairs
578// of PointerSize'ed values for the address and size of section(s) with line
579// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000580static void EmitGenDwarfAranges(MCStreamer *MCOS,
581 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000582 MCContext &context = MCOS->getContext();
583
Oliver Stannard8b273082014-06-19 15:52:37 +0000584 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000585
586 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
587
588 // This will be the length of the .debug_aranges section, first account for
589 // the size of each item in the header (see below where we emit these items).
590 int Length = 4 + 2 + 4 + 1 + 1;
591
592 // Figure the padding after the header before the table of address and size
593 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000594 const MCAsmInfo *asmInfo = context.getAsmInfo();
595 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000596 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
597 if (Pad == 2 * AddrSize)
598 Pad = 0;
599 Length += Pad;
600
601 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000602 // of each section we have in the table.
603 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000604 // And the pair of terminating zeros.
605 Length += 2 * AddrSize;
606
607
608 // Emit the header for this section.
609 // The 4 byte length not including the 4 byte value for the length.
610 MCOS->EmitIntValue(Length - 4, 4);
611 // The 2 byte version, which is 2.
612 MCOS->EmitIntValue(2, 2);
613 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000614 // of the .debug_info.
615 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000616 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
617 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000618 else
619 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000620 // The 1 byte size of an address.
621 MCOS->EmitIntValue(AddrSize, 1);
622 // The 1 byte size of a segment descriptor, we use a value of zero.
623 MCOS->EmitIntValue(0, 1);
624 // Align the header with the padding if needed, before we put out the table.
625 for(int i = 0; i < Pad; i++)
626 MCOS->EmitIntValue(0, 1);
627
Oliver Stannard8b273082014-06-19 15:52:37 +0000628 // Now emit the table of pairs of PointerSize'ed values for the section
629 // addresses and sizes.
630 for (const auto &sec : Sections) {
Eric Christopherd29430d2014-06-19 20:00:09 +0000631 MCSymbol *StartSymbol = sec.second.first;
632 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000633 assert(StartSymbol && "StartSymbol must not be NULL");
634 assert(EndSymbol && "EndSymbol must not be NULL");
635
636 const MCExpr *Addr = MCSymbolRefExpr::Create(
637 StartSymbol, MCSymbolRefExpr::VK_None, context);
638 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
639 *StartSymbol, *EndSymbol, 0);
640 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000641 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000642 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000643
644 // And finally the pair of terminating zeros.
645 MCOS->EmitIntValue(0, AddrSize);
646 MCOS->EmitIntValue(0, AddrSize);
647}
648
649// When generating dwarf for assembly source files this emits the data for
650// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000651// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000652static void EmitGenDwarfInfo(MCStreamer *MCOS,
653 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000654 const MCSymbol *LineSectionSymbol,
655 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000656 MCContext &context = MCOS->getContext();
657
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000658 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000659
660 // Create a symbol at the start and end of this section used in here for the
661 // expression to calculate the length in the header.
662 MCSymbol *InfoStart = context.CreateTempSymbol();
663 MCOS->EmitLabel(InfoStart);
664 MCSymbol *InfoEnd = context.CreateTempSymbol();
665
666 // First part: the header.
667
668 // The 4 byte total length of the information for this compilation unit, not
669 // including these 4 bytes.
670 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000671 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000672
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000673 // The 2 byte DWARF version.
674 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000675
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000676 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000677 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
678 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000679 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000680 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000681 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000682 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
683 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000684
Bill Wendlingbc07a892013-06-18 07:20:20 +0000685 const MCAsmInfo *asmInfo = context.getAsmInfo();
686 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000687 // The 1 byte size of an address.
688 MCOS->EmitIntValue(AddrSize, 1);
689
690 // Second part: the compile_unit DIE.
691
692 // The DW_TAG_compile_unit DIE abbrev (1).
693 MCOS->EmitULEB128IntValue(1);
694
695 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
696 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000697 if (LineSectionSymbol)
698 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
699 AsmInfo.needsDwarfSectionOffsetDirective());
700 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000701 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000702
Oliver Stannard8b273082014-06-19 15:52:37 +0000703 if (RangesSectionSymbol) {
704 // There are multiple sections containing code, so we must use the
705 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000706
Oliver Stannard8b273082014-06-19 15:52:37 +0000707 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
708 // to the address range list for this compilation unit.
709 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
710 } else {
711 // If we only have one non-empty code section, we can use the simpler
712 // AT_low_pc and AT_high_pc attributes.
713
714 // Find the first (and only) non-empty text section
715 auto &Sections = context.getGenDwarfSectionSyms();
716 const auto TextSection = Sections.begin();
717 assert(TextSection != Sections.end() && "No text section found");
718
Eric Christopherd29430d2014-06-19 20:00:09 +0000719 MCSymbol *StartSymbol = TextSection->second.first;
720 MCSymbol *EndSymbol = TextSection->second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000721 assert(StartSymbol && "StartSymbol must not be NULL");
722 assert(EndSymbol && "EndSymbol must not be NULL");
723
724 // AT_low_pc, the first address of the default .text section.
725 const MCExpr *Start = MCSymbolRefExpr::Create(
726 StartSymbol, MCSymbolRefExpr::VK_None, context);
727 MCOS->EmitValue(Start, AddrSize);
728
729 // AT_high_pc, the last address of the default .text section.
730 const MCExpr *End = MCSymbolRefExpr::Create(
731 EndSymbol, MCSymbolRefExpr::VK_None, context);
732 MCOS->EmitValue(End, AddrSize);
733 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000734
735 // AT_name, the name of the source file. Reconstruct from the first directory
736 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000737 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000738 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000739 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000740 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000742 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000743 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000744 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000745 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
746
747 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000748 if (!context.getCompilationDir().empty()) {
749 MCOS->EmitBytes(context.getCompilationDir());
750 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
751 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000752
753 // AT_APPLE_flags, the command line arguments of the assembler tool.
754 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
755 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000756 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000757 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
758 }
759
760 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000761 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000762 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000763 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000764 else
765 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000766 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
767
768 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
769 // draft has no standard code for assembler.
770 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
771
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000772 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000774 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000775 const std::vector<MCGenDwarfLabelEntry> &Entries =
776 MCOS->getContext().getMCGenDwarfLabelEntries();
777 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000778 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000779 MCOS->EmitULEB128IntValue(2);
780
781 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000782 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
784
785 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000786 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000787
788 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000789 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000790
791 // AT_low_pc, start address of the label.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000792 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000793 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000794 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795
Kevin Enderbye7739d42011-12-09 18:09:40 +0000796 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
797 MCOS->EmitIntValue(0, 1);
798
799 // The DW_TAG_unspecified_parameters DIE abbrev (3).
800 MCOS->EmitULEB128IntValue(3);
801
802 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
803 MCOS->EmitIntValue(0, 1);
804 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000805
806 // Add the NULL DIE terminating the Compile Unit DIE's.
807 MCOS->EmitIntValue(0, 1);
808
809 // Now set the value of the symbol at the end of the info section.
810 MCOS->EmitLabel(InfoEnd);
811}
812
Oliver Stannard8b273082014-06-19 15:52:37 +0000813// When generating dwarf for assembly source files this emits the data for
814// .debug_ranges section. We only emit one range list, which spans all of the
815// executable sections of this file.
816static void EmitGenDwarfRanges(MCStreamer *MCOS) {
817 MCContext &context = MCOS->getContext();
818 auto &Sections = context.getGenDwarfSectionSyms();
819
820 const MCAsmInfo *AsmInfo = context.getAsmInfo();
821 int AddrSize = AsmInfo->getPointerSize();
822
823 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
824
825 for (const auto sec : Sections) {
826
Eric Christopherd29430d2014-06-19 20:00:09 +0000827 MCSymbol *StartSymbol = sec.second.first;
828 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000829 assert(StartSymbol && "StartSymbol must not be NULL");
830 assert(EndSymbol && "EndSymbol must not be NULL");
831
832 // Emit a base address selection entry for the start of this section
833 const MCExpr *SectionStartAddr = MCSymbolRefExpr::Create(
834 StartSymbol, MCSymbolRefExpr::VK_None, context);
835 MCOS->EmitFill(AddrSize, 0xFF);
836 MCOS->EmitValue(SectionStartAddr, AddrSize);
837
838 // Emit a range list entry spanning this section
839 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
840 *StartSymbol, *EndSymbol, 0);
841 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000842 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000843 }
844
845 // Emit end of list entry
846 MCOS->EmitIntValue(0, AddrSize);
847 MCOS->EmitIntValue(0, AddrSize);
848}
849
Kevin Enderbye7739d42011-12-09 18:09:40 +0000850//
851// When generating dwarf for assembly source files this emits the Dwarf
852// sections.
853//
David Blaikie8bf66c42014-04-01 07:35:52 +0000854void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000855 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000856
857 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000858 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000859 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000860 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000861 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000862 if (CreateDwarfSectionSymbols)
863 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000864 MCSymbol *AbbrevSectionSymbol = nullptr;
865 MCSymbol *InfoSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000866 MCSymbol *RangesSectionSymbol = NULL;
867
868 // Create end symbols for each section, and remove empty sections
869 MCOS->getContext().finalizeDwarfSections(*MCOS);
870
871 // If there are no sections to generate debug info for, we don't need
872 // to do anything
873 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
874 return;
875
876 // We only need to use the .debug_ranges section if we have multiple
877 // code sections.
878 const bool UseRangesSection =
879 MCOS->getContext().getGenDwarfSectionSyms().size() > 1;
880 CreateDwarfSectionSymbols |= UseRangesSection;
881
Kevin Enderbye7739d42011-12-09 18:09:40 +0000882 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000883 if (CreateDwarfSectionSymbols) {
884 InfoSectionSymbol = context.CreateTempSymbol();
885 MCOS->EmitLabel(InfoSectionSymbol);
886 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000887 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000888 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000889 AbbrevSectionSymbol = context.CreateTempSymbol();
890 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000891 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000892 if (UseRangesSection) {
893 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
894 if (CreateDwarfSectionSymbols) {
895 RangesSectionSymbol = context.CreateTempSymbol();
896 MCOS->EmitLabel(RangesSectionSymbol);
897 }
898 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000899
Oliver Stannard8b273082014-06-19 15:52:37 +0000900 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
901
902 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000903
904 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000905 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000906
Oliver Stannard8b273082014-06-19 15:52:37 +0000907 if (UseRangesSection)
908 EmitGenDwarfRanges(MCOS);
909
Kevin Enderbye7739d42011-12-09 18:09:40 +0000910 // Output the data for .debug_abbrev section.
911 EmitGenDwarfAbbrev(MCOS);
912
913 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000914 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
915 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000916}
917
918//
919// When generating dwarf for assembly source files this is called when symbol
920// for a label is created. If this symbol is not a temporary and is in the
921// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000922// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000923//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000924void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000925 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000926 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000927 if (Symbol->isTemporary())
928 return;
929 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000930 // We won't create dwarf labels for symbols in sections that we are not
931 // generating debug info for.
932 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000933 return;
934
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000935 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000936 // underbar if any.
937 StringRef Name = Symbol->getName();
938 if (Name.startswith("_"))
939 Name = Name.substr(1, Name.size()-1);
940
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000941 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000942 unsigned FileNumber = context.getGenDwarfFileNumber();
943
944 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000945 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000946 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000947 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
948
949 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
950 // values so that they don't have things like an ARM thumb bit from the
951 // original symbol. So when used they won't get a low bit set after
952 // relocation.
953 MCSymbol *Label = context.CreateTempSymbol();
954 MCOS->EmitLabel(Label);
955
956 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000957 MCOS->getContext().addMCGenDwarfLabelEntry(
958 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000959}
960
Rafael Espindola0a017a62010-12-10 07:39:47 +0000961static int getDataAlignmentFactor(MCStreamer &streamer) {
962 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000963 const MCAsmInfo *asmInfo = context.getAsmInfo();
964 int size = asmInfo->getCalleeSaveStackSlotSize();
965 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000966 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000967 else
968 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000969}
970
Rafael Espindola5395f442011-04-22 00:08:43 +0000971static unsigned getSizeForEncoding(MCStreamer &streamer,
972 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000973 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000974 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000975 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000976 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000977 case dwarf::DW_EH_PE_absptr:
978 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000979 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000980 case dwarf::DW_EH_PE_udata2:
981 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000982 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000983 case dwarf::DW_EH_PE_udata4:
984 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000985 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000986 case dwarf::DW_EH_PE_udata8:
987 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000988 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000989 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000990}
991
Rafael Espindolafe963b12014-08-15 03:07:13 +0000992static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
993 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000994 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000995 const MCAsmInfo *asmInfo = context.getAsmInfo();
996 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
997 symbolEncoding,
998 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000999 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001000 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +00001001 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +00001002 else
1003 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001004}
1005
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001006static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
1007 unsigned symbolEncoding) {
1008 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001009 const MCAsmInfo *asmInfo = context.getAsmInfo();
1010 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1011 symbolEncoding,
1012 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001013 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001014 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001015}
1016
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001017namespace {
1018 class FrameEmitterImpl {
1019 int CFAOffset;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001020 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +00001021 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001022 public:
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001023 FrameEmitterImpl(bool isEH)
Rafael Espindola1c509712014-08-20 17:33:44 +00001024 : CFAOffset(0), IsEH(isEH), SectionStart(nullptr) {}
Bill Wendling4d9aa512011-07-22 21:18:59 +00001025
1026 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001027
Rafael Espindolafe963b12014-08-15 03:07:13 +00001028 /// Emit the unwind information in a compact way.
1029 void EmitCompactUnwind(MCObjectStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001030 const MCDwarfFrameInfo &frame);
1031
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001032 const MCSymbol &EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001033 const MCSymbol *personality,
1034 unsigned personalityEncoding,
1035 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001036 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001037 unsigned lsdaEncoding,
1038 bool IsSimple);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001039 MCSymbol *EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001040 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001041 const MCDwarfFrameInfo &frame);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001042 void EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001043 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001044 MCSymbol *BaseLabel);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001045 void EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001046 const MCCFIInstruction &Instr);
1047 };
Bill Wendling567a1ae2011-06-30 21:25:51 +00001048
1049} // end anonymous namespace
1050
Rafael Espindolafe963b12014-08-15 03:07:13 +00001051static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001052 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001053}
1054
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001055void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001056 const MCCFIInstruction &Instr) {
1057 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
1058
1059 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001060 case MCCFIInstruction::OpRegister: {
1061 unsigned Reg1 = Instr.getRegister();
1062 unsigned Reg2 = Instr.getRegister2();
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 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001068 case MCCFIInstruction::OpWindowSave: {
1069 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1070 return;
1071 }
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: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001095 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001096 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001097 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001098 Streamer.EmitULEB128IntValue(CFAOffset);
1099
1100 return;
1101 }
1102
1103 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001104 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001105 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001106
1107 return;
1108 }
1109
1110 case MCCFIInstruction::OpOffset:
1111 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001112 const bool IsRelative =
1113 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001114
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001115 unsigned Reg = Instr.getRegister();
1116 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001117 if (IsRelative)
1118 Offset -= CFAOffset;
1119 Offset = Offset / dataAlignmentFactor;
1120
1121 if (Offset < 0) {
1122 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1123 Streamer.EmitULEB128IntValue(Reg);
1124 Streamer.EmitSLEB128IntValue(Offset);
1125 } else if (Reg < 64) {
1126 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001127 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001128 } else {
1129 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001130 Streamer.EmitULEB128IntValue(Reg);
1131 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001132 }
1133 return;
1134 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001135 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001136 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1137 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001138 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001139 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1140 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001141 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001142 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001143 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001144 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001145 return;
1146 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001147 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001148 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001149 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1150 return;
1151 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001152 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001153 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001154 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001155 }
1156 llvm_unreachable("Unhandled case in switch");
1157}
1158
Rafael Espindolafe963b12014-08-15 03:07:13 +00001159/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001160void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001161 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001162 MCSymbol *BaseLabel) {
1163 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1164 const MCCFIInstruction &Instr = Instrs[i];
1165 MCSymbol *Label = Instr.getLabel();
1166 // Throw out move if the label is invalid.
1167 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1168
1169 // Advance row if new location.
1170 if (BaseLabel && Label) {
1171 MCSymbol *ThisSym = Label;
1172 if (ThisSym != BaseLabel) {
1173 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1174 BaseLabel = ThisSym;
1175 }
1176 }
1177
1178 EmitCFIInstruction(streamer, Instr);
1179 }
1180}
1181
Rafael Espindolafe963b12014-08-15 03:07:13 +00001182/// Emit the unwind information in a compact way.
1183void FrameEmitterImpl::EmitCompactUnwind(MCObjectStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001184 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001185 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001186 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001187
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001188 // range-start range-length compact-unwind-enc personality-func lsda
1189 // _foo LfooEnd-_foo 0x00000023 0 0
1190 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1191 //
1192 // .section __LD,__compact_unwind,regular,debug
1193 //
1194 // # compact unwind for _foo
1195 // .quad _foo
1196 // .set L1,LfooEnd-_foo
1197 // .long L1
1198 // .long 0x01010001
1199 // .quad 0
1200 // .quad 0
1201 //
1202 // # compact unwind for _bar
1203 // .quad _bar
1204 // .set L2,LbarEnd-_bar
1205 // .long L2
1206 // .long 0x01020011
1207 // .quad __gxx_personality
1208 // .quad except_tab1
1209
Bill Wendling49bc6802011-07-19 00:06:12 +00001210 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001211 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001212 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001213
Bill Wendlingdafd5982011-07-14 23:34:45 +00001214 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001215 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001216 Encoding |= 0x40000000;
1217
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001218 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001219 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001220 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001221 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001222
1223 // Range Length
1224 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1225 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001226 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001227
Bill Wendling75174662011-06-30 00:30:52 +00001228 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001229 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1230 Streamer.EmitIntValue(Encoding, Size);
1231
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001232 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001233 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001234 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001235 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001236 else
1237 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001238
1239 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001240 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001241 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001242 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001243 else
1244 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001245}
1246
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001247const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001248 const MCSymbol *personality,
1249 unsigned personalityEncoding,
1250 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001251 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001252 unsigned lsdaEncoding,
1253 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001254 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001255 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001256 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001257
Rafael Espindolab4076b292014-06-20 23:54:32 +00001258 MCSymbol *sectionStart = context.CreateTempSymbol();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001259 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001260
Bill Wendlingb6adf462011-07-07 00:54:13 +00001261 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001262
1263 // Length
1264 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1265 *sectionEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001266 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001267
1268 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001269 unsigned CIE_ID = IsEH ? 0 : -1;
1270 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001271
1272 // Version
Oliver Stannardf7693f42014-06-19 15:39:33 +00001273 // For DWARF2, we use CIE version 1
1274 // For DWARF3+, we use CIE version 3
1275 uint8_t CIEVersion = context.getDwarfVersion() <= 2 ? 1 : 3;
1276 streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001277
1278 // Augmentation String
1279 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001280 if (IsEH) {
1281 Augmentation += "z";
1282 if (personality)
1283 Augmentation += "P";
1284 if (lsda)
1285 Augmentation += "L";
1286 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001287 if (IsSignalFrame)
1288 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001289 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001290 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001291 streamer.EmitIntValue(0, 1);
1292
1293 // Code Alignment Factor
Bill Wendlingbc07a892013-06-18 07:20:20 +00001294 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001295
1296 // Data Alignment Factor
1297 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1298
1299 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001300 if (CIEVersion == 1) {
1301 assert(MRI->getRARegister() <= 255 &&
1302 "DWARF 2 encodes return_address_register in one byte");
1303 streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true), 1);
1304 } else {
1305 streamer.EmitULEB128IntValue(
1306 MRI->getDwarfRegNum(MRI->getRARegister(), true));
1307 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001308
1309 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001310
1311 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001312 if (IsEH) {
1313 if (personality) {
1314 // Personality Encoding
1315 augmentationLength += 1;
1316 // Personality
1317 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1318 }
1319 if (lsda)
1320 augmentationLength += 1;
1321 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001322 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001323
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001324 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001325
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001326 // Augmentation Data (optional)
1327 if (personality) {
1328 // Personality Encoding
Rafael Espindolafe963b12014-08-15 03:07:13 +00001329 emitEncodingByte(streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001330 // Personality
1331 EmitPersonality(streamer, *personality, personalityEncoding);
1332 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001333
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001334 if (lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001335 emitEncodingByte(streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001336
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001337 // Encoding of the FDE pointers
Rafael Espindolafe963b12014-08-15 03:07:13 +00001338 emitEncodingByte(streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001339 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001340
1341 // Initial Instructions
1342
Bill Wendlingbc07a892013-06-18 07:20:20 +00001343 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001344 if (!IsSimple) {
1345 const std::vector<MCCFIInstruction> &Instructions =
1346 MAI->getInitialFrameState();
Craig Topperbb694de2014-04-13 04:57:38 +00001347 EmitCFIInstructions(streamer, Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001348 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001349
1350 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001351 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001352
1353 streamer.EmitLabel(sectionEnd);
1354 return *sectionStart;
1355}
1356
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001357MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001358 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001359 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001360 MCContext &context = streamer.getContext();
1361 MCSymbol *fdeStart = context.CreateTempSymbol();
1362 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001363 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001364
1365 // Length
1366 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001367 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001368
1369 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001370
Rafael Espindola0a017a62010-12-10 07:39:47 +00001371 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001372 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001373 if (IsEH) {
1374 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1375 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001376 emitAbsValue(streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001377 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001378 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1379 cieStart, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001380 emitAbsValue(streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001381 } else {
1382 streamer.EmitSymbolValue(&cieStart, 4);
1383 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001384
Rafael Espindola0a017a62010-12-10 07:39:47 +00001385 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001386 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001387 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Nick Lewycky333449c2012-08-12 08:09:45 +00001388 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Rafael Espindolafe963b12014-08-15 03:07:13 +00001389 emitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001390
1391 // PC Range
1392 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1393 *frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001394 emitAbsValue(streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001395
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001396 if (IsEH) {
1397 // Augmentation Data Length
1398 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001399
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001400 if (frame.Lsda)
1401 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001402
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001403 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001404
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001405 // Augmentation Data
1406 if (frame.Lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001407 emitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001408 }
Rafael Espindola68067662011-04-29 03:06:29 +00001409
Rafael Espindola0a017a62010-12-10 07:39:47 +00001410 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001411 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001412
Rafael Espindola0a017a62010-12-10 07:39:47 +00001413 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001414 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001415
1416 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001417}
1418
Benjamin Kramer570dd782010-12-30 22:34:44 +00001419namespace {
1420 struct CIEKey {
Craig Topperbb694de2014-04-13 04:57:38 +00001421 static const CIEKey getEmptyKey() {
1422 return CIEKey(nullptr, 0, -1, false, false);
1423 }
1424 static const CIEKey getTombstoneKey() {
1425 return CIEKey(nullptr, -1, 0, false, false);
1426 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001427
Eric Christopherd29430d2014-06-19 20:00:09 +00001428 CIEKey(const MCSymbol *Personality_, unsigned PersonalityEncoding_,
1429 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_)
1430 : Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1431 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1432 IsSimple(IsSimple_) {}
1433 const MCSymbol *Personality;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001434 unsigned PersonalityEncoding;
1435 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001436 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001437 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001438 };
1439}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001440
1441namespace llvm {
1442 template <>
1443 struct DenseMapInfo<CIEKey> {
1444 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001445 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001446 }
1447 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001448 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001449 }
1450 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001451 return static_cast<unsigned>(hash_combine(Key.Personality,
1452 Key.PersonalityEncoding,
1453 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001454 Key.IsSignalFrame,
1455 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001456 }
1457 static bool isEqual(const CIEKey &LHS,
1458 const CIEKey &RHS) {
1459 return LHS.Personality == RHS.Personality &&
1460 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001461 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001462 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1463 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001464 }
1465 };
1466}
1467
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001468void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001469 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001470 Streamer.generateCompactUnwindEncodings(MAB);
1471
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001472 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001473 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001474 FrameEmitterImpl Emitter(IsEH);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001475 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001476
Bill Wendling9803abb2011-09-06 18:37:11 +00001477 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001478 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001479 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001480 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001481 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1482 const MCDwarfFrameInfo &Frame = FrameArray[i];
1483 if (Frame.CompactUnwindEncoding == 0) continue;
1484 if (!SectionEmitted) {
1485 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001486 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001487 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001488 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001489 NeedsEHFrameSection |=
1490 Frame.CompactUnwindEncoding ==
1491 MOFI->getCompactUnwindDwarfEHFrameOnly();
Bob Wilson0e180f22013-05-07 20:56:33 +00001492 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001493 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001494 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001495
Tim Northoverd1c6f512014-03-29 09:03:13 +00001496 if (!NeedsEHFrameSection) return;
1497
Bill Wendlinga800f952013-05-30 18:52:57 +00001498 const MCSection &Section =
1499 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1500 *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001501
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001502 Streamer.SwitchSection(&Section);
1503 MCSymbol *SectionStart = Context.CreateTempSymbol();
1504 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001505 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001506
Craig Topperbb694de2014-04-13 04:57:38 +00001507 MCSymbol *FDEEnd = nullptr;
Eric Christopherd29430d2014-06-19 20:00:09 +00001508 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001509
Craig Topperbb694de2014-04-13 04:57:38 +00001510 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001511 NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001512 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1513 const MCDwarfFrameInfo &Frame = FrameArray[i];
Tim Northoverd1c6f512014-03-29 09:03:13 +00001514
1515 // Emit the label from the previous iteration
1516 if (FDEEnd) {
1517 Streamer.EmitLabel(FDEEnd);
Craig Topperbb694de2014-04-13 04:57:38 +00001518 FDEEnd = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001519 }
1520
1521 if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding !=
1522 MOFI->getCompactUnwindDwarfEHFrameOnly())
1523 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1524 // of by the compact unwind encoding.
1525 continue;
1526
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001527 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001528 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001529 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1530 if (!CIEStart)
1531 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1532 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001533 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001534 Frame.LsdaEncoding,
1535 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001536
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001537 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001538 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001539
Bill Wendlingbc07a892013-06-18 07:20:20 +00001540 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001541 if (FDEEnd)
1542 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001543}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001544
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001545void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001546 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001547 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001548 SmallString<256> Tmp;
1549 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001550 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001551 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001552}
1553
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001554void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1555 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001556 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001557 // Scale the address delta by the minimum instruction length.
1558 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1559
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001560 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001561 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001562 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1563 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001564 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001565 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1566 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001567 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001568 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001569 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001570 OS << uint8_t( AddrDelta & 0xff);
1571 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001572 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001573 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001574 assert(isUInt<32>(AddrDelta));
1575 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001576 OS << uint8_t( AddrDelta & 0xff);
1577 OS << uint8_t((AddrDelta >> 8) & 0xff);
1578 OS << uint8_t((AddrDelta >> 16) & 0xff);
1579 OS << uint8_t((AddrDelta >> 24) & 0xff);
1580
Rafael Espindola736a35d2010-12-28 05:39:27 +00001581 }
1582}