blob: 2c1874f5e91436b9ff561b11fd372ff8602316b3 [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 Blaikie11765bc2014-03-13 17:55:28 +000085 .getMCDwarfFileTable(MCOS->getContext().getDwarfCompileUnitID())
86 .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//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000207const MCSymbol *MCDwarfFileTable::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 Blaikie11765bc2014-03-13 17:55:28 +0000213 auto I = MCOS->getContext().getMCDwarfFileTables().begin(),
214 E = MCOS->getContext().getMCDwarfFileTables().end();
215
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 Blaikie11765bc2014-03-13 17:55:28 +0000227const MCSymbol *MCDwarfFileTable::EmitCU(MCStreamer *MCOS) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000228 MCContext &context = MCOS->getContext();
229
David Blaikie11765bc2014-03-13 17:55:28 +0000230
231
Manman Ren4e042a62013-02-05 21:52:47 +0000232 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000233 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000234 if (!LineStartSym)
235 LineStartSym = context.CreateTempSymbol();
236 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000237 MCOS->EmitLabel(LineStartSym);
238
239 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000240 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000241
242 // The first 4 bytes is the total length of the information for this
243 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000244 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000245 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000246
247 // Next 2 bytes is the Version, which is Dwarf 2.
248 MCOS->EmitIntValue(2, 2);
249
250 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000251 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000252
253 // Length of the prologue, is the next 4 bytes. Which is the start of the
254 // section to the end of the prologue. Not including the 4 bytes for the
255 // total length, the 2 bytes for the version, and these 4 bytes for the
256 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000257 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000258 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000259
260 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000261 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000262 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
263 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
264 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
265 MCOS->EmitIntValue(DWARF2_LINE_OPCODE_BASE, 1);
266
267 // Standard opcode lengths
268 MCOS->EmitIntValue(0, 1); // length of DW_LNS_copy
269 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_pc
270 MCOS->EmitIntValue(1, 1); // length of DW_LNS_advance_line
271 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_file
272 MCOS->EmitIntValue(1, 1); // length of DW_LNS_set_column
273 MCOS->EmitIntValue(0, 1); // length of DW_LNS_negate_stmt
274 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_basic_block
275 MCOS->EmitIntValue(0, 1); // length of DW_LNS_const_add_pc
276 MCOS->EmitIntValue(1, 1); // length of DW_LNS_fixed_advance_pc
277 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_prologue_end
278 MCOS->EmitIntValue(0, 1); // length of DW_LNS_set_epilogue_begin
279 MCOS->EmitIntValue(1, 1); // DW_LNS_set_isa
280
281 // Put out the directory and file tables.
282
283 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000284 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000285 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
286 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000287 }
288 MCOS->EmitIntValue(0, 1); // Terminate the directory list
289
290 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000291 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiea55ddad2014-03-13 18:55:04 +0000292 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000293 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000294 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000295 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000296 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
297 MCOS->EmitIntValue(0, 1); // filesize (always 0)
298 }
299 MCOS->EmitIntValue(0, 1); // Terminate the file list
300
301 // This is the end of the prologue, so set the value of the symbol at the
302 // end of the prologue (that was used in a previous expression).
303 MCOS->EmitLabel(ProEndSym);
304
305 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000306 for (const auto &LineSec : MCLineSections.getMCLineEntries())
307 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000308
David Blaikiea55e64f2014-03-12 22:28:56 +0000309 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines() &&
David Blaikie11765bc2014-03-13 17:55:28 +0000310 MCLineSections.getMCLineEntries().empty()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000311 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000312 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000313 // total_length >= prologue_length + 10
314 // We are 4 bytes short, since we have total_length = 51 and
315 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000316
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000317 // The regular end_sequence should be sufficient.
318 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000319 }
320
Kevin Enderbye46564a2010-09-30 16:52:03 +0000321 // This is the end of the section, so set the value of the symbol at the end
322 // of this section (that was used in a previous expression).
323 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000324
325 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000326}
327
Kevin Enderbye46564a2010-09-30 16:52:03 +0000328/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000329void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000330 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000331 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000332 SmallString<256> Tmp;
333 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000334 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000335 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000336}
337
338/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000339void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
340 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000341 uint64_t Temp, Opcode;
342 bool NeedCopy = false;
343
344 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000345 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000346
347 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000348 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000349 // end_sequence to emit the matrix entry.
350 if (LineDelta == INT64_MAX) {
351 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
352 OS << char(dwarf::DW_LNS_const_add_pc);
353 else {
354 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000355 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000356 }
357 OS << char(dwarf::DW_LNS_extended_op);
358 OS << char(1);
359 OS << char(dwarf::DW_LNE_end_sequence);
360 return;
361 }
362
363 // Bias the line delta by the base.
364 Temp = LineDelta - DWARF2_LINE_BASE;
365
366 // If the line increment is out of range of a special opcode, we must encode
367 // it with DW_LNS_advance_line.
368 if (Temp >= DWARF2_LINE_RANGE) {
369 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000370 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000371
372 LineDelta = 0;
373 Temp = 0 - DWARF2_LINE_BASE;
374 NeedCopy = true;
375 }
376
377 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
378 if (LineDelta == 0 && AddrDelta == 0) {
379 OS << char(dwarf::DW_LNS_copy);
380 return;
381 }
382
383 // Bias the opcode by the special opcode base.
384 Temp += DWARF2_LINE_OPCODE_BASE;
385
386 // Avoid overflow when addr_delta is large.
387 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
388 // Try using a special opcode.
389 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
390 if (Opcode <= 255) {
391 OS << char(Opcode);
392 return;
393 }
394
395 // Try using DW_LNS_const_add_pc followed by special op.
396 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
397 if (Opcode <= 255) {
398 OS << char(dwarf::DW_LNS_const_add_pc);
399 OS << char(Opcode);
400 return;
401 }
402 }
403
404 // Otherwise use DW_LNS_advance_pc.
405 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000406 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000407
408 if (NeedCopy)
409 OS << char(dwarf::DW_LNS_copy);
410 else
411 OS << char(Temp);
412}
413
Kevin Enderbye7739d42011-12-09 18:09:40 +0000414// Utility function to write a tuple for .debug_abbrev.
415static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
416 MCOS->EmitULEB128IntValue(Name);
417 MCOS->EmitULEB128IntValue(Form);
418}
419
420// When generating dwarf for assembly source files this emits
421// the data for .debug_abbrev section which contains three DIEs.
422static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
423 MCContext &context = MCOS->getContext();
424 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
425
426 // DW_TAG_compile_unit DIE abbrev (1).
427 MCOS->EmitULEB128IntValue(1);
428 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
429 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
430 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
431 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
432 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
433 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000434 if (!context.getCompilationDir().empty())
435 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000436 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
437 if (!DwarfDebugFlags.empty())
438 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
439 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
440 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
441 EmitAbbrev(MCOS, 0, 0);
442
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000443 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000444 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000445 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000446 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
447 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
448 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
449 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
450 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000451 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
452 EmitAbbrev(MCOS, 0, 0);
453
454 // DW_TAG_unspecified_parameters DIE abbrev (3).
455 MCOS->EmitULEB128IntValue(3);
456 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
457 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
458 EmitAbbrev(MCOS, 0, 0);
459
460 // Terminate the abbreviations for this compilation unit.
461 MCOS->EmitIntValue(0, 1);
462}
463
464// When generating dwarf for assembly source files this emits the data for
465// .debug_aranges section. Which contains a header and a table of pairs of
466// PointerSize'ed values for the address and size of section(s) with line table
467// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000468static void EmitGenDwarfAranges(MCStreamer *MCOS,
469 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000470 MCContext &context = MCOS->getContext();
471
472 // Create a symbol at the end of the section that we are creating the dwarf
473 // debugging info to use later in here as part of the expression to calculate
474 // the size of the section for the table.
475 MCOS->SwitchSection(context.getGenDwarfSection());
476 MCSymbol *SectionEndSym = context.CreateTempSymbol();
477 MCOS->EmitLabel(SectionEndSym);
478 context.setGenDwarfSectionEndSym(SectionEndSym);
479
480 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
481
482 // This will be the length of the .debug_aranges section, first account for
483 // the size of each item in the header (see below where we emit these items).
484 int Length = 4 + 2 + 4 + 1 + 1;
485
486 // Figure the padding after the header before the table of address and size
487 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000488 const MCAsmInfo *asmInfo = context.getAsmInfo();
489 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000490 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
491 if (Pad == 2 * AddrSize)
492 Pad = 0;
493 Length += Pad;
494
495 // Add the size of the pair of PointerSize'ed values for the address and size
496 // of the one default .text section we have in the table.
497 Length += 2 * AddrSize;
498 // And the pair of terminating zeros.
499 Length += 2 * AddrSize;
500
501
502 // Emit the header for this section.
503 // The 4 byte length not including the 4 byte value for the length.
504 MCOS->EmitIntValue(Length - 4, 4);
505 // The 2 byte version, which is 2.
506 MCOS->EmitIntValue(2, 2);
507 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000508 // of the .debug_info.
509 if (InfoSectionSymbol)
510 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
511 else
512 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000513 // The 1 byte size of an address.
514 MCOS->EmitIntValue(AddrSize, 1);
515 // The 1 byte size of a segment descriptor, we use a value of zero.
516 MCOS->EmitIntValue(0, 1);
517 // Align the header with the padding if needed, before we put out the table.
518 for(int i = 0; i < Pad; i++)
519 MCOS->EmitIntValue(0, 1);
520
521 // Now emit the table of pairs of PointerSize'ed values for the section(s)
522 // address and size, in our case just the one default .text section.
523 const MCExpr *Addr = MCSymbolRefExpr::Create(
524 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
525 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
526 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
527 MCOS->EmitAbsValue(Addr, AddrSize);
528 MCOS->EmitAbsValue(Size, AddrSize);
529
530 // And finally the pair of terminating zeros.
531 MCOS->EmitIntValue(0, AddrSize);
532 MCOS->EmitIntValue(0, AddrSize);
533}
534
535// When generating dwarf for assembly source files this emits the data for
536// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000537// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000538static void EmitGenDwarfInfo(MCStreamer *MCOS,
539 const MCSymbol *AbbrevSectionSymbol,
540 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000541 MCContext &context = MCOS->getContext();
542
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000543 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000544
545 // Create a symbol at the start and end of this section used in here for the
546 // expression to calculate the length in the header.
547 MCSymbol *InfoStart = context.CreateTempSymbol();
548 MCOS->EmitLabel(InfoStart);
549 MCSymbol *InfoEnd = context.CreateTempSymbol();
550
551 // First part: the header.
552
553 // The 4 byte total length of the information for this compilation unit, not
554 // including these 4 bytes.
555 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
556 MCOS->EmitAbsValue(Length, 4);
557
558 // The 2 byte DWARF version, which is 2.
559 MCOS->EmitIntValue(2, 2);
560
561 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
562 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000563 if (AbbrevSectionSymbol) {
564 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
565 } else {
566 MCOS->EmitIntValue(0, 4);
567 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000568
Bill Wendlingbc07a892013-06-18 07:20:20 +0000569 const MCAsmInfo *asmInfo = context.getAsmInfo();
570 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000571 // The 1 byte size of an address.
572 MCOS->EmitIntValue(AddrSize, 1);
573
574 // Second part: the compile_unit DIE.
575
576 // The DW_TAG_compile_unit DIE abbrev (1).
577 MCOS->EmitULEB128IntValue(1);
578
579 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
580 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000581 if (LineSectionSymbol) {
582 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
583 } else {
584 MCOS->EmitIntValue(0, 4);
585 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000586
587 // AT_low_pc, the first address of the default .text section.
588 const MCExpr *Start = MCSymbolRefExpr::Create(
589 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
590 MCOS->EmitAbsValue(Start, AddrSize);
591
592 // AT_high_pc, the last address of the default .text section.
593 const MCExpr *End = MCSymbolRefExpr::Create(
594 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
595 MCOS->EmitAbsValue(End, AddrSize);
596
597 // AT_name, the name of the source file. Reconstruct from the first directory
598 // and file table entries.
Manman Ren5ce24ff2013-03-12 20:17:00 +0000599 const SmallVectorImpl<StringRef> &MCDwarfDirs =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000600 context.getMCDwarfDirs();
601 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000602 MCOS->EmitBytes(MCDwarfDirs[0]);
603 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000604 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000605 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000606 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000607 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000608 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
609
610 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000611 if (!context.getCompilationDir().empty()) {
612 MCOS->EmitBytes(context.getCompilationDir());
613 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
614 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000615
616 // AT_APPLE_flags, the command line arguments of the assembler tool.
617 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
618 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000619 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000620 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
621 }
622
623 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000624 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
625 if (!DwarfDebugProducer.empty()){
626 MCOS->EmitBytes(DwarfDebugProducer);
627 }
628 else {
629 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
630 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
631 MCOS->EmitBytes(StringRef(")"));
632 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000633 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
634
635 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
636 // draft has no standard code for assembler.
637 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
638
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000639 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000640
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000641 // Loop on saved info for dwarf labels and create the DIEs for them.
642 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
643 MCOS->getContext().getMCGenDwarfLabelEntries();
644 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000645 Entries.begin(), ie = Entries.end(); it != ie;
646 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000647 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000648
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000649 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000650 MCOS->EmitULEB128IntValue(2);
651
652 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000653 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000654 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
655
656 // AT_decl_file, index into the file table.
657 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
658
659 // AT_decl_line, source line number.
660 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
661
662 // AT_low_pc, start address of the label.
663 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
664 MCSymbolRefExpr::VK_None, context);
665 MCOS->EmitAbsValue(AT_low_pc, AddrSize);
666
Kevin Enderbye7739d42011-12-09 18:09:40 +0000667 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
668 MCOS->EmitIntValue(0, 1);
669
670 // The DW_TAG_unspecified_parameters DIE abbrev (3).
671 MCOS->EmitULEB128IntValue(3);
672
673 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
674 MCOS->EmitIntValue(0, 1);
675 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000676 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
677 // for the dwarf labels.
678 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000679 Entries.begin(), ie = Entries.end(); it != ie;
680 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000681 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000682 delete Entry;
683 }
684
685 // Add the NULL DIE terminating the Compile Unit DIE's.
686 MCOS->EmitIntValue(0, 1);
687
688 // Now set the value of the symbol at the end of the info section.
689 MCOS->EmitLabel(InfoEnd);
690}
691
692//
693// When generating dwarf for assembly source files this emits the Dwarf
694// sections.
695//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000696void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000697 // Create the dwarf sections in this order (.debug_line already created).
698 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000699 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000700 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000701 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000702 if (!CreateDwarfSectionSymbols)
703 LineSectionSymbol = NULL;
704 MCSymbol *AbbrevSectionSymbol = NULL;
705 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000706 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000707 if (CreateDwarfSectionSymbols) {
708 InfoSectionSymbol = context.CreateTempSymbol();
709 MCOS->EmitLabel(InfoSectionSymbol);
710 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000711 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000712 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000713 AbbrevSectionSymbol = context.CreateTempSymbol();
714 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000715 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000716 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
717
718 // If there are no line table entries then do not emit any section contents.
David Blaikie11765bc2014-03-13 17:55:28 +0000719 if (!context.hasMCLineSections())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000720 return;
721
722 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000723 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000724
725 // Output the data for .debug_abbrev section.
726 EmitGenDwarfAbbrev(MCOS);
727
728 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000729 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000730}
731
732//
733// When generating dwarf for assembly source files this is called when symbol
734// for a label is created. If this symbol is not a temporary and is in the
735// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000736// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000737//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000738void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000739 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000740 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741 // the default text.
742 if (Symbol->isTemporary())
743 return;
744 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000745 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000746 return;
747
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000748 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000749 // underbar if any.
750 StringRef Name = Symbol->getName();
751 if (Name.startswith("_"))
752 Name = Name.substr(1, Name.size()-1);
753
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000754 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000755 unsigned FileNumber = context.getGenDwarfFileNumber();
756
757 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000758 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000759 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
760 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
761
762 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
763 // values so that they don't have things like an ARM thumb bit from the
764 // original symbol. So when used they won't get a low bit set after
765 // relocation.
766 MCSymbol *Label = context.CreateTempSymbol();
767 MCOS->EmitLabel(Label);
768
769 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000770 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000771 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
772 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773}
774
Rafael Espindola0a017a62010-12-10 07:39:47 +0000775static int getDataAlignmentFactor(MCStreamer &streamer) {
776 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000777 const MCAsmInfo *asmInfo = context.getAsmInfo();
778 int size = asmInfo->getCalleeSaveStackSlotSize();
779 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000780 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000781 else
782 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000783}
784
Rafael Espindola5395f442011-04-22 00:08:43 +0000785static unsigned getSizeForEncoding(MCStreamer &streamer,
786 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000787 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000788 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000789 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000790 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000791 case dwarf::DW_EH_PE_absptr:
792 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000793 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000794 case dwarf::DW_EH_PE_udata2:
795 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000796 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000797 case dwarf::DW_EH_PE_udata4:
798 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000799 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000800 case dwarf::DW_EH_PE_udata8:
801 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000802 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000803 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000804}
805
Iain Sandoe618def62014-01-08 10:22:54 +0000806static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
807 unsigned symbolEncoding, bool isEH,
808 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000809 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000810 const MCAsmInfo *asmInfo = context.getAsmInfo();
811 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
812 symbolEncoding,
813 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000814 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000815 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000816 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
817 streamer.EmitAbsValue(v, size);
818 else
819 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000820}
821
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000822static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
823 unsigned symbolEncoding) {
824 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000825 const MCAsmInfo *asmInfo = context.getAsmInfo();
826 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
827 symbolEncoding,
828 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000829 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000830 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000831}
832
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000833namespace {
834 class FrameEmitterImpl {
835 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000836 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000837 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000838 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000839 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000840 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000841 FrameEmitterImpl(bool usingCFI, bool isEH)
842 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
843 SectionStart(0) {}
844
845 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000846
Bill Wendling3af441f2013-09-05 00:54:52 +0000847 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000848 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000849 const MCDwarfFrameInfo &frame);
850
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000851 const MCSymbol &EmitCIE(MCStreamer &streamer,
852 const MCSymbol *personality,
853 unsigned personalityEncoding,
854 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000855 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000856 unsigned lsdaEncoding,
857 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000858 MCSymbol *EmitFDE(MCStreamer &streamer,
859 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000860 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000861 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000862 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000863 MCSymbol *BaseLabel);
864 void EmitCFIInstruction(MCStreamer &Streamer,
865 const MCCFIInstruction &Instr);
866 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000867
868} // end anonymous namespace
869
870static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
871 StringRef Prefix) {
872 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000873 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000874 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000875 default: EncStr = "<unknown encoding>"; break;
876 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
877 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
878 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
879 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
880 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
881 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
882 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
883 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
884 EncStr = "pcrel udata4";
885 break;
886 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
887 EncStr = "pcrel sdata4";
888 break;
889 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
890 EncStr = "pcrel udata8";
891 break;
892 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
893 EncStr = "screl sdata8";
894 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000895 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
896 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000897 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000898 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
899 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000900 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000901 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
902 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000903 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000904 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
905 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000906 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000907 }
908
909 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
910 }
911
912 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000913}
914
915void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
916 const MCCFIInstruction &Instr) {
917 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +0000918 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000919
920 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +0000921 case MCCFIInstruction::OpRegister: {
922 unsigned Reg1 = Instr.getRegister();
923 unsigned Reg2 = Instr.getRegister2();
924 if (VerboseAsm) {
925 Streamer.AddComment("DW_CFA_register");
926 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
927 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
928 }
929 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
930 Streamer.EmitULEB128IntValue(Reg1);
931 Streamer.EmitULEB128IntValue(Reg2);
932 return;
933 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +0000934 case MCCFIInstruction::OpWindowSave: {
935 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
936 return;
937 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000938 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000939 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +0000940 if (VerboseAsm) {
941 Streamer.AddComment("DW_CFA_undefined");
942 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
943 }
944 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
945 Streamer.EmitULEB128IntValue(Reg);
946 return;
947 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000948 case MCCFIInstruction::OpAdjustCfaOffset:
949 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000950 const bool IsRelative =
951 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
952
953 if (VerboseAsm)
954 Streamer.AddComment("DW_CFA_def_cfa_offset");
955 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
956
957 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000958 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000959 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000960 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000961
962 if (VerboseAsm)
963 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
964 Streamer.EmitULEB128IntValue(CFAOffset);
965
966 return;
967 }
968 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000969 if (VerboseAsm)
970 Streamer.AddComment("DW_CFA_def_cfa");
971 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
972
973 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000974 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
975 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000976
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000977 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000978
979 if (VerboseAsm)
980 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
981 Streamer.EmitULEB128IntValue(CFAOffset);
982
983 return;
984 }
985
986 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000987 if (VerboseAsm)
988 Streamer.AddComment("DW_CFA_def_cfa_register");
989 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
990
991 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +0000992 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
993 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +0000994
995 return;
996 }
997
998 case MCCFIInstruction::OpOffset:
999 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001000 const bool IsRelative =
1001 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001002
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001003 unsigned Reg = Instr.getRegister();
1004 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001005 if (IsRelative)
1006 Offset -= CFAOffset;
1007 Offset = Offset / dataAlignmentFactor;
1008
1009 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001010 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001011 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001012 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001013 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001014 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001015 Streamer.EmitSLEB128IntValue(Offset);
1016 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001017 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1018 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001019 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001020 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001021 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001022 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001023 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001024 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001025 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001026 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001027 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001028 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001029 }
1030 return;
1031 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001032 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001033 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001034 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1035 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001036 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001037 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001038 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1039 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001040 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001041 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001042 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001043 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001044 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001045 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001046 return;
1047 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001048 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001049 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001050 if (VerboseAsm) {
1051 Streamer.AddComment("DW_CFA_restore");
1052 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1053 }
1054 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1055 return;
1056 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001057 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001058 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001059 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001060 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001061 }
1062 llvm_unreachable("Unhandled case in switch");
1063}
1064
1065/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1066/// frame.
1067void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001068 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001069 MCSymbol *BaseLabel) {
1070 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1071 const MCCFIInstruction &Instr = Instrs[i];
1072 MCSymbol *Label = Instr.getLabel();
1073 // Throw out move if the label is invalid.
1074 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1075
1076 // Advance row if new location.
1077 if (BaseLabel && Label) {
1078 MCSymbol *ThisSym = Label;
1079 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001080 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001081 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1082 BaseLabel = ThisSym;
1083 }
1084 }
1085
1086 EmitCFIInstruction(streamer, Instr);
1087 }
1088}
1089
Bill Wendling3af441f2013-09-05 00:54:52 +00001090/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001091void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001092 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001093 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001094 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001095 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001096
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001097 // range-start range-length compact-unwind-enc personality-func lsda
1098 // _foo LfooEnd-_foo 0x00000023 0 0
1099 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1100 //
1101 // .section __LD,__compact_unwind,regular,debug
1102 //
1103 // # compact unwind for _foo
1104 // .quad _foo
1105 // .set L1,LfooEnd-_foo
1106 // .long L1
1107 // .long 0x01010001
1108 // .quad 0
1109 // .quad 0
1110 //
1111 // # compact unwind for _bar
1112 // .quad _bar
1113 // .set L2,LbarEnd-_bar
1114 // .long L2
1115 // .long 0x01020011
1116 // .quad __gxx_personality
1117 // .quad except_tab1
1118
Bill Wendling49bc6802011-07-19 00:06:12 +00001119 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001120 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001121 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001122
Bill Wendlingdafd5982011-07-14 23:34:45 +00001123 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001124 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001125 Encoding |= 0x40000000;
1126
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001127 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001128 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001129 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001130 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001131 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001132
1133 // Range Length
1134 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1135 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001136 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001137 Streamer.EmitAbsValue(Range, 4);
1138
Bill Wendling75174662011-06-30 00:30:52 +00001139 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001140 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001141 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1142 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001143 Streamer.EmitIntValue(Encoding, Size);
1144
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001145 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001146 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001147 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001148 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001149 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001150 else
1151 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001152
1153 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001154 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001155 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001156 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001157 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001158 else
1159 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001160}
1161
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001162const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1163 const MCSymbol *personality,
1164 unsigned personalityEncoding,
1165 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001166 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001167 unsigned lsdaEncoding,
1168 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001169 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001170 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001171 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001172 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001173
1174 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001175 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001176 sectionStart = context.CreateTempSymbol();
1177 else
1178 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1179
Bill Wendling567a1ae2011-06-30 21:25:51 +00001180 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001181 CIENum++;
1182
Bill Wendlingb6adf462011-07-07 00:54:13 +00001183 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001184
1185 // Length
1186 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1187 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001188 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001189 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001190
1191 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001192 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001193 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001194 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001195
1196 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001197 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001198 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1199
1200 // Augmentation String
1201 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001202 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001203 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001204 Augmentation += "z";
1205 if (personality)
1206 Augmentation += "P";
1207 if (lsda)
1208 Augmentation += "L";
1209 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001210 if (IsSignalFrame)
1211 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001212 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001213 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001214 streamer.EmitIntValue(0, 1);
1215
1216 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001217 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001218 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001219
1220 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001221 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001222 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1223
1224 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001225 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001226 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001227
1228 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001229
1230 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001231 if (IsEH) {
1232 if (personality) {
1233 // Personality Encoding
1234 augmentationLength += 1;
1235 // Personality
1236 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1237 }
1238 if (lsda)
1239 augmentationLength += 1;
1240 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001241 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001242
Bill Wendling567a1ae2011-06-30 21:25:51 +00001243 if (verboseAsm) streamer.AddComment("Augmentation Size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001244 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001245
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001246 // Augmentation Data (optional)
1247 if (personality) {
1248 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001249 EmitEncodingByte(streamer, personalityEncoding,
1250 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001251 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001252 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001253 EmitPersonality(streamer, *personality, personalityEncoding);
1254 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001255
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001256 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001257 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1258
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001259 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001260 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001261 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001262 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001263
1264 // Initial Instructions
1265
Bill Wendlingbc07a892013-06-18 07:20:20 +00001266 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001267 if (!IsSimple) {
1268 const std::vector<MCCFIInstruction> &Instructions =
1269 MAI->getInitialFrameState();
1270 EmitCFIInstructions(streamer, Instructions, NULL);
1271 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001272
1273 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001274 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001275
1276 streamer.EmitLabel(sectionEnd);
1277 return *sectionStart;
1278}
1279
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001280MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1281 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001282 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001283 MCContext &context = streamer.getContext();
1284 MCSymbol *fdeStart = context.CreateTempSymbol();
1285 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001286 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001287 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001288
Evan Chengeee86452011-08-24 22:31:37 +00001289 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001290 MCSymbol *EHSym =
1291 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001292 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001293 streamer.EmitLabel(EHSym);
1294 }
1295
Rafael Espindola0a017a62010-12-10 07:39:47 +00001296 // Length
1297 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001298 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001299 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001300
1301 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001302
Rafael Espindola0a017a62010-12-10 07:39:47 +00001303 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001304 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001305 if (IsEH) {
1306 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1307 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001308 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001309 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001310 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001311 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1312 cieStart, 0);
1313 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001314 } else {
1315 streamer.EmitSymbolValue(&cieStart, 4);
1316 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001317
Rafael Espindola0a017a62010-12-10 07:39:47 +00001318 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001319 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1320 : (unsigned)dwarf::DW_EH_PE_absptr;
1321 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001322 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001323
1324 // PC Range
1325 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1326 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001327 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001328 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001329
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001330 if (IsEH) {
1331 // Augmentation Data Length
1332 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001333
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001334 if (frame.Lsda)
1335 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001336
Bill Wendlingf166ab42011-06-30 22:02:20 +00001337 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001338 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001339
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001340 // Augmentation Data
1341 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001342 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1343 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001344 }
Rafael Espindola68067662011-04-29 03:06:29 +00001345
Rafael Espindola0a017a62010-12-10 07:39:47 +00001346 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001347 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001348
Rafael Espindola0a017a62010-12-10 07:39:47 +00001349 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001350 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001351
1352 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001353}
1354
Benjamin Kramer570dd782010-12-30 22:34:44 +00001355namespace {
1356 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001357 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1358 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001359
Benjamin Kramer570dd782010-12-30 22:34:44 +00001360 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001361 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001362 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001363 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1364 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001365 }
1366 const MCSymbol* Personality;
1367 unsigned PersonalityEncoding;
1368 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001369 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001370 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001371 };
1372}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001373
1374namespace llvm {
1375 template <>
1376 struct DenseMapInfo<CIEKey> {
1377 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001378 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001379 }
1380 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001381 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001382 }
1383 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001384 return static_cast<unsigned>(hash_combine(Key.Personality,
1385 Key.PersonalityEncoding,
1386 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001387 Key.IsSignalFrame,
1388 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001389 }
1390 static bool isEqual(const CIEKey &LHS,
1391 const CIEKey &RHS) {
1392 return LHS.Personality == RHS.Personality &&
1393 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001394 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001395 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1396 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001397 }
1398 };
1399}
1400
Bill Wendling550c76d2013-09-09 19:48:37 +00001401void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1402 bool UsingCFI, bool IsEH) {
1403 Streamer.generateCompactUnwindEncodings(MAB);
1404
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001405 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001406 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001407 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001408 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001409
Bill Wendling9803abb2011-09-06 18:37:11 +00001410 // Emit the compact unwind info if available.
Bill Wendling4e9fc022013-04-24 03:11:14 +00001411 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001412 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001413 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1414 const MCDwarfFrameInfo &Frame = FrameArray[i];
1415 if (Frame.CompactUnwindEncoding == 0) continue;
1416 if (!SectionEmitted) {
1417 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001418 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001419 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001420 }
Bob Wilson0e180f22013-05-07 20:56:33 +00001421 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001422 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001423 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001424
Bill Wendlinga800f952013-05-30 18:52:57 +00001425 const MCSection &Section =
1426 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1427 *MOFI->getDwarfFrameSection();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001428 Streamer.SwitchSection(&Section);
1429 MCSymbol *SectionStart = Context.CreateTempSymbol();
1430 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001431 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001432
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001433 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001434 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001435
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001436 const MCSymbol *DummyDebugKey = NULL;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001437 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1438 const MCDwarfFrameInfo &Frame = FrameArray[i];
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001439 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001440 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001441 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1442 if (!CIEStart)
1443 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1444 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001445 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001446 Frame.LsdaEncoding,
1447 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001448
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001449 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001450
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001451 if (i != n - 1)
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001452 Streamer.EmitLabel(FDEEnd);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001453 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001454
Bill Wendlingbc07a892013-06-18 07:20:20 +00001455 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001456 if (FDEEnd)
1457 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001458}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001459
1460void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1461 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001462 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001463 SmallString<256> Tmp;
1464 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001465 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001466 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001467}
1468
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001469void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1470 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001471 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001472 // Scale the address delta by the minimum instruction length.
1473 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1474
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001475 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001476 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001477 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1478 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001479 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001480 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1481 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001482 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001483 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001484 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001485 OS << uint8_t( AddrDelta & 0xff);
1486 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001487 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001488 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001489 assert(isUInt<32>(AddrDelta));
1490 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001491 OS << uint8_t( AddrDelta & 0xff);
1492 OS << uint8_t((AddrDelta >> 8) & 0xff);
1493 OS << uint8_t((AddrDelta >> 16) & 0xff);
1494 OS << uint8_t((AddrDelta >> 24) & 0xff);
1495
Rafael Espindola736a35d2010-12-28 05:39:27 +00001496 }
1497}