blob: 98d78737cc6bee961b4d879026797e8a6e10ba70 [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"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/Twine.h"
14#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000018#include "llvm/MC/MCObjectFileInfo.h"
Evan Cheng76792992011-07-20 05:58:47 +000019#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindola556f2032010-11-22 11:53:17 +000020#include "llvm/MC/MCStreamer.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000021#include "llvm/MC/MCSymbol.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000022#include "llvm/Support/Debug.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000023#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000024#include "llvm/Support/LEB128.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000025#include "llvm/Support/Path.h"
26#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/raw_ostream.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000028using namespace llvm;
29
Kevin Enderbye46564a2010-09-30 16:52:03 +000030// Given a special op, return the address skip amount (in units of
31// DWARF2_LINE_MIN_INSN_LENGTH.
32#define SPECIAL_ADDR(op) (((op) - DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)
33
34// The maximum address skip amount that can be encoded with a special op.
Bill Wendlingc737ac12011-06-30 23:59:38 +000035#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbye46564a2010-09-30 16:52:03 +000036
37// First special line opcode - leave room for the standard opcodes.
38// Note: If you want to change this, you'll have to update the
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000039// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc737ac12011-06-30 23:59:38 +000040#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbye46564a2010-09-30 16:52:03 +000041
42// Minimum line offset in a special line info. opcode. This value
43// was chosen to give a reasonable range of values.
Bill Wendlingc737ac12011-06-30 23:59:38 +000044#define DWARF2_LINE_BASE -5
Kevin Enderbye46564a2010-09-30 16:52:03 +000045
46// Range of line offsets in a special line info. opcode.
Bill Wendlingc737ac12011-06-30 23:59:38 +000047#define DWARF2_LINE_RANGE 14
Kevin Enderbye46564a2010-09-30 16:52:03 +000048
Ulrich Weigand32d725b2013-06-12 14:46:54 +000049static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000050 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000051 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000052 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000053 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000054 // TODO: report this error, but really only once.
55 ;
56 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000057 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000058}
59
60//
61// This is called when an instruction is assembled into the specified section
62// and if there is information from the last .loc directive that has yet to have
63// a line entry made for it is made.
64//
Rafael Espindolab58867c2010-11-19 02:26:16 +000065void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000066 if (!MCOS->getContext().getDwarfLocSeen())
67 return;
68
69 // Create a symbol at in the current section for use in the line entry.
70 MCSymbol *LineSym = MCOS->getContext().CreateTempSymbol();
71 // Set the value of the symbol to use for the MCLineEntry.
72 MCOS->EmitLabel(LineSym);
73
74 // Get the current .loc info saved in the context.
75 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
76
77 // Create a (local) line entry with the symbol and the current .loc info.
78 MCLineEntry LineEntry(LineSym, DwarfLoc);
79
80 // clear DwarfLocSeen saying the current .loc info is now used.
Kevin Enderbya68d0042010-10-04 20:17:24 +000081 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000082
83 // Get the MCLineSection for this section, if one does not exist for this
84 // section create it.
Rafael Espindola9900b482010-11-19 07:41:23 +000085 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbye46564a2010-09-30 16:52:03 +000086 MCOS->getContext().getMCLineSections();
Rafael Espindola9900b482010-11-19 07:41:23 +000087 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000088 if (!LineSection) {
89 // Create a new MCLineSection. This will be deleted after the dwarf line
90 // table is created using it by iterating through the MCLineSections
91 // DenseMap.
92 LineSection = new MCLineSection;
93 // Save a pointer to the new LineSection into the MCLineSections DenseMap.
Rafael Espindola9900b482010-11-19 07:41:23 +000094 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbye46564a2010-09-30 16:52:03 +000095 }
96
97 // Add the line entry to this section's entries.
Manman Ren4e042a62013-02-05 21:52:47 +000098 LineSection->addLineEntry(LineEntry,
99 MCOS->getContext().getDwarfCompileUnitID());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000100}
101
102//
103// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000104//
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000105static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
106 const MCSymbol &Start,
107 const MCSymbol &End,
108 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000109 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
110 const MCExpr *Res =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000111 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000112 const MCExpr *RHS =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000113 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000114 const MCExpr *Res1 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000115 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000116 const MCExpr *Res2 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000117 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000118 const MCExpr *Res3 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000119 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000120 return Res3;
121}
122
Kevin Enderbye46564a2010-09-30 16:52:03 +0000123//
124// This emits the Dwarf line table for the specified section from the entries
125// in the LineSection.
126//
Rafael Espindolab58867c2010-11-19 02:26:16 +0000127static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000128 const MCSection *Section,
Manman Ren4e042a62013-02-05 21:52:47 +0000129 const MCLineSection *LineSection,
130 unsigned CUID) {
131 // This LineSection does not contain any LineEntry for the given Compile Unit.
132 if (!LineSection->containEntriesForID(CUID))
133 return;
134
Kevin Enderbye46564a2010-09-30 16:52:03 +0000135 unsigned FileNum = 1;
136 unsigned LastLine = 1;
137 unsigned Column = 0;
138 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
139 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000140 unsigned Discriminator = 0;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000141 MCSymbol *LastLabel = NULL;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000142
143 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola9900b482010-11-19 07:41:23 +0000144 for (MCLineSection::const_iterator
Manman Ren4e042a62013-02-05 21:52:47 +0000145 it = LineSection->getMCLineEntries(CUID).begin(),
146 ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000147
148 if (FileNum != it->getFileNum()) {
149 FileNum = it->getFileNum();
150 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000151 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000152 }
153 if (Column != it->getColumn()) {
154 Column = it->getColumn();
155 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000156 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000157 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000158 if (Discriminator != it->getDiscriminator()) {
159 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000160 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000161 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
162 MCOS->EmitULEB128IntValue(Size + 1);
163 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
164 MCOS->EmitULEB128IntValue(Discriminator);
165 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000166 if (Isa != it->getIsa()) {
167 Isa = it->getIsa();
168 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000169 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000170 }
171 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
172 Flags = it->getFlags();
173 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
174 }
175 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
176 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
177 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
178 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
179 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
180 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
181
Rafael Espindola1d37f352010-11-13 01:06:27 +0000182 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000183 MCSymbol *Label = it->getLabel();
184
185 // At this point we want to emit/create the sequence to encode the delta in
186 // line numbers and the increment of the address from the previous Label
187 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000188 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000189 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000190 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000191
192 LastLine = it->getLine();
193 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000194 }
195
196 // Emit a DW_LNE_end_sequence for the end of the section.
197 // Using the pointer Section create a temporary label at the end of the
198 // section and use that and the LastLabel to compute the address delta
199 // and use INT64_MAX as the line delta which is the signal that this is
200 // actually a DW_LNE_end_sequence.
201
202 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000203 // TODO: keep track of the last subsection so that this symbol appears in the
204 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000205 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000206
207 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000208 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000209 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000210 // Set the value of the symbol, as we are at the end of the section.
211 MCOS->EmitLabel(SectionEnd);
212
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000213 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000214 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000215
Bill Wendlingbc07a892013-06-18 07:20:20 +0000216 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000217 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000218 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000219}
220
221//
222// This emits the Dwarf file and the line tables.
223//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000224const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000225 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000226 // Switch to the section where the table will be emitted into.
Evan Cheng76792992011-07-20 05:58:47 +0000227 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000228
Manman Ren4e042a62013-02-05 21:52:47 +0000229 const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
230 MCOS->getContext().getMCLineTableSymbols();
231 // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
232 // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
233 // Handle Compile Unit 0, the line table start symbol is the section symbol.
234 const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
235 // Handle the rest of the Compile Units.
236 for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
237 EmitCU(MCOS, Is);
238
239 // Now delete the MCLineSections that were created in MCLineEntry::Make()
240 // and used to emit the line table.
241 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
242 MCOS->getContext().getMCLineSections();
243 for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
244 MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
245 ++it)
246 delete it->second;
247
248 return LineStartSym;
249}
250
251const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
252 MCContext &context = MCOS->getContext();
253
254 // Create a symbol at the beginning of the line table.
255 MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
256 if (!LineStartSym)
257 LineStartSym = context.CreateTempSymbol();
258 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000259 MCOS->EmitLabel(LineStartSym);
260
261 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000262 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000263
264 // The first 4 bytes is the total length of the information for this
265 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000266 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000267 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000268
269 // Next 2 bytes is the Version, which is Dwarf 2.
270 MCOS->EmitIntValue(2, 2);
271
272 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000273 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000274
275 // Length of the prologue, is the next 4 bytes. Which is the start of the
276 // section to the end of the prologue. Not including the 4 bytes for the
277 // total length, the 2 bytes for the version, and these 4 bytes for the
278 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000279 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000280 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000281
282 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000283 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000284 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
285 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
286 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
287 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
288
289 // Standard opcode lengths
290 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
291 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
292 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
293 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
294 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
295 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
296 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
297 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
298 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
299 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
300 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
301 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
302
303 // Put out the directory and file tables.
304
305 // First the directory table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000306 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Manman Ren1e427202013-03-07 01:42:00 +0000307 context.getMCDwarfDirs(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000309 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
310 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311 }
312 MCOS->EmitIntValue(0, 1); // Terminate the directory list
313
314 // Second the file table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000315 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Manman Ren1e427202013-03-07 01:42:00 +0000316 MCOS->getContext().getMCDwarfFiles(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000317 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000318 MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
319 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000320 // the Directory num
321 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000322 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
323 MCOS->EmitIntValue(0, 1); // filesize (always 0)
324 }
325 MCOS->EmitIntValue(0, 1); // Terminate the file list
326
327 // This is the end of the prologue, so set the value of the symbol at the
328 // end of the prologue (that was used in a previous expression).
329 MCOS->EmitLabel(ProEndSym);
330
331 // Put out the line tables.
Rafael Espindola9900b482010-11-19 07:41:23 +0000332 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbye46564a2010-09-30 16:52:03 +0000333 MCOS->getContext().getMCLineSections();
Rafael Espindola9900b482010-11-19 07:41:23 +0000334 const std::vector<const MCSection *> &MCLineSectionOrder =
335 MCOS->getContext().getMCLineSectionOrder();
336 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc737ac12011-06-30 23:59:38 +0000337 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola9900b482010-11-19 07:41:23 +0000338 ++it) {
339 const MCSection *Sec = *it;
340 const MCLineSection *Line = MCLineSections.lookup(Sec);
Manman Ren4e042a62013-02-05 21:52:47 +0000341 EmitDwarfLineTable(MCOS, Sec, Line, CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000342 }
343
Bill Wendlingbc07a892013-06-18 07:20:20 +0000344 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines()
Rafael Espindola1048e752010-12-04 00:31:13 +0000345 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000346 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000347 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000348 // total_length >= prologue_length + 10
349 // We are 4 bytes short, since we have total_length = 51 and
350 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000351
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000352 // The regular end_sequence should be sufficient.
353 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000354 }
355
Kevin Enderbye46564a2010-09-30 16:52:03 +0000356 // This is the end of the section, so set the value of the symbol at the end
357 // of this section (that was used in a previous expression).
358 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000359
360 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000361}
362
Kevin Enderbye46564a2010-09-30 16:52:03 +0000363/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000364void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000365 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000366 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000367 SmallString<256> Tmp;
368 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000369 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000370 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000371}
372
373/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000374void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
375 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000376 uint64_t Temp, Opcode;
377 bool NeedCopy = false;
378
379 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000380 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000381
382 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000383 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000384 // end_sequence to emit the matrix entry.
385 if (LineDelta == INT64_MAX) {
386 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
387 OS << char(dwarf::DW_LNS_const_add_pc);
388 else {
389 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000390 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000391 }
392 OS << char(dwarf::DW_LNS_extended_op);
393 OS << char(1);
394 OS << char(dwarf::DW_LNE_end_sequence);
395 return;
396 }
397
398 // Bias the line delta by the base.
399 Temp = LineDelta - DWARF2_LINE_BASE;
400
401 // If the line increment is out of range of a special opcode, we must encode
402 // it with DW_LNS_advance_line.
403 if (Temp >= DWARF2_LINE_RANGE) {
404 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000405 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000406
407 LineDelta = 0;
408 Temp = 0 - DWARF2_LINE_BASE;
409 NeedCopy = true;
410 }
411
412 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
413 if (LineDelta == 0 && AddrDelta == 0) {
414 OS << char(dwarf::DW_LNS_copy);
415 return;
416 }
417
418 // Bias the opcode by the special opcode base.
419 Temp += DWARF2_LINE_OPCODE_BASE;
420
421 // Avoid overflow when addr_delta is large.
422 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
423 // Try using a special opcode.
424 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
425 if (Opcode <= 255) {
426 OS << char(Opcode);
427 return;
428 }
429
430 // Try using DW_LNS_const_add_pc followed by special op.
431 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
432 if (Opcode <= 255) {
433 OS << char(dwarf::DW_LNS_const_add_pc);
434 OS << char(Opcode);
435 return;
436 }
437 }
438
439 // Otherwise use DW_LNS_advance_pc.
440 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000441 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000442
443 if (NeedCopy)
444 OS << char(dwarf::DW_LNS_copy);
445 else
446 OS << char(Temp);
447}
448
Kevin Enderbye5930f12010-07-28 20:55:35 +0000449void MCDwarfFile::print(raw_ostream &OS) const {
450 OS << '"' << getName() << '"';
451}
452
Manman Ren49d684e2012-09-12 05:06:18 +0000453#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderbye5930f12010-07-28 20:55:35 +0000454void MCDwarfFile::dump() const {
455 print(dbgs());
456}
Manman Renc3366cc2012-09-06 19:55:56 +0000457#endif
Kevin Enderbye46564a2010-09-30 16:52:03 +0000458
Kevin Enderbye7739d42011-12-09 18:09:40 +0000459// Utility function to write a tuple for .debug_abbrev.
460static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
461 MCOS->EmitULEB128IntValue(Name);
462 MCOS->EmitULEB128IntValue(Form);
463}
464
465// When generating dwarf for assembly source files this emits
466// the data for .debug_abbrev section which contains three DIEs.
467static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
468 MCContext &context = MCOS->getContext();
469 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
470
471 // DW_TAG_compile_unit DIE abbrev (1).
472 MCOS->EmitULEB128IntValue(1);
473 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
474 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
475 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
476 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
477 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
478 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000479 if (!context.getCompilationDir().empty())
480 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000481 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
482 if (!DwarfDebugFlags.empty())
483 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
484 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
485 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
486 EmitAbbrev(MCOS, 0, 0);
487
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000488 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000489 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000490 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000491 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
492 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
493 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
494 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
495 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000496 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
497 EmitAbbrev(MCOS, 0, 0);
498
499 // DW_TAG_unspecified_parameters DIE abbrev (3).
500 MCOS->EmitULEB128IntValue(3);
501 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
502 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
503 EmitAbbrev(MCOS, 0, 0);
504
505 // Terminate the abbreviations for this compilation unit.
506 MCOS->EmitIntValue(0, 1);
507}
508
509// When generating dwarf for assembly source files this emits the data for
510// .debug_aranges section. Which contains a header and a table of pairs of
511// PointerSize'ed values for the address and size of section(s) with line table
512// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000513static void EmitGenDwarfAranges(MCStreamer *MCOS,
514 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000515 MCContext &context = MCOS->getContext();
516
517 // Create a symbol at the end of the section that we are creating the dwarf
518 // debugging info to use later in here as part of the expression to calculate
519 // the size of the section for the table.
520 MCOS->SwitchSection(context.getGenDwarfSection());
521 MCSymbol *SectionEndSym = context.CreateTempSymbol();
522 MCOS->EmitLabel(SectionEndSym);
523 context.setGenDwarfSectionEndSym(SectionEndSym);
524
525 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
526
527 // This will be the length of the .debug_aranges section, first account for
528 // the size of each item in the header (see below where we emit these items).
529 int Length = 4 + 2 + 4 + 1 + 1;
530
531 // Figure the padding after the header before the table of address and size
532 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000533 const MCAsmInfo *asmInfo = context.getAsmInfo();
534 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000535 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
536 if (Pad == 2 * AddrSize)
537 Pad = 0;
538 Length += Pad;
539
540 // Add the size of the pair of PointerSize'ed values for the address and size
541 // of the one default .text section we have in the table.
542 Length += 2 * AddrSize;
543 // And the pair of terminating zeros.
544 Length += 2 * AddrSize;
545
546
547 // Emit the header for this section.
548 // The 4 byte length not including the 4 byte value for the length.
549 MCOS->EmitIntValue(Length - 4, 4);
550 // The 2 byte version, which is 2.
551 MCOS->EmitIntValue(2, 2);
552 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000553 // of the .debug_info.
554 if (InfoSectionSymbol)
555 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
556 else
557 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000558 // The 1 byte size of an address.
559 MCOS->EmitIntValue(AddrSize, 1);
560 // The 1 byte size of a segment descriptor, we use a value of zero.
561 MCOS->EmitIntValue(0, 1);
562 // Align the header with the padding if needed, before we put out the table.
563 for(int i = 0; i < Pad; i++)
564 MCOS->EmitIntValue(0, 1);
565
566 // Now emit the table of pairs of PointerSize'ed values for the section(s)
567 // address and size, in our case just the one default .text section.
568 const MCExpr *Addr = MCSymbolRefExpr::Create(
569 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
570 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
571 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
572 MCOS->EmitAbsValue(Addr, AddrSize);
573 MCOS->EmitAbsValue(Size, AddrSize);
574
575 // And finally the pair of terminating zeros.
576 MCOS->EmitIntValue(0, AddrSize);
577 MCOS->EmitIntValue(0, AddrSize);
578}
579
580// When generating dwarf for assembly source files this emits the data for
581// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000582// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000583static void EmitGenDwarfInfo(MCStreamer *MCOS,
584 const MCSymbol *AbbrevSectionSymbol,
585 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000586 MCContext &context = MCOS->getContext();
587
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000588 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000589
590 // Create a symbol at the start and end of this section used in here for the
591 // expression to calculate the length in the header.
592 MCSymbol *InfoStart = context.CreateTempSymbol();
593 MCOS->EmitLabel(InfoStart);
594 MCSymbol *InfoEnd = context.CreateTempSymbol();
595
596 // First part: the header.
597
598 // The 4 byte total length of the information for this compilation unit, not
599 // including these 4 bytes.
600 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
601 MCOS->EmitAbsValue(Length, 4);
602
603 // The 2 byte DWARF version, which is 2.
604 MCOS->EmitIntValue(2, 2);
605
606 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
607 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000608 if (AbbrevSectionSymbol) {
609 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
610 } else {
611 MCOS->EmitIntValue(0, 4);
612 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000613
Bill Wendlingbc07a892013-06-18 07:20:20 +0000614 const MCAsmInfo *asmInfo = context.getAsmInfo();
615 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000616 // The 1 byte size of an address.
617 MCOS->EmitIntValue(AddrSize, 1);
618
619 // Second part: the compile_unit DIE.
620
621 // The DW_TAG_compile_unit DIE abbrev (1).
622 MCOS->EmitULEB128IntValue(1);
623
624 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
625 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000626 if (LineSectionSymbol) {
627 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
628 } else {
629 MCOS->EmitIntValue(0, 4);
630 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000631
632 // AT_low_pc, the first address of the default .text section.
633 const MCExpr *Start = MCSymbolRefExpr::Create(
634 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
635 MCOS->EmitAbsValue(Start, AddrSize);
636
637 // AT_high_pc, the last address of the default .text section.
638 const MCExpr *End = MCSymbolRefExpr::Create(
639 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
640 MCOS->EmitAbsValue(End, AddrSize);
641
642 // AT_name, the name of the source file. Reconstruct from the first directory
643 // and file table entries.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000644 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000645 context.getMCDwarfDirs();
646 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000647 MCOS->EmitBytes(MCDwarfDirs[0]);
648 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000649 }
Manman Ren5ce24ff2013-03-12 20:17:00 +0000650 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000651 MCOS->getContext().getMCDwarfFiles();
Eric Christophere3ab3d02013-01-09 01:57:54 +0000652 MCOS->EmitBytes(MCDwarfFiles[1]->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000653 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
654
655 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000656 if (!context.getCompilationDir().empty()) {
657 MCOS->EmitBytes(context.getCompilationDir());
658 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
659 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000660
661 // AT_APPLE_flags, the command line arguments of the assembler tool.
662 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
663 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000664 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000665 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
666 }
667
668 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000669 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
670 if (!DwarfDebugProducer.empty()){
671 MCOS->EmitBytes(DwarfDebugProducer);
672 }
673 else {
674 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
675 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
676 MCOS->EmitBytes(StringRef(")"));
677 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000678 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
679
680 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
681 // draft has no standard code for assembler.
682 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
683
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000684 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000685
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000686 // Loop on saved info for dwarf labels and create the DIEs for them.
687 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
688 MCOS->getContext().getMCGenDwarfLabelEntries();
689 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000690 Entries.begin(), ie = Entries.end(); it != ie;
691 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000692 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000693
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000694 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000695 MCOS->EmitULEB128IntValue(2);
696
697 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000698 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000699 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
700
701 // AT_decl_file, index into the file table.
702 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
703
704 // AT_decl_line, source line number.
705 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
706
707 // AT_low_pc, start address of the label.
708 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
709 MCSymbolRefExpr::VK_None, context);
710 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
711
Kevin Enderbye7739d42011-12-09 18:09:40 +0000712 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
713 MCOS->EmitIntValue(0, 1);
714
715 // The DW_TAG_unspecified_parameters DIE abbrev (3).
716 MCOS->EmitULEB128IntValue(3);
717
718 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
719 MCOS->EmitIntValue(0, 1);
720 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000721 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
722 // for the dwarf labels.
723 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000724 Entries.begin(), ie = Entries.end(); it != ie;
725 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000726 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000727 delete Entry;
728 }
729
730 // Add the NULL DIE terminating the Compile Unit DIE's.
731 MCOS->EmitIntValue(0, 1);
732
733 // Now set the value of the symbol at the end of the info section.
734 MCOS->EmitLabel(InfoEnd);
735}
736
737//
738// When generating dwarf for assembly source files this emits the Dwarf
739// sections.
740//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000741void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000742 // Create the dwarf sections in this order (.debug_line already created).
743 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000744 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000745 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000746 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000747 if (!CreateDwarfSectionSymbols)
748 LineSectionSymbol = NULL;
749 MCSymbol *AbbrevSectionSymbol = NULL;
750 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000751 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000752 if (CreateDwarfSectionSymbols) {
753 InfoSectionSymbol = context.CreateTempSymbol();
754 MCOS->EmitLabel(InfoSectionSymbol);
755 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000756 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000757 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000758 AbbrevSectionSymbol = context.CreateTempSymbol();
759 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000760 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000761 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
762
763 // If there are no line table entries then do not emit any section contents.
764 if (context.getMCLineSections().empty())
765 return;
766
767 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000768 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000769
770 // Output the data for .debug_abbrev section.
771 EmitGenDwarfAbbrev(MCOS);
772
773 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000774 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000775}
776
777//
778// When generating dwarf for assembly source files this is called when symbol
779// for a label is created. If this symbol is not a temporary and is in the
780// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000781// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000782//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000783void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000784 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000785 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000786 // the default text.
787 if (Symbol->isTemporary())
788 return;
789 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000790 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000791 return;
792
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000793 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000794 // underbar if any.
795 StringRef Name = Symbol->getName();
796 if (Name.startswith("_"))
797 Name = Name.substr(1, Name.size()-1);
798
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000799 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000800 unsigned FileNumber = context.getGenDwarfFileNumber();
801
802 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000803 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000804 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
805 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
806
807 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
808 // values so that they don't have things like an ARM thumb bit from the
809 // original symbol. So when used they won't get a low bit set after
810 // relocation.
811 MCSymbol *Label = context.CreateTempSymbol();
812 MCOS->EmitLabel(Label);
813
814 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000815 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000816 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
817 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000818}
819
Rafael Espindola0a017a62010-12-10 07:39:47 +0000820static int getDataAlignmentFactor(MCStreamer &streamer) {
821 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000822 const MCAsmInfo *asmInfo = context.getAsmInfo();
823 int size = asmInfo->getCalleeSaveStackSlotSize();
824 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000825 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000826 else
827 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000828}
829
Rafael Espindola5395f442011-04-22 00:08:43 +0000830static unsigned getSizeForEncoding(MCStreamer &streamer,
831 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000832 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000833 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000834 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000835 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000836 case dwarf::DW_EH_PE_absptr:
837 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000838 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000839 case dwarf::DW_EH_PE_udata2:
840 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000841 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000842 case dwarf::DW_EH_PE_udata4:
843 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000844 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000845 case dwarf::DW_EH_PE_udata8:
846 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000847 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000848 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000849}
850
Iain Sandoe618def62014-01-08 10:22:54 +0000851static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
852 unsigned symbolEncoding, bool isEH,
853 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000854 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000855 const MCAsmInfo *asmInfo = context.getAsmInfo();
856 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
857 symbolEncoding,
858 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000859 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000860 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000861 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
862 streamer.EmitAbsValue(v, size);
863 else
864 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000865}
866
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000867static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
868 unsigned symbolEncoding) {
869 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000870 const MCAsmInfo *asmInfo = context.getAsmInfo();
871 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
872 symbolEncoding,
873 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000874 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000875 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000876}
877
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000878namespace {
879 class FrameEmitterImpl {
880 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000881 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000882 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000883 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000884 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000885 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000886 FrameEmitterImpl(bool usingCFI, bool isEH)
887 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
888 SectionStart(0) {}
889
890 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000891
Bill Wendling3af441f2013-09-05 00:54:52 +0000892 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000893 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000894 const MCDwarfFrameInfo &frame);
895
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000896 const MCSymbol &EmitCIE(MCStreamer &streamer,
897 const MCSymbol *personality,
898 unsigned personalityEncoding,
899 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000900 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000901 unsigned lsdaEncoding,
902 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000903 MCSymbol *EmitFDE(MCStreamer &streamer,
904 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000905 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000906 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000907 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000908 MCSymbol *BaseLabel);
909 void EmitCFIInstruction(MCStreamer &Streamer,
910 const MCCFIInstruction &Instr);
911 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000912
913} // end anonymous namespace
914
915static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
916 StringRef Prefix) {
917 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000918 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000919 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000920 default: EncStr = "<unknown encoding>"; break;
921 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
922 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
923 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
924 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
925 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
926 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
927 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
928 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
929 EncStr = "pcrel udata4";
930 break;
931 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
932 EncStr = "pcrel sdata4";
933 break;
934 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
935 EncStr = "pcrel udata8";
936 break;
937 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
938 EncStr = "screl sdata8";
939 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000940 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
941 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000942 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000943 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
944 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000945 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000946 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
947 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000948 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000949 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
950 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000951 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000952 }
953
954 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
955 }
956
957 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000958}
959
960void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
961 const MCCFIInstruction &Instr) {
962 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000963 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000964
965 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000966 case MCCFIInstruction::OpRegister: {
967 unsigned Reg1 = Instr.getRegister();
968 unsigned Reg2 = Instr.getRegister2();
969 if (VerboseAsm) {
970 Streamer.AddComment("DW_CFA_register");
971 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
972 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
973 }
974 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
975 Streamer.EmitULEB128IntValue(Reg1);
976 Streamer.EmitULEB128IntValue(Reg2);
977 return;
978 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000979 case MCCFIInstruction::OpWindowSave: {
980 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
981 return;
982 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000983 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000984 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000985 if (VerboseAsm) {
986 Streamer.AddComment("DW_CFA_undefined");
987 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
988 }
989 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
990 Streamer.EmitULEB128IntValue(Reg);
991 return;
992 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000993 case MCCFIInstruction::OpAdjustCfaOffset:
994 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000995 const bool IsRelative =
996 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
997
998 if (VerboseAsm)
999 Streamer.AddComment("DW_CFA_def_cfa_offset");
1000 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1001
1002 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001003 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001004 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001005 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001006
1007 if (VerboseAsm)
1008 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1009 Streamer.EmitULEB128IntValue(CFAOffset);
1010
1011 return;
1012 }
1013 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001014 if (VerboseAsm)
1015 Streamer.AddComment("DW_CFA_def_cfa");
1016 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1017
1018 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001019 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1020 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001021
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001022 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001023
1024 if (VerboseAsm)
1025 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1026 Streamer.EmitULEB128IntValue(CFAOffset);
1027
1028 return;
1029 }
1030
1031 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001032 if (VerboseAsm)
1033 Streamer.AddComment("DW_CFA_def_cfa_register");
1034 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1035
1036 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001037 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1038 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001039
1040 return;
1041 }
1042
1043 case MCCFIInstruction::OpOffset:
1044 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001045 const bool IsRelative =
1046 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001047
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001048 unsigned Reg = Instr.getRegister();
1049 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001050 if (IsRelative)
1051 Offset -= CFAOffset;
1052 Offset = Offset / dataAlignmentFactor;
1053
1054 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001055 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001056 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001057 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001058 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001059 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001060 Streamer.EmitSLEB128IntValue(Offset);
1061 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001062 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1063 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001064 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001065 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001066 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001067 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001068 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001069 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001070 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001071 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001072 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001073 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001074 }
1075 return;
1076 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001077 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001078 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001079 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1080 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001081 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001082 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001083 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1084 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001085 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001086 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001087 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001088 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001089 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001090 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001091 return;
1092 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001093 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001094 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001095 if (VerboseAsm) {
1096 Streamer.AddComment("DW_CFA_restore");
1097 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1098 }
1099 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1100 return;
1101 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001102 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001103 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001104 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001105 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001106 }
1107 llvm_unreachable("Unhandled case in switch");
1108}
1109
1110/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1111/// frame.
1112void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001113 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001114 MCSymbol *BaseLabel) {
1115 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1116 const MCCFIInstruction &Instr = Instrs[i];
1117 MCSymbol *Label = Instr.getLabel();
1118 // Throw out move if the label is invalid.
1119 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1120
1121 // Advance row if new location.
1122 if (BaseLabel && Label) {
1123 MCSymbol *ThisSym = Label;
1124 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001125 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001126 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1127 BaseLabel = ThisSym;
1128 }
1129 }
1130
1131 EmitCFIInstruction(streamer, Instr);
1132 }
1133}
1134
Bill Wendling3af441f2013-09-05 00:54:52 +00001135/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001136void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001137 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001138 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001139 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001140 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001141
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001142 // range-start range-length compact-unwind-enc personality-func lsda
1143 // _foo LfooEnd-_foo 0x00000023 0 0
1144 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1145 //
1146 // .section __LD,__compact_unwind,regular,debug
1147 //
1148 // # compact unwind for _foo
1149 // .quad _foo
1150 // .set L1,LfooEnd-_foo
1151 // .long L1
1152 // .long 0x01010001
1153 // .quad 0
1154 // .quad 0
1155 //
1156 // # compact unwind for _bar
1157 // .quad _bar
1158 // .set L2,LbarEnd-_bar
1159 // .long L2
1160 // .long 0x01020011
1161 // .quad __gxx_personality
1162 // .quad except_tab1
1163
Bill Wendling49bc6802011-07-19 00:06:12 +00001164 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001165 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001166 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001167
Bill Wendlingdafd5982011-07-14 23:34:45 +00001168 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001169 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001170 Encoding |= 0x40000000;
1171
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001172 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001173 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001174 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001175 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001176 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001177
1178 // Range Length
1179 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1180 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001181 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001182 Streamer.EmitAbsValue(Range, 4);
1183
Bill Wendling75174662011-06-30 00:30:52 +00001184 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001185 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001186 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1187 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001188 Streamer.EmitIntValue(Encoding, Size);
1189
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001190 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001191 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001192 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001193 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001194 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001195 else
1196 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001197
1198 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001199 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001200 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001201 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001202 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001203 else
1204 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001205}
1206
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001207const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1208 const MCSymbol *personality,
1209 unsigned personalityEncoding,
1210 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001211 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001212 unsigned lsdaEncoding,
1213 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001214 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001215 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001216 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001217 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001218
1219 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001220 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001221 sectionStart = context.CreateTempSymbol();
1222 else
1223 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1224
Bill Wendling567a1ae2011-06-30 21:25:51 +00001225 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001226 CIENum++;
1227
Bill Wendlingb6adf462011-07-07 00:54:13 +00001228 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001229
1230 // Length
1231 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1232 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001233 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001234 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001235
1236 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001237 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001238 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001239 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001240
1241 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001242 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001243 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1244
1245 // Augmentation String
1246 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001247 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001248 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001249 Augmentation += "z";
1250 if (personality)
1251 Augmentation += "P";
1252 if (lsda)
1253 Augmentation += "L";
1254 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001255 if (IsSignalFrame)
1256 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001257 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001258 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001259 streamer.EmitIntValue(0, 1);
1260
1261 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001262 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001263 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001264
1265 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001266 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001267 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1268
1269 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001270 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001271 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001272
1273 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001274
1275 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001276 if (IsEH) {
1277 if (personality) {
1278 // Personality Encoding
1279 augmentationLength += 1;
1280 // Personality
1281 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1282 }
1283 if (lsda)
1284 augmentationLength += 1;
1285 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001286 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001287
Bill Wendling567a1ae2011-06-30 21:25:51 +00001288 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001289 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001290
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001291 // Augmentation Data (optional)
1292 if (personality) {
1293 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001294 EmitEncodingByte(streamer, personalityEncoding,
1295 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001296 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001297 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001298 EmitPersonality(streamer, *personality, personalityEncoding);
1299 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001300
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001301 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001302 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1303
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001304 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001305 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001306 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001307 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001308
1309 // Initial Instructions
1310
Bill Wendlingbc07a892013-06-18 07:20:20 +00001311 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001312 if (!IsSimple) {
1313 const std::vector<MCCFIInstruction> &Instructions =
1314 MAI->getInitialFrameState();
1315 EmitCFIInstructions(streamer, Instructions, NULL);
1316 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001317
1318 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001319 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001320
1321 streamer.EmitLabel(sectionEnd);
1322 return *sectionStart;
1323}
1324
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001325MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1326 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001327 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001328 MCContext &context = streamer.getContext();
1329 MCSymbol *fdeStart = context.CreateTempSymbol();
1330 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001331 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001332 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001333
Evan Chengeee86452011-08-24 22:31:37 +00001334 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001335 MCSymbol *EHSym =
1336 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001337 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001338 streamer.EmitLabel(EHSym);
1339 }
1340
Rafael Espindola0a017a62010-12-10 07:39:47 +00001341 // Length
1342 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001343 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001344 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001345
1346 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001347
Rafael Espindola0a017a62010-12-10 07:39:47 +00001348 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001349 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001350 if (IsEH) {
1351 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1352 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001353 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001354 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001355 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001356 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1357 cieStart, 0);
1358 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001359 } else {
1360 streamer.EmitSymbolValue(&cieStart, 4);
1361 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001362
Rafael Espindola0a017a62010-12-10 07:39:47 +00001363 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001364 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1365 : (unsigned)dwarf::DW_EH_PE_absptr;
1366 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001367 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001368
1369 // PC Range
1370 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1371 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001372 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001373 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001374
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001375 if (IsEH) {
1376 // Augmentation Data Length
1377 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001378
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001379 if (frame.Lsda)
1380 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001381
Bill Wendlingf166ab42011-06-30 22:02:20 +00001382 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001383 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001384
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001385 // Augmentation Data
1386 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001387 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1388 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001389 }
Rafael Espindola68067662011-04-29 03:06:29 +00001390
Rafael Espindola0a017a62010-12-10 07:39:47 +00001391 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001392 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001393
Rafael Espindola0a017a62010-12-10 07:39:47 +00001394 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001395 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001396
1397 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001398}
1399
Benjamin Kramer570dd782010-12-30 22:34:44 +00001400namespace {
1401 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001402 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1403 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001404
Benjamin Kramer570dd782010-12-30 22:34:44 +00001405 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001406 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001407 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001408 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1409 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001410 }
1411 const MCSymbol* Personality;
1412 unsigned PersonalityEncoding;
1413 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001414 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001415 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001416 };
1417}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001418
1419namespace llvm {
1420 template <>
1421 struct DenseMapInfo<CIEKey> {
1422 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001423 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001424 }
1425 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001426 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001427 }
1428 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001429 return static_cast<unsigned>(hash_combine(Key.Personality,
1430 Key.PersonalityEncoding,
1431 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001432 Key.IsSignalFrame,
1433 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001434 }
1435 static bool isEqual(const CIEKey &LHS,
1436 const CIEKey &RHS) {
1437 return LHS.Personality == RHS.Personality &&
1438 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001439 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001440 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1441 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001442 }
1443 };
1444}
1445
Bill Wendling550c76d2013-09-09 19:48:37 +00001446void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1447 bool UsingCFI, bool IsEH) {
1448 Streamer.generateCompactUnwindEncodings(MAB);
1449
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001450 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001451 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001452 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001453 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001454
Bill Wendling9803abb2011-09-06 18:37:11 +00001455 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001456 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001457 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001458 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1459 const MCDwarfFrameInfo &Frame = FrameArray[i];
1460 if (Frame.CompactUnwindEncoding == 0) continue;
1461 if (!SectionEmitted) {
1462 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001463 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001464 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001465 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001466 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001467 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001468 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001469
Bill Wendlinga800f952013-05-30 18:52:57 +00001470 const MCSection &Section =
1471 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1472 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001473 Streamer.SwitchSection(&Section);
1474 MCSymbol *SectionStart = Context.CreateTempSymbol();
1475 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001476 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001477
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001478 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001479 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001480
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001481 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001482 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1483 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001484 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001485 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001486 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1487 if (!CIEStart)
1488 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1489 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001490 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001491 Frame.LsdaEncoding,
1492 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001493
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001494 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001495
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001496 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001497 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001498 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001499
Bill Wendlingbc07a892013-06-18 07:20:20 +00001500 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001501 if (FDEEnd)
1502 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001503}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001504
1505void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1506 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001507 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001508 SmallString<256> Tmp;
1509 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001510 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001511 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001512}
1513
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001514void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1515 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001516 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001517 // Scale the address delta by the minimum instruction length.
1518 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1519
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001520 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001521 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001522 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1523 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001524 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001525 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1526 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001527 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001528 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001529 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001530 OS << uint8_t( AddrDelta & 0xff);
1531 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001532 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001533 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001534 assert(isUInt<32>(AddrDelta));
1535 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001536 OS << uint8_t( AddrDelta & 0xff);
1537 OS << uint8_t((AddrDelta >> 8) & 0xff);
1538 OS << uint8_t((AddrDelta >> 16) & 0xff);
1539 OS << uint8_t((AddrDelta >> 24) & 0xff);
1540
Rafael Espindola736a35d2010-12-28 05:39:27 +00001541 }
1542}