blob: ccfba6691e8a3b4a1d658b177f1d9133a4837f63 [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 };
Gabor Horvathc759e542015-03-16 10:19:53 +0000246 assert(array_lengthof(StandardOpcodeLengths) ==
247 (DWARF2_LINE_OPCODE_BASE - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000248 return Emit(MCOS, StandardOpcodeLengths);
249}
250
Rafael Espindolac23174b2014-08-15 15:12:13 +0000251static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
252 MCContext &Context = OS.getContext();
253 assert(!isa<MCSymbolRefExpr>(Expr));
254 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
255 return Expr;
256
257 MCSymbol *ABS = Context.CreateTempSymbol();
258 OS.EmitAssignment(ABS, Expr);
259 return MCSymbolRefExpr::Create(ABS, Context);
260}
261
262static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
263 const MCExpr *ABS = forceExpAbs(OS, Value);
264 OS.EmitValue(ABS, Size);
265}
266
David Blaikie8287aff2014-03-18 02:13:23 +0000267std::pair<MCSymbol *, MCSymbol *>
268MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
269 ArrayRef<char> StandardOpcodeLengths) const {
270
Manman Ren4e042a62013-02-05 21:52:47 +0000271 MCContext &context = MCOS->getContext();
272
273 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000274 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000275 if (!LineStartSym)
276 LineStartSym = context.CreateTempSymbol();
277 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000278 MCOS->EmitLabel(LineStartSym);
279
280 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000281 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000282
283 // The first 4 bytes is the total length of the information for this
284 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000285 emitAbsValue(*MCOS,
286 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000287
288 // Next 2 bytes is the Version, which is Dwarf 2.
289 MCOS->EmitIntValue(2, 2);
290
291 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000292 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000293
294 // Length of the prologue, is the next 4 bytes. Which is the start of the
295 // section to the end of the prologue. Not including the 4 bytes for the
296 // total length, the 2 bytes for the version, and these 4 bytes for the
297 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000298 emitAbsValue(
299 *MCOS,
300 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000301
302 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000303 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000304 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
305 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
306 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000307 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308
309 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000310 for (char Length : StandardOpcodeLengths)
311 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000312
313 // Put out the directory and file tables.
314
315 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000316 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000317 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
318 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000319 }
320 MCOS->EmitIntValue(0, 1); // Terminate the directory list
321
322 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000323 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000324 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000325 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000326 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000327 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000328 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000329 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
330 MCOS->EmitIntValue(0, 1); // filesize (always 0)
331 }
332 MCOS->EmitIntValue(0, 1); // Terminate the file list
333
334 // This is the end of the prologue, so set the value of the symbol at the
335 // end of the prologue (that was used in a previous expression).
336 MCOS->EmitLabel(ProEndSym);
337
David Blaikie5cff0e62014-03-13 21:47:12 +0000338 return std::make_pair(LineStartSym, LineEndSym);
339}
340
Rafael Espindola4066e8d2014-05-12 14:28:48 +0000341void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS) const {
David Blaikie8bf66c42014-04-01 07:35:52 +0000342 MCSymbol *LineEndSym = Header.Emit(MCOS).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000343
Kevin Enderbye46564a2010-09-30 16:52:03 +0000344 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000345 for (const auto &LineSec : MCLineSections.getMCLineEntries())
346 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000347
Kevin Enderbye46564a2010-09-30 16:52:03 +0000348 // This is the end of the section, so set the value of the symbol at the end
349 // of this section (that was used in a previous expression).
350 MCOS->EmitLabel(LineEndSym);
351}
352
David Blaikiec2df16b2014-03-17 18:13:58 +0000353unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000354 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000355 return Header.getFile(Directory, FileName, FileNumber);
356}
357
David Blaikiec2df16b2014-03-17 18:13:58 +0000358unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
359 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000360 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000361 if (Directory == CompilationDir)
362 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000363 if (FileName.empty()) {
364 FileName = "<stdin>";
365 Directory = "";
366 }
367 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000368 if (FileNumber == 0) {
369 FileNumber = SourceIdMap.size() + 1;
370 assert((MCDwarfFiles.empty() || FileNumber == MCDwarfFiles.size()) &&
371 "Don't mix autonumbered and explicit numbered line table usage");
David Blaikie5106ce72014-11-19 05:49:42 +0000372 auto IterBool = SourceIdMap.insert(
373 std::make_pair((Directory + Twine('\0') + FileName).str(), FileNumber));
374 if (!IterBool.second)
375 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000376 }
David Blaikie498589c2014-03-13 19:15:04 +0000377 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
378 MCDwarfFiles.resize(FileNumber + 1);
379
380 // Get the new MCDwarfFile slot for this FileNumber.
381 MCDwarfFile &File = MCDwarfFiles[FileNumber];
382
383 // It is an error to use see the same number more than once.
384 if (!File.Name.empty())
385 return 0;
386
387 if (Directory.empty()) {
388 // Separate the directory part from the basename of the FileName.
389 StringRef tFileName = sys::path::filename(FileName);
390 if (!tFileName.empty()) {
391 Directory = sys::path::parent_path(FileName);
392 if (!Directory.empty())
393 FileName = tFileName;
394 }
395 }
396
397 // Find or make an entry in the MCDwarfDirs vector for this Directory.
398 // Capture directory name.
399 unsigned DirIndex;
400 if (Directory.empty()) {
401 // For FileNames with no directories a DirIndex of 0 is used.
402 DirIndex = 0;
403 } else {
404 DirIndex = 0;
405 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
406 if (Directory == MCDwarfDirs[DirIndex])
407 break;
408 }
409 if (DirIndex >= MCDwarfDirs.size())
410 MCDwarfDirs.push_back(Directory);
411 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
412 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
413 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
414 // are stored at MCDwarfFiles[FileNumber].Name .
415 DirIndex++;
416 }
417
418 File.Name = FileName;
419 File.DirIndex = DirIndex;
420
421 // return the allocated FileNumber.
422 return FileNumber;
423}
424
Kevin Enderbye46564a2010-09-30 16:52:03 +0000425/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000426void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000427 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000428 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000429 SmallString<256> Tmp;
430 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000431 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000432 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000433}
434
435/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000436void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
437 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000438 uint64_t Temp, Opcode;
439 bool NeedCopy = false;
440
441 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000442 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000443
444 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000445 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000446 // end_sequence to emit the matrix entry.
447 if (LineDelta == INT64_MAX) {
448 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
449 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000450 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000451 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000452 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000453 }
454 OS << char(dwarf::DW_LNS_extended_op);
455 OS << char(1);
456 OS << char(dwarf::DW_LNE_end_sequence);
457 return;
458 }
459
460 // Bias the line delta by the base.
461 Temp = LineDelta - DWARF2_LINE_BASE;
462
463 // If the line increment is out of range of a special opcode, we must encode
464 // it with DW_LNS_advance_line.
465 if (Temp >= DWARF2_LINE_RANGE) {
466 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000467 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000468
469 LineDelta = 0;
470 Temp = 0 - DWARF2_LINE_BASE;
471 NeedCopy = true;
472 }
473
474 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
475 if (LineDelta == 0 && AddrDelta == 0) {
476 OS << char(dwarf::DW_LNS_copy);
477 return;
478 }
479
480 // Bias the opcode by the special opcode base.
481 Temp += DWARF2_LINE_OPCODE_BASE;
482
483 // Avoid overflow when addr_delta is large.
484 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
485 // Try using a special opcode.
486 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
487 if (Opcode <= 255) {
488 OS << char(Opcode);
489 return;
490 }
491
492 // Try using DW_LNS_const_add_pc followed by special op.
493 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
494 if (Opcode <= 255) {
495 OS << char(dwarf::DW_LNS_const_add_pc);
496 OS << char(Opcode);
497 return;
498 }
499 }
500
501 // Otherwise use DW_LNS_advance_pc.
502 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000503 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000504
505 if (NeedCopy)
506 OS << char(dwarf::DW_LNS_copy);
507 else
508 OS << char(Temp);
509}
510
Kevin Enderbye7739d42011-12-09 18:09:40 +0000511// Utility function to write a tuple for .debug_abbrev.
512static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
513 MCOS->EmitULEB128IntValue(Name);
514 MCOS->EmitULEB128IntValue(Form);
515}
516
517// When generating dwarf for assembly source files this emits
518// the data for .debug_abbrev section which contains three DIEs.
519static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
520 MCContext &context = MCOS->getContext();
521 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
522
523 // DW_TAG_compile_unit DIE abbrev (1).
524 MCOS->EmitULEB128IntValue(1);
525 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
526 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
527 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
Oliver Stannard14f97d02014-09-22 10:45:16 +0000528 if (MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
529 MCOS->getContext().getDwarfVersion() >= 3) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000530 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4);
531 } else {
532 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
533 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
534 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000535 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000536 if (!context.getCompilationDir().empty())
537 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000538 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
539 if (!DwarfDebugFlags.empty())
540 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
541 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
542 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
543 EmitAbbrev(MCOS, 0, 0);
544
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000545 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000546 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000547 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000548 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
549 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
550 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
551 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
552 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000553 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
554 EmitAbbrev(MCOS, 0, 0);
555
556 // DW_TAG_unspecified_parameters DIE abbrev (3).
557 MCOS->EmitULEB128IntValue(3);
558 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
559 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
560 EmitAbbrev(MCOS, 0, 0);
561
562 // Terminate the abbreviations for this compilation unit.
563 MCOS->EmitIntValue(0, 1);
564}
565
566// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000567// .debug_aranges section. This section contains a header and a table of pairs
568// of PointerSize'ed values for the address and size of section(s) with line
569// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000570static void EmitGenDwarfAranges(MCStreamer *MCOS,
571 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000572 MCContext &context = MCOS->getContext();
573
Oliver Stannard8b273082014-06-19 15:52:37 +0000574 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000575
576 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
577
578 // This will be the length of the .debug_aranges section, first account for
579 // the size of each item in the header (see below where we emit these items).
580 int Length = 4 + 2 + 4 + 1 + 1;
581
582 // Figure the padding after the header before the table of address and size
583 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000584 const MCAsmInfo *asmInfo = context.getAsmInfo();
585 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000586 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
587 if (Pad == 2 * AddrSize)
588 Pad = 0;
589 Length += Pad;
590
591 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000592 // of each section we have in the table.
593 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000594 // And the pair of terminating zeros.
595 Length += 2 * AddrSize;
596
597
598 // Emit the header for this section.
599 // The 4 byte length not including the 4 byte value for the length.
600 MCOS->EmitIntValue(Length - 4, 4);
601 // The 2 byte version, which is 2.
602 MCOS->EmitIntValue(2, 2);
603 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000604 // of the .debug_info.
605 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000606 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
607 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000608 else
609 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000610 // The 1 byte size of an address.
611 MCOS->EmitIntValue(AddrSize, 1);
612 // The 1 byte size of a segment descriptor, we use a value of zero.
613 MCOS->EmitIntValue(0, 1);
614 // Align the header with the padding if needed, before we put out the table.
615 for(int i = 0; i < Pad; i++)
616 MCOS->EmitIntValue(0, 1);
617
Oliver Stannard8b273082014-06-19 15:52:37 +0000618 // Now emit the table of pairs of PointerSize'ed values for the section
619 // addresses and sizes.
620 for (const auto &sec : Sections) {
Eric Christopherd29430d2014-06-19 20:00:09 +0000621 MCSymbol *StartSymbol = sec.second.first;
622 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000623 assert(StartSymbol && "StartSymbol must not be NULL");
624 assert(EndSymbol && "EndSymbol must not be NULL");
625
626 const MCExpr *Addr = MCSymbolRefExpr::Create(
627 StartSymbol, MCSymbolRefExpr::VK_None, context);
628 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
629 *StartSymbol, *EndSymbol, 0);
630 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000631 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000632 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633
634 // And finally the pair of terminating zeros.
635 MCOS->EmitIntValue(0, AddrSize);
636 MCOS->EmitIntValue(0, AddrSize);
637}
638
639// When generating dwarf for assembly source files this emits the data for
640// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000641// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000642static void EmitGenDwarfInfo(MCStreamer *MCOS,
643 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000644 const MCSymbol *LineSectionSymbol,
645 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000646 MCContext &context = MCOS->getContext();
647
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000648 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000649
650 // Create a symbol at the start and end of this section used in here for the
651 // expression to calculate the length in the header.
652 MCSymbol *InfoStart = context.CreateTempSymbol();
653 MCOS->EmitLabel(InfoStart);
654 MCSymbol *InfoEnd = context.CreateTempSymbol();
655
656 // First part: the header.
657
658 // The 4 byte total length of the information for this compilation unit, not
659 // including these 4 bytes.
660 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000661 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000662
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000663 // The 2 byte DWARF version.
664 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000665
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000666 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000667 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
668 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000669 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000670 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000671 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000672 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
673 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000674
Bill Wendlingbc07a892013-06-18 07:20:20 +0000675 const MCAsmInfo *asmInfo = context.getAsmInfo();
676 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000677 // The 1 byte size of an address.
678 MCOS->EmitIntValue(AddrSize, 1);
679
680 // Second part: the compile_unit DIE.
681
682 // The DW_TAG_compile_unit DIE abbrev (1).
683 MCOS->EmitULEB128IntValue(1);
684
685 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
686 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000687 if (LineSectionSymbol)
688 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
689 AsmInfo.needsDwarfSectionOffsetDirective());
690 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000691 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000692
Oliver Stannard8b273082014-06-19 15:52:37 +0000693 if (RangesSectionSymbol) {
694 // There are multiple sections containing code, so we must use the
695 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000696
Oliver Stannard8b273082014-06-19 15:52:37 +0000697 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
698 // to the address range list for this compilation unit.
699 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
700 } else {
701 // If we only have one non-empty code section, we can use the simpler
702 // AT_low_pc and AT_high_pc attributes.
703
704 // Find the first (and only) non-empty text section
705 auto &Sections = context.getGenDwarfSectionSyms();
706 const auto TextSection = Sections.begin();
707 assert(TextSection != Sections.end() && "No text section found");
708
Eric Christopherd29430d2014-06-19 20:00:09 +0000709 MCSymbol *StartSymbol = TextSection->second.first;
710 MCSymbol *EndSymbol = TextSection->second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000711 assert(StartSymbol && "StartSymbol must not be NULL");
712 assert(EndSymbol && "EndSymbol must not be NULL");
713
714 // AT_low_pc, the first address of the default .text section.
715 const MCExpr *Start = MCSymbolRefExpr::Create(
716 StartSymbol, MCSymbolRefExpr::VK_None, context);
717 MCOS->EmitValue(Start, AddrSize);
718
719 // AT_high_pc, the last address of the default .text section.
720 const MCExpr *End = MCSymbolRefExpr::Create(
721 EndSymbol, MCSymbolRefExpr::VK_None, context);
722 MCOS->EmitValue(End, AddrSize);
723 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000724
725 // AT_name, the name of the source file. Reconstruct from the first directory
726 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000727 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000728 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000729 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000730 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000731 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000732 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000733 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000734 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000735 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
736
737 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000738 if (!context.getCompilationDir().empty()) {
739 MCOS->EmitBytes(context.getCompilationDir());
740 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
741 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000742
743 // AT_APPLE_flags, the command line arguments of the assembler tool.
744 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
745 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000746 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000747 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
748 }
749
750 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000751 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000752 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000753 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000754 else
755 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000756 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
757
758 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
759 // draft has no standard code for assembler.
760 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
761
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000762 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000763
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000764 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000765 const std::vector<MCGenDwarfLabelEntry> &Entries =
766 MCOS->getContext().getMCGenDwarfLabelEntries();
767 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000768 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000769 MCOS->EmitULEB128IntValue(2);
770
771 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000772 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
774
775 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000776 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777
778 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000779 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000780
781 // AT_low_pc, start address of the label.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000782 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000784 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785
Kevin Enderbye7739d42011-12-09 18:09:40 +0000786 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
787 MCOS->EmitIntValue(0, 1);
788
789 // The DW_TAG_unspecified_parameters DIE abbrev (3).
790 MCOS->EmitULEB128IntValue(3);
791
792 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
793 MCOS->EmitIntValue(0, 1);
794 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795
796 // Add the NULL DIE terminating the Compile Unit DIE's.
797 MCOS->EmitIntValue(0, 1);
798
799 // Now set the value of the symbol at the end of the info section.
800 MCOS->EmitLabel(InfoEnd);
801}
802
Oliver Stannard8b273082014-06-19 15:52:37 +0000803// When generating dwarf for assembly source files this emits the data for
804// .debug_ranges section. We only emit one range list, which spans all of the
805// executable sections of this file.
806static void EmitGenDwarfRanges(MCStreamer *MCOS) {
807 MCContext &context = MCOS->getContext();
808 auto &Sections = context.getGenDwarfSectionSyms();
809
810 const MCAsmInfo *AsmInfo = context.getAsmInfo();
811 int AddrSize = AsmInfo->getPointerSize();
812
813 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
814
815 for (const auto sec : Sections) {
816
Eric Christopherd29430d2014-06-19 20:00:09 +0000817 MCSymbol *StartSymbol = sec.second.first;
818 MCSymbol *EndSymbol = sec.second.second;
Oliver Stannard8b273082014-06-19 15:52:37 +0000819 assert(StartSymbol && "StartSymbol must not be NULL");
820 assert(EndSymbol && "EndSymbol must not be NULL");
821
822 // Emit a base address selection entry for the start of this section
823 const MCExpr *SectionStartAddr = MCSymbolRefExpr::Create(
824 StartSymbol, MCSymbolRefExpr::VK_None, context);
825 MCOS->EmitFill(AddrSize, 0xFF);
826 MCOS->EmitValue(SectionStartAddr, AddrSize);
827
828 // Emit a range list entry spanning this section
829 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
830 *StartSymbol, *EndSymbol, 0);
831 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000832 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000833 }
834
835 // Emit end of list entry
836 MCOS->EmitIntValue(0, AddrSize);
837 MCOS->EmitIntValue(0, AddrSize);
838}
839
Kevin Enderbye7739d42011-12-09 18:09:40 +0000840//
841// When generating dwarf for assembly source files this emits the Dwarf
842// sections.
843//
David Blaikie8bf66c42014-04-01 07:35:52 +0000844void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000845 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000846
847 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000848 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000849 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000850 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000851 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000852 if (CreateDwarfSectionSymbols)
853 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000854 MCSymbol *AbbrevSectionSymbol = nullptr;
855 MCSymbol *InfoSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000856 MCSymbol *RangesSectionSymbol = NULL;
857
858 // Create end symbols for each section, and remove empty sections
859 MCOS->getContext().finalizeDwarfSections(*MCOS);
860
861 // If there are no sections to generate debug info for, we don't need
862 // to do anything
863 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
864 return;
865
Oliver Stannard14f97d02014-09-22 10:45:16 +0000866 // We only use the .debug_ranges section if we have multiple code sections,
867 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000868 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000869 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
870 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000871 CreateDwarfSectionSymbols |= UseRangesSection;
872
Kevin Enderbye7739d42011-12-09 18:09:40 +0000873 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000874 if (CreateDwarfSectionSymbols) {
875 InfoSectionSymbol = context.CreateTempSymbol();
876 MCOS->EmitLabel(InfoSectionSymbol);
877 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000878 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000879 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000880 AbbrevSectionSymbol = context.CreateTempSymbol();
881 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000882 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000883 if (UseRangesSection) {
884 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
885 if (CreateDwarfSectionSymbols) {
886 RangesSectionSymbol = context.CreateTempSymbol();
887 MCOS->EmitLabel(RangesSectionSymbol);
888 }
889 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000890
Oliver Stannard8b273082014-06-19 15:52:37 +0000891 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
892
893 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000894
895 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000896 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000897
Oliver Stannard8b273082014-06-19 15:52:37 +0000898 if (UseRangesSection)
899 EmitGenDwarfRanges(MCOS);
900
Kevin Enderbye7739d42011-12-09 18:09:40 +0000901 // Output the data for .debug_abbrev section.
902 EmitGenDwarfAbbrev(MCOS);
903
904 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000905 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
906 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000907}
908
909//
910// When generating dwarf for assembly source files this is called when symbol
911// for a label is created. If this symbol is not a temporary and is in the
912// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000913// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000914//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000915void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000916 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000917 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000918 if (Symbol->isTemporary())
919 return;
920 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000921 // We won't create dwarf labels for symbols in sections that we are not
922 // generating debug info for.
923 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000924 return;
925
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000926 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000927 // underbar if any.
928 StringRef Name = Symbol->getName();
929 if (Name.startswith("_"))
930 Name = Name.substr(1, Name.size()-1);
931
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000932 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000933 unsigned FileNumber = context.getGenDwarfFileNumber();
934
935 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000936 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000937 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000938 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
939
940 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
941 // values so that they don't have things like an ARM thumb bit from the
942 // original symbol. So when used they won't get a low bit set after
943 // relocation.
944 MCSymbol *Label = context.CreateTempSymbol();
945 MCOS->EmitLabel(Label);
946
947 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000948 MCOS->getContext().addMCGenDwarfLabelEntry(
949 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000950}
951
Rafael Espindola0a017a62010-12-10 07:39:47 +0000952static int getDataAlignmentFactor(MCStreamer &streamer) {
953 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000954 const MCAsmInfo *asmInfo = context.getAsmInfo();
955 int size = asmInfo->getCalleeSaveStackSlotSize();
956 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000957 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000958 else
959 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000960}
961
Rafael Espindola5395f442011-04-22 00:08:43 +0000962static unsigned getSizeForEncoding(MCStreamer &streamer,
963 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000964 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000965 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000966 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000967 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000968 case dwarf::DW_EH_PE_absptr:
969 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000970 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000971 case dwarf::DW_EH_PE_udata2:
972 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000973 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000974 case dwarf::DW_EH_PE_udata4:
975 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000976 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000977 case dwarf::DW_EH_PE_udata8:
978 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000979 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000980 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000981}
982
Rafael Espindolafe963b12014-08-15 03:07:13 +0000983static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
984 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000985 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000986 const MCAsmInfo *asmInfo = context.getAsmInfo();
987 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
988 symbolEncoding,
989 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000990 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000991 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000992 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000993 else
994 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000995}
996
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000997static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
998 unsigned symbolEncoding) {
999 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001000 const MCAsmInfo *asmInfo = context.getAsmInfo();
1001 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
1002 symbolEncoding,
1003 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001004 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001005 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001006}
1007
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001008namespace {
1009 class FrameEmitterImpl {
1010 int CFAOffset;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001011 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +00001012 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001013 public:
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001014 FrameEmitterImpl(bool isEH)
Rafael Espindola1c509712014-08-20 17:33:44 +00001015 : CFAOffset(0), IsEH(isEH), SectionStart(nullptr) {}
Bill Wendling4d9aa512011-07-22 21:18:59 +00001016
1017 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001018
Rafael Espindolafe963b12014-08-15 03:07:13 +00001019 /// Emit the unwind information in a compact way.
1020 void EmitCompactUnwind(MCObjectStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001021 const MCDwarfFrameInfo &frame);
1022
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001023 const MCSymbol &EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001024 const MCSymbol *personality,
1025 unsigned personalityEncoding,
1026 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001027 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001028 unsigned lsdaEncoding,
1029 bool IsSimple);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001030 MCSymbol *EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001031 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001032 const MCDwarfFrameInfo &frame);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001033 void EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001034 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001035 MCSymbol *BaseLabel);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001036 void EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001037 const MCCFIInstruction &Instr);
1038 };
Bill Wendling567a1ae2011-06-30 21:25:51 +00001039
1040} // end anonymous namespace
1041
Rafael Espindolafe963b12014-08-15 03:07:13 +00001042static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001043 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001044}
1045
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001046void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001047 const MCCFIInstruction &Instr) {
1048 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001049 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001050
1051 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001052 case MCCFIInstruction::OpRegister: {
1053 unsigned Reg1 = Instr.getRegister();
1054 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001055 if (!IsEH) {
1056 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1057 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1058 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001059 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1060 Streamer.EmitULEB128IntValue(Reg1);
1061 Streamer.EmitULEB128IntValue(Reg2);
1062 return;
1063 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001064 case MCCFIInstruction::OpWindowSave: {
1065 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1066 return;
1067 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001068 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001069 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1071 Streamer.EmitULEB128IntValue(Reg);
1072 return;
1073 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001074 case MCCFIInstruction::OpAdjustCfaOffset:
1075 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001076 const bool IsRelative =
1077 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1078
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001079 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1080
1081 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001082 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001083 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001084 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001085
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001086 Streamer.EmitULEB128IntValue(CFAOffset);
1087
1088 return;
1089 }
1090 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001091 unsigned Reg = Instr.getRegister();
1092 if (!IsEH)
1093 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001094 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001095 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001096 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001097 Streamer.EmitULEB128IntValue(CFAOffset);
1098
1099 return;
1100 }
1101
1102 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001103 unsigned Reg = Instr.getRegister();
1104 if (!IsEH)
1105 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001106 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001107 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001108
1109 return;
1110 }
1111
1112 case MCCFIInstruction::OpOffset:
1113 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001114 const bool IsRelative =
1115 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001116
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001117 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001118 if (!IsEH)
1119 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1120
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001121 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001122 if (IsRelative)
1123 Offset -= CFAOffset;
1124 Offset = Offset / dataAlignmentFactor;
1125
1126 if (Offset < 0) {
1127 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1128 Streamer.EmitULEB128IntValue(Reg);
1129 Streamer.EmitSLEB128IntValue(Offset);
1130 } else if (Reg < 64) {
1131 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001132 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001133 } else {
1134 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001135 Streamer.EmitULEB128IntValue(Reg);
1136 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001137 }
1138 return;
1139 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001140 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001141 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1142 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001143 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001144 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1145 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001146 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001147 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001148 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001149 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001150 return;
1151 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001152 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001153 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001154 if (!IsEH)
1155 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001156 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1157 return;
1158 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001159 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001160 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001161 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001162 }
1163 llvm_unreachable("Unhandled case in switch");
1164}
1165
Rafael Espindolafe963b12014-08-15 03:07:13 +00001166/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001167void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001168 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001169 MCSymbol *BaseLabel) {
1170 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1171 const MCCFIInstruction &Instr = Instrs[i];
1172 MCSymbol *Label = Instr.getLabel();
1173 // Throw out move if the label is invalid.
1174 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1175
1176 // Advance row if new location.
1177 if (BaseLabel && Label) {
1178 MCSymbol *ThisSym = Label;
1179 if (ThisSym != BaseLabel) {
1180 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1181 BaseLabel = ThisSym;
1182 }
1183 }
1184
1185 EmitCFIInstruction(streamer, Instr);
1186 }
1187}
1188
Rafael Espindolafe963b12014-08-15 03:07:13 +00001189/// Emit the unwind information in a compact way.
1190void FrameEmitterImpl::EmitCompactUnwind(MCObjectStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001191 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001192 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001193 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001194
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001195 // range-start range-length compact-unwind-enc personality-func lsda
1196 // _foo LfooEnd-_foo 0x00000023 0 0
1197 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1198 //
1199 // .section __LD,__compact_unwind,regular,debug
1200 //
1201 // # compact unwind for _foo
1202 // .quad _foo
1203 // .set L1,LfooEnd-_foo
1204 // .long L1
1205 // .long 0x01010001
1206 // .quad 0
1207 // .quad 0
1208 //
1209 // # compact unwind for _bar
1210 // .quad _bar
1211 // .set L2,LbarEnd-_bar
1212 // .long L2
1213 // .long 0x01020011
1214 // .quad __gxx_personality
1215 // .quad except_tab1
1216
Bill Wendling49bc6802011-07-19 00:06:12 +00001217 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001218 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001219 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001220
Bill Wendlingdafd5982011-07-14 23:34:45 +00001221 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001222 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001223 Encoding |= 0x40000000;
1224
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001225 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001226 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001227 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001228 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001229
1230 // Range Length
1231 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1232 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001233 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001234
Bill Wendling75174662011-06-30 00:30:52 +00001235 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001236 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1237 Streamer.EmitIntValue(Encoding, Size);
1238
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001239 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001240 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001241 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001242 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001243 else
1244 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001245
1246 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001247 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001248 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001249 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001250 else
1251 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001252}
1253
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001254const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001255 const MCSymbol *personality,
1256 unsigned personalityEncoding,
1257 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001258 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001259 unsigned lsdaEncoding,
1260 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001261 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001262 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001263 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001264
Rafael Espindolab4076b292014-06-20 23:54:32 +00001265 MCSymbol *sectionStart = context.CreateTempSymbol();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001266 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001267
Bill Wendlingb6adf462011-07-07 00:54:13 +00001268 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001269
1270 // Length
1271 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1272 *sectionEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001273 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001274
1275 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001276 unsigned CIE_ID = IsEH ? 0 : -1;
1277 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001278
1279 // Version
Oliver Stannardf7693f42014-06-19 15:39:33 +00001280 // For DWARF2, we use CIE version 1
1281 // For DWARF3+, we use CIE version 3
1282 uint8_t CIEVersion = context.getDwarfVersion() <= 2 ? 1 : 3;
1283 streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001284
1285 // Augmentation String
1286 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001287 if (IsEH) {
1288 Augmentation += "z";
1289 if (personality)
1290 Augmentation += "P";
1291 if (lsda)
1292 Augmentation += "L";
1293 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001294 if (IsSignalFrame)
1295 Augmentation += "S";
Yaron Keren92e1b622015-03-18 10:17:07 +00001296 streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001297 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001298 streamer.EmitIntValue(0, 1);
1299
1300 // Code Alignment Factor
Bill Wendlingbc07a892013-06-18 07:20:20 +00001301 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001302
1303 // Data Alignment Factor
1304 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1305
1306 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001307 if (CIEVersion == 1) {
1308 assert(MRI->getRARegister() <= 255 &&
1309 "DWARF 2 encodes return_address_register in one byte");
Frederic Rissadbb3f22015-02-26 19:48:07 +00001310 streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001311 } else {
1312 streamer.EmitULEB128IntValue(
Frederic Rissadbb3f22015-02-26 19:48:07 +00001313 MRI->getDwarfRegNum(MRI->getRARegister(), IsEH));
Oliver Stannardf7693f42014-06-19 15:39:33 +00001314 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001315
1316 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001317
1318 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001319 if (IsEH) {
1320 if (personality) {
1321 // Personality Encoding
1322 augmentationLength += 1;
1323 // Personality
1324 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1325 }
1326 if (lsda)
1327 augmentationLength += 1;
1328 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001329 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001330
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001331 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001332
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001333 // Augmentation Data (optional)
1334 if (personality) {
1335 // Personality Encoding
Rafael Espindolafe963b12014-08-15 03:07:13 +00001336 emitEncodingByte(streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001337 // Personality
1338 EmitPersonality(streamer, *personality, personalityEncoding);
1339 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001340
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001341 if (lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001342 emitEncodingByte(streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001343
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001344 // Encoding of the FDE pointers
Rafael Espindolafe963b12014-08-15 03:07:13 +00001345 emitEncodingByte(streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001346 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001347
1348 // Initial Instructions
1349
Bill Wendlingbc07a892013-06-18 07:20:20 +00001350 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001351 if (!IsSimple) {
1352 const std::vector<MCCFIInstruction> &Instructions =
1353 MAI->getInitialFrameState();
Craig Topperbb694de2014-04-13 04:57:38 +00001354 EmitCFIInstructions(streamer, Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001355 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001356
1357 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001358 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001359
1360 streamer.EmitLabel(sectionEnd);
1361 return *sectionStart;
1362}
1363
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001364MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001365 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001366 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001367 MCContext &context = streamer.getContext();
1368 MCSymbol *fdeStart = context.CreateTempSymbol();
1369 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001370 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001371
1372 // Length
1373 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001374 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001375
1376 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001377
Rafael Espindola0a017a62010-12-10 07:39:47 +00001378 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001379 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001380 if (IsEH) {
1381 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1382 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001383 emitAbsValue(streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001384 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001385 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1386 cieStart, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001387 emitAbsValue(streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001388 } else {
1389 streamer.EmitSymbolValue(&cieStart, 4);
1390 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001391
Rafael Espindola0a017a62010-12-10 07:39:47 +00001392 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001393 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001394 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Nick Lewycky333449c2012-08-12 08:09:45 +00001395 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Rafael Espindolafe963b12014-08-15 03:07:13 +00001396 emitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001397
1398 // PC Range
1399 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1400 *frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001401 emitAbsValue(streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001402
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001403 if (IsEH) {
1404 // Augmentation Data Length
1405 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001406
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001407 if (frame.Lsda)
1408 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001409
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001410 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001411
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001412 // Augmentation Data
1413 if (frame.Lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001414 emitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001415 }
Rafael Espindola68067662011-04-29 03:06:29 +00001416
Rafael Espindola0a017a62010-12-10 07:39:47 +00001417 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001418 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001419
Rafael Espindola0a017a62010-12-10 07:39:47 +00001420 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001421 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001422
1423 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001424}
1425
Benjamin Kramer570dd782010-12-30 22:34:44 +00001426namespace {
1427 struct CIEKey {
Craig Topperbb694de2014-04-13 04:57:38 +00001428 static const CIEKey getEmptyKey() {
1429 return CIEKey(nullptr, 0, -1, false, false);
1430 }
1431 static const CIEKey getTombstoneKey() {
1432 return CIEKey(nullptr, -1, 0, false, false);
1433 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001434
Eric Christopherd29430d2014-06-19 20:00:09 +00001435 CIEKey(const MCSymbol *Personality_, unsigned PersonalityEncoding_,
1436 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_)
1437 : Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1438 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1439 IsSimple(IsSimple_) {}
1440 const MCSymbol *Personality;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001441 unsigned PersonalityEncoding;
1442 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001443 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001444 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001445 };
1446}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001447
1448namespace llvm {
1449 template <>
1450 struct DenseMapInfo<CIEKey> {
1451 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001452 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001453 }
1454 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001455 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001456 }
1457 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001458 return static_cast<unsigned>(hash_combine(Key.Personality,
1459 Key.PersonalityEncoding,
1460 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001461 Key.IsSignalFrame,
1462 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001463 }
1464 static bool isEqual(const CIEKey &LHS,
1465 const CIEKey &RHS) {
1466 return LHS.Personality == RHS.Personality &&
1467 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001468 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001469 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1470 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001471 }
1472 };
1473}
1474
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001475void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001476 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001477 Streamer.generateCompactUnwindEncodings(MAB);
1478
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001479 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001480 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001481 FrameEmitterImpl Emitter(IsEH);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001482 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001483
Bill Wendling9803abb2011-09-06 18:37:11 +00001484 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001485 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001486 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001487 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001488 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1489 const MCDwarfFrameInfo &Frame = FrameArray[i];
1490 if (Frame.CompactUnwindEncoding == 0) continue;
1491 if (!SectionEmitted) {
1492 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001493 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001494 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001495 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001496 NeedsEHFrameSection |=
1497 Frame.CompactUnwindEncoding ==
1498 MOFI->getCompactUnwindDwarfEHFrameOnly();
Bob Wilson0e180f22013-05-07 20:56:33 +00001499 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001500 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001501 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001502
Tim Northoverd1c6f512014-03-29 09:03:13 +00001503 if (!NeedsEHFrameSection) return;
1504
Bill Wendlinga800f952013-05-30 18:52:57 +00001505 const MCSection &Section =
1506 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1507 *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001508
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001509 Streamer.SwitchSection(&Section);
1510 MCSymbol *SectionStart = Context.CreateTempSymbol();
1511 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001512 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001513
Craig Topperbb694de2014-04-13 04:57:38 +00001514 MCSymbol *FDEEnd = nullptr;
Eric Christopherd29430d2014-06-19 20:00:09 +00001515 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001516
Craig Topperbb694de2014-04-13 04:57:38 +00001517 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001518 NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001519 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1520 const MCDwarfFrameInfo &Frame = FrameArray[i];
Tim Northoverd1c6f512014-03-29 09:03:13 +00001521
1522 // Emit the label from the previous iteration
1523 if (FDEEnd) {
1524 Streamer.EmitLabel(FDEEnd);
Craig Topperbb694de2014-04-13 04:57:38 +00001525 FDEEnd = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001526 }
1527
1528 if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding !=
1529 MOFI->getCompactUnwindDwarfEHFrameOnly())
1530 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1531 // of by the compact unwind encoding.
1532 continue;
1533
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001534 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001535 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001536 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1537 if (!CIEStart)
1538 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1539 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001540 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001541 Frame.LsdaEncoding,
1542 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001543
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001544 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001545 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001546
Bill Wendlingbc07a892013-06-18 07:20:20 +00001547 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001548 if (FDEEnd)
1549 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001550}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001551
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001552void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001553 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001554 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001555 SmallString<256> Tmp;
1556 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001557 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001558 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001559}
1560
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001561void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1562 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001563 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001564 // Scale the address delta by the minimum instruction length.
1565 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1566
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001567 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001568 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001569 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1570 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001571 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001572 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1573 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001574 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001575 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001576 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001577 OS << uint8_t( AddrDelta & 0xff);
1578 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001579 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001580 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001581 assert(isUInt<32>(AddrDelta));
1582 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001583 OS << uint8_t( AddrDelta & 0xff);
1584 OS << uint8_t((AddrDelta >> 8) & 0xff);
1585 OS << uint8_t((AddrDelta >> 16) & 0xff);
1586 OS << uint8_t((AddrDelta >> 24) & 0xff);
1587
Rafael Espindola736a35d2010-12-28 05:39:27 +00001588 }
1589}