blob: 479f4452d4a3b501c065540265dc449b048cf540 [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;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000140 MCSymbol *LastLabel = NULL;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000141
142 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola9900b482010-11-19 07:41:23 +0000143 for (MCLineSection::const_iterator
Manman Ren4e042a62013-02-05 21:52:47 +0000144 it = LineSection->getMCLineEntries(CUID).begin(),
145 ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000146
147 if (FileNum != it->getFileNum()) {
148 FileNum = it->getFileNum();
149 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000150 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000151 }
152 if (Column != it->getColumn()) {
153 Column = it->getColumn();
154 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000155 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000156 }
157 if (Isa != it->getIsa()) {
158 Isa = it->getIsa();
159 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000160 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000161 }
162 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
163 Flags = it->getFlags();
164 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
165 }
166 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
167 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
168 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
169 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
170 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
171 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
172
Rafael Espindola1d37f352010-11-13 01:06:27 +0000173 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000174 MCSymbol *Label = it->getLabel();
175
176 // At this point we want to emit/create the sequence to encode the delta in
177 // line numbers and the increment of the address from the previous Label
178 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000179 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000180 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000181 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000182
183 LastLine = it->getLine();
184 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000185 }
186
187 // Emit a DW_LNE_end_sequence for the end of the section.
188 // Using the pointer Section create a temporary label at the end of the
189 // section and use that and the LastLabel to compute the address delta
190 // and use INT64_MAX as the line delta which is the signal that this is
191 // actually a DW_LNE_end_sequence.
192
193 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000194 // TODO: keep track of the last subsection so that this symbol appears in the
195 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000196 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000197
198 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000199 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000200 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000201 // Set the value of the symbol, as we are at the end of the section.
202 MCOS->EmitLabel(SectionEnd);
203
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000204 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000205 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000206
Bill Wendlingbc07a892013-06-18 07:20:20 +0000207 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000208 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000209 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000210}
211
212//
213// This emits the Dwarf file and the line tables.
214//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000215const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000216 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000217 // Switch to the section where the table will be emitted into.
Evan Cheng76792992011-07-20 05:58:47 +0000218 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000219
Manman Ren4e042a62013-02-05 21:52:47 +0000220 const DenseMap<unsigned, MCSymbol *> &MCLineTableSymbols =
221 MCOS->getContext().getMCLineTableSymbols();
222 // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
223 // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
224 // Handle Compile Unit 0, the line table start symbol is the section symbol.
225 const MCSymbol *LineStartSym = EmitCU(MCOS, 0);
226 // Handle the rest of the Compile Units.
227 for (unsigned Is = 1, Ie = MCLineTableSymbols.size(); Is < Ie; Is++)
228 EmitCU(MCOS, Is);
229
230 // Now delete the MCLineSections that were created in MCLineEntry::Make()
231 // and used to emit the line table.
232 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
233 MCOS->getContext().getMCLineSections();
234 for (DenseMap<const MCSection *, MCLineSection *>::const_iterator it =
235 MCLineSections.begin(), ie = MCLineSections.end(); it != ie;
236 ++it)
237 delete it->second;
238
239 return LineStartSym;
240}
241
242const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS, unsigned CUID) {
243 MCContext &context = MCOS->getContext();
244
245 // Create a symbol at the beginning of the line table.
246 MCSymbol *LineStartSym = MCOS->getContext().getMCLineTableSymbol(CUID);
247 if (!LineStartSym)
248 LineStartSym = context.CreateTempSymbol();
249 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000250 MCOS->EmitLabel(LineStartSym);
251
252 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000253 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000254
255 // The first 4 bytes is the total length of the information for this
256 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000257 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000258 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000259
260 // Next 2 bytes is the Version, which is Dwarf 2.
261 MCOS->EmitIntValue(2, 2);
262
263 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000264 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000265
266 // Length of the prologue, is the next 4 bytes. Which is the start of the
267 // section to the end of the prologue. Not including the 4 bytes for the
268 // total length, the 2 bytes for the version, and these 4 bytes for the
269 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000270 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000271 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000272
273 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000274 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000275 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
276 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
277 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
278 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
279
280 // Standard opcode lengths
281 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
282 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
283 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
284 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
285 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
286 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
287 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
288 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
289 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
290 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
291 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
292 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
293
294 // Put out the directory and file tables.
295
296 // First the directory table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000297 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Manman Ren1e427202013-03-07 01:42:00 +0000298 context.getMCDwarfDirs(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000299 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000300 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
301 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000302 }
303 MCOS->EmitIntValue(0, 1); // Terminate the directory list
304
305 // Second the file table.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000306 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Manman Ren1e427202013-03-07 01:42:00 +0000307 MCOS->getContext().getMCDwarfFiles(CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000309 MCOS->EmitBytes(MCDwarfFiles[i]->getName()); // FileName
310 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000311 // the Directory num
312 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000313 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
314 MCOS->EmitIntValue(0, 1); // filesize (always 0)
315 }
316 MCOS->EmitIntValue(0, 1); // Terminate the file list
317
318 // This is the end of the prologue, so set the value of the symbol at the
319 // end of the prologue (that was used in a previous expression).
320 MCOS->EmitLabel(ProEndSym);
321
322 // Put out the line tables.
Rafael Espindola9900b482010-11-19 07:41:23 +0000323 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbye46564a2010-09-30 16:52:03 +0000324 MCOS->getContext().getMCLineSections();
Rafael Espindola9900b482010-11-19 07:41:23 +0000325 const std::vector<const MCSection *> &MCLineSectionOrder =
326 MCOS->getContext().getMCLineSectionOrder();
327 for (std::vector<const MCSection*>::const_iterator it =
Bill Wendlingc737ac12011-06-30 23:59:38 +0000328 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola9900b482010-11-19 07:41:23 +0000329 ++it) {
330 const MCSection *Sec = *it;
331 const MCLineSection *Line = MCLineSections.lookup(Sec);
Manman Ren4e042a62013-02-05 21:52:47 +0000332 EmitDwarfLineTable(MCOS, Sec, Line, CUID);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000333 }
334
Bill Wendlingbc07a892013-06-18 07:20:20 +0000335 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines()
Rafael Espindola1048e752010-12-04 00:31:13 +0000336 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000337 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000338 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000339 // total_length >= prologue_length + 10
340 // We are 4 bytes short, since we have total_length = 51 and
341 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000342
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000343 // The regular end_sequence should be sufficient.
344 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000345 }
346
Kevin Enderbye46564a2010-09-30 16:52:03 +0000347 // This is the end of the section, so set the value of the symbol at the end
348 // of this section (that was used in a previous expression).
349 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000350
351 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000352}
353
Kevin Enderbye46564a2010-09-30 16:52:03 +0000354/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000355void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000356 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000357 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000358 SmallString<256> Tmp;
359 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000360 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000361 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000362}
363
364/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000365void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
366 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000367 uint64_t Temp, Opcode;
368 bool NeedCopy = false;
369
370 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000371 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000372
373 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000374 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000375 // end_sequence to emit the matrix entry.
376 if (LineDelta == INT64_MAX) {
377 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
378 OS << char(dwarf::DW_LNS_const_add_pc);
379 else {
380 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000381 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000382 }
383 OS << char(dwarf::DW_LNS_extended_op);
384 OS << char(1);
385 OS << char(dwarf::DW_LNE_end_sequence);
386 return;
387 }
388
389 // Bias the line delta by the base.
390 Temp = LineDelta - DWARF2_LINE_BASE;
391
392 // If the line increment is out of range of a special opcode, we must encode
393 // it with DW_LNS_advance_line.
394 if (Temp >= DWARF2_LINE_RANGE) {
395 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000396 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000397
398 LineDelta = 0;
399 Temp = 0 - DWARF2_LINE_BASE;
400 NeedCopy = true;
401 }
402
403 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
404 if (LineDelta == 0 && AddrDelta == 0) {
405 OS << char(dwarf::DW_LNS_copy);
406 return;
407 }
408
409 // Bias the opcode by the special opcode base.
410 Temp += DWARF2_LINE_OPCODE_BASE;
411
412 // Avoid overflow when addr_delta is large.
413 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
414 // Try using a special opcode.
415 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
416 if (Opcode <= 255) {
417 OS << char(Opcode);
418 return;
419 }
420
421 // Try using DW_LNS_const_add_pc followed by special op.
422 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
423 if (Opcode <= 255) {
424 OS << char(dwarf::DW_LNS_const_add_pc);
425 OS << char(Opcode);
426 return;
427 }
428 }
429
430 // Otherwise use DW_LNS_advance_pc.
431 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000432 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000433
434 if (NeedCopy)
435 OS << char(dwarf::DW_LNS_copy);
436 else
437 OS << char(Temp);
438}
439
Kevin Enderbye5930f12010-07-28 20:55:35 +0000440void MCDwarfFile::print(raw_ostream &OS) const {
441 OS << '"' << getName() << '"';
442}
443
Manman Ren49d684e2012-09-12 05:06:18 +0000444#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderbye5930f12010-07-28 20:55:35 +0000445void MCDwarfFile::dump() const {
446 print(dbgs());
447}
Manman Renc3366cc2012-09-06 19:55:56 +0000448#endif
Kevin Enderbye46564a2010-09-30 16:52:03 +0000449
Kevin Enderbye7739d42011-12-09 18:09:40 +0000450// Utility function to write a tuple for .debug_abbrev.
451static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
452 MCOS->EmitULEB128IntValue(Name);
453 MCOS->EmitULEB128IntValue(Form);
454}
455
456// When generating dwarf for assembly source files this emits
457// the data for .debug_abbrev section which contains three DIEs.
458static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
459 MCContext &context = MCOS->getContext();
460 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
461
462 // DW_TAG_compile_unit DIE abbrev (1).
463 MCOS->EmitULEB128IntValue(1);
464 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
465 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
466 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
467 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
468 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
469 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000470 if (!context.getCompilationDir().empty())
471 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000472 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
473 if (!DwarfDebugFlags.empty())
474 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
475 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
476 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
477 EmitAbbrev(MCOS, 0, 0);
478
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000479 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000480 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000481 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000482 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
483 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
484 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
485 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
486 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000487 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
488 EmitAbbrev(MCOS, 0, 0);
489
490 // DW_TAG_unspecified_parameters DIE abbrev (3).
491 MCOS->EmitULEB128IntValue(3);
492 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
493 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
494 EmitAbbrev(MCOS, 0, 0);
495
496 // Terminate the abbreviations for this compilation unit.
497 MCOS->EmitIntValue(0, 1);
498}
499
500// When generating dwarf for assembly source files this emits the data for
501// .debug_aranges section. Which contains a header and a table of pairs of
502// PointerSize'ed values for the address and size of section(s) with line table
503// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000504static void EmitGenDwarfAranges(MCStreamer *MCOS,
505 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000506 MCContext &context = MCOS->getContext();
507
508 // Create a symbol at the end of the section that we are creating the dwarf
509 // debugging info to use later in here as part of the expression to calculate
510 // the size of the section for the table.
511 MCOS->SwitchSection(context.getGenDwarfSection());
512 MCSymbol *SectionEndSym = context.CreateTempSymbol();
513 MCOS->EmitLabel(SectionEndSym);
514 context.setGenDwarfSectionEndSym(SectionEndSym);
515
516 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
517
518 // This will be the length of the .debug_aranges section, first account for
519 // the size of each item in the header (see below where we emit these items).
520 int Length = 4 + 2 + 4 + 1 + 1;
521
522 // Figure the padding after the header before the table of address and size
523 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000524 const MCAsmInfo *asmInfo = context.getAsmInfo();
525 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000526 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
527 if (Pad == 2 * AddrSize)
528 Pad = 0;
529 Length += Pad;
530
531 // Add the size of the pair of PointerSize'ed values for the address and size
532 // of the one default .text section we have in the table.
533 Length += 2 * AddrSize;
534 // And the pair of terminating zeros.
535 Length += 2 * AddrSize;
536
537
538 // Emit the header for this section.
539 // The 4 byte length not including the 4 byte value for the length.
540 MCOS->EmitIntValue(Length - 4, 4);
541 // The 2 byte version, which is 2.
542 MCOS->EmitIntValue(2, 2);
543 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000544 // of the .debug_info.
545 if (InfoSectionSymbol)
546 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
547 else
548 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000549 // The 1 byte size of an address.
550 MCOS->EmitIntValue(AddrSize, 1);
551 // The 1 byte size of a segment descriptor, we use a value of zero.
552 MCOS->EmitIntValue(0, 1);
553 // Align the header with the padding if needed, before we put out the table.
554 for(int i = 0; i < Pad; i++)
555 MCOS->EmitIntValue(0, 1);
556
557 // Now emit the table of pairs of PointerSize'ed values for the section(s)
558 // address and size, in our case just the one default .text section.
559 const MCExpr *Addr = MCSymbolRefExpr::Create(
560 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
561 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
562 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
563 MCOS->EmitAbsValue(Addr, AddrSize);
564 MCOS->EmitAbsValue(Size, AddrSize);
565
566 // And finally the pair of terminating zeros.
567 MCOS->EmitIntValue(0, AddrSize);
568 MCOS->EmitIntValue(0, AddrSize);
569}
570
571// When generating dwarf for assembly source files this emits the data for
572// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000573// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000574static void EmitGenDwarfInfo(MCStreamer *MCOS,
575 const MCSymbol *AbbrevSectionSymbol,
576 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000577 MCContext &context = MCOS->getContext();
578
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000579 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000580
581 // Create a symbol at the start and end of this section used in here for the
582 // expression to calculate the length in the header.
583 MCSymbol *InfoStart = context.CreateTempSymbol();
584 MCOS->EmitLabel(InfoStart);
585 MCSymbol *InfoEnd = context.CreateTempSymbol();
586
587 // First part: the header.
588
589 // The 4 byte total length of the information for this compilation unit, not
590 // including these 4 bytes.
591 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
592 MCOS->EmitAbsValue(Length, 4);
593
594 // The 2 byte DWARF version, which is 2.
595 MCOS->EmitIntValue(2, 2);
596
597 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
598 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000599 if (AbbrevSectionSymbol) {
600 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
601 } else {
602 MCOS->EmitIntValue(0, 4);
603 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000604
Bill Wendlingbc07a892013-06-18 07:20:20 +0000605 const MCAsmInfo *asmInfo = context.getAsmInfo();
606 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000607 // The 1 byte size of an address.
608 MCOS->EmitIntValue(AddrSize, 1);
609
610 // Second part: the compile_unit DIE.
611
612 // The DW_TAG_compile_unit DIE abbrev (1).
613 MCOS->EmitULEB128IntValue(1);
614
615 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
616 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000617 if (LineSectionSymbol) {
618 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
619 } else {
620 MCOS->EmitIntValue(0, 4);
621 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000622
623 // AT_low_pc, the first address of the default .text section.
624 const MCExpr *Start = MCSymbolRefExpr::Create(
625 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
626 MCOS->EmitAbsValue(Start, AddrSize);
627
628 // AT_high_pc, the last address of the default .text section.
629 const MCExpr *End = MCSymbolRefExpr::Create(
630 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
631 MCOS->EmitAbsValue(End, AddrSize);
632
633 // AT_name, the name of the source file. Reconstruct from the first directory
634 // and file table entries.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000635 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000636 context.getMCDwarfDirs();
637 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000638 MCOS->EmitBytes(MCDwarfDirs[0]);
639 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000640 }
Manman Ren5ce24ff2013-03-12 20:17:00 +0000641 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000642 MCOS->getContext().getMCDwarfFiles();
Eric Christophere3ab3d02013-01-09 01:57:54 +0000643 MCOS->EmitBytes(MCDwarfFiles[1]->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000644 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
645
646 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000647 if (!context.getCompilationDir().empty()) {
648 MCOS->EmitBytes(context.getCompilationDir());
649 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
650 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000651
652 // AT_APPLE_flags, the command line arguments of the assembler tool.
653 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
654 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000655 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000656 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
657 }
658
659 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000660 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
661 if (!DwarfDebugProducer.empty()){
662 MCOS->EmitBytes(DwarfDebugProducer);
663 }
664 else {
665 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
666 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
667 MCOS->EmitBytes(StringRef(")"));
668 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000669 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
670
671 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
672 // draft has no standard code for assembler.
673 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
674
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000675 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000676
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000677 // Loop on saved info for dwarf labels and create the DIEs for them.
678 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
679 MCOS->getContext().getMCGenDwarfLabelEntries();
680 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000681 Entries.begin(), ie = Entries.end(); it != ie;
682 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000683 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000684
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000685 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686 MCOS->EmitULEB128IntValue(2);
687
688 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000689 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000690 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
691
692 // AT_decl_file, index into the file table.
693 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
694
695 // AT_decl_line, source line number.
696 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
697
698 // AT_low_pc, start address of the label.
699 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
700 MCSymbolRefExpr::VK_None, context);
701 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
702
Kevin Enderbye7739d42011-12-09 18:09:40 +0000703 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
704 MCOS->EmitIntValue(0, 1);
705
706 // The DW_TAG_unspecified_parameters DIE abbrev (3).
707 MCOS->EmitULEB128IntValue(3);
708
709 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
710 MCOS->EmitIntValue(0, 1);
711 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000712 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
713 // for the dwarf labels.
714 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000715 Entries.begin(), ie = Entries.end(); it != ie;
716 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000717 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000718 delete Entry;
719 }
720
721 // Add the NULL DIE terminating the Compile Unit DIE's.
722 MCOS->EmitIntValue(0, 1);
723
724 // Now set the value of the symbol at the end of the info section.
725 MCOS->EmitLabel(InfoEnd);
726}
727
728//
729// When generating dwarf for assembly source files this emits the Dwarf
730// sections.
731//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000732void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000733 // Create the dwarf sections in this order (.debug_line already created).
734 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000735 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000736 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000737 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000738 if (!CreateDwarfSectionSymbols)
739 LineSectionSymbol = NULL;
740 MCSymbol *AbbrevSectionSymbol = NULL;
741 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000742 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000743 if (CreateDwarfSectionSymbols) {
744 InfoSectionSymbol = context.CreateTempSymbol();
745 MCOS->EmitLabel(InfoSectionSymbol);
746 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000747 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000748 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000749 AbbrevSectionSymbol = context.CreateTempSymbol();
750 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000751 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000752 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
753
754 // If there are no line table entries then do not emit any section contents.
755 if (context.getMCLineSections().empty())
756 return;
757
758 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000759 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000760
761 // Output the data for .debug_abbrev section.
762 EmitGenDwarfAbbrev(MCOS);
763
764 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000765 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000766}
767
768//
769// When generating dwarf for assembly source files this is called when symbol
770// for a label is created. If this symbol is not a temporary and is in the
771// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000772// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000774void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000775 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000776 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777 // the default text.
778 if (Symbol->isTemporary())
779 return;
780 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000781 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000782 return;
783
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000784 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785 // underbar if any.
786 StringRef Name = Symbol->getName();
787 if (Name.startswith("_"))
788 Name = Name.substr(1, Name.size()-1);
789
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000790 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000791 unsigned FileNumber = context.getGenDwarfFileNumber();
792
793 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000794 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000795 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
796 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
797
798 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
799 // values so that they don't have things like an ARM thumb bit from the
800 // original symbol. So when used they won't get a low bit set after
801 // relocation.
802 MCSymbol *Label = context.CreateTempSymbol();
803 MCOS->EmitLabel(Label);
804
805 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000806 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000807 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
808 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000809}
810
Rafael Espindola0a017a62010-12-10 07:39:47 +0000811static int getDataAlignmentFactor(MCStreamer &streamer) {
812 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000813 const MCAsmInfo *asmInfo = context.getAsmInfo();
814 int size = asmInfo->getCalleeSaveStackSlotSize();
815 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000816 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000817 else
818 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000819}
820
Rafael Espindola5395f442011-04-22 00:08:43 +0000821static unsigned getSizeForEncoding(MCStreamer &streamer,
822 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000823 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000824 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000825 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000826 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000827 case dwarf::DW_EH_PE_absptr:
828 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000829 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000830 case dwarf::DW_EH_PE_udata2:
831 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000832 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000833 case dwarf::DW_EH_PE_udata4:
834 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000835 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000836 case dwarf::DW_EH_PE_udata8:
837 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000838 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000839 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000840}
841
842static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendlingf166ab42011-06-30 22:02:20 +0000843 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000844 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000845 const MCAsmInfo *asmInfo = context.getAsmInfo();
846 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
847 symbolEncoding,
848 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000849 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000850 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindolafd057852011-05-01 03:50:49 +0000851 streamer.EmitAbsValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000852}
853
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000854static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
855 unsigned symbolEncoding) {
856 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000857 const MCAsmInfo *asmInfo = context.getAsmInfo();
858 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
859 symbolEncoding,
860 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000861 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000862 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000863}
864
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000865namespace {
866 class FrameEmitterImpl {
867 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000868 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000869 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000870 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000871 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000872 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000873 FrameEmitterImpl(bool usingCFI, bool isEH)
874 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
875 SectionStart(0) {}
876
877 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000878
Bill Wendling3af441f2013-09-05 00:54:52 +0000879 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000880 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000881 const MCDwarfFrameInfo &frame);
882
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000883 const MCSymbol &EmitCIE(MCStreamer &streamer,
884 const MCSymbol *personality,
885 unsigned personalityEncoding,
886 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000887 bool IsSignalFrame,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000888 unsigned lsdaEncoding);
889 MCSymbol *EmitFDE(MCStreamer &streamer,
890 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000891 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000892 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000893 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000894 MCSymbol *BaseLabel);
895 void EmitCFIInstruction(MCStreamer &Streamer,
896 const MCCFIInstruction &Instr);
897 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000898
899} // end anonymous namespace
900
901static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
902 StringRef Prefix) {
903 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000904 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000905 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000906 default: EncStr = "<unknown encoding>"; break;
907 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
908 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
909 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
910 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
911 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
912 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
913 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
914 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
915 EncStr = "pcrel udata4";
916 break;
917 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
918 EncStr = "pcrel sdata4";
919 break;
920 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
921 EncStr = "pcrel udata8";
922 break;
923 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
924 EncStr = "screl sdata8";
925 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000926 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
927 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000928 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000929 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
930 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000931 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000932 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
933 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000934 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000935 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
936 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000937 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000938 }
939
940 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
941 }
942
943 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000944}
945
946void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
947 const MCCFIInstruction &Instr) {
948 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000949 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000950
951 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000952 case MCCFIInstruction::OpRegister: {
953 unsigned Reg1 = Instr.getRegister();
954 unsigned Reg2 = Instr.getRegister2();
955 if (VerboseAsm) {
956 Streamer.AddComment("DW_CFA_register");
957 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
958 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
959 }
960 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
961 Streamer.EmitULEB128IntValue(Reg1);
962 Streamer.EmitULEB128IntValue(Reg2);
963 return;
964 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000965 case MCCFIInstruction::OpWindowSave: {
966 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
967 return;
968 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000969 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000970 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000971 if (VerboseAsm) {
972 Streamer.AddComment("DW_CFA_undefined");
973 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
974 }
975 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
976 Streamer.EmitULEB128IntValue(Reg);
977 return;
978 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000979 case MCCFIInstruction::OpAdjustCfaOffset:
980 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000981 const bool IsRelative =
982 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
983
984 if (VerboseAsm)
985 Streamer.AddComment("DW_CFA_def_cfa_offset");
986 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
987
988 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000989 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000990 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000991 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000992
993 if (VerboseAsm)
994 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
995 Streamer.EmitULEB128IntValue(CFAOffset);
996
997 return;
998 }
999 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001000 if (VerboseAsm)
1001 Streamer.AddComment("DW_CFA_def_cfa");
1002 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1003
1004 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001005 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1006 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001007
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001008 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001009
1010 if (VerboseAsm)
1011 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1012 Streamer.EmitULEB128IntValue(CFAOffset);
1013
1014 return;
1015 }
1016
1017 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001018 if (VerboseAsm)
1019 Streamer.AddComment("DW_CFA_def_cfa_register");
1020 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1021
1022 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001023 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1024 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001025
1026 return;
1027 }
1028
1029 case MCCFIInstruction::OpOffset:
1030 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001031 const bool IsRelative =
1032 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001033
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001034 unsigned Reg = Instr.getRegister();
1035 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001036 if (IsRelative)
1037 Offset -= CFAOffset;
1038 Offset = Offset / dataAlignmentFactor;
1039
1040 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001041 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001042 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001043 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001044 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001045 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001046 Streamer.EmitSLEB128IntValue(Offset);
1047 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001048 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1049 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001050 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001051 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001052 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001053 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001054 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001055 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001056 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001057 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001058 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001059 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001060 }
1061 return;
1062 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001063 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001064 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001065 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1066 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001067 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001068 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001069 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1070 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001071 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001072 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001073 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001074 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001075 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001076 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001077 return;
1078 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001079 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001080 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001081 if (VerboseAsm) {
1082 Streamer.AddComment("DW_CFA_restore");
1083 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1084 }
1085 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1086 return;
1087 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001088 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001089 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001090 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001091 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001092 }
1093 llvm_unreachable("Unhandled case in switch");
1094}
1095
1096/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1097/// frame.
1098void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001099 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001100 MCSymbol *BaseLabel) {
1101 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1102 const MCCFIInstruction &Instr = Instrs[i];
1103 MCSymbol *Label = Instr.getLabel();
1104 // Throw out move if the label is invalid.
1105 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1106
1107 // Advance row if new location.
1108 if (BaseLabel && Label) {
1109 MCSymbol *ThisSym = Label;
1110 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001111 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001112 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1113 BaseLabel = ThisSym;
1114 }
1115 }
1116
1117 EmitCFIInstruction(streamer, Instr);
1118 }
1119}
1120
Bill Wendling3af441f2013-09-05 00:54:52 +00001121/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001122void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001123 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001124 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001125 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001126 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001127
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001128 // range-start range-length compact-unwind-enc personality-func lsda
1129 // _foo LfooEnd-_foo 0x00000023 0 0
1130 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1131 //
1132 // .section __LD,__compact_unwind,regular,debug
1133 //
1134 // # compact unwind for _foo
1135 // .quad _foo
1136 // .set L1,LfooEnd-_foo
1137 // .long L1
1138 // .long 0x01010001
1139 // .quad 0
1140 // .quad 0
1141 //
1142 // # compact unwind for _bar
1143 // .quad _bar
1144 // .set L2,LbarEnd-_bar
1145 // .long L2
1146 // .long 0x01020011
1147 // .quad __gxx_personality
1148 // .quad except_tab1
1149
Bill Wendling49bc6802011-07-19 00:06:12 +00001150 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001151 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001152 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001153
Bill Wendlingdafd5982011-07-14 23:34:45 +00001154 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001155 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001156 Encoding |= 0x40000000;
1157
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001158 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001159 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001160 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001161 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001162 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001163
1164 // Range Length
1165 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1166 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001167 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001168 Streamer.EmitAbsValue(Range, 4);
1169
Bill Wendling75174662011-06-30 00:30:52 +00001170 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001171 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001172 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1173 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001174 Streamer.EmitIntValue(Encoding, Size);
1175
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001176 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001177 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001178 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001179 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001180 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001181 else
1182 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001183
1184 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001185 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001186 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001187 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001188 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001189 else
1190 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001191}
1192
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001193const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1194 const MCSymbol *personality,
1195 unsigned personalityEncoding,
1196 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001197 bool IsSignalFrame,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001198 unsigned lsdaEncoding) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001199 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001200 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001201 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001202 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001203
1204 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001205 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001206 sectionStart = context.CreateTempSymbol();
1207 else
1208 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1209
Bill Wendling567a1ae2011-06-30 21:25:51 +00001210 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001211 CIENum++;
1212
Bill Wendlingb6adf462011-07-07 00:54:13 +00001213 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001214
1215 // Length
1216 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1217 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001218 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001219 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001220
1221 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001222 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001223 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001224 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001225
1226 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001227 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001228 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1229
1230 // Augmentation String
1231 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001232 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001233 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001234 Augmentation += "z";
1235 if (personality)
1236 Augmentation += "P";
1237 if (lsda)
1238 Augmentation += "L";
1239 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001240 if (IsSignalFrame)
1241 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001242 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001243 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001244 streamer.EmitIntValue(0, 1);
1245
1246 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001247 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001248 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001249
1250 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001251 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001252 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1253
1254 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001255 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001256 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001257
1258 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001259
1260 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001261 if (IsEH) {
1262 if (personality) {
1263 // Personality Encoding
1264 augmentationLength += 1;
1265 // Personality
1266 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1267 }
1268 if (lsda)
1269 augmentationLength += 1;
1270 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001271 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001272
Bill Wendling567a1ae2011-06-30 21:25:51 +00001273 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001274 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001275
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001276 // Augmentation Data (optional)
1277 if (personality) {
1278 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001279 EmitEncodingByte(streamer, personalityEncoding,
1280 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001281 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001282 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001283 EmitPersonality(streamer, *personality, personalityEncoding);
1284 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001285
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001286 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001287 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1288
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001289 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001290 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001291 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001292 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001293
1294 // Initial Instructions
1295
Bill Wendlingbc07a892013-06-18 07:20:20 +00001296 const MCAsmInfo *MAI = context.getAsmInfo();
Rafael Espindola227144c2013-05-13 01:16:13 +00001297 const std::vector<MCCFIInstruction> &Instructions =
Bill Wendlingbc07a892013-06-18 07:20:20 +00001298 MAI->getInitialFrameState();
Rafael Espindola290d7162010-12-29 01:42:56 +00001299 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001300
1301 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001302 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001303
1304 streamer.EmitLabel(sectionEnd);
1305 return *sectionStart;
1306}
1307
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001308MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1309 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001310 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001311 MCContext &context = streamer.getContext();
1312 MCSymbol *fdeStart = context.CreateTempSymbol();
1313 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001314 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001315 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001316
Evan Chengeee86452011-08-24 22:31:37 +00001317 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001318 MCSymbol *EHSym =
1319 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001320 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001321 streamer.EmitLabel(EHSym);
1322 }
1323
Rafael Espindola0a017a62010-12-10 07:39:47 +00001324 // Length
1325 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001326 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001327 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001328
1329 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001330
Rafael Espindola0a017a62010-12-10 07:39:47 +00001331 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001332 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001333 if (IsEH) {
1334 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1335 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001336 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001337 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001338 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001339 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1340 cieStart, 0);
1341 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001342 } else {
1343 streamer.EmitSymbolValue(&cieStart, 4);
1344 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001345
Rafael Espindola0a017a62010-12-10 07:39:47 +00001346 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001347 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1348 : (unsigned)dwarf::DW_EH_PE_absptr;
1349 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
1350 EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001351
1352 // PC Range
1353 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1354 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001355 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001356 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001357
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001358 if (IsEH) {
1359 // Augmentation Data Length
1360 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001361
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001362 if (frame.Lsda)
1363 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001364
Bill Wendlingf166ab42011-06-30 22:02:20 +00001365 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001366 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001367
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001368 // Augmentation Data
1369 if (frame.Lsda)
Bill Wendlingf166ab42011-06-30 22:02:20 +00001370 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1371 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001372 }
Rafael Espindola68067662011-04-29 03:06:29 +00001373
Rafael Espindola0a017a62010-12-10 07:39:47 +00001374 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001375 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001376
Rafael Espindola0a017a62010-12-10 07:39:47 +00001377 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001378 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001379
1380 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001381}
1382
Benjamin Kramer570dd782010-12-30 22:34:44 +00001383namespace {
1384 struct CIEKey {
Rafael Espindola3c47e372012-01-23 21:51:52 +00001385 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1386 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001387
Benjamin Kramer570dd782010-12-30 22:34:44 +00001388 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001389 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1390 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1391 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001392 }
1393 const MCSymbol* Personality;
1394 unsigned PersonalityEncoding;
1395 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001396 bool IsSignalFrame;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001397 };
1398}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001399
1400namespace llvm {
1401 template <>
1402 struct DenseMapInfo<CIEKey> {
1403 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001404 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001405 }
1406 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001407 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001408 }
1409 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001410 return static_cast<unsigned>(hash_combine(Key.Personality,
1411 Key.PersonalityEncoding,
1412 Key.LsdaEncoding,
1413 Key.IsSignalFrame));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001414 }
1415 static bool isEqual(const CIEKey &LHS,
1416 const CIEKey &RHS) {
1417 return LHS.Personality == RHS.Personality &&
1418 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001419 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1420 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001421 }
1422 };
1423}
1424
Bill Wendling550c76d2013-09-09 19:48:37 +00001425void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1426 bool UsingCFI, bool IsEH) {
1427 Streamer.generateCompactUnwindEncodings(MAB);
1428
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001429 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001430 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001431 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001432 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001433
Bill Wendling9803abb2011-09-06 18:37:11 +00001434 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001435 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001436 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001437 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1438 const MCDwarfFrameInfo &Frame = FrameArray[i];
1439 if (Frame.CompactUnwindEncoding == 0) continue;
1440 if (!SectionEmitted) {
1441 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001442 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001443 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001444 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001445 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001446 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001447 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001448
Bill Wendlinga800f952013-05-30 18:52:57 +00001449 const MCSection &Section =
1450 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1451 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001452 Streamer.SwitchSection(&Section);
1453 MCSymbol *SectionStart = Context.CreateTempSymbol();
1454 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001455 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001456
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001457 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001458 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001459
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001460 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001461 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1462 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001463 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001464 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001465 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1466 if (!CIEStart)
1467 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1468 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001469 Frame.IsSignalFrame,
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001470 Frame.LsdaEncoding);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001471
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001472 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001473
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001474 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001475 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001476 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001477
Bill Wendlingbc07a892013-06-18 07:20:20 +00001478 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001479 if (FDEEnd)
1480 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001481}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001482
1483void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1484 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001485 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001486 SmallString<256> Tmp;
1487 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001488 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001489 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001490}
1491
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001492void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1493 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001494 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001495 // Scale the address delta by the minimum instruction length.
1496 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1497
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001498 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001499 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001500 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1501 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001502 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001503 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1504 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001505 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001506 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001507 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001508 OS << uint8_t( AddrDelta & 0xff);
1509 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001510 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001511 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001512 assert(isUInt<32>(AddrDelta));
1513 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001514 OS << uint8_t( AddrDelta & 0xff);
1515 OS << uint8_t((AddrDelta >> 8) & 0xff);
1516 OS << uint8_t((AddrDelta >> 16) & 0xff);
1517 OS << uint8_t((AddrDelta >> 24) & 0xff);
1518
Rafael Espindola736a35d2010-12-28 05:39:27 +00001519 }
1520}