blob: a5528a2d42f8bca63e204aeb4027580de3454c09 [file] [log] [blame]
Kevin Enderby7cbf73a2010-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 Carruthd04a8d42012-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 Chenge76a33b2011-07-20 05:58:47 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/MC/MCExpr.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000018#include "llvm/MC/MCObjectFileInfo.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000019#include "llvm/MC/MCRegisterInfo.h"
Rafael Espindolaad8aaa02010-11-22 11:53:17 +000020#include "llvm/MC/MCStreamer.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000021#include "llvm/MC/MCSymbol.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000022#include "llvm/Support/Debug.h"
Rafael Espindolafe024d02010-12-28 18:36:23 +000023#include "llvm/Support/ErrorHandling.h"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000024#include "llvm/Support/LEB128.h"
Kevin Enderby94c2e852011-12-09 18:09:40 +000025#include "llvm/Support/Path.h"
26#include "llvm/Support/SourceMgr.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/raw_ostream.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000028using namespace llvm;
29
Kevin Enderbyc0957932010-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 Wendlingc3882162011-06-30 23:59:38 +000035#define MAX_SPECIAL_ADDR_DELTA SPECIAL_ADDR(255)
Kevin Enderbyc0957932010-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 Grosbach2684d9e2012-05-11 01:41:30 +000039// "standard_opcode_lengths" table that is emitted in DwarfFileTable::Emit().
Bill Wendlingc3882162011-06-30 23:59:38 +000040#define DWARF2_LINE_OPCODE_BASE 13
Kevin Enderbyc0957932010-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 Wendlingc3882162011-06-30 23:59:38 +000044#define DWARF2_LINE_BASE -5
Kevin Enderbyc0957932010-09-30 16:52:03 +000045
46// Range of line offsets in a special line info. opcode.
Bill Wendlingc3882162011-06-30 23:59:38 +000047#define DWARF2_LINE_RANGE 14
Kevin Enderbyc0957932010-09-30 16:52:03 +000048
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +000049static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendling99cb6222013-06-18 07:20:20 +000050 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +000051 if (MinInsnLength == 1)
Kevin Enderbyc0957932010-09-30 16:52:03 +000052 return AddrDelta;
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +000053 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbyc0957932010-09-30 16:52:03 +000054 // TODO: report this error, but really only once.
55 ;
56 }
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +000057 return AddrDelta / MinInsnLength;
Kevin Enderbyc0957932010-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 Espindola195a0ce2010-11-19 02:26:16 +000065void MCLineEntry::Make(MCStreamer *MCOS, const MCSection *Section) {
Kevin Enderbyc0957932010-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 Enderby3f55c242010-10-04 20:17:24 +000081 MCOS->getContext().ClearDwarfLocSeen();
Kevin Enderbyc0957932010-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 Espindola17fd7bd2010-11-19 07:41:23 +000085 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +000086 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-11-19 07:41:23 +000087 MCLineSection *LineSection = MCLineSections.lookup(Section);
Kevin Enderbyc0957932010-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 Espindola17fd7bd2010-11-19 07:41:23 +000094 MCOS->getContext().addMCLineSection(Section, LineSection);
Kevin Enderbyc0957932010-09-30 16:52:03 +000095 }
96
97 // Add the line entry to this section's entries.
Manman Ren43213cf2013-02-05 21:52:47 +000098 LineSection->addLineEntry(LineEntry,
99 MCOS->getContext().getDwarfCompileUnitID());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000100}
101
102//
103// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000104//
Rafael Espindola5fad7a92010-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 Enderbyc0957932010-09-30 16:52:03 +0000109 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
110 const MCExpr *Res =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000111 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000112 const MCExpr *RHS =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000113 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000114 const MCExpr *Res1 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000115 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000116 const MCExpr *Res2 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000117 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000118 const MCExpr *Res3 =
Rafael Espindola5fad7a92010-12-09 23:08:35 +0000119 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000120 return Res3;
121}
122
Kevin Enderbyc0957932010-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 Espindola195a0ce2010-11-19 02:26:16 +0000127static inline void EmitDwarfLineTable(MCStreamer *MCOS,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000128 const MCSection *Section,
Manman Ren43213cf2013-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 Enderbyc0957932010-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 Enderbyc0957932010-09-30 16:52:03 +0000140 MCSymbol *LastLabel = NULL;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000141
142 // Loop through each MCLineEntry and encode the dwarf line number table.
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000143 for (MCLineSection::const_iterator
Manman Ren43213cf2013-02-05 21:52:47 +0000144 it = LineSection->getMCLineEntries(CUID).begin(),
145 ie = LineSection->getMCLineEntries(CUID).end(); it != ie; ++it) {
Kevin Enderbyc0957932010-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 Espindola3ff57092010-11-02 17:22:24 +0000150 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbyc0957932010-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 Espindola3ff57092010-11-02 17:22:24 +0000155 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbyc0957932010-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 Espindola3ff57092010-11-02 17:22:24 +0000160 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbyc0957932010-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 Espindola64185cc2010-11-13 01:06:27 +0000173 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbyc0957932010-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 Wendling99cb6222013-06-18 07:20:20 +0000179 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000180 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendling99cb6222013-06-18 07:20:20 +0000181 asmInfo->getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000182
183 LastLine = it->getLine();
184 LastLabel = Label;
Kevin Enderbyc0957932010-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 Collingbournedf39be62013-04-17 21:18:16 +0000194 // TODO: keep track of the last subsection so that this symbol appears in the
195 // correct place.
Kevin Enderbyc0957932010-09-30 16:52:03 +0000196 MCOS->SwitchSection(Section);
Rafael Espindola89b93722010-12-10 07:39:47 +0000197
198 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000199 // Create a symbol at the end of the section.
Rafael Espindola89b93722010-12-10 07:39:47 +0000200 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbyc0957932010-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 Ledruc8e41c52012-07-23 08:51:15 +0000204 // Switch back the dwarf line section.
Evan Chenge76a33b2011-07-20 05:58:47 +0000205 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000206
Bill Wendling99cb6222013-06-18 07:20:20 +0000207 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Cheng672b93a2011-07-14 05:43:07 +0000208 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendling99cb6222013-06-18 07:20:20 +0000209 asmInfo->getPointerSize());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000210}
211
212//
213// This emits the Dwarf file and the line tables.
214//
Rafael Espindola489d6792012-02-28 21:13:05 +0000215const MCSymbol *MCDwarfFileTable::Emit(MCStreamer *MCOS) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000216 MCContext &context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000217 // Switch to the section where the table will be emitted into.
Evan Chenge76a33b2011-07-20 05:58:47 +0000218 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000219
Manman Ren43213cf2013-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 Enderbyc0957932010-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 Espindola89b93722010-12-10 07:39:47 +0000253 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbyc0957932010-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 Espindola5fad7a92010-12-09 23:08:35 +0000257 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola0bbe0b42010-12-06 17:27:56 +0000258 4);
Kevin Enderbyc0957932010-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 Espindola89b93722010-12-10 07:39:47 +0000264 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbyc0957932010-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 Espindola5fad7a92010-12-09 23:08:35 +0000270 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindolaa3863ea2013-07-02 15:49:13 +0000271 (4 + 2 + 4)), 4);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000272
273 // Parameters of the state machine, are next.
Bill Wendling99cb6222013-06-18 07:20:20 +0000274 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbyc0957932010-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 Ren9e999ad2013-03-12 20:17:00 +0000297 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Manman Ren3de61b42013-03-07 01:42:00 +0000298 context.getMCDwarfDirs(CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000299 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christopher68ca5622013-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 Enderbyc0957932010-09-30 16:52:03 +0000302 }
303 MCOS->EmitIntValue(0, 1); // Terminate the directory list
304
305 // Second the file table.
Manman Ren9e999ad2013-03-12 20:17:00 +0000306 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Manman Ren3de61b42013-03-07 01:42:00 +0000307 MCOS->getContext().getMCDwarfFiles(CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000308 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
Eric Christopher68ca5622013-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 Espindola3ff57092010-11-02 17:22:24 +0000311 // the Directory num
312 MCOS->EmitULEB128IntValue(MCDwarfFiles[i]->getDirIndex());
Kevin Enderbyc0957932010-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 Espindola17fd7bd2010-11-19 07:41:23 +0000323 const DenseMap<const MCSection *, MCLineSection *> &MCLineSections =
Kevin Enderbyc0957932010-09-30 16:52:03 +0000324 MCOS->getContext().getMCLineSections();
Rafael Espindola17fd7bd2010-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 Wendlingc3882162011-06-30 23:59:38 +0000328 MCLineSectionOrder.begin(), ie = MCLineSectionOrder.end(); it != ie;
Rafael Espindola17fd7bd2010-11-19 07:41:23 +0000329 ++it) {
330 const MCSection *Sec = *it;
331 const MCLineSection *Line = MCLineSections.lookup(Sec);
Manman Ren43213cf2013-02-05 21:52:47 +0000332 EmitDwarfLineTable(MCOS, Sec, Line, CUID);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000333 }
334
Bill Wendling99cb6222013-06-18 07:20:20 +0000335 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines()
Rafael Espindola767b1be2010-12-04 00:31:13 +0000336 && MCLineSectionOrder.begin() == MCLineSectionOrder.end()) {
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000337 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000338 // it requires:
Rafael Espindolaa8de83c2010-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 Patel5113cdb2010-12-03 00:10:48 +0000342
Rafael Espindolaa8de83c2010-12-03 23:36:59 +0000343 // The regular end_sequence should be sufficient.
344 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5113cdb2010-12-03 00:10:48 +0000345 }
346
Kevin Enderbyc0957932010-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 Espindola489d6792012-02-28 21:13:05 +0000350
351 return LineStartSym;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000352}
353
Kevin Enderbyc0957932010-09-30 16:52:03 +0000354/// Utility function to emit the encoding to a streamer.
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000355void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbyc0957932010-09-30 16:52:03 +0000356 uint64_t AddrDelta) {
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +0000357 MCContext &Context = MCOS->getContext();
Kevin Enderbyc0957932010-09-30 16:52:03 +0000358 SmallString<256> Tmp;
359 raw_svector_ostream OS(Tmp);
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +0000360 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christopher68ca5622013-01-09 01:57:54 +0000361 MCOS->EmitBytes(OS.str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000362}
363
364/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +0000365void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
366 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbyc0957932010-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 Weigandc1f4a4b2013-06-12 14:46:54 +0000371 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbyc0957932010-09-30 16:52:03 +0000372
373 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000374 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbyc0957932010-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 Grosbach2d39a0e2012-08-08 23:56:06 +0000381 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-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 Grosbach2d39a0e2012-08-08 23:56:06 +0000396 encodeSLEB128(LineDelta, OS);
Kevin Enderbyc0957932010-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 Grosbach2d39a0e2012-08-08 23:56:06 +0000432 encodeULEB128(AddrDelta, OS);
Kevin Enderbyc0957932010-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 Enderby7cbf73a2010-07-28 20:55:35 +0000440void MCDwarfFile::print(raw_ostream &OS) const {
441 OS << '"' << getName() << '"';
442}
443
Manman Ren286c4dc2012-09-12 05:06:18 +0000444#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000445void MCDwarfFile::dump() const {
446 print(dbgs());
447}
Manman Rencc77eec2012-09-06 19:55:56 +0000448#endif
Kevin Enderbyc0957932010-09-30 16:52:03 +0000449
Kevin Enderby94c2e852011-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);
470 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
471 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
472 if (!DwarfDebugFlags.empty())
473 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
474 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
475 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
476 EmitAbbrev(MCOS, 0, 0);
477
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000478 // DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000479 MCOS->EmitULEB128IntValue(2);
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000480 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000481 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
482 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
483 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
484 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
485 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000486 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
487 EmitAbbrev(MCOS, 0, 0);
488
489 // DW_TAG_unspecified_parameters DIE abbrev (3).
490 MCOS->EmitULEB128IntValue(3);
491 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
492 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
493 EmitAbbrev(MCOS, 0, 0);
494
495 // Terminate the abbreviations for this compilation unit.
496 MCOS->EmitIntValue(0, 1);
497}
498
499// When generating dwarf for assembly source files this emits the data for
500// .debug_aranges section. Which contains a header and a table of pairs of
501// PointerSize'ed values for the address and size of section(s) with line table
502// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000503static void EmitGenDwarfAranges(MCStreamer *MCOS,
504 const MCSymbol *InfoSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000505 MCContext &context = MCOS->getContext();
506
507 // Create a symbol at the end of the section that we are creating the dwarf
508 // debugging info to use later in here as part of the expression to calculate
509 // the size of the section for the table.
510 MCOS->SwitchSection(context.getGenDwarfSection());
511 MCSymbol *SectionEndSym = context.CreateTempSymbol();
512 MCOS->EmitLabel(SectionEndSym);
513 context.setGenDwarfSectionEndSym(SectionEndSym);
514
515 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
516
517 // This will be the length of the .debug_aranges section, first account for
518 // the size of each item in the header (see below where we emit these items).
519 int Length = 4 + 2 + 4 + 1 + 1;
520
521 // Figure the padding after the header before the table of address and size
522 // pairs who's values are PointerSize'ed.
Bill Wendling99cb6222013-06-18 07:20:20 +0000523 const MCAsmInfo *asmInfo = context.getAsmInfo();
524 int AddrSize = asmInfo->getPointerSize();
Kevin Enderby94c2e852011-12-09 18:09:40 +0000525 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
526 if (Pad == 2 * AddrSize)
527 Pad = 0;
528 Length += Pad;
529
530 // Add the size of the pair of PointerSize'ed values for the address and size
531 // of the one default .text section we have in the table.
532 Length += 2 * AddrSize;
533 // And the pair of terminating zeros.
534 Length += 2 * AddrSize;
535
536
537 // Emit the header for this section.
538 // The 4 byte length not including the 4 byte value for the length.
539 MCOS->EmitIntValue(Length - 4, 4);
540 // The 2 byte version, which is 2.
541 MCOS->EmitIntValue(2, 2);
542 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000543 // of the .debug_info.
544 if (InfoSectionSymbol)
545 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
546 else
547 MCOS->EmitIntValue(0, 4);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000548 // The 1 byte size of an address.
549 MCOS->EmitIntValue(AddrSize, 1);
550 // The 1 byte size of a segment descriptor, we use a value of zero.
551 MCOS->EmitIntValue(0, 1);
552 // Align the header with the padding if needed, before we put out the table.
553 for(int i = 0; i < Pad; i++)
554 MCOS->EmitIntValue(0, 1);
555
556 // Now emit the table of pairs of PointerSize'ed values for the section(s)
557 // address and size, in our case just the one default .text section.
558 const MCExpr *Addr = MCSymbolRefExpr::Create(
559 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
560 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
561 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
562 MCOS->EmitAbsValue(Addr, AddrSize);
563 MCOS->EmitAbsValue(Size, AddrSize);
564
565 // And finally the pair of terminating zeros.
566 MCOS->EmitIntValue(0, AddrSize);
567 MCOS->EmitIntValue(0, AddrSize);
568}
569
570// When generating dwarf for assembly source files this emits the data for
571// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000572// DIE and a list of label DIEs.
Rafael Espindola489d6792012-02-28 21:13:05 +0000573static void EmitGenDwarfInfo(MCStreamer *MCOS,
574 const MCSymbol *AbbrevSectionSymbol,
575 const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000576 MCContext &context = MCOS->getContext();
577
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000578 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000579
580 // Create a symbol at the start and end of this section used in here for the
581 // expression to calculate the length in the header.
582 MCSymbol *InfoStart = context.CreateTempSymbol();
583 MCOS->EmitLabel(InfoStart);
584 MCSymbol *InfoEnd = context.CreateTempSymbol();
585
586 // First part: the header.
587
588 // The 4 byte total length of the information for this compilation unit, not
589 // including these 4 bytes.
590 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
591 MCOS->EmitAbsValue(Length, 4);
592
593 // The 2 byte DWARF version, which is 2.
594 MCOS->EmitIntValue(2, 2);
595
596 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
597 // it is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000598 if (AbbrevSectionSymbol) {
599 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
600 } else {
601 MCOS->EmitIntValue(0, 4);
602 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000603
Bill Wendling99cb6222013-06-18 07:20:20 +0000604 const MCAsmInfo *asmInfo = context.getAsmInfo();
605 int AddrSize = asmInfo->getPointerSize();
Kevin Enderby94c2e852011-12-09 18:09:40 +0000606 // The 1 byte size of an address.
607 MCOS->EmitIntValue(AddrSize, 1);
608
609 // Second part: the compile_unit DIE.
610
611 // The DW_TAG_compile_unit DIE abbrev (1).
612 MCOS->EmitULEB128IntValue(1);
613
614 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
615 // which is at the start of that section so this is zero.
Rafael Espindola489d6792012-02-28 21:13:05 +0000616 if (LineSectionSymbol) {
617 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
618 } else {
619 MCOS->EmitIntValue(0, 4);
620 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000621
622 // AT_low_pc, the first address of the default .text section.
623 const MCExpr *Start = MCSymbolRefExpr::Create(
624 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
625 MCOS->EmitAbsValue(Start, AddrSize);
626
627 // AT_high_pc, the last address of the default .text section.
628 const MCExpr *End = MCSymbolRefExpr::Create(
629 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
630 MCOS->EmitAbsValue(End, AddrSize);
631
632 // AT_name, the name of the source file. Reconstruct from the first directory
633 // and file table entries.
Manman Ren9e999ad2013-03-12 20:17:00 +0000634 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000635 context.getMCDwarfDirs();
636 if (MCDwarfDirs.size() > 0) {
Eric Christopher68ca5622013-01-09 01:57:54 +0000637 MCOS->EmitBytes(MCDwarfDirs[0]);
638 MCOS->EmitBytes("/");
Kevin Enderby94c2e852011-12-09 18:09:40 +0000639 }
Manman Ren9e999ad2013-03-12 20:17:00 +0000640 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000641 MCOS->getContext().getMCDwarfFiles();
Eric Christopher68ca5622013-01-09 01:57:54 +0000642 MCOS->EmitBytes(MCDwarfFiles[1]->getName());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000643 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
644
645 // AT_comp_dir, the working directory the assembly was done in.
Eric Christopher68ca5622013-01-09 01:57:54 +0000646 MCOS->EmitBytes(context.getCompilationDir());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000647 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
648
649 // AT_APPLE_flags, the command line arguments of the assembler tool.
650 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
651 if (!DwarfDebugFlags.empty()){
Eric Christopher68ca5622013-01-09 01:57:54 +0000652 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000653 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
654 }
655
656 // AT_producer, the version of the assembler tool.
Kevin Enderby75c9b932013-01-16 17:46:23 +0000657 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
658 if (!DwarfDebugProducer.empty()){
659 MCOS->EmitBytes(DwarfDebugProducer);
660 }
661 else {
662 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
663 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
664 MCOS->EmitBytes(StringRef(")"));
665 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000666 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
667
668 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
669 // draft has no standard code for assembler.
670 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
671
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000672 // Third part: the list of label DIEs.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000673
Kevin Enderby11c2def2012-01-10 21:12:34 +0000674 // Loop on saved info for dwarf labels and create the DIEs for them.
675 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
676 MCOS->getContext().getMCGenDwarfLabelEntries();
677 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000678 Entries.begin(), ie = Entries.end(); it != ie;
679 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000680 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000681
Kevin Enderby38fdb7d2012-01-10 17:52:29 +0000682 // The DW_TAG_label DIE abbrev (2).
Kevin Enderby94c2e852011-12-09 18:09:40 +0000683 MCOS->EmitULEB128IntValue(2);
684
685 // AT_name, of the label without any leading underbar.
Eric Christopher68ca5622013-01-09 01:57:54 +0000686 MCOS->EmitBytes(Entry->getName());
Kevin Enderby94c2e852011-12-09 18:09:40 +0000687 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
688
689 // AT_decl_file, index into the file table.
690 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
691
692 // AT_decl_line, source line number.
693 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
694
695 // AT_low_pc, start address of the label.
696 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
697 MCSymbolRefExpr::VK_None, context);
698 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
699
Kevin Enderby94c2e852011-12-09 18:09:40 +0000700 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
701 MCOS->EmitIntValue(0, 1);
702
703 // The DW_TAG_unspecified_parameters DIE abbrev (3).
704 MCOS->EmitULEB128IntValue(3);
705
706 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
707 MCOS->EmitIntValue(0, 1);
708 }
Kevin Enderby11c2def2012-01-10 21:12:34 +0000709 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
710 // for the dwarf labels.
711 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderby94c2e852011-12-09 18:09:40 +0000712 Entries.begin(), ie = Entries.end(); it != ie;
713 ++it) {
Kevin Enderby11c2def2012-01-10 21:12:34 +0000714 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000715 delete Entry;
716 }
717
718 // Add the NULL DIE terminating the Compile Unit DIE's.
719 MCOS->EmitIntValue(0, 1);
720
721 // Now set the value of the symbol at the end of the info section.
722 MCOS->EmitLabel(InfoEnd);
723}
724
725//
726// When generating dwarf for assembly source files this emits the Dwarf
727// sections.
728//
Rafael Espindola489d6792012-02-28 21:13:05 +0000729void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderby94c2e852011-12-09 18:09:40 +0000730 // Create the dwarf sections in this order (.debug_line already created).
731 MCContext &context = MCOS->getContext();
Bill Wendling99cb6222013-06-18 07:20:20 +0000732 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000733 bool CreateDwarfSectionSymbols =
Bill Wendling99cb6222013-06-18 07:20:20 +0000734 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000735 if (!CreateDwarfSectionSymbols)
736 LineSectionSymbol = NULL;
737 MCSymbol *AbbrevSectionSymbol = NULL;
738 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderby94c2e852011-12-09 18:09:40 +0000739 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000740 if (CreateDwarfSectionSymbols) {
741 InfoSectionSymbol = context.CreateTempSymbol();
742 MCOS->EmitLabel(InfoSectionSymbol);
743 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000744 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000745 if (CreateDwarfSectionSymbols) {
Rafael Espindola489d6792012-02-28 21:13:05 +0000746 AbbrevSectionSymbol = context.CreateTempSymbol();
747 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindola489d6792012-02-28 21:13:05 +0000748 }
Kevin Enderby94c2e852011-12-09 18:09:40 +0000749 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
750
751 // If there are no line table entries then do not emit any section contents.
752 if (context.getMCLineSections().empty())
753 return;
754
755 // Output the data for .debug_aranges section.
Alexey Samsonov15ab1152012-11-14 09:55:38 +0000756 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000757
758 // Output the data for .debug_abbrev section.
759 EmitGenDwarfAbbrev(MCOS);
760
761 // Output the data for .debug_info section.
Rafael Espindola489d6792012-02-28 21:13:05 +0000762 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000763}
764
765//
766// When generating dwarf for assembly source files this is called when symbol
767// for a label is created. If this symbol is not a temporary and is in the
768// section that dwarf is being generated for, save the needed info to create
Kevin Enderby11c2def2012-01-10 21:12:34 +0000769// a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000770//
Kevin Enderby11c2def2012-01-10 21:12:34 +0000771void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderby94c2e852011-12-09 18:09:40 +0000772 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher3c3cdcc2012-02-25 01:02:44 +0000773 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderby94c2e852011-12-09 18:09:40 +0000774 // the default text.
775 if (Symbol->isTemporary())
776 return;
777 MCContext &context = MCOS->getContext();
Peter Collingbournedf39be62013-04-17 21:18:16 +0000778 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderby94c2e852011-12-09 18:09:40 +0000779 return;
780
Kevin Enderby11c2def2012-01-10 21:12:34 +0000781 // The dwarf label's name does not have the symbol name's leading
Kevin Enderby94c2e852011-12-09 18:09:40 +0000782 // underbar if any.
783 StringRef Name = Symbol->getName();
784 if (Name.startswith("_"))
785 Name = Name.substr(1, Name.size()-1);
786
Kevin Enderby11c2def2012-01-10 21:12:34 +0000787 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000788 unsigned FileNumber = context.getGenDwarfFileNumber();
789
790 // Finding the line number is the expensive part which is why we just don't
Kevin Enderby11c2def2012-01-10 21:12:34 +0000791 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderby94c2e852011-12-09 18:09:40 +0000792 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
793 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
794
795 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
796 // values so that they don't have things like an ARM thumb bit from the
797 // original symbol. So when used they won't get a low bit set after
798 // relocation.
799 MCSymbol *Label = context.CreateTempSymbol();
800 MCOS->EmitLabel(Label);
801
802 // Create and entry for the info and add it to the other entries.
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000803 MCGenDwarfLabelEntry *Entry =
Kevin Enderby11c2def2012-01-10 21:12:34 +0000804 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
805 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderby94c2e852011-12-09 18:09:40 +0000806}
807
Rafael Espindola89b93722010-12-10 07:39:47 +0000808static int getDataAlignmentFactor(MCStreamer &streamer) {
809 MCContext &context = streamer.getContext();
Bill Wendling99cb6222013-06-18 07:20:20 +0000810 const MCAsmInfo *asmInfo = context.getAsmInfo();
811 int size = asmInfo->getCalleeSaveStackSlotSize();
812 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola89b93722010-12-10 07:39:47 +0000813 return size;
Evan Cheng1be0e272011-07-15 02:09:41 +0000814 else
815 return -size;
Rafael Espindola89b93722010-12-10 07:39:47 +0000816}
817
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000818static unsigned getSizeForEncoding(MCStreamer &streamer,
819 unsigned symbolEncoding) {
Rafael Espindolabdc31672010-12-27 15:56:22 +0000820 MCContext &context = streamer.getContext();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000821 unsigned format = symbolEncoding & 0x0f;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000822 switch (format) {
Craig Topper85814382012-02-07 05:05:23 +0000823 default: llvm_unreachable("Unknown Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +0000824 case dwarf::DW_EH_PE_absptr:
825 case dwarf::DW_EH_PE_signed:
Bill Wendling99cb6222013-06-18 07:20:20 +0000826 return context.getAsmInfo()->getPointerSize();
Rafael Espindolabdc31672010-12-27 15:56:22 +0000827 case dwarf::DW_EH_PE_udata2:
828 case dwarf::DW_EH_PE_sdata2:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000829 return 2;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000830 case dwarf::DW_EH_PE_udata4:
831 case dwarf::DW_EH_PE_sdata4:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000832 return 4;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000833 case dwarf::DW_EH_PE_udata8:
834 case dwarf::DW_EH_PE_sdata8:
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000835 return 8;
Rafael Espindolabdc31672010-12-27 15:56:22 +0000836 }
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000837}
838
839static void EmitSymbol(MCStreamer &streamer, const MCSymbol &symbol,
Bill Wendling2541c412011-06-30 22:02:20 +0000840 unsigned symbolEncoding, const char *comment = 0) {
Rafael Espindolaa0057ca2011-04-28 21:04:39 +0000841 MCContext &context = streamer.getContext();
Bill Wendling99cb6222013-06-18 07:20:20 +0000842 const MCAsmInfo *asmInfo = context.getAsmInfo();
843 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
844 symbolEncoding,
845 streamer);
Rafael Espindolaabf9af62011-04-22 00:08:43 +0000846 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendling2541c412011-06-30 22:02:20 +0000847 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000848 streamer.EmitAbsValue(v, size);
Rafael Espindolabdc31672010-12-27 15:56:22 +0000849}
850
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000851static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
852 unsigned symbolEncoding) {
853 MCContext &context = streamer.getContext();
Bill Wendling99cb6222013-06-18 07:20:20 +0000854 const MCAsmInfo *asmInfo = context.getAsmInfo();
855 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
856 symbolEncoding,
857 streamer);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000858 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000859 streamer.EmitValue(v, size);
Rafael Espindolabfa27cc2011-04-28 16:09:09 +0000860}
861
Rafael Espindola25f492e2011-04-12 16:12:03 +0000862namespace {
863 class FrameEmitterImpl {
864 int CFAOffset;
Rafael Espindola514cecc2011-04-28 03:26:11 +0000865 int CIENum;
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000866 bool UsingCFI;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +0000867 bool IsEH;
Rafael Espindolae3a0e982011-05-10 20:59:42 +0000868 const MCSymbol *SectionStart;
Rafael Espindola25f492e2011-04-12 16:12:03 +0000869 public:
Bill Wendling1424c3c2011-07-22 21:18:59 +0000870 FrameEmitterImpl(bool usingCFI, bool isEH)
871 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
872 SectionStart(0) {}
873
874 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola25f492e2011-04-12 16:12:03 +0000875
Bill Wendlingd6d2ef12013-09-05 00:54:52 +0000876 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling3c08eef2013-04-10 22:03:59 +0000877 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge3cd13f2011-06-23 01:06:23 +0000878 const MCDwarfFrameInfo &frame);
879
Rafael Espindola25f492e2011-04-12 16:12:03 +0000880 const MCSymbol &EmitCIE(MCStreamer &streamer,
881 const MCSymbol *personality,
882 unsigned personalityEncoding,
883 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +0000884 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000885 unsigned lsdaEncoding);
886 MCSymbol *EmitFDE(MCStreamer &streamer,
887 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +0000888 const MCDwarfFrameInfo &frame);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000889 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling9e4bd872013-09-04 22:35:29 +0000890 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola25f492e2011-04-12 16:12:03 +0000891 MCSymbol *BaseLabel);
892 void EmitCFIInstruction(MCStreamer &Streamer,
893 const MCCFIInstruction &Instr);
894 };
Bill Wendling3c163cf2011-06-30 21:25:51 +0000895
896} // end anonymous namespace
897
898static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
899 StringRef Prefix) {
900 if (Streamer.isVerboseAsm()) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000901 const char *EncStr;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000902 switch (Encoding) {
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000903 default: EncStr = "<unknown encoding>"; break;
904 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
905 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
906 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
907 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
908 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
909 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
910 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
911 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
912 EncStr = "pcrel udata4";
913 break;
914 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
915 EncStr = "pcrel sdata4";
916 break;
917 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
918 EncStr = "pcrel udata8";
919 break;
920 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
921 EncStr = "screl sdata8";
922 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000923 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
924 EncStr = "indirect pcrel udata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000925 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000926 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
927 EncStr = "indirect pcrel sdata4";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000928 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000929 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
930 EncStr = "indirect pcrel udata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000931 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000932 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
933 EncStr = "indirect pcrel sdata8";
Benjamin Kramereb9fa662012-01-20 14:42:37 +0000934 break;
Bill Wendling3c163cf2011-06-30 21:25:51 +0000935 }
936
937 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
938 }
939
940 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola25f492e2011-04-12 16:12:03 +0000941}
942
943void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
944 const MCCFIInstruction &Instr) {
945 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlingefd24dd2011-06-30 21:45:12 +0000946 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola25f492e2011-04-12 16:12:03 +0000947
948 switch (Instr.getOperation()) {
Rafael Espindolaf4f14f62012-11-25 15:14:49 +0000949 case MCCFIInstruction::OpRegister: {
950 unsigned Reg1 = Instr.getRegister();
951 unsigned Reg2 = Instr.getRegister2();
952 if (VerboseAsm) {
953 Streamer.AddComment("DW_CFA_register");
954 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
955 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
956 }
957 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
958 Streamer.EmitULEB128IntValue(Reg1);
959 Streamer.EmitULEB128IntValue(Reg2);
960 return;
961 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000962 case MCCFIInstruction::OpUndefined: {
Rafael Espindolaff233c92012-11-24 04:33:48 +0000963 unsigned Reg = Instr.getRegister();
Rafael Espindolac8fec7e2012-11-23 16:59:41 +0000964 if (VerboseAsm) {
965 Streamer.AddComment("DW_CFA_undefined");
966 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
967 }
968 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
969 Streamer.EmitULEB128IntValue(Reg);
970 return;
971 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000972 case MCCFIInstruction::OpAdjustCfaOffset:
973 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000974 const bool IsRelative =
975 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
976
977 if (VerboseAsm)
978 Streamer.AddComment("DW_CFA_def_cfa_offset");
979 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
980
981 if (IsRelative)
Rafael Espindolaff233c92012-11-24 04:33:48 +0000982 CFAOffset += Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000983 else
Rafael Espindolaff233c92012-11-24 04:33:48 +0000984 CFAOffset = -Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000985
986 if (VerboseAsm)
987 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
988 Streamer.EmitULEB128IntValue(CFAOffset);
989
990 return;
991 }
992 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +0000993 if (VerboseAsm)
994 Streamer.AddComment("DW_CFA_def_cfa");
995 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
996
997 if (VerboseAsm)
Rafael Espindolaff233c92012-11-24 04:33:48 +0000998 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
999 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001000
Rafael Espindolaff233c92012-11-24 04:33:48 +00001001 CFAOffset = -Instr.getOffset();
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001002
1003 if (VerboseAsm)
1004 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1005 Streamer.EmitULEB128IntValue(CFAOffset);
1006
1007 return;
1008 }
1009
1010 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001011 if (VerboseAsm)
1012 Streamer.AddComment("DW_CFA_def_cfa_register");
1013 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1014
1015 if (VerboseAsm)
Rafael Espindolaff233c92012-11-24 04:33:48 +00001016 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1017 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001018
1019 return;
1020 }
1021
1022 case MCCFIInstruction::OpOffset:
1023 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001024 const bool IsRelative =
1025 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001026
Rafael Espindolaff233c92012-11-24 04:33:48 +00001027 unsigned Reg = Instr.getRegister();
1028 int Offset = Instr.getOffset();
Rafael Espindola25f492e2011-04-12 16:12:03 +00001029 if (IsRelative)
1030 Offset -= CFAOffset;
1031 Offset = Offset / dataAlignmentFactor;
1032
1033 if (Offset < 0) {
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001034 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001035 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001036 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola25f492e2011-04-12 16:12:03 +00001037 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001038 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola25f492e2011-04-12 16:12:03 +00001039 Streamer.EmitSLEB128IntValue(Offset);
1040 } else if (Reg < 64) {
Bill Wendlinge08d4332011-06-30 23:47:40 +00001041 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1042 Twine(Reg) + ")");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001043 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001044 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001045 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001046 } else {
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001047 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001048 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001049 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001050 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001051 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaeccbad72011-04-21 23:26:40 +00001052 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001053 }
1054 return;
1055 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001056 case MCCFIInstruction::OpRememberState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001057 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001058 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1059 return;
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001060 case MCCFIInstruction::OpRestoreState:
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001061 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001062 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1063 return;
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001064 case MCCFIInstruction::OpSameValue: {
Rafael Espindolaff233c92012-11-24 04:33:48 +00001065 unsigned Reg = Instr.getRegister();
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001066 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001067 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlingefd24dd2011-06-30 21:45:12 +00001068 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolae8cfbd82011-04-21 23:39:26 +00001069 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola25f492e2011-04-12 16:12:03 +00001070 return;
1071 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001072 case MCCFIInstruction::OpRestore: {
Rafael Espindolaff233c92012-11-24 04:33:48 +00001073 unsigned Reg = Instr.getRegister();
Rafael Espindolaed23bdb2011-12-29 21:43:03 +00001074 if (VerboseAsm) {
1075 Streamer.AddComment("DW_CFA_restore");
1076 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1077 }
1078 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1079 return;
1080 }
Rafael Espindola7f74d2c2012-11-24 03:10:54 +00001081 case MCCFIInstruction::OpEscape:
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001082 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christopher68ca5622013-01-09 01:57:54 +00001083 Streamer.EmitBytes(Instr.getValues());
Rafael Espindola6f0b1812011-12-29 20:24:47 +00001084 return;
Rafael Espindola25f492e2011-04-12 16:12:03 +00001085 }
1086 llvm_unreachable("Unhandled case in switch");
1087}
1088
1089/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1090/// frame.
1091void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling9e4bd872013-09-04 22:35:29 +00001092 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001093 MCSymbol *BaseLabel) {
1094 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1095 const MCCFIInstruction &Instr = Instrs[i];
1096 MCSymbol *Label = Instr.getLabel();
1097 // Throw out move if the label is invalid.
1098 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1099
1100 // Advance row if new location.
1101 if (BaseLabel && Label) {
1102 MCSymbol *ThisSym = Label;
1103 if (ThisSym != BaseLabel) {
Bill Wendlingd221cd62011-06-30 22:35:49 +00001104 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola25f492e2011-04-12 16:12:03 +00001105 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1106 BaseLabel = ThisSym;
1107 }
1108 }
1109
1110 EmitCFIInstruction(streamer, Instr);
1111 }
1112}
1113
Bill Wendlingd6d2ef12013-09-05 00:54:52 +00001114/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling3c08eef2013-04-10 22:03:59 +00001115void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001116 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001117 MCContext &Context = Streamer.getContext();
Evan Chenge76a33b2011-07-20 05:58:47 +00001118 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001119 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001120
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001121 // range-start range-length compact-unwind-enc personality-func lsda
1122 // _foo LfooEnd-_foo 0x00000023 0 0
1123 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1124 //
1125 // .section __LD,__compact_unwind,regular,debug
1126 //
1127 // # compact unwind for _foo
1128 // .quad _foo
1129 // .set L1,LfooEnd-_foo
1130 // .long L1
1131 // .long 0x01010001
1132 // .quad 0
1133 // .quad 0
1134 //
1135 // # compact unwind for _bar
1136 // .quad _bar
1137 // .set L2,LbarEnd-_bar
1138 // .long L2
1139 // .long 0x01020011
1140 // .quad __gxx_personality
1141 // .quad except_tab1
1142
Bill Wendlinge52e3f22011-07-19 00:06:12 +00001143 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendlingfa2b25c2013-04-18 23:16:46 +00001144 if (!Encoding) return;
Bill Wendling62c75ad2013-04-10 21:42:06 +00001145 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001146
Bill Wendling4bb5b032011-07-14 23:34:45 +00001147 // The encoding needs to know we have an LSDA.
Bill Wendling62c75ad2013-04-10 21:42:06 +00001148 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling4bb5b032011-07-14 23:34:45 +00001149 Encoding |= 0x40000000;
1150
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001151 // Range Start
Evan Cheng203576a2011-07-20 19:50:42 +00001152 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling9056e902011-06-29 23:49:12 +00001153 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001154 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling9056e902011-06-29 23:49:12 +00001155 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001156
1157 // Range Length
1158 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1159 *Frame.End, 0);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001160 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling9287a6e2011-06-30 00:30:52 +00001161 Streamer.EmitAbsValue(Range, 4);
1162
Bill Wendling9287a6e2011-06-30 00:30:52 +00001163 // Compact Encoding
Bill Wendling9287a6e2011-06-30 00:30:52 +00001164 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer70be28a2011-11-07 21:00:59 +00001165 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1166 Twine::utohexstr(Encoding));
Bill Wendling9287a6e2011-06-30 00:30:52 +00001167 Streamer.EmitIntValue(Encoding, Size);
1168
Bill Wendling9056e902011-06-29 23:49:12 +00001169 // Personality Function
Bill Wendling4bb5b032011-07-14 23:34:45 +00001170 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001171 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling62c75ad2013-04-10 21:42:06 +00001172 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling9056e902011-06-29 23:49:12 +00001173 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001174 else
1175 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling9056e902011-06-29 23:49:12 +00001176
1177 // LSDA
Bill Wendling4498d392011-06-29 23:53:16 +00001178 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001179 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling62c75ad2013-04-10 21:42:06 +00001180 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling9056e902011-06-29 23:49:12 +00001181 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4498d392011-06-29 23:53:16 +00001182 else
1183 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001184}
1185
Rafael Espindola25f492e2011-04-12 16:12:03 +00001186const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1187 const MCSymbol *personality,
1188 unsigned personalityEncoding,
1189 const MCSymbol *lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001190 bool IsSignalFrame,
Rafael Espindola25f492e2011-04-12 16:12:03 +00001191 unsigned lsdaEncoding) {
Rafael Espindola89b93722010-12-10 07:39:47 +00001192 MCContext &context = streamer.getContext();
Bill Wendling99cb6222013-06-18 07:20:20 +00001193 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Chenge76a33b2011-07-20 05:58:47 +00001194 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling3c163cf2011-06-30 21:25:51 +00001195 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola514cecc2011-04-28 03:26:11 +00001196
1197 MCSymbol *sectionStart;
Evan Chenge76a33b2011-07-20 05:58:47 +00001198 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindola514cecc2011-04-28 03:26:11 +00001199 sectionStart = context.CreateTempSymbol();
1200 else
1201 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1202
Bill Wendling3c163cf2011-06-30 21:25:51 +00001203 streamer.EmitLabel(sectionStart);
Rafael Espindola514cecc2011-04-28 03:26:11 +00001204 CIENum++;
1205
Bill Wendling6a6b8c32011-07-07 00:54:13 +00001206 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola89b93722010-12-10 07:39:47 +00001207
1208 // Length
1209 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1210 *sectionEnd, 4);
Bill Wendling3c163cf2011-06-30 21:25:51 +00001211 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001212 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001213
1214 // CIE ID
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001215 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling3c163cf2011-06-30 21:25:51 +00001216 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001217 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001218
1219 // Version
Bill Wendling3c163cf2011-06-30 21:25:51 +00001220 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola89b93722010-12-10 07:39:47 +00001221 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1222
1223 // Augmentation String
1224 SmallString<8> Augmentation;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001225 if (IsEH) {
Bill Wendling3c163cf2011-06-30 21:25:51 +00001226 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001227 Augmentation += "z";
1228 if (personality)
1229 Augmentation += "P";
1230 if (lsda)
1231 Augmentation += "L";
1232 Augmentation += "R";
Rafael Espindola16d7d432012-01-23 21:51:52 +00001233 if (IsSignalFrame)
1234 Augmentation += "S";
Eric Christopher68ca5622013-01-09 01:57:54 +00001235 streamer.EmitBytes(Augmentation.str());
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001236 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001237 streamer.EmitIntValue(0, 1);
1238
1239 // Code Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001240 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendling99cb6222013-06-18 07:20:20 +00001241 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola89b93722010-12-10 07:39:47 +00001242
1243 // Data Alignment Factor
Bill Wendling3c163cf2011-06-30 21:25:51 +00001244 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola89b93722010-12-10 07:39:47 +00001245 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1246
1247 // Return Address Register
Bill Wendling3c163cf2011-06-30 21:25:51 +00001248 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendling99cb6222013-06-18 07:20:20 +00001249 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola89b93722010-12-10 07:39:47 +00001250
1251 // Augmentation Data Length (optional)
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001252
1253 unsigned augmentationLength = 0;
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001254 if (IsEH) {
1255 if (personality) {
1256 // Personality Encoding
1257 augmentationLength += 1;
1258 // Personality
1259 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1260 }
1261 if (lsda)
1262 augmentationLength += 1;
1263 // Encoding of the FDE pointers
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001264 augmentationLength += 1;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001265
Bill Wendling3c163cf2011-06-30 21:25:51 +00001266 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001267 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001268
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001269 // Augmentation Data (optional)
1270 if (personality) {
1271 // Personality Encoding
Bill Wendling3c163cf2011-06-30 21:25:51 +00001272 EmitEncodingByte(streamer, personalityEncoding,
1273 "Personality Encoding");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001274 // Personality
Bill Wendling3c163cf2011-06-30 21:25:51 +00001275 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001276 EmitPersonality(streamer, *personality, personalityEncoding);
1277 }
Bill Wendling3c163cf2011-06-30 21:25:51 +00001278
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001279 if (lsda)
Bill Wendling3c163cf2011-06-30 21:25:51 +00001280 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1281
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001282 // Encoding of the FDE pointers
Evan Cheng203576a2011-07-20 19:50:42 +00001283 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling3c163cf2011-06-30 21:25:51 +00001284 "FDE Encoding");
Rafael Espindolabdc31672010-12-27 15:56:22 +00001285 }
Rafael Espindola89b93722010-12-10 07:39:47 +00001286
1287 // Initial Instructions
1288
Bill Wendling99cb6222013-06-18 07:20:20 +00001289 const MCAsmInfo *MAI = context.getAsmInfo();
Rafael Espindola4a971702013-05-13 01:16:13 +00001290 const std::vector<MCCFIInstruction> &Instructions =
Bill Wendling99cb6222013-06-18 07:20:20 +00001291 MAI->getInitialFrameState();
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001292 EmitCFIInstructions(streamer, Instructions, NULL);
Rafael Espindola89b93722010-12-10 07:39:47 +00001293
1294 // Padding
Bill Wendling99cb6222013-06-18 07:20:20 +00001295 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola89b93722010-12-10 07:39:47 +00001296
1297 streamer.EmitLabel(sectionEnd);
1298 return *sectionStart;
1299}
1300
Rafael Espindola25f492e2011-04-12 16:12:03 +00001301MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1302 const MCSymbol &cieStart,
Rafael Espindola9f270da2011-05-10 03:01:39 +00001303 const MCDwarfFrameInfo &frame) {
Rafael Espindolabdc31672010-12-27 15:56:22 +00001304 MCContext &context = streamer.getContext();
1305 MCSymbol *fdeStart = context.CreateTempSymbol();
1306 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Chenge76a33b2011-07-20 05:58:47 +00001307 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling2541c412011-06-30 22:02:20 +00001308 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola89b93722010-12-10 07:39:47 +00001309
Evan Cheng5fbe5e72011-08-24 22:31:37 +00001310 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendling2541c412011-06-30 22:02:20 +00001311 MCSymbol *EHSym =
1312 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola8bca4102011-04-28 12:50:37 +00001313 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindolaa8cfb872011-04-28 02:46:42 +00001314 streamer.EmitLabel(EHSym);
1315 }
1316
Rafael Espindola89b93722010-12-10 07:39:47 +00001317 // Length
1318 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001319 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindola9266cc42011-04-27 01:43:49 +00001320 streamer.EmitAbsValue(Length, 4);
Rafael Espindola89b93722010-12-10 07:39:47 +00001321
1322 streamer.EmitLabel(fdeStart);
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001323
Rafael Espindola89b93722010-12-10 07:39:47 +00001324 // CIE Pointer
Bill Wendling99cb6222013-06-18 07:20:20 +00001325 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001326 if (IsEH) {
1327 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1328 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001329 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001330 streamer.EmitAbsValue(offset, 4);
Bill Wendling99cb6222013-06-18 07:20:20 +00001331 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindolae3a0e982011-05-10 20:59:42 +00001332 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1333 cieStart, 0);
1334 streamer.EmitAbsValue(offset, 4);
Rafael Espindola0d450dc2011-05-10 15:20:23 +00001335 } else {
1336 streamer.EmitSymbolValue(&cieStart, 4);
1337 }
Bill Wendling2541c412011-06-30 22:02:20 +00001338
Rafael Espindola89b93722010-12-10 07:39:47 +00001339 // PC Begin
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001340 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1341 : (unsigned)dwarf::DW_EH_PE_absptr;
1342 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
1343 EmitSymbol(streamer, *frame.Begin, PCEncoding, "FDE initial location");
Rafael Espindola89b93722010-12-10 07:39:47 +00001344
1345 // PC Range
1346 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1347 *frame.End, 0);
Bill Wendling2541c412011-06-30 22:02:20 +00001348 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001349 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola89b93722010-12-10 07:39:47 +00001350
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001351 if (IsEH) {
1352 // Augmentation Data Length
1353 unsigned augmentationLength = 0;
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001354
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001355 if (frame.Lsda)
1356 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola1f6c8752011-04-29 21:50:57 +00001357
Bill Wendling2541c412011-06-30 22:02:20 +00001358 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001359 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola89b93722010-12-10 07:39:47 +00001360
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001361 // Augmentation Data
1362 if (frame.Lsda)
Bill Wendling2541c412011-06-30 22:02:20 +00001363 EmitSymbol(streamer, *frame.Lsda, frame.LsdaEncoding,
1364 "Language Specific Data Area");
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001365 }
Rafael Espindola4892dff2011-04-29 03:06:29 +00001366
Rafael Espindola89b93722010-12-10 07:39:47 +00001367 // Call Frame Instructions
Rafael Espindolab40a71f2010-12-29 01:42:56 +00001368 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindola5bba0842010-12-28 04:15:37 +00001369
Rafael Espindola89b93722010-12-10 07:39:47 +00001370 // Padding
Nick Lewycky0c5602d2012-08-12 08:09:45 +00001371 streamer.EmitValueToAlignment(PCSize);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001372
1373 return fdeEnd;
Rafael Espindola89b93722010-12-10 07:39:47 +00001374}
1375
Benjamin Kramer19282362010-12-30 22:34:44 +00001376namespace {
1377 struct CIEKey {
Rafael Espindola16d7d432012-01-23 21:51:52 +00001378 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false); }
1379 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false); }
Rafael Espindolabdc31672010-12-27 15:56:22 +00001380
Benjamin Kramer19282362010-12-30 22:34:44 +00001381 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001382 unsigned LsdaEncoding_, bool IsSignalFrame_) :
1383 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1384 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_) {
Benjamin Kramer19282362010-12-30 22:34:44 +00001385 }
1386 const MCSymbol* Personality;
1387 unsigned PersonalityEncoding;
1388 unsigned LsdaEncoding;
Rafael Espindola16d7d432012-01-23 21:51:52 +00001389 bool IsSignalFrame;
Benjamin Kramer19282362010-12-30 22:34:44 +00001390 };
1391}
Rafael Espindolabdc31672010-12-27 15:56:22 +00001392
1393namespace llvm {
1394 template <>
1395 struct DenseMapInfo<CIEKey> {
1396 static CIEKey getEmptyKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001397 return CIEKey::getEmptyKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001398 }
1399 static CIEKey getTombstoneKey() {
Benjamin Kramer19282362010-12-30 22:34:44 +00001400 return CIEKey::getTombstoneKey();
Rafael Espindolabdc31672010-12-27 15:56:22 +00001401 }
1402 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer74849202012-04-11 14:06:39 +00001403 return static_cast<unsigned>(hash_combine(Key.Personality,
1404 Key.PersonalityEncoding,
1405 Key.LsdaEncoding,
1406 Key.IsSignalFrame));
Rafael Espindolabdc31672010-12-27 15:56:22 +00001407 }
1408 static bool isEqual(const CIEKey &LHS,
1409 const CIEKey &RHS) {
1410 return LHS.Personality == RHS.Personality &&
1411 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola16d7d432012-01-23 21:51:52 +00001412 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1413 LHS.IsSignalFrame == RHS.IsSignalFrame;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001414 }
1415 };
1416}
1417
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001418void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer,
1419 bool UsingCFI,
1420 bool IsEH) {
1421 MCContext &Context = Streamer.getContext();
Bill Wendling79b40f12013-05-30 18:52:57 +00001422 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001423 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001424 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling1424c3c2011-07-22 21:18:59 +00001425
Bill Wendlingd8ab8e92011-09-06 18:37:11 +00001426 // Emit the compact unwind info if available.
Bill Wendlingc60e2522013-04-24 03:11:14 +00001427 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendlingc60e2522013-04-24 03:11:14 +00001428 bool SectionEmitted = false;
Bob Wilson32ec93d2013-05-07 20:56:33 +00001429 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1430 const MCDwarfFrameInfo &Frame = FrameArray[i];
1431 if (Frame.CompactUnwindEncoding == 0) continue;
1432 if (!SectionEmitted) {
1433 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendling99cb6222013-06-18 07:20:20 +00001434 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson32ec93d2013-05-07 20:56:33 +00001435 SectionEmitted = true;
Bill Wendlingc60e2522013-04-24 03:11:14 +00001436 }
Bob Wilson32ec93d2013-05-07 20:56:33 +00001437 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001438 }
Bill Wendlingc60e2522013-04-24 03:11:14 +00001439 }
Bill Wendling1424c3c2011-07-22 21:18:59 +00001440
Bill Wendling79b40f12013-05-30 18:52:57 +00001441 const MCSection &Section =
1442 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1443 *MOFI->getDwarfFrameSection();
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001444 Streamer.SwitchSection(&Section);
1445 MCSymbol *SectionStart = Context.CreateTempSymbol();
1446 Streamer.EmitLabel(SectionStart);
Bill Wendling1424c3c2011-07-22 21:18:59 +00001447 Emitter.setSectionStart(SectionStart);
Rafael Espindola90998132011-04-29 02:42:28 +00001448
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001449 MCSymbol *FDEEnd = NULL;
Rafael Espindolabdc31672010-12-27 15:56:22 +00001450 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001451
Rafael Espindola40a7dbb2011-05-10 03:54:12 +00001452 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling1424c3c2011-07-22 21:18:59 +00001453 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1454 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001455 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001456 Frame.LsdaEncoding, Frame.IsSignalFrame);
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001457 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1458 if (!CIEStart)
1459 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1460 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola16d7d432012-01-23 21:51:52 +00001461 Frame.IsSignalFrame,
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001462 Frame.LsdaEncoding);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001463
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001464 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge3cd13f2011-06-23 01:06:23 +00001465
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001466 if (i != n - 1)
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001467 Streamer.EmitLabel(FDEEnd);
Rafael Espindoladfe125c2010-12-17 00:28:02 +00001468 }
Rafael Espindolad7c8cca2010-12-26 20:20:31 +00001469
Bill Wendling99cb6222013-06-18 07:20:20 +00001470 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga8c9e6a2011-06-23 07:44:54 +00001471 if (FDEEnd)
1472 Streamer.EmitLabel(FDEEnd);
Rafael Espindola89b93722010-12-10 07:39:47 +00001473}
Rafael Espindola245a1e22010-12-28 05:39:27 +00001474
1475void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1476 uint64_t AddrDelta) {
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +00001477 MCContext &Context = Streamer.getContext();
Rafael Espindola245a1e22010-12-28 05:39:27 +00001478 SmallString<256> Tmp;
1479 raw_svector_ostream OS(Tmp);
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +00001480 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christopher68ca5622013-01-09 01:57:54 +00001481 Streamer.EmitBytes(OS.str());
Rafael Espindola245a1e22010-12-28 05:39:27 +00001482}
1483
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +00001484void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1485 uint64_t AddrDelta,
Rafael Espindola4eafe102011-05-08 14:35:21 +00001486 raw_ostream &OS) {
Ulrich Weigandc1f4a4b2013-06-12 14:46:54 +00001487 // Scale the address delta by the minimum instruction length.
1488 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1489
Rafael Espindola3b78cdc2010-12-28 23:38:03 +00001490 if (AddrDelta == 0) {
Rafael Espindola4eafe102011-05-08 14:35:21 +00001491 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001492 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1493 OS << Opcode;
Rafael Espindola4eafe102011-05-08 14:35:21 +00001494 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola245a1e22010-12-28 05:39:27 +00001495 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1496 OS << uint8_t(AddrDelta);
Rafael Espindola4eafe102011-05-08 14:35:21 +00001497 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001498 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001499 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001500 OS << uint8_t( AddrDelta & 0xff);
1501 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola245a1e22010-12-28 05:39:27 +00001502 } else {
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001503 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola245a1e22010-12-28 05:39:27 +00001504 assert(isUInt<32>(AddrDelta));
1505 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindolaa7e45052010-12-29 02:30:49 +00001506 OS << uint8_t( AddrDelta & 0xff);
1507 OS << uint8_t((AddrDelta >> 8) & 0xff);
1508 OS << uint8_t((AddrDelta >> 16) & 0xff);
1509 OS << uint8_t((AddrDelta >> 24) & 0xff);
1510
Rafael Espindola245a1e22010-12-28 05:39:27 +00001511 }
1512}