blob: fe20a6d9a98752cb01c7652a56d8cd123707571f [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
Iain Sandoe618def62014-01-08 10:22:54 +0000842static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
843 unsigned symbolEncoding, bool isEH,
844 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000845 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000846 const MCAsmInfo *asmInfo = context.getAsmInfo();
847 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
848 symbolEncoding,
849 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000850 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000851 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000852 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
853 streamer.EmitAbsValue(v, size);
854 else
855 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000856}
857
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000858static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
859 unsigned symbolEncoding) {
860 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000861 const MCAsmInfo *asmInfo = context.getAsmInfo();
862 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
863 symbolEncoding,
864 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000865 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000866 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000867}
868
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000869namespace {
870 class FrameEmitterImpl {
871 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000872 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000873 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000874 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000875 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000876 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000877 FrameEmitterImpl(bool usingCFI, bool isEH)
878 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
879 SectionStart(0) {}
880
881 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000882
Bill Wendling3af441f2013-09-05 00:54:52 +0000883 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000884 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000885 const MCDwarfFrameInfo &frame);
886
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000887 const MCSymbol &EmitCIE(MCStreamer &streamer,
888 const MCSymbol *personality,
889 unsigned personalityEncoding,
890 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000891 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000892 unsigned lsdaEncoding,
893 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000894 MCSymbol *EmitFDE(MCStreamer &streamer,
895 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000896 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000897 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000898 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000899 MCSymbol *BaseLabel);
900 void EmitCFIInstruction(MCStreamer &Streamer,
901 const MCCFIInstruction &Instr);
902 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000903
904} // end anonymous namespace
905
906static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
907 StringRef Prefix) {
908 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000909 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000910 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000911 default: EncStr = "<unknown encoding>"; break;
912 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
913 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
914 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
915 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
916 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
917 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
918 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
919 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
920 EncStr = "pcrel udata4";
921 break;
922 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
923 EncStr = "pcrel sdata4";
924 break;
925 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
926 EncStr = "pcrel udata8";
927 break;
928 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
929 EncStr = "screl sdata8";
930 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000931 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
932 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000933 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000934 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
935 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000936 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000937 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
938 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000939 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000940 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
941 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000942 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000943 }
944
945 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
946 }
947
948 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000949}
950
951void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
952 const MCCFIInstruction &Instr) {
953 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000954 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000955
956 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000957 case MCCFIInstruction::OpRegister: {
958 unsigned Reg1 = Instr.getRegister();
959 unsigned Reg2 = Instr.getRegister2();
960 if (VerboseAsm) {
961 Streamer.AddComment("DW_CFA_register");
962 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
963 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
964 }
965 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
966 Streamer.EmitULEB128IntValue(Reg1);
967 Streamer.EmitULEB128IntValue(Reg2);
968 return;
969 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000970 case MCCFIInstruction::OpWindowSave: {
971 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
972 return;
973 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000974 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000975 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000976 if (VerboseAsm) {
977 Streamer.AddComment("DW_CFA_undefined");
978 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
979 }
980 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
981 Streamer.EmitULEB128IntValue(Reg);
982 return;
983 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000984 case MCCFIInstruction::OpAdjustCfaOffset:
985 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000986 const bool IsRelative =
987 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
988
989 if (VerboseAsm)
990 Streamer.AddComment("DW_CFA_def_cfa_offset");
991 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
992
993 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000994 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000995 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000996 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000997
998 if (VerboseAsm)
999 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1000 Streamer.EmitULEB128IntValue(CFAOffset);
1001
1002 return;
1003 }
1004 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001005 if (VerboseAsm)
1006 Streamer.AddComment("DW_CFA_def_cfa");
1007 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1008
1009 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001010 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1011 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001012
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001013 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001014
1015 if (VerboseAsm)
1016 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1017 Streamer.EmitULEB128IntValue(CFAOffset);
1018
1019 return;
1020 }
1021
1022 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001023 if (VerboseAsm)
1024 Streamer.AddComment("DW_CFA_def_cfa_register");
1025 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1026
1027 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001028 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1029 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001030
1031 return;
1032 }
1033
1034 case MCCFIInstruction::OpOffset:
1035 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001036 const bool IsRelative =
1037 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001038
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001039 unsigned Reg = Instr.getRegister();
1040 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001041 if (IsRelative)
1042 Offset -= CFAOffset;
1043 Offset = Offset / dataAlignmentFactor;
1044
1045 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001046 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001047 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001048 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001049 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001050 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001051 Streamer.EmitSLEB128IntValue(Offset);
1052 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001053 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1054 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001055 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001056 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001057 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001058 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001059 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001060 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001061 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001062 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001063 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001064 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001065 }
1066 return;
1067 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001068 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001069 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1071 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001072 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001073 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001074 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1075 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001076 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001077 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001078 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001079 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001080 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001081 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001082 return;
1083 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001084 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001085 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001086 if (VerboseAsm) {
1087 Streamer.AddComment("DW_CFA_restore");
1088 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1089 }
1090 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1091 return;
1092 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001093 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001094 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001095 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001096 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001097 }
1098 llvm_unreachable("Unhandled case in switch");
1099}
1100
1101/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1102/// frame.
1103void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001104 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001105 MCSymbol *BaseLabel) {
1106 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1107 const MCCFIInstruction &Instr = Instrs[i];
1108 MCSymbol *Label = Instr.getLabel();
1109 // Throw out move if the label is invalid.
1110 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1111
1112 // Advance row if new location.
1113 if (BaseLabel && Label) {
1114 MCSymbol *ThisSym = Label;
1115 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001116 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001117 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1118 BaseLabel = ThisSym;
1119 }
1120 }
1121
1122 EmitCFIInstruction(streamer, Instr);
1123 }
1124}
1125
Bill Wendling3af441f2013-09-05 00:54:52 +00001126/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001127void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001128 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001129 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001130 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001131 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001132
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001133 // range-start range-length compact-unwind-enc personality-func lsda
1134 // _foo LfooEnd-_foo 0x00000023 0 0
1135 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1136 //
1137 // .section __LD,__compact_unwind,regular,debug
1138 //
1139 // # compact unwind for _foo
1140 // .quad _foo
1141 // .set L1,LfooEnd-_foo
1142 // .long L1
1143 // .long 0x01010001
1144 // .quad 0
1145 // .quad 0
1146 //
1147 // # compact unwind for _bar
1148 // .quad _bar
1149 // .set L2,LbarEnd-_bar
1150 // .long L2
1151 // .long 0x01020011
1152 // .quad __gxx_personality
1153 // .quad except_tab1
1154
Bill Wendling49bc6802011-07-19 00:06:12 +00001155 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001156 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001157 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001158
Bill Wendlingdafd5982011-07-14 23:34:45 +00001159 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001160 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001161 Encoding |= 0x40000000;
1162
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001163 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001164 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001165 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001166 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001167 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001168
1169 // Range Length
1170 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1171 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001172 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001173 Streamer.EmitAbsValue(Range, 4);
1174
Bill Wendling75174662011-06-30 00:30:52 +00001175 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001176 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001177 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1178 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001179 Streamer.EmitIntValue(Encoding, Size);
1180
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001181 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001182 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001183 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001184 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001185 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001186 else
1187 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001188
1189 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001190 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001191 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001192 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001193 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001194 else
1195 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001196}
1197
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001198const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1199 const MCSymbol *personality,
1200 unsigned personalityEncoding,
1201 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001202 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001203 unsigned lsdaEncoding,
1204 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001205 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001206 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001207 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001208 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001209
1210 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001211 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001212 sectionStart = context.CreateTempSymbol();
1213 else
1214 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1215
Bill Wendling567a1ae2011-06-30 21:25:51 +00001216 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001217 CIENum++;
1218
Bill Wendlingb6adf462011-07-07 00:54:13 +00001219 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001220
1221 // Length
1222 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1223 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001224 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001225 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001226
1227 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001228 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001229 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001230 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001231
1232 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001233 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001234 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1235
1236 // Augmentation String
1237 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001238 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001239 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001240 Augmentation += "z";
1241 if (personality)
1242 Augmentation += "P";
1243 if (lsda)
1244 Augmentation += "L";
1245 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001246 if (IsSignalFrame)
1247 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001248 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001249 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001250 streamer.EmitIntValue(0, 1);
1251
1252 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001253 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001254 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001255
1256 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001257 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001258 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1259
1260 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001261 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001262 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001263
1264 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001265
1266 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001267 if (IsEH) {
1268 if (personality) {
1269 // Personality Encoding
1270 augmentationLength += 1;
1271 // Personality
1272 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1273 }
1274 if (lsda)
1275 augmentationLength += 1;
1276 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001277 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001278
Bill Wendling567a1ae2011-06-30 21:25:51 +00001279 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001280 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001281
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001282 // Augmentation Data (optional)
1283 if (personality) {
1284 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001285 EmitEncodingByte(streamer, personalityEncoding,
1286 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001287 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001288 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001289 EmitPersonality(streamer, *personality, personalityEncoding);
1290 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001291
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001292 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001293 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1294
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001295 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001296 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001297 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001298 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001299
1300 // Initial Instructions
1301
Bill Wendlingbc07a892013-06-18 07:20:20 +00001302 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001303 if (!IsSimple) {
1304 const std::vector<MCCFIInstruction> &Instructions =
1305 MAI->getInitialFrameState();
1306 EmitCFIInstructions(streamer, Instructions, NULL);
1307 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001308
1309 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001310 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001311
1312 streamer.EmitLabel(sectionEnd);
1313 return *sectionStart;
1314}
1315
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001316MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1317 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001318 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001319 MCContext &context = streamer.getContext();
1320 MCSymbol *fdeStart = context.CreateTempSymbol();
1321 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001322 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001323 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001324
Evan Chengeee86452011-08-24 22:31:37 +00001325 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001326 MCSymbol *EHSym =
1327 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001328 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001329 streamer.EmitLabel(EHSym);
1330 }
1331
Rafael Espindola0a017a62010-12-10 07:39:47 +00001332 // Length
1333 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001334 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001335 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001336
1337 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001338
Rafael Espindola0a017a62010-12-10 07:39:47 +00001339 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001340 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001341 if (IsEH) {
1342 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1343 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001344 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001345 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001346 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001347 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1348 cieStart, 0);
1349 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001350 } else {
1351 streamer.EmitSymbolValue(&cieStart, 4);
1352 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001353
Rafael Espindola0a017a62010-12-10 07:39:47 +00001354 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001355 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1356 : (unsigned)dwarf::DW_EH_PE_absptr;
1357 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001358 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001359
1360 // PC Range
1361 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1362 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001363 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001364 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001365
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001366 if (IsEH) {
1367 // Augmentation Data Length
1368 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001369
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001370 if (frame.Lsda)
1371 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001372
Bill Wendlingf166ab42011-06-30 22:02:20 +00001373 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001374 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001375
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001376 // Augmentation Data
1377 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001378 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1379 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001380 }
Rafael Espindola68067662011-04-29 03:06:29 +00001381
Rafael Espindola0a017a62010-12-10 07:39:47 +00001382 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001383 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001384
Rafael Espindola0a017a62010-12-10 07:39:47 +00001385 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001386 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001387
1388 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001389}
1390
Benjamin Kramer570dd782010-12-30 22:34:44 +00001391namespace {
1392 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001393 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1394 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001395
Benjamin Kramer570dd782010-12-30 22:34:44 +00001396 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001397 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001398 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001399 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1400 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001401 }
1402 const MCSymbol* Personality;
1403 unsigned PersonalityEncoding;
1404 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001405 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001406 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001407 };
1408}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001409
1410namespace llvm {
1411 template <>
1412 struct DenseMapInfo<CIEKey> {
1413 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001414 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001415 }
1416 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001417 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001418 }
1419 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001420 return static_cast<unsigned>(hash_combine(Key.Personality,
1421 Key.PersonalityEncoding,
1422 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001423 Key.IsSignalFrame,
1424 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001425 }
1426 static bool isEqual(const CIEKey &LHS,
1427 const CIEKey &RHS) {
1428 return LHS.Personality == RHS.Personality &&
1429 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001430 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001431 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1432 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001433 }
1434 };
1435}
1436
Bill Wendling550c76d2013-09-09 19:48:37 +00001437void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1438 bool UsingCFI, bool IsEH) {
1439 Streamer.generateCompactUnwindEncodings(MAB);
1440
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001441 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001442 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001443 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001444 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001445
Bill Wendling9803abb2011-09-06 18:37:11 +00001446 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001447 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001448 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001449 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1450 const MCDwarfFrameInfo &Frame = FrameArray[i];
1451 if (Frame.CompactUnwindEncoding == 0) continue;
1452 if (!SectionEmitted) {
1453 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001454 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001455 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001456 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001457 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001458 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001459 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001460
Bill Wendlinga800f952013-05-30 18:52:57 +00001461 const MCSection &Section =
1462 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1463 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001464 Streamer.SwitchSection(&Section);
1465 MCSymbol *SectionStart = Context.CreateTempSymbol();
1466 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001467 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001468
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001469 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001470 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001471
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001472 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001473 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1474 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001475 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001476 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001477 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1478 if (!CIEStart)
1479 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1480 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001481 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001482 Frame.LsdaEncoding,
1483 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001484
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001485 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001486
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001487 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001488 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001489 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001490
Bill Wendlingbc07a892013-06-18 07:20:20 +00001491 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001492 if (FDEEnd)
1493 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001494}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001495
1496void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1497 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001498 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001499 SmallString<256> Tmp;
1500 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001501 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001502 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001503}
1504
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001505void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1506 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001507 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001508 // Scale the address delta by the minimum instruction length.
1509 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1510
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001511 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001512 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001513 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1514 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001515 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001516 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1517 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001518 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001519 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001520 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001521 OS << uint8_t( AddrDelta & 0xff);
1522 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001523 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001524 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001525 assert(isUInt<32>(AddrDelta));
1526 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001527 OS << uint8_t( AddrDelta & 0xff);
1528 OS << uint8_t((AddrDelta >> 8) & 0xff);
1529 OS << uint8_t((AddrDelta >> 16) & 0xff);
1530 OS << uint8_t((AddrDelta >> 24) & 0xff);
1531
Rafael Espindola736a35d2010-12-28 05:39:27 +00001532 }
1533}