blob: 181e73dd432f1d3580e5d7eb394404ad2598ddfc [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
Kevin Enderbye46564a2010-09-30 16:52:03 +000083 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000084 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000085 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000086 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000087 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000088}
89
90//
91// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000092//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000093static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
94 const MCSymbol &Start,
95 const MCSymbol &End,
96 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000097 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
98 const MCExpr *Res =
Rafael Espindolad66e65d2010-12-09 23:08:35 +000099 MCSymbolRefExpr::Create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000100 const MCExpr *RHS =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000101 MCSymbolRefExpr::Create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102 const MCExpr *Res1 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000103 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000104 const MCExpr *Res2 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000105 MCConstantExpr::Create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000106 const MCExpr *Res3 =
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000107 MCBinaryExpr::Create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000108 return Res3;
109}
110
Kevin Enderbye46564a2010-09-30 16:52:03 +0000111//
112// This emits the Dwarf line table for the specified section from the entries
113// in the LineSection.
114//
David Blaikief4ad6982014-03-12 22:35:23 +0000115static inline void
116EmitDwarfLineTable(MCStreamer *MCOS, const MCSection *Section,
117 const MCLineSection::MCLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000118 unsigned FileNum = 1;
119 unsigned LastLine = 1;
120 unsigned Column = 0;
121 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
122 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000123 unsigned Discriminator = 0;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000124 MCSymbol *LastLabel = NULL;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000125
126 // Loop through each MCLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000127 for (auto it = LineEntries.begin(),
128 ie = LineEntries.end();
129 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000130
131 if (FileNum != it->getFileNum()) {
132 FileNum = it->getFileNum();
133 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000134 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000135 }
136 if (Column != it->getColumn()) {
137 Column = it->getColumn();
138 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000139 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000140 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000141 if (Discriminator != it->getDiscriminator()) {
142 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000143 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000144 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
145 MCOS->EmitULEB128IntValue(Size + 1);
146 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
147 MCOS->EmitULEB128IntValue(Discriminator);
148 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000149 if (Isa != it->getIsa()) {
150 Isa = it->getIsa();
151 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000152 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000153 }
154 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
155 Flags = it->getFlags();
156 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
157 }
158 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
159 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
160 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
161 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
162 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
163 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
164
Rafael Espindola1d37f352010-11-13 01:06:27 +0000165 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000166 MCSymbol *Label = it->getLabel();
167
168 // At this point we want to emit/create the sequence to encode the delta in
169 // line numbers and the increment of the address from the previous Label
170 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000171 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000172 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000173 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000174
175 LastLine = it->getLine();
176 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000177 }
178
179 // Emit a DW_LNE_end_sequence for the end of the section.
180 // Using the pointer Section create a temporary label at the end of the
181 // section and use that and the LastLabel to compute the address delta
182 // and use INT64_MAX as the line delta which is the signal that this is
183 // actually a DW_LNE_end_sequence.
184
185 // Switch to the section to be able to create a symbol at its end.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000186 // TODO: keep track of the last subsection so that this symbol appears in the
187 // correct place.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000188 MCOS->SwitchSection(Section);
Rafael Espindola0a017a62010-12-10 07:39:47 +0000189
190 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000191 // Create a symbol at the end of the section.
Rafael Espindola0a017a62010-12-10 07:39:47 +0000192 MCSymbol *SectionEnd = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000193 // Set the value of the symbol, as we are at the end of the section.
194 MCOS->EmitLabel(SectionEnd);
195
Sylvestre Ledru35521e22012-07-23 08:51:15 +0000196 // Switch back the dwarf line section.
Evan Cheng76792992011-07-20 05:58:47 +0000197 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000198
Bill Wendlingbc07a892013-06-18 07:20:20 +0000199 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000200 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000201 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000202}
203
204//
205// This emits the Dwarf file and the line tables.
206//
David Blaikied9012ba2014-03-13 21:59:51 +0000207const MCSymbol *MCDwarfLineTable::Emit(MCStreamer *MCOS) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000208 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000209
Manman Ren4e042a62013-02-05 21:52:47 +0000210 // CUID and MCLineTableSymbols are set in DwarfDebug, when DwarfDebug does
211 // not exist, CUID will be 0 and MCLineTableSymbols will be empty.
212 // Handle Compile Unit 0, the line table start symbol is the section symbol.
David Blaikied9012ba2014-03-13 21:59:51 +0000213 auto I = MCOS->getContext().getMCDwarfLineTables().begin(),
214 E = MCOS->getContext().getMCDwarfLineTables().end();
David Blaikie11765bc2014-03-13 17:55:28 +0000215
216 // Switch to the section where the table will be emitted into.
217 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
218
219 const MCSymbol *LineStartSym = I->second.EmitCU(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000220 // Handle the rest of the Compile Units.
David Blaikie11765bc2014-03-13 17:55:28 +0000221 for (++I; I != E; ++I)
222 I->second.EmitCU(MCOS);
Manman Ren4e042a62013-02-05 21:52:47 +0000223
Manman Ren4e042a62013-02-05 21:52:47 +0000224 return LineStartSym;
225}
226
David Blaikie5cff0e62014-03-13 21:47:12 +0000227std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000228 MCContext &context = MCOS->getContext();
229
230 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000231 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000232 if (!LineStartSym)
233 LineStartSym = context.CreateTempSymbol();
234 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000235 MCOS->EmitLabel(LineStartSym);
236
237 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000238 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000239
240 // The first 4 bytes is the total length of the information for this
241 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000242 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000243 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000244
245 // Next 2 bytes is the Version, which is Dwarf 2.
246 MCOS->EmitIntValue(2, 2);
247
248 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000249 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000250
251 // Length of the prologue, is the next 4 bytes. Which is the start of the
252 // section to the end of the prologue. Not including the 4 bytes for the
253 // total length, the 2 bytes for the version, and these 4 bytes for the
254 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000255 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000256 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000257
258 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000259 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000260 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
261 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
262 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
263 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
264
265 // Standard opcode lengths
266 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
267 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
268 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
269 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
270 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
271 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
272 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
273 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
274 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
275 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
276 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
277 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
278
279 // Put out the directory and file tables.
280
281 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000282 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000283 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
284 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000285 }
286 MCOS->EmitIntValue(0, 1); // Terminate the directory list
287
288 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000289 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000290 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000291 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000292 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000293 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000294 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
295 MCOS->EmitIntValue(0, 1); // filesize (always 0)
296 }
297 MCOS->EmitIntValue(0, 1); // Terminate the file list
298
299 // This is the end of the prologue, so set the value of the symbol at the
300 // end of the prologue (that was used in a previous expression).
301 MCOS->EmitLabel(ProEndSym);
302
David Blaikie5cff0e62014-03-13 21:47:12 +0000303 return std::make_pair(LineStartSym, LineEndSym);
304}
305
David Blaikied9012ba2014-03-13 21:59:51 +0000306const MCSymbol *MCDwarfLineTable::EmitCU(MCStreamer *MCOS) const {
David Blaikie5cff0e62014-03-13 21:47:12 +0000307 MCSymbol *LineStartSym;
308 MCSymbol *LineEndSym;
309 std::tie(LineStartSym, LineEndSym) = Header.Emit(MCOS);
310
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000312 for (const auto &LineSec : MCLineSections.getMCLineEntries())
313 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000314
David Blaikiea55e64f2014-03-12 22:28:56 +0000315 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines() &&
David Blaikie11765bc2014-03-13 17:55:28 +0000316 MCLineSections.getMCLineEntries().empty()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000317 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000318 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000319 // total_length >= prologue_length + 10
320 // We are 4 bytes short, since we have total_length = 51 and
321 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000322
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000323 // The regular end_sequence should be sufficient.
324 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000325 }
326
Kevin Enderbye46564a2010-09-30 16:52:03 +0000327 // This is the end of the section, so set the value of the symbol at the end
328 // of this section (that was used in a previous expression).
329 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000330
331 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000332}
333
David Blaikied9012ba2014-03-13 21:59:51 +0000334unsigned MCDwarfLineTable::getFile(StringRef Directory, StringRef FileName,
335 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000336 return Header.getFile(Directory, FileName, FileNumber);
337}
338
339unsigned MCDwarfLineTableHeader::getFile(StringRef Directory, StringRef FileName, unsigned FileNumber) {
David Blaikie498589c2014-03-13 19:15:04 +0000340 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
341 MCDwarfFiles.resize(FileNumber + 1);
342
343 // Get the new MCDwarfFile slot for this FileNumber.
344 MCDwarfFile &File = MCDwarfFiles[FileNumber];
345
346 // It is an error to use see the same number more than once.
347 if (!File.Name.empty())
348 return 0;
349
350 if (Directory.empty()) {
351 // Separate the directory part from the basename of the FileName.
352 StringRef tFileName = sys::path::filename(FileName);
353 if (!tFileName.empty()) {
354 Directory = sys::path::parent_path(FileName);
355 if (!Directory.empty())
356 FileName = tFileName;
357 }
358 }
359
360 // Find or make an entry in the MCDwarfDirs vector for this Directory.
361 // Capture directory name.
362 unsigned DirIndex;
363 if (Directory.empty()) {
364 // For FileNames with no directories a DirIndex of 0 is used.
365 DirIndex = 0;
366 } else {
367 DirIndex = 0;
368 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
369 if (Directory == MCDwarfDirs[DirIndex])
370 break;
371 }
372 if (DirIndex >= MCDwarfDirs.size())
373 MCDwarfDirs.push_back(Directory);
374 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
375 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
376 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
377 // are stored at MCDwarfFiles[FileNumber].Name .
378 DirIndex++;
379 }
380
381 File.Name = FileName;
382 File.DirIndex = DirIndex;
383
384 // return the allocated FileNumber.
385 return FileNumber;
386}
387
Kevin Enderbye46564a2010-09-30 16:52:03 +0000388/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000389void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000390 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000391 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000392 SmallString<256> Tmp;
393 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000394 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000395 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000396}
397
398/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000399void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
400 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000401 uint64_t Temp, Opcode;
402 bool NeedCopy = false;
403
404 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000405 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000406
407 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000408 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000409 // end_sequence to emit the matrix entry.
410 if (LineDelta == INT64_MAX) {
411 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
412 OS << char(dwarf::DW_LNS_const_add_pc);
413 else {
414 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000415 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000416 }
417 OS << char(dwarf::DW_LNS_extended_op);
418 OS << char(1);
419 OS << char(dwarf::DW_LNE_end_sequence);
420 return;
421 }
422
423 // Bias the line delta by the base.
424 Temp = LineDelta - DWARF2_LINE_BASE;
425
426 // If the line increment is out of range of a special opcode, we must encode
427 // it with DW_LNS_advance_line.
428 if (Temp >= DWARF2_LINE_RANGE) {
429 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000430 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000431
432 LineDelta = 0;
433 Temp = 0 - DWARF2_LINE_BASE;
434 NeedCopy = true;
435 }
436
437 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
438 if (LineDelta == 0 && AddrDelta == 0) {
439 OS << char(dwarf::DW_LNS_copy);
440 return;
441 }
442
443 // Bias the opcode by the special opcode base.
444 Temp += DWARF2_LINE_OPCODE_BASE;
445
446 // Avoid overflow when addr_delta is large.
447 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
448 // Try using a special opcode.
449 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
450 if (Opcode <= 255) {
451 OS << char(Opcode);
452 return;
453 }
454
455 // Try using DW_LNS_const_add_pc followed by special op.
456 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
457 if (Opcode <= 255) {
458 OS << char(dwarf::DW_LNS_const_add_pc);
459 OS << char(Opcode);
460 return;
461 }
462 }
463
464 // Otherwise use DW_LNS_advance_pc.
465 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000466 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000467
468 if (NeedCopy)
469 OS << char(dwarf::DW_LNS_copy);
470 else
471 OS << char(Temp);
472}
473
Kevin Enderbye7739d42011-12-09 18:09:40 +0000474// Utility function to write a tuple for .debug_abbrev.
475static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
476 MCOS->EmitULEB128IntValue(Name);
477 MCOS->EmitULEB128IntValue(Form);
478}
479
480// When generating dwarf for assembly source files this emits
481// the data for .debug_abbrev section which contains three DIEs.
482static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
483 MCContext &context = MCOS->getContext();
484 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
485
486 // DW_TAG_compile_unit DIE abbrev (1).
487 MCOS->EmitULEB128IntValue(1);
488 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
489 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
490 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
491 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
492 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
493 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000494 if (!context.getCompilationDir().empty())
495 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000496 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
497 if (!DwarfDebugFlags.empty())
498 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
499 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
500 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
501 EmitAbbrev(MCOS, 0, 0);
502
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000503 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000504 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000505 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000506 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
507 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
508 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
509 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
510 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000511 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
512 EmitAbbrev(MCOS, 0, 0);
513
514 // DW_TAG_unspecified_parameters DIE abbrev (3).
515 MCOS->EmitULEB128IntValue(3);
516 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
517 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
518 EmitAbbrev(MCOS, 0, 0);
519
520 // Terminate the abbreviations for this compilation unit.
521 MCOS->EmitIntValue(0, 1);
522}
523
524// When generating dwarf for assembly source files this emits the data for
525// .debug_aranges section. Which contains a header and a table of pairs of
526// PointerSize'ed values for the address and size of section(s) with line table
527// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000528static void EmitGenDwarfAranges(MCStreamer *MCOS,
529 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000530 MCContext &context = MCOS->getContext();
531
532 // Create a symbol at the end of the section that we are creating the dwarf
533 // debugging info to use later in here as part of the expression to calculate
534 // the size of the section for the table.
535 MCOS->SwitchSection(context.getGenDwarfSection());
536 MCSymbol *SectionEndSym = context.CreateTempSymbol();
537 MCOS->EmitLabel(SectionEndSym);
538 context.setGenDwarfSectionEndSym(SectionEndSym);
539
540 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
541
542 // This will be the length of the .debug_aranges section, first account for
543 // the size of each item in the header (see below where we emit these items).
544 int Length = 4 + 2 + 4 + 1 + 1;
545
546 // Figure the padding after the header before the table of address and size
547 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000548 const MCAsmInfo *asmInfo = context.getAsmInfo();
549 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000550 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
551 if (Pad == 2 * AddrSize)
552 Pad = 0;
553 Length += Pad;
554
555 // Add the size of the pair of PointerSize'ed values for the address and size
556 // of the one default .text section we have in the table.
557 Length += 2 * AddrSize;
558 // And the pair of terminating zeros.
559 Length += 2 * AddrSize;
560
561
562 // Emit the header for this section.
563 // The 4 byte length not including the 4 byte value for the length.
564 MCOS->EmitIntValue(Length - 4, 4);
565 // The 2 byte version, which is 2.
566 MCOS->EmitIntValue(2, 2);
567 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000568 // of the .debug_info.
569 if (InfoSectionSymbol)
570 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
571 else
572 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000573 // The 1 byte size of an address.
574 MCOS->EmitIntValue(AddrSize, 1);
575 // The 1 byte size of a segment descriptor, we use a value of zero.
576 MCOS->EmitIntValue(0, 1);
577 // Align the header with the padding if needed, before we put out the table.
578 for(int i = 0; i < Pad; i++)
579 MCOS->EmitIntValue(0, 1);
580
581 // Now emit the table of pairs of PointerSize'ed values for the section(s)
582 // address and size, in our case just the one default .text section.
583 const MCExpr *Addr = MCSymbolRefExpr::Create(
584 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
585 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
586 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
587 MCOS->EmitAbsValue(Addr, AddrSize);
588 MCOS->EmitAbsValue(Size, AddrSize);
589
590 // And finally the pair of terminating zeros.
591 MCOS->EmitIntValue(0, AddrSize);
592 MCOS->EmitIntValue(0, AddrSize);
593}
594
595// When generating dwarf for assembly source files this emits the data for
596// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000597// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000598static void EmitGenDwarfInfo(MCStreamer *MCOS,
599 const MCSymbol *AbbrevSectionSymbol,
600 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000601 MCContext &context = MCOS->getContext();
602
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000603 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000604
605 // Create a symbol at the start and end of this section used in here for the
606 // expression to calculate the length in the header.
607 MCSymbol *InfoStart = context.CreateTempSymbol();
608 MCOS->EmitLabel(InfoStart);
609 MCSymbol *InfoEnd = context.CreateTempSymbol();
610
611 // First part: the header.
612
613 // The 4 byte total length of the information for this compilation unit, not
614 // including these 4 bytes.
615 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
616 MCOS->EmitAbsValue(Length, 4);
617
618 // The 2 byte DWARF version, which is 2.
619 MCOS->EmitIntValue(2, 2);
620
621 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
622 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000623 if (AbbrevSectionSymbol) {
624 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
625 } else {
626 MCOS->EmitIntValue(0, 4);
627 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000628
Bill Wendlingbc07a892013-06-18 07:20:20 +0000629 const MCAsmInfo *asmInfo = context.getAsmInfo();
630 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000631 // The 1 byte size of an address.
632 MCOS->EmitIntValue(AddrSize, 1);
633
634 // Second part: the compile_unit DIE.
635
636 // The DW_TAG_compile_unit DIE abbrev (1).
637 MCOS->EmitULEB128IntValue(1);
638
639 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
640 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000641 if (LineSectionSymbol) {
642 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
643 } else {
644 MCOS->EmitIntValue(0, 4);
645 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000646
647 // AT_low_pc, the first address of the default .text section.
648 const MCExpr *Start = MCSymbolRefExpr::Create(
649 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
650 MCOS->EmitAbsValue(Start, AddrSize);
651
652 // AT_high_pc, the last address of the default .text section.
653 const MCExpr *End = MCSymbolRefExpr::Create(
654 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
655 MCOS->EmitAbsValue(End, AddrSize);
656
657 // AT_name, the name of the source file. Reconstruct from the first directory
658 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000659 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000660 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000661 MCOS->EmitBytes(MCDwarfDirs[0]);
662 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000663 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000664 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000665 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000666 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000667 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
668
669 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000670 if (!context.getCompilationDir().empty()) {
671 MCOS->EmitBytes(context.getCompilationDir());
672 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
673 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000674
675 // AT_APPLE_flags, the command line arguments of the assembler tool.
676 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
677 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000678 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000679 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
680 }
681
682 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000683 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
684 if (!DwarfDebugProducer.empty()){
685 MCOS->EmitBytes(DwarfDebugProducer);
686 }
687 else {
688 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
689 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
690 MCOS->EmitBytes(StringRef(")"));
691 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000692 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
693
694 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
695 // draft has no standard code for assembler.
696 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
697
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000698 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000699
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000700 // Loop on saved info for dwarf labels and create the DIEs for them.
701 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
702 MCOS->getContext().getMCGenDwarfLabelEntries();
703 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000704 Entries.begin(), ie = Entries.end(); it != ie;
705 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000706 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000707
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000708 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000709 MCOS->EmitULEB128IntValue(2);
710
711 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000712 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000713 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
714
715 // AT_decl_file, index into the file table.
716 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
717
718 // AT_decl_line, source line number.
719 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
720
721 // AT_low_pc, start address of the label.
722 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
723 MCSymbolRefExpr::VK_None, context);
724 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
725
Kevin Enderbye7739d42011-12-09 18:09:40 +0000726 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
727 MCOS->EmitIntValue(0, 1);
728
729 // The DW_TAG_unspecified_parameters DIE abbrev (3).
730 MCOS->EmitULEB128IntValue(3);
731
732 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
733 MCOS->EmitIntValue(0, 1);
734 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000735 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
736 // for the dwarf labels.
737 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000738 Entries.begin(), ie = Entries.end(); it != ie;
739 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000740 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741 delete Entry;
742 }
743
744 // Add the NULL DIE terminating the Compile Unit DIE's.
745 MCOS->EmitIntValue(0, 1);
746
747 // Now set the value of the symbol at the end of the info section.
748 MCOS->EmitLabel(InfoEnd);
749}
750
751//
752// When generating dwarf for assembly source files this emits the Dwarf
753// sections.
754//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000755void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000756 // Create the dwarf sections in this order (.debug_line already created).
757 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000758 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000759 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000760 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000761 if (!CreateDwarfSectionSymbols)
762 LineSectionSymbol = NULL;
763 MCSymbol *AbbrevSectionSymbol = NULL;
764 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000765 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000766 if (CreateDwarfSectionSymbols) {
767 InfoSectionSymbol = context.CreateTempSymbol();
768 MCOS->EmitLabel(InfoSectionSymbol);
769 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000770 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000771 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000772 AbbrevSectionSymbol = context.CreateTempSymbol();
773 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000774 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000775 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
776
777 // If there are no line table entries then do not emit any section contents.
David Blaikie11765bc2014-03-13 17:55:28 +0000778 if (!context.hasMCLineSections())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000779 return;
780
781 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000782 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783
784 // Output the data for .debug_abbrev section.
785 EmitGenDwarfAbbrev(MCOS);
786
787 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000788 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000789}
790
791//
792// When generating dwarf for assembly source files this is called when symbol
793// for a label is created. If this symbol is not a temporary and is in the
794// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000795// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000796//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000797void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000798 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000799 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000800 // the default text.
801 if (Symbol->isTemporary())
802 return;
803 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000804 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000805 return;
806
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000807 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000808 // underbar if any.
809 StringRef Name = Symbol->getName();
810 if (Name.startswith("_"))
811 Name = Name.substr(1, Name.size()-1);
812
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000813 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000814 unsigned FileNumber = context.getGenDwarfFileNumber();
815
816 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000817 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000818 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
819 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
820
821 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
822 // values so that they don't have things like an ARM thumb bit from the
823 // original symbol. So when used they won't get a low bit set after
824 // relocation.
825 MCSymbol *Label = context.CreateTempSymbol();
826 MCOS->EmitLabel(Label);
827
828 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000829 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000830 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
831 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000832}
833
Rafael Espindola0a017a62010-12-10 07:39:47 +0000834static int getDataAlignmentFactor(MCStreamer &streamer) {
835 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000836 const MCAsmInfo *asmInfo = context.getAsmInfo();
837 int size = asmInfo->getCalleeSaveStackSlotSize();
838 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000839 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000840 else
841 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000842}
843
Rafael Espindola5395f442011-04-22 00:08:43 +0000844static unsigned getSizeForEncoding(MCStreamer &streamer,
845 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000846 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000847 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000848 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000849 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000850 case dwarf::DW_EH_PE_absptr:
851 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000852 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000853 case dwarf::DW_EH_PE_udata2:
854 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000855 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000856 case dwarf::DW_EH_PE_udata4:
857 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000858 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000859 case dwarf::DW_EH_PE_udata8:
860 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000861 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000862 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000863}
864
Iain Sandoe618def62014-01-08 10:22:54 +0000865static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
866 unsigned symbolEncoding, bool isEH,
867 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000868 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000869 const MCAsmInfo *asmInfo = context.getAsmInfo();
870 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
871 symbolEncoding,
872 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000873 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000874 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000875 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
876 streamer.EmitAbsValue(v, size);
877 else
878 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000879}
880
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000881static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
882 unsigned symbolEncoding) {
883 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000884 const MCAsmInfo *asmInfo = context.getAsmInfo();
885 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
886 symbolEncoding,
887 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000888 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000889 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000890}
891
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000892namespace {
893 class FrameEmitterImpl {
894 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000895 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000896 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000897 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000898 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000899 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000900 FrameEmitterImpl(bool usingCFI, bool isEH)
901 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
902 SectionStart(0) {}
903
904 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000905
Bill Wendling3af441f2013-09-05 00:54:52 +0000906 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000907 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000908 const MCDwarfFrameInfo &frame);
909
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000910 const MCSymbol &EmitCIE(MCStreamer &streamer,
911 const MCSymbol *personality,
912 unsigned personalityEncoding,
913 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000914 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000915 unsigned lsdaEncoding,
916 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000917 MCSymbol *EmitFDE(MCStreamer &streamer,
918 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000919 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000920 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000921 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000922 MCSymbol *BaseLabel);
923 void EmitCFIInstruction(MCStreamer &Streamer,
924 const MCCFIInstruction &Instr);
925 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000926
927} // end anonymous namespace
928
929static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
930 StringRef Prefix) {
931 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000932 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000933 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000934 default: EncStr = "<unknown encoding>"; break;
935 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
936 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
937 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
938 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
939 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
940 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
941 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
942 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
943 EncStr = "pcrel udata4";
944 break;
945 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
946 EncStr = "pcrel sdata4";
947 break;
948 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
949 EncStr = "pcrel udata8";
950 break;
951 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
952 EncStr = "screl sdata8";
953 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000954 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
955 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000956 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000957 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
958 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000959 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000960 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
961 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000962 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000963 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
964 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000965 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000966 }
967
968 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
969 }
970
971 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000972}
973
974void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
975 const MCCFIInstruction &Instr) {
976 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000977 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000978
979 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000980 case MCCFIInstruction::OpRegister: {
981 unsigned Reg1 = Instr.getRegister();
982 unsigned Reg2 = Instr.getRegister2();
983 if (VerboseAsm) {
984 Streamer.AddComment("DW_CFA_register");
985 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
986 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
987 }
988 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
989 Streamer.EmitULEB128IntValue(Reg1);
990 Streamer.EmitULEB128IntValue(Reg2);
991 return;
992 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000993 case MCCFIInstruction::OpWindowSave: {
994 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
995 return;
996 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000997 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000998 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000999 if (VerboseAsm) {
1000 Streamer.AddComment("DW_CFA_undefined");
1001 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1002 }
1003 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1004 Streamer.EmitULEB128IntValue(Reg);
1005 return;
1006 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001007 case MCCFIInstruction::OpAdjustCfaOffset:
1008 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001009 const bool IsRelative =
1010 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1011
1012 if (VerboseAsm)
1013 Streamer.AddComment("DW_CFA_def_cfa_offset");
1014 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1015
1016 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001017 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001018 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001019 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001020
1021 if (VerboseAsm)
1022 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1023 Streamer.EmitULEB128IntValue(CFAOffset);
1024
1025 return;
1026 }
1027 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001028 if (VerboseAsm)
1029 Streamer.AddComment("DW_CFA_def_cfa");
1030 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1031
1032 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001033 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1034 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001035
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001036 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001037
1038 if (VerboseAsm)
1039 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1040 Streamer.EmitULEB128IntValue(CFAOffset);
1041
1042 return;
1043 }
1044
1045 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001046 if (VerboseAsm)
1047 Streamer.AddComment("DW_CFA_def_cfa_register");
1048 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1049
1050 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001051 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1052 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001053
1054 return;
1055 }
1056
1057 case MCCFIInstruction::OpOffset:
1058 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001059 const bool IsRelative =
1060 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001061
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001062 unsigned Reg = Instr.getRegister();
1063 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001064 if (IsRelative)
1065 Offset -= CFAOffset;
1066 Offset = Offset / dataAlignmentFactor;
1067
1068 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001069 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001071 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001072 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001073 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001074 Streamer.EmitSLEB128IntValue(Offset);
1075 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001076 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1077 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001078 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001079 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001080 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001081 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001082 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001083 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001084 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001085 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001086 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001087 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001088 }
1089 return;
1090 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001091 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001092 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001093 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1094 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001095 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001096 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001097 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1098 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001099 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001100 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001101 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001102 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001103 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001104 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001105 return;
1106 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001107 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001108 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001109 if (VerboseAsm) {
1110 Streamer.AddComment("DW_CFA_restore");
1111 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1112 }
1113 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1114 return;
1115 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001116 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001117 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001118 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001119 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001120 }
1121 llvm_unreachable("Unhandled case in switch");
1122}
1123
1124/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1125/// frame.
1126void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001127 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001128 MCSymbol *BaseLabel) {
1129 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1130 const MCCFIInstruction &Instr = Instrs[i];
1131 MCSymbol *Label = Instr.getLabel();
1132 // Throw out move if the label is invalid.
1133 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1134
1135 // Advance row if new location.
1136 if (BaseLabel && Label) {
1137 MCSymbol *ThisSym = Label;
1138 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001139 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001140 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1141 BaseLabel = ThisSym;
1142 }
1143 }
1144
1145 EmitCFIInstruction(streamer, Instr);
1146 }
1147}
1148
Bill Wendling3af441f2013-09-05 00:54:52 +00001149/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001150void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001151 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001152 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001153 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001154 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001155
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001156 // range-start range-length compact-unwind-enc personality-func lsda
1157 // _foo LfooEnd-_foo 0x00000023 0 0
1158 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1159 //
1160 // .section __LD,__compact_unwind,regular,debug
1161 //
1162 // # compact unwind for _foo
1163 // .quad _foo
1164 // .set L1,LfooEnd-_foo
1165 // .long L1
1166 // .long 0x01010001
1167 // .quad 0
1168 // .quad 0
1169 //
1170 // # compact unwind for _bar
1171 // .quad _bar
1172 // .set L2,LbarEnd-_bar
1173 // .long L2
1174 // .long 0x01020011
1175 // .quad __gxx_personality
1176 // .quad except_tab1
1177
Bill Wendling49bc6802011-07-19 00:06:12 +00001178 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001179 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001180 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001181
Bill Wendlingdafd5982011-07-14 23:34:45 +00001182 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001183 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001184 Encoding |= 0x40000000;
1185
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001186 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001187 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001188 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001189 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001190 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001191
1192 // Range Length
1193 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1194 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001195 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001196 Streamer.EmitAbsValue(Range, 4);
1197
Bill Wendling75174662011-06-30 00:30:52 +00001198 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001199 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001200 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1201 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001202 Streamer.EmitIntValue(Encoding, Size);
1203
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001204 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001205 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001206 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001207 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001208 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001209 else
1210 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001211
1212 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001213 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001214 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001215 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001216 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001217 else
1218 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001219}
1220
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001221const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1222 const MCSymbol *personality,
1223 unsigned personalityEncoding,
1224 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001225 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001226 unsigned lsdaEncoding,
1227 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001228 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001229 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001230 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001231 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001232
1233 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001234 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001235 sectionStart = context.CreateTempSymbol();
1236 else
1237 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1238
Bill Wendling567a1ae2011-06-30 21:25:51 +00001239 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001240 CIENum++;
1241
Bill Wendlingb6adf462011-07-07 00:54:13 +00001242 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001243
1244 // Length
1245 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1246 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001247 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001248 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001249
1250 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001251 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001252 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001253 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001254
1255 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001256 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001257 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1258
1259 // Augmentation String
1260 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001261 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001262 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001263 Augmentation += "z";
1264 if (personality)
1265 Augmentation += "P";
1266 if (lsda)
1267 Augmentation += "L";
1268 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001269 if (IsSignalFrame)
1270 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001271 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001272 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001273 streamer.EmitIntValue(0, 1);
1274
1275 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001276 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001277 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001278
1279 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001280 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001281 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1282
1283 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001284 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001285 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001286
1287 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001288
1289 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001290 if (IsEH) {
1291 if (personality) {
1292 // Personality Encoding
1293 augmentationLength += 1;
1294 // Personality
1295 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1296 }
1297 if (lsda)
1298 augmentationLength += 1;
1299 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001300 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001301
Bill Wendling567a1ae2011-06-30 21:25:51 +00001302 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001303 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001304
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001305 // Augmentation Data (optional)
1306 if (personality) {
1307 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001308 EmitEncodingByte(streamer, personalityEncoding,
1309 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001310 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001311 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001312 EmitPersonality(streamer, *personality, personalityEncoding);
1313 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001314
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001315 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001316 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1317
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001318 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001319 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001320 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001321 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001322
1323 // Initial Instructions
1324
Bill Wendlingbc07a892013-06-18 07:20:20 +00001325 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001326 if (!IsSimple) {
1327 const std::vector<MCCFIInstruction> &Instructions =
1328 MAI->getInitialFrameState();
1329 EmitCFIInstructions(streamer, Instructions, NULL);
1330 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001331
1332 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001333 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001334
1335 streamer.EmitLabel(sectionEnd);
1336 return *sectionStart;
1337}
1338
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001339MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1340 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001341 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001342 MCContext &context = streamer.getContext();
1343 MCSymbol *fdeStart = context.CreateTempSymbol();
1344 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001345 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001346 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001347
Evan Chengeee86452011-08-24 22:31:37 +00001348 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001349 MCSymbol *EHSym =
1350 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001351 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001352 streamer.EmitLabel(EHSym);
1353 }
1354
Rafael Espindola0a017a62010-12-10 07:39:47 +00001355 // Length
1356 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001357 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001358 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001359
1360 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001361
Rafael Espindola0a017a62010-12-10 07:39:47 +00001362 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001363 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001364 if (IsEH) {
1365 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1366 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001367 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001368 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001369 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001370 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1371 cieStart, 0);
1372 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001373 } else {
1374 streamer.EmitSymbolValue(&cieStart, 4);
1375 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001376
Rafael Espindola0a017a62010-12-10 07:39:47 +00001377 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001378 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1379 : (unsigned)dwarf::DW_EH_PE_absptr;
1380 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001381 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001382
1383 // PC Range
1384 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1385 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001386 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001387 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001388
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001389 if (IsEH) {
1390 // Augmentation Data Length
1391 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001392
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001393 if (frame.Lsda)
1394 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001395
Bill Wendlingf166ab42011-06-30 22:02:20 +00001396 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001397 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001398
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001399 // Augmentation Data
1400 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001401 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1402 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001403 }
Rafael Espindola68067662011-04-29 03:06:29 +00001404
Rafael Espindola0a017a62010-12-10 07:39:47 +00001405 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001406 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001407
Rafael Espindola0a017a62010-12-10 07:39:47 +00001408 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001409 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001410
1411 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001412}
1413
Benjamin Kramer570dd782010-12-30 22:34:44 +00001414namespace {
1415 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001416 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1417 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001418
Benjamin Kramer570dd782010-12-30 22:34:44 +00001419 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001420 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001421 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001422 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1423 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001424 }
1425 const MCSymbol* Personality;
1426 unsigned PersonalityEncoding;
1427 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001428 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001429 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001430 };
1431}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001432
1433namespace llvm {
1434 template <>
1435 struct DenseMapInfo<CIEKey> {
1436 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001437 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001438 }
1439 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001440 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001441 }
1442 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001443 return static_cast<unsigned>(hash_combine(Key.Personality,
1444 Key.PersonalityEncoding,
1445 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001446 Key.IsSignalFrame,
1447 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001448 }
1449 static bool isEqual(const CIEKey &LHS,
1450 const CIEKey &RHS) {
1451 return LHS.Personality == RHS.Personality &&
1452 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001453 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001454 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1455 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001456 }
1457 };
1458}
1459
Bill Wendling550c76d2013-09-09 19:48:37 +00001460void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1461 bool UsingCFI, bool IsEH) {
1462 Streamer.generateCompactUnwindEncodings(MAB);
1463
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001464 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001465 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001466 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001467 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001468
Bill Wendling9803abb2011-09-06 18:37:11 +00001469 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001470 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001471 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001472 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1473 const MCDwarfFrameInfo &Frame = FrameArray[i];
1474 if (Frame.CompactUnwindEncoding == 0) continue;
1475 if (!SectionEmitted) {
1476 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001477 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001478 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001479 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001480 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001481 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001482 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001483
Bill Wendlinga800f952013-05-30 18:52:57 +00001484 const MCSection &Section =
1485 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1486 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001487 Streamer.SwitchSection(&Section);
1488 MCSymbol *SectionStart = Context.CreateTempSymbol();
1489 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001490 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001491
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001492 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001493 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001494
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001495 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001496 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1497 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001498 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001499 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001500 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1501 if (!CIEStart)
1502 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1503 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001504 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001505 Frame.LsdaEncoding,
1506 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001507
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001508 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001509
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001510 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001511 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001512 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001513
Bill Wendlingbc07a892013-06-18 07:20:20 +00001514 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001515 if (FDEEnd)
1516 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001517}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001518
1519void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1520 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001521 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001522 SmallString<256> Tmp;
1523 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001524 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001525 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001526}
1527
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001528void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1529 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001530 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001531 // Scale the address delta by the minimum instruction length.
1532 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1533
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001534 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001535 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001536 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1537 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001538 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001539 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1540 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001541 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001542 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001543 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001544 OS << uint8_t( AddrDelta & 0xff);
1545 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001546 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001547 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001548 assert(isUInt<32>(AddrDelta));
1549 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001550 OS << uint8_t( AddrDelta & 0xff);
1551 OS << uint8_t((AddrDelta >> 8) & 0xff);
1552 OS << uint8_t((AddrDelta >> 16) & 0xff);
1553 OS << uint8_t((AddrDelta >> 24) & 0xff);
1554
Rafael Espindola736a35d2010-12-28 05:39:27 +00001555 }
1556}