blob: d52c7a76928757c3bd2bffcf9a78cdf707f22650 [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();
160 unsigned Size =
161 MCOS->getContext().getAsmInfo()->getULEB128Size(Discriminator);
162 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
163 MCOS->EmitULEB128IntValue(Size + 1);
164 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
165 MCOS->EmitULEB128IntValue(Discriminator);
166 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000167 if (Isa != it->getIsa()) {
168 Isa = it->getIsa();
169 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000170 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000171 }
172 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
173 Flags = it->getFlags();
174 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
175 }
176 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
177 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
178 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
179 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
180 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
181 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
182
Rafael Espindola1d37f352010-11-13 01:06:27 +0000183 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000184 MCSymbol *Label = it->getLabel();
185
186 // At this point we want to emit/create the sequence to encode the delta in
187 // line numbers and the increment of the address from the previous Label
188 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000189 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000190 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000191 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000192
193 LastLine = it->getLine();
194 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000195 }
196
197 // Emit a DW_LNE_end_sequence for the end of the section.
198 // Using the pointer Section create a temporary label at the end of the
199 // section and use that and the LastLabel to compute the address delta
200 // and use INT64_MAX as the line delta which is the signal that this is
201 // actually a DW_LNE_end_sequence.
202
203 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000204 // TODO: keep track of the last subsection so that this symbol appears in the
205 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000206 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000207
208 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000209 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000210 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000211 // Set the value of the symbol, as we are at the end of the section.
212 MCOS->EmitLabel(SectionEnd);
213
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000214 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000215 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000216
Bill Wendlingbc07a892013-06-18 07:20:20 +0000217 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000218 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000219 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000220}
221
222//
223// This emits the Dwarf file and the line tables.
224//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000225const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000226 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000227 // Switch to the section where the table will be emitted into.
Evan Cheng76792992011-07-20 05:58:47 +0000228 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000229
Manman Ren4e042a62013-02-05 21:52:47 +0000230 const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
231 MCOS->getContext().getMCLineTableSymbols();
232 // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
233 // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
234 // Handle Compile Unit 0, the line table start symbol is the section symbol.
235 const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
236 // Handle the rest of the Compile Units.
237 for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
238 EmitCU(MCOS, Is);
239
240 // Now delete the MCLineSections that were created in MCLineEntry::Make()
241 // and used to emit the line table.
242 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
243 MCOS->getContext().getMCLineSections();
244 for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
245 MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
246 ++it)
247 delete it->second;
248
249 return LineStartSym;
250}
251
252const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
253 MCContext &context = MCOS->getContext();
254
255 // Create a symbol at the beginning of the line table.
256 MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
257 if (!LineStartSym)
258 LineStartSym = context.CreateTempSymbol();
259 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000260 MCOS->EmitLabel(LineStartSym);
261
262 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000263 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000264
265 // The first 4 bytes is the total length of the information for this
266 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000267 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000268 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000269
270 // Next 2 bytes is the Version, which is Dwarf 2.
271 MCOS->EmitIntValue(2, 2);
272
273 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000274 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000275
276 // Length of the prologue, is the next 4 bytes. Which is the start of the
277 // section to the end of the prologue. Not including the 4 bytes for the
278 // total length, the 2 bytes for the version, and these 4 bytes for the
279 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000280 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000281 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000282
283 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000284 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000285 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
286 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
287 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
288 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
289
290 // Standard opcode lengths
291 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
292 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
293 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
294 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
295 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
296 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
297 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
298 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
299 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
300 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
301 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
302 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
303
304 // Put out the directory and file tables.
305
306 // First the directory table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000307 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Manman Ren1e427202013-03-07 01:42:00 +0000308 context.getMCDwarfDirs(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000309 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000310 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
311 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000312 }
313 MCOS->EmitIntValue(0, 1); // Terminate the directory list
314
315 // Second the file table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000316 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Manman Ren1e427202013-03-07 01:42:00 +0000317 MCOS->getContext().getMCDwarfFiles(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000318 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000319 MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
320 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000321 // the Directory num
322 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000323 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
324 MCOS->EmitIntValue(0, 1); // filesize (always 0)
325 }
326 MCOS->EmitIntValue(0, 1); // Terminate the file list
327
328 // This is the end of the prologue, so set the value of the symbol at the
329 // end of the prologue (that was used in a previous expression).
330 MCOS->EmitLabel(ProEndSym);
331
332 // Put out the line tables.
Rafael Espindola9900b482010-11-19 07:41:23 +0000333 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbye46564a2010-09-30 16:52:03 +0000334 MCOS->getContext().getMCLineSections();
Rafael Espindola9900b482010-11-19 07:41:23 +0000335 const std::vector<const MCSection *> &MCLineSectionOrder =
336 MCOS->getContext().getMCLineSectionOrder();
337 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc737ac12011-06-30 23:59:38 +0000338 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola9900b482010-11-19 07:41:23 +0000339 ++it) {
340 const MCSection *Sec = *it;
341 const MCLineSection *Line = MCLineSections.lookup(Sec);
Manman Ren4e042a62013-02-05 21:52:47 +0000342 EmitDwarfLineTable(MCOS, Sec, Line, CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000343 }
344
Bill Wendlingbc07a892013-06-18 07:20:20 +0000345 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines()
Rafael Espindola1048e752010-12-04 00:31:13 +0000346 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000347 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000348 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000349 // total_length >= prologue_length + 10
350 // We are 4 bytes short, since we have total_length = 51 and
351 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000352
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000353 // The regular end_sequence should be sufficient.
354 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000355 }
356
Kevin Enderbye46564a2010-09-30 16:52:03 +0000357 // This is the end of the section, so set the value of the symbol at the end
358 // of this section (that was used in a previous expression).
359 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000360
361 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000362}
363
Kevin Enderbye46564a2010-09-30 16:52:03 +0000364/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000365void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000366 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000367 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000368 SmallString<256> Tmp;
369 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000370 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000371 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000372}
373
374/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000375void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
376 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000377 uint64_t Temp, Opcode;
378 bool NeedCopy = false;
379
380 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000381 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000382
383 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000384 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000385 // end_sequence to emit the matrix entry.
386 if (LineDelta == INT64_MAX) {
387 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
388 OS << char(dwarf::DW_LNS_const_add_pc);
389 else {
390 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000391 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000392 }
393 OS << char(dwarf::DW_LNS_extended_op);
394 OS << char(1);
395 OS << char(dwarf::DW_LNE_end_sequence);
396 return;
397 }
398
399 // Bias the line delta by the base.
400 Temp = LineDelta - DWARF2_LINE_BASE;
401
402 // If the line increment is out of range of a special opcode, we must encode
403 // it with DW_LNS_advance_line.
404 if (Temp >= DWARF2_LINE_RANGE) {
405 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000406 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000407
408 LineDelta = 0;
409 Temp = 0 - DWARF2_LINE_BASE;
410 NeedCopy = true;
411 }
412
413 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
414 if (LineDelta == 0 && AddrDelta == 0) {
415 OS << char(dwarf::DW_LNS_copy);
416 return;
417 }
418
419 // Bias the opcode by the special opcode base.
420 Temp += DWARF2_LINE_OPCODE_BASE;
421
422 // Avoid overflow when addr_delta is large.
423 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
424 // Try using a special opcode.
425 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
426 if (Opcode <= 255) {
427 OS << char(Opcode);
428 return;
429 }
430
431 // Try using DW_LNS_const_add_pc followed by special op.
432 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
433 if (Opcode <= 255) {
434 OS << char(dwarf::DW_LNS_const_add_pc);
435 OS << char(Opcode);
436 return;
437 }
438 }
439
440 // Otherwise use DW_LNS_advance_pc.
441 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000442 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000443
444 if (NeedCopy)
445 OS << char(dwarf::DW_LNS_copy);
446 else
447 OS << char(Temp);
448}
449
Kevin Enderbye5930f12010-07-28 20:55:35 +0000450void MCDwarfFile::print(raw_ostream &OS) const {
451 OS << '"' << getName() << '"';
452}
453
Manman Ren49d684e2012-09-12 05:06:18 +0000454#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderbye5930f12010-07-28 20:55:35 +0000455void MCDwarfFile::dump() const {
456 print(dbgs());
457}
Manman Renc3366cc2012-09-06 19:55:56 +0000458#endif
Kevin Enderbye46564a2010-09-30 16:52:03 +0000459
Kevin Enderbye7739d42011-12-09 18:09:40 +0000460// Utility function to write a tuple for .debug_abbrev.
461static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
462 MCOS->EmitULEB128IntValue(Name);
463 MCOS->EmitULEB128IntValue(Form);
464}
465
466// When generating dwarf for assembly source files this emits
467// the data for .debug_abbrev section which contains three DIEs.
468static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
469 MCContext &context = MCOS->getContext();
470 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
471
472 // DW_TAG_compile_unit DIE abbrev (1).
473 MCOS->EmitULEB128IntValue(1);
474 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
475 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
476 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
477 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
478 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
479 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000480 if (!context.getCompilationDir().empty())
481 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000482 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
483 if (!DwarfDebugFlags.empty())
484 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
485 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
486 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
487 EmitAbbrev(MCOS, 0, 0);
488
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000489 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000490 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000491 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000492 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
493 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
494 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
495 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
496 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000497 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
498 EmitAbbrev(MCOS, 0, 0);
499
500 // DW_TAG_unspecified_parameters DIE abbrev (3).
501 MCOS->EmitULEB128IntValue(3);
502 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
503 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
504 EmitAbbrev(MCOS, 0, 0);
505
506 // Terminate the abbreviations for this compilation unit.
507 MCOS->EmitIntValue(0, 1);
508}
509
510// When generating dwarf for assembly source files this emits the data for
511// .debug_aranges section. Which contains a header and a table of pairs of
512// PointerSize'ed values for the address and size of section(s) with line table
513// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000514static void EmitGenDwarfAranges(MCStreamer *MCOS,
515 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000516 MCContext &context = MCOS->getContext();
517
518 // Create a symbol at the end of the section that we are creating the dwarf
519 // debugging info to use later in here as part of the expression to calculate
520 // the size of the section for the table.
521 MCOS->SwitchSection(context.getGenDwarfSection());
522 MCSymbol *SectionEndSym = context.CreateTempSymbol();
523 MCOS->EmitLabel(SectionEndSym);
524 context.setGenDwarfSectionEndSym(SectionEndSym);
525
526 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
527
528 // This will be the length of the .debug_aranges section, first account for
529 // the size of each item in the header (see below where we emit these items).
530 int Length = 4 + 2 + 4 + 1 + 1;
531
532 // Figure the padding after the header before the table of address and size
533 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000534 const MCAsmInfo *asmInfo = context.getAsmInfo();
535 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000536 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
537 if (Pad == 2 * AddrSize)
538 Pad = 0;
539 Length += Pad;
540
541 // Add the size of the pair of PointerSize'ed values for the address and size
542 // of the one default .text section we have in the table.
543 Length += 2 * AddrSize;
544 // And the pair of terminating zeros.
545 Length += 2 * AddrSize;
546
547
548 // Emit the header for this section.
549 // The 4 byte length not including the 4 byte value for the length.
550 MCOS->EmitIntValue(Length - 4, 4);
551 // The 2 byte version, which is 2.
552 MCOS->EmitIntValue(2, 2);
553 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000554 // of the .debug_info.
555 if (InfoSectionSymbol)
556 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
557 else
558 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000559 // The 1 byte size of an address.
560 MCOS->EmitIntValue(AddrSize, 1);
561 // The 1 byte size of a segment descriptor, we use a value of zero.
562 MCOS->EmitIntValue(0, 1);
563 // Align the header with the padding if needed, before we put out the table.
564 for(int i = 0; i < Pad; i++)
565 MCOS->EmitIntValue(0, 1);
566
567 // Now emit the table of pairs of PointerSize'ed values for the section(s)
568 // address and size, in our case just the one default .text section.
569 const MCExpr *Addr = MCSymbolRefExpr::Create(
570 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
571 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
572 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
573 MCOS->EmitAbsValue(Addr, AddrSize);
574 MCOS->EmitAbsValue(Size, AddrSize);
575
576 // And finally the pair of terminating zeros.
577 MCOS->EmitIntValue(0, AddrSize);
578 MCOS->EmitIntValue(0, AddrSize);
579}
580
581// When generating dwarf for assembly source files this emits the data for
582// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000583// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000584static void EmitGenDwarfInfo(MCStreamer *MCOS,
585 const MCSymbol *AbbrevSectionSymbol,
586 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000587 MCContext &context = MCOS->getContext();
588
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000589 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000590
591 // Create a symbol at the start and end of this section used in here for the
592 // expression to calculate the length in the header.
593 MCSymbol *InfoStart = context.CreateTempSymbol();
594 MCOS->EmitLabel(InfoStart);
595 MCSymbol *InfoEnd = context.CreateTempSymbol();
596
597 // First part: the header.
598
599 // The 4 byte total length of the information for this compilation unit, not
600 // including these 4 bytes.
601 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
602 MCOS->EmitAbsValue(Length, 4);
603
604 // The 2 byte DWARF version, which is 2.
605 MCOS->EmitIntValue(2, 2);
606
607 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
608 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000609 if (AbbrevSectionSymbol) {
610 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
611 } else {
612 MCOS->EmitIntValue(0, 4);
613 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000614
Bill Wendlingbc07a892013-06-18 07:20:20 +0000615 const MCAsmInfo *asmInfo = context.getAsmInfo();
616 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000617 // The 1 byte size of an address.
618 MCOS->EmitIntValue(AddrSize, 1);
619
620 // Second part: the compile_unit DIE.
621
622 // The DW_TAG_compile_unit DIE abbrev (1).
623 MCOS->EmitULEB128IntValue(1);
624
625 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
626 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000627 if (LineSectionSymbol) {
628 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
629 } else {
630 MCOS->EmitIntValue(0, 4);
631 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000632
633 // AT_low_pc, the first address of the default .text section.
634 const MCExpr *Start = MCSymbolRefExpr::Create(
635 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
636 MCOS->EmitAbsValue(Start, AddrSize);
637
638 // AT_high_pc, the last address of the default .text section.
639 const MCExpr *End = MCSymbolRefExpr::Create(
640 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
641 MCOS->EmitAbsValue(End, AddrSize);
642
643 // AT_name, the name of the source file. Reconstruct from the first directory
644 // and file table entries.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000645 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000646 context.getMCDwarfDirs();
647 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000648 MCOS->EmitBytes(MCDwarfDirs[0]);
649 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000650 }
Manman Ren5ce24ff2013-03-12 20:17:00 +0000651 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000652 MCOS->getContext().getMCDwarfFiles();
Eric Christophere3ab3d02013-01-09 01:57:54 +0000653 MCOS->EmitBytes(MCDwarfFiles[1]->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000654 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
655
656 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000657 if (!context.getCompilationDir().empty()) {
658 MCOS->EmitBytes(context.getCompilationDir());
659 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
660 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000661
662 // AT_APPLE_flags, the command line arguments of the assembler tool.
663 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
664 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000665 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000666 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
667 }
668
669 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000670 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
671 if (!DwarfDebugProducer.empty()){
672 MCOS->EmitBytes(DwarfDebugProducer);
673 }
674 else {
675 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
676 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
677 MCOS->EmitBytes(StringRef(")"));
678 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000679 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
680
681 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
682 // draft has no standard code for assembler.
683 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
684
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000685 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000687 // Loop on saved info for dwarf labels and create the DIEs for them.
688 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
689 MCOS->getContext().getMCGenDwarfLabelEntries();
690 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000691 Entries.begin(), ie = Entries.end(); it != ie;
692 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000693 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000694
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000695 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000696 MCOS->EmitULEB128IntValue(2);
697
698 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000699 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000700 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
701
702 // AT_decl_file, index into the file table.
703 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
704
705 // AT_decl_line, source line number.
706 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
707
708 // AT_low_pc, start address of the label.
709 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
710 MCSymbolRefExpr::VK_None, context);
711 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
712
Kevin Enderbye7739d42011-12-09 18:09:40 +0000713 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
714 MCOS->EmitIntValue(0, 1);
715
716 // The DW_TAG_unspecified_parameters DIE abbrev (3).
717 MCOS->EmitULEB128IntValue(3);
718
719 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
720 MCOS->EmitIntValue(0, 1);
721 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000722 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
723 // for the dwarf labels.
724 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000725 Entries.begin(), ie = Entries.end(); it != ie;
726 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000727 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000728 delete Entry;
729 }
730
731 // Add the NULL DIE terminating the Compile Unit DIE's.
732 MCOS->EmitIntValue(0, 1);
733
734 // Now set the value of the symbol at the end of the info section.
735 MCOS->EmitLabel(InfoEnd);
736}
737
738//
739// When generating dwarf for assembly source files this emits the Dwarf
740// sections.
741//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000742void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000743 // Create the dwarf sections in this order (.debug_line already created).
744 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000745 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000746 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000747 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000748 if (!CreateDwarfSectionSymbols)
749 LineSectionSymbol = NULL;
750 MCSymbol *AbbrevSectionSymbol = NULL;
751 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000752 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000753 if (CreateDwarfSectionSymbols) {
754 InfoSectionSymbol = context.CreateTempSymbol();
755 MCOS->EmitLabel(InfoSectionSymbol);
756 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000757 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000758 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000759 AbbrevSectionSymbol = context.CreateTempSymbol();
760 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000761 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000762 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
763
764 // If there are no line table entries then do not emit any section contents.
765 if (context.getMCLineSections().empty())
766 return;
767
768 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000769 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000770
771 // Output the data for .debug_abbrev section.
772 EmitGenDwarfAbbrev(MCOS);
773
774 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000775 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000776}
777
778//
779// When generating dwarf for assembly source files this is called when symbol
780// for a label is created. If this symbol is not a temporary and is in the
781// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000782// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000784void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000786 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000787 // the default text.
788 if (Symbol->isTemporary())
789 return;
790 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000791 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000792 return;
793
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000794 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795 // underbar if any.
796 StringRef Name = Symbol->getName();
797 if (Name.startswith("_"))
798 Name = Name.substr(1, Name.size()-1);
799
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000800 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000801 unsigned FileNumber = context.getGenDwarfFileNumber();
802
803 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000804 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000805 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
806 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
807
808 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
809 // values so that they don't have things like an ARM thumb bit from the
810 // original symbol. So when used they won't get a low bit set after
811 // relocation.
812 MCSymbol *Label = context.CreateTempSymbol();
813 MCOS->EmitLabel(Label);
814
815 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000816 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000817 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
818 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000819}
820
Rafael Espindola0a017a62010-12-10 07:39:47 +0000821static int getDataAlignmentFactor(MCStreamer &streamer) {
822 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000823 const MCAsmInfo *asmInfo = context.getAsmInfo();
824 int size = asmInfo->getCalleeSaveStackSlotSize();
825 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000826 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000827 else
828 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000829}
830
Rafael Espindola5395f442011-04-22 00:08:43 +0000831static unsigned getSizeForEncoding(MCStreamer &streamer,
832 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000833 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000834 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000835 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000836 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000837 case dwarf::DW_EH_PE_absptr:
838 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000839 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000840 case dwarf::DW_EH_PE_udata2:
841 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000842 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000843 case dwarf::DW_EH_PE_udata4:
844 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000845 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000846 case dwarf::DW_EH_PE_udata8:
847 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000848 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000849 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000850}
851
Iain Sandoe618def62014-01-08 10:22:54 +0000852static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
853 unsigned symbolEncoding, bool isEH,
854 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000855 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000856 const MCAsmInfo *asmInfo = context.getAsmInfo();
857 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
858 symbolEncoding,
859 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000860 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000861 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000862 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
863 streamer.EmitAbsValue(v, size);
864 else
865 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000866}
867
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000868static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
869 unsigned symbolEncoding) {
870 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000871 const MCAsmInfo *asmInfo = context.getAsmInfo();
872 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
873 symbolEncoding,
874 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000875 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000876 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000877}
878
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000879namespace {
880 class FrameEmitterImpl {
881 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000882 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000883 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000884 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000885 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000886 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000887 FrameEmitterImpl(bool usingCFI, bool isEH)
888 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
889 SectionStart(0) {}
890
891 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000892
Bill Wendling3af441f2013-09-05 00:54:52 +0000893 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000894 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000895 const MCDwarfFrameInfo &frame);
896
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000897 const MCSymbol &EmitCIE(MCStreamer &streamer,
898 const MCSymbol *personality,
899 unsigned personalityEncoding,
900 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000901 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000902 unsigned lsdaEncoding,
903 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000904 MCSymbol *EmitFDE(MCStreamer &streamer,
905 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000906 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000907 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000908 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000909 MCSymbol *BaseLabel);
910 void EmitCFIInstruction(MCStreamer &Streamer,
911 const MCCFIInstruction &Instr);
912 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000913
914} // end anonymous namespace
915
916static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
917 StringRef Prefix) {
918 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000919 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000920 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000921 default: EncStr = "<unknown encoding>"; break;
922 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
923 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
924 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
925 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
926 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
927 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
928 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
929 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
930 EncStr = "pcrel udata4";
931 break;
932 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
933 EncStr = "pcrel sdata4";
934 break;
935 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
936 EncStr = "pcrel udata8";
937 break;
938 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
939 EncStr = "screl sdata8";
940 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000941 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
942 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000943 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000944 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
945 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000946 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000947 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
948 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000949 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000950 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
951 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000952 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000953 }
954
955 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
956 }
957
958 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000959}
960
961void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
962 const MCCFIInstruction &Instr) {
963 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000964 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000965
966 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000967 case MCCFIInstruction::OpRegister: {
968 unsigned Reg1 = Instr.getRegister();
969 unsigned Reg2 = Instr.getRegister2();
970 if (VerboseAsm) {
971 Streamer.AddComment("DW_CFA_register");
972 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
973 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
974 }
975 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
976 Streamer.EmitULEB128IntValue(Reg1);
977 Streamer.EmitULEB128IntValue(Reg2);
978 return;
979 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000980 case MCCFIInstruction::OpWindowSave: {
981 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
982 return;
983 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000984 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000985 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000986 if (VerboseAsm) {
987 Streamer.AddComment("DW_CFA_undefined");
988 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
989 }
990 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
991 Streamer.EmitULEB128IntValue(Reg);
992 return;
993 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000994 case MCCFIInstruction::OpAdjustCfaOffset:
995 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000996 const bool IsRelative =
997 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
998
999 if (VerboseAsm)
1000 Streamer.AddComment("DW_CFA_def_cfa_offset");
1001 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1002
1003 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001004 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001005 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001006 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001007
1008 if (VerboseAsm)
1009 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1010 Streamer.EmitULEB128IntValue(CFAOffset);
1011
1012 return;
1013 }
1014 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001015 if (VerboseAsm)
1016 Streamer.AddComment("DW_CFA_def_cfa");
1017 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1018
1019 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001020 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1021 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001022
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001023 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001024
1025 if (VerboseAsm)
1026 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1027 Streamer.EmitULEB128IntValue(CFAOffset);
1028
1029 return;
1030 }
1031
1032 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001033 if (VerboseAsm)
1034 Streamer.AddComment("DW_CFA_def_cfa_register");
1035 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1036
1037 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001038 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1039 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001040
1041 return;
1042 }
1043
1044 case MCCFIInstruction::OpOffset:
1045 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001046 const bool IsRelative =
1047 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001048
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001049 unsigned Reg = Instr.getRegister();
1050 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001051 if (IsRelative)
1052 Offset -= CFAOffset;
1053 Offset = Offset / dataAlignmentFactor;
1054
1055 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001056 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001057 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001058 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001059 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001060 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001061 Streamer.EmitSLEB128IntValue(Offset);
1062 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001063 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1064 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001065 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001066 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001067 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001068 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001069 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001071 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001072 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001073 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001074 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001075 }
1076 return;
1077 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001078 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001079 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001080 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1081 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001082 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001083 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001084 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1085 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001086 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001087 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001088 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001089 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001090 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001091 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001092 return;
1093 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001094 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001095 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001096 if (VerboseAsm) {
1097 Streamer.AddComment("DW_CFA_restore");
1098 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1099 }
1100 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1101 return;
1102 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001103 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001104 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001105 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001106 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001107 }
1108 llvm_unreachable("Unhandled case in switch");
1109}
1110
1111/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1112/// frame.
1113void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001114 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001115 MCSymbol *BaseLabel) {
1116 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1117 const MCCFIInstruction &Instr = Instrs[i];
1118 MCSymbol *Label = Instr.getLabel();
1119 // Throw out move if the label is invalid.
1120 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1121
1122 // Advance row if new location.
1123 if (BaseLabel && Label) {
1124 MCSymbol *ThisSym = Label;
1125 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001126 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001127 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1128 BaseLabel = ThisSym;
1129 }
1130 }
1131
1132 EmitCFIInstruction(streamer, Instr);
1133 }
1134}
1135
Bill Wendling3af441f2013-09-05 00:54:52 +00001136/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001137void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001138 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001139 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001140 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001141 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001142
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001143 // range-start range-length compact-unwind-enc personality-func lsda
1144 // _foo LfooEnd-_foo 0x00000023 0 0
1145 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1146 //
1147 // .section __LD,__compact_unwind,regular,debug
1148 //
1149 // # compact unwind for _foo
1150 // .quad _foo
1151 // .set L1,LfooEnd-_foo
1152 // .long L1
1153 // .long 0x01010001
1154 // .quad 0
1155 // .quad 0
1156 //
1157 // # compact unwind for _bar
1158 // .quad _bar
1159 // .set L2,LbarEnd-_bar
1160 // .long L2
1161 // .long 0x01020011
1162 // .quad __gxx_personality
1163 // .quad except_tab1
1164
Bill Wendling49bc6802011-07-19 00:06:12 +00001165 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001166 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001167 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001168
Bill Wendlingdafd5982011-07-14 23:34:45 +00001169 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001170 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001171 Encoding |= 0x40000000;
1172
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001173 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001174 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001175 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001176 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001177 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001178
1179 // Range Length
1180 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1181 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001182 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001183 Streamer.EmitAbsValue(Range, 4);
1184
Bill Wendling75174662011-06-30 00:30:52 +00001185 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001186 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001187 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1188 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001189 Streamer.EmitIntValue(Encoding, Size);
1190
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001191 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001192 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001193 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001194 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001195 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001196 else
1197 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001198
1199 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001200 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001201 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001202 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001203 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001204 else
1205 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001206}
1207
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001208const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1209 const MCSymbol *personality,
1210 unsigned personalityEncoding,
1211 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001212 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001213 unsigned lsdaEncoding,
1214 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001215 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001216 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001217 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001218 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001219
1220 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001221 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001222 sectionStart = context.CreateTempSymbol();
1223 else
1224 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1225
Bill Wendling567a1ae2011-06-30 21:25:51 +00001226 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001227 CIENum++;
1228
Bill Wendlingb6adf462011-07-07 00:54:13 +00001229 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001230
1231 // Length
1232 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1233 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001234 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001235 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001236
1237 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001238 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001239 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001240 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001241
1242 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001243 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001244 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1245
1246 // Augmentation String
1247 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001248 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001249 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001250 Augmentation += "z";
1251 if (personality)
1252 Augmentation += "P";
1253 if (lsda)
1254 Augmentation += "L";
1255 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001256 if (IsSignalFrame)
1257 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001258 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001259 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001260 streamer.EmitIntValue(0, 1);
1261
1262 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001263 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001264 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001265
1266 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001267 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001268 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1269
1270 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001271 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001272 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001273
1274 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001275
1276 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001277 if (IsEH) {
1278 if (personality) {
1279 // Personality Encoding
1280 augmentationLength += 1;
1281 // Personality
1282 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1283 }
1284 if (lsda)
1285 augmentationLength += 1;
1286 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001287 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001288
Bill Wendling567a1ae2011-06-30 21:25:51 +00001289 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001290 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001291
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001292 // Augmentation Data (optional)
1293 if (personality) {
1294 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001295 EmitEncodingByte(streamer, personalityEncoding,
1296 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001297 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001298 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001299 EmitPersonality(streamer, *personality, personalityEncoding);
1300 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001301
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001302 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001303 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1304
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001305 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001306 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001307 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001308 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001309
1310 // Initial Instructions
1311
Bill Wendlingbc07a892013-06-18 07:20:20 +00001312 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001313 if (!IsSimple) {
1314 const std::vector<MCCFIInstruction> &Instructions =
1315 MAI->getInitialFrameState();
1316 EmitCFIInstructions(streamer, Instructions, NULL);
1317 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001318
1319 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001320 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001321
1322 streamer.EmitLabel(sectionEnd);
1323 return *sectionStart;
1324}
1325
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001326MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1327 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001328 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001329 MCContext &context = streamer.getContext();
1330 MCSymbol *fdeStart = context.CreateTempSymbol();
1331 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001332 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001333 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001334
Evan Chengeee86452011-08-24 22:31:37 +00001335 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001336 MCSymbol *EHSym =
1337 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001338 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001339 streamer.EmitLabel(EHSym);
1340 }
1341
Rafael Espindola0a017a62010-12-10 07:39:47 +00001342 // Length
1343 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001344 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001345 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001346
1347 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001348
Rafael Espindola0a017a62010-12-10 07:39:47 +00001349 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001350 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001351 if (IsEH) {
1352 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1353 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001354 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001355 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001356 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001357 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1358 cieStart, 0);
1359 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001360 } else {
1361 streamer.EmitSymbolValue(&cieStart, 4);
1362 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001363
Rafael Espindola0a017a62010-12-10 07:39:47 +00001364 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001365 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1366 : (unsigned)dwarf::DW_EH_PE_absptr;
1367 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001368 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001369
1370 // PC Range
1371 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1372 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001373 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001374 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001375
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001376 if (IsEH) {
1377 // Augmentation Data Length
1378 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001379
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001380 if (frame.Lsda)
1381 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001382
Bill Wendlingf166ab42011-06-30 22:02:20 +00001383 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001384 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001385
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001386 // Augmentation Data
1387 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001388 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1389 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001390 }
Rafael Espindola68067662011-04-29 03:06:29 +00001391
Rafael Espindola0a017a62010-12-10 07:39:47 +00001392 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001393 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001394
Rafael Espindola0a017a62010-12-10 07:39:47 +00001395 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001396 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001397
1398 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001399}
1400
Benjamin Kramer570dd782010-12-30 22:34:44 +00001401namespace {
1402 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001403 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1404 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001405
Benjamin Kramer570dd782010-12-30 22:34:44 +00001406 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001407 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001408 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001409 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1410 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001411 }
1412 const MCSymbol* Personality;
1413 unsigned PersonalityEncoding;
1414 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001415 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001416 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001417 };
1418}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001419
1420namespace llvm {
1421 template <>
1422 struct DenseMapInfo<CIEKey> {
1423 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001424 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001425 }
1426 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001427 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001428 }
1429 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001430 return static_cast<unsigned>(hash_combine(Key.Personality,
1431 Key.PersonalityEncoding,
1432 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001433 Key.IsSignalFrame,
1434 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001435 }
1436 static bool isEqual(const CIEKey &LHS,
1437 const CIEKey &RHS) {
1438 return LHS.Personality == RHS.Personality &&
1439 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001440 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001441 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1442 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001443 }
1444 };
1445}
1446
Bill Wendling550c76d2013-09-09 19:48:37 +00001447void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1448 bool UsingCFI, bool IsEH) {
1449 Streamer.generateCompactUnwindEncodings(MAB);
1450
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001451 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001452 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001453 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001454 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001455
Bill Wendling9803abb2011-09-06 18:37:11 +00001456 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001457 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001458 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001459 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1460 const MCDwarfFrameInfo &Frame = FrameArray[i];
1461 if (Frame.CompactUnwindEncoding == 0) continue;
1462 if (!SectionEmitted) {
1463 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001464 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001465 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001466 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001467 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001468 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001469 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001470
Bill Wendlinga800f952013-05-30 18:52:57 +00001471 const MCSection &Section =
1472 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1473 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001474 Streamer.SwitchSection(&Section);
1475 MCSymbol *SectionStart = Context.CreateTempSymbol();
1476 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001477 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001478
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001479 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001480 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001481
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001482 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001483 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1484 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001485 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001486 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001487 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1488 if (!CIEStart)
1489 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1490 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001491 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001492 Frame.LsdaEncoding,
1493 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001494
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001495 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001496
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001497 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001498 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001499 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001500
Bill Wendlingbc07a892013-06-18 07:20:20 +00001501 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001502 if (FDEEnd)
1503 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001504}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001505
1506void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1507 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001508 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001509 SmallString<256> Tmp;
1510 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001511 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001512 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001513}
1514
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001515void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1516 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001517 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001518 // Scale the address delta by the minimum instruction length.
1519 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1520
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001521 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001522 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001523 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1524 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001525 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001526 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1527 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001528 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001529 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001530 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001531 OS << uint8_t( AddrDelta & 0xff);
1532 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001533 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001534 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001535 assert(isUInt<32>(AddrDelta));
1536 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001537 OS << uint8_t( AddrDelta & 0xff);
1538 OS << uint8_t((AddrDelta >> 8) & 0xff);
1539 OS << uint8_t((AddrDelta >> 16) & 0xff);
1540 OS << uint8_t((AddrDelta >> 24) & 0xff);
1541
Rafael Espindola736a35d2010-12-28 05:39:27 +00001542 }
1543}