blob: 5fa8f66422b9ba5e5f9968aab2352a8da8e65402 [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 Blaikie8287aff2014-03-18 02:13:23 +0000227void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS) const {
228 MCOS.EmitLabel(Header.Emit(&MCOS, None).second);
229}
230
David Blaikie5cff0e62014-03-13 21:47:12 +0000231std::pair<MCSymbol *, MCSymbol *> MCDwarfLineTableHeader::Emit(MCStreamer *MCOS) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000232 static const char StandardOpcodeLengths[] = {
233 0, // length of DW_LNS_copy
234 1, // length of DW_LNS_advance_pc
235 1, // length of DW_LNS_advance_line
236 1, // length of DW_LNS_set_file
237 1, // length of DW_LNS_set_column
238 0, // length of DW_LNS_negate_stmt
239 0, // length of DW_LNS_set_basic_block
240 0, // length of DW_LNS_const_add_pc
241 1, // length of DW_LNS_fixed_advance_pc
242 0, // length of DW_LNS_set_prologue_end
243 0, // length of DW_LNS_set_epilogue_begin
244 1 // DW_LNS_set_isa
245 };
246 assert(array_lengthof(StandardOpcodeLengths) == (DWARF2_LINE_OPCODE_BASE - 1));
247 return Emit(MCOS, StandardOpcodeLengths);
248}
249
250std::pair<MCSymbol *, MCSymbol *>
251MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
252 ArrayRef<char> StandardOpcodeLengths) const {
253
Manman Ren4e042a62013-02-05 21:52:47 +0000254 MCContext &context = MCOS->getContext();
255
256 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000257 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000258 if (!LineStartSym)
259 LineStartSym = context.CreateTempSymbol();
260 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000261 MCOS->EmitLabel(LineStartSym);
262
263 // Create a symbol for the end of the section (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000264 MCSymbol *LineEndSym = context.CreateTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000265
266 // The first 4 bytes is the total length of the information for this
267 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000268 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym,4),
Rafael Espindola44bbe362010-12-06 17:27:56 +0000269 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000270
271 // Next 2 bytes is the Version, which is Dwarf 2.
272 MCOS->EmitIntValue(2, 2);
273
274 // Create a symbol for the end of the prologue (to be set when we get there).
Rafael Espindola0a017a62010-12-10 07:39:47 +0000275 MCSymbol *ProEndSym = context.CreateTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000276
277 // Length of the prologue, is the next 4 bytes. Which is the start of the
278 // section to the end of the prologue. Not including the 4 bytes for the
279 // total length, the 2 bytes for the version, and these 4 bytes for the
280 // length of the prologue.
Rafael Espindolad66e65d2010-12-09 23:08:35 +0000281 MCOS->EmitAbsValue(MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym,
Rafael Espindola64e1af82013-07-02 15:49:13 +0000282 (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000283
284 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000285 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000286 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
287 MCOS->EmitIntValue(DWARF2_LINE_BASE, 1);
288 MCOS->EmitIntValue(DWARF2_LINE_RANGE, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000289 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000290
291 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000292 for (char Length : StandardOpcodeLengths)
293 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000294
295 // Put out the directory and file tables.
296
297 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000298 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000299 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
300 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000301 }
302 MCOS->EmitIntValue(0, 1); // Terminate the directory list
303
304 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000305 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000306 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000307 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000308 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000309 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000310 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000311 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
312 MCOS->EmitIntValue(0, 1); // filesize (always 0)
313 }
314 MCOS->EmitIntValue(0, 1); // Terminate the file list
315
316 // This is the end of the prologue, so set the value of the symbol at the
317 // end of the prologue (that was used in a previous expression).
318 MCOS->EmitLabel(ProEndSym);
319
David Blaikie5cff0e62014-03-13 21:47:12 +0000320 return std::make_pair(LineStartSym, LineEndSym);
321}
322
David Blaikied9012ba2014-03-13 21:59:51 +0000323const MCSymbol *MCDwarfLineTable::EmitCU(MCStreamer *MCOS) const {
David Blaikie5cff0e62014-03-13 21:47:12 +0000324 MCSymbol *LineStartSym;
325 MCSymbol *LineEndSym;
326 std::tie(LineStartSym, LineEndSym) = Header.Emit(MCOS);
327
Kevin Enderbye46564a2010-09-30 16:52:03 +0000328 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000329 for (const auto &LineSec : MCLineSections.getMCLineEntries())
330 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000331
David Blaikiea55e64f2014-03-12 22:28:56 +0000332 if (MCOS->getContext().getAsmInfo()->getLinkerRequiresNonEmptyDwarfLines() &&
David Blaikie11765bc2014-03-13 17:55:28 +0000333 MCLineSections.getMCLineEntries().empty()) {
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000334 // The darwin9 linker has a bug (see PR8715). For for 32-bit architectures
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000335 // it requires:
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000336 // total_length >= prologue_length + 10
337 // We are 4 bytes short, since we have total_length = 51 and
338 // prologue_length = 45
Devang Patel5eed2e62010-12-03 00:10:48 +0000339
Rafael Espindolaf8af7782010-12-03 23:36:59 +0000340 // The regular end_sequence should be sufficient.
341 MCDwarfLineAddr::Emit(MCOS, INT64_MAX, 0);
Devang Patel5eed2e62010-12-03 00:10:48 +0000342 }
343
Kevin Enderbye46564a2010-09-30 16:52:03 +0000344 // This is the end of the section, so set the value of the symbol at the end
345 // of this section (that was used in a previous expression).
346 MCOS->EmitLabel(LineEndSym);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000347
348 return LineStartSym;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000349}
350
David Blaikiec2df16b2014-03-17 18:13:58 +0000351unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000352 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000353 return Header.getFile(Directory, FileName, FileNumber);
354}
355
David Blaikiec2df16b2014-03-17 18:13:58 +0000356unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
357 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000358 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000359 if (Directory == CompilationDir)
360 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000361 if (FileName.empty()) {
362 FileName = "<stdin>";
363 Directory = "";
364 }
365 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000366 if (FileNumber == 0) {
367 FileNumber = SourceIdMap.size() + 1;
368 assert((MCDwarfFiles.empty() || FileNumber == MCDwarfFiles.size()) &&
369 "Don't mix autonumbered and explicit numbered line table usage");
370 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(
371 (Directory + Twine('\0') + FileName).str(), FileNumber);
372 if (Ent.getValue() != FileNumber)
373 return Ent.getValue();
374 }
David Blaikie498589c2014-03-13 19:15:04 +0000375 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
376 MCDwarfFiles.resize(FileNumber + 1);
377
378 // Get the new MCDwarfFile slot for this FileNumber.
379 MCDwarfFile &File = MCDwarfFiles[FileNumber];
380
381 // It is an error to use see the same number more than once.
382 if (!File.Name.empty())
383 return 0;
384
385 if (Directory.empty()) {
386 // Separate the directory part from the basename of the FileName.
387 StringRef tFileName = sys::path::filename(FileName);
388 if (!tFileName.empty()) {
389 Directory = sys::path::parent_path(FileName);
390 if (!Directory.empty())
391 FileName = tFileName;
392 }
393 }
394
395 // Find or make an entry in the MCDwarfDirs vector for this Directory.
396 // Capture directory name.
397 unsigned DirIndex;
398 if (Directory.empty()) {
399 // For FileNames with no directories a DirIndex of 0 is used.
400 DirIndex = 0;
401 } else {
402 DirIndex = 0;
403 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
404 if (Directory == MCDwarfDirs[DirIndex])
405 break;
406 }
407 if (DirIndex >= MCDwarfDirs.size())
408 MCDwarfDirs.push_back(Directory);
409 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
410 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
411 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
412 // are stored at MCDwarfFiles[FileNumber].Name .
413 DirIndex++;
414 }
415
416 File.Name = FileName;
417 File.DirIndex = DirIndex;
418
419 // return the allocated FileNumber.
420 return FileNumber;
421}
422
Kevin Enderbye46564a2010-09-30 16:52:03 +0000423/// Utility function to emit the encoding to a streamer.
Rafael Espindolab58867c2010-11-19 02:26:16 +0000424void MCDwarfLineAddr::Emit(MCStreamer *MCOS, int64_t LineDelta,
Kevin Enderbye46564a2010-09-30 16:52:03 +0000425 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000426 MCContext &Context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000427 SmallString<256> Tmp;
428 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000429 MCDwarfLineAddr::Encode(Context, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000430 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000431}
432
433/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000434void MCDwarfLineAddr::Encode(MCContext &Context, int64_t LineDelta,
435 uint64_t AddrDelta, raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000436 uint64_t Temp, Opcode;
437 bool NeedCopy = false;
438
439 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000440 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000441
442 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000443 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000444 // end_sequence to emit the matrix entry.
445 if (LineDelta == INT64_MAX) {
446 if (AddrDelta == MAX_SPECIAL_ADDR_DELTA)
447 OS << char(dwarf::DW_LNS_const_add_pc);
448 else {
449 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000450 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000451 }
452 OS << char(dwarf::DW_LNS_extended_op);
453 OS << char(1);
454 OS << char(dwarf::DW_LNE_end_sequence);
455 return;
456 }
457
458 // Bias the line delta by the base.
459 Temp = LineDelta - DWARF2_LINE_BASE;
460
461 // If the line increment is out of range of a special opcode, we must encode
462 // it with DW_LNS_advance_line.
463 if (Temp >= DWARF2_LINE_RANGE) {
464 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000465 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000466
467 LineDelta = 0;
468 Temp = 0 - DWARF2_LINE_BASE;
469 NeedCopy = true;
470 }
471
472 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
473 if (LineDelta == 0 && AddrDelta == 0) {
474 OS << char(dwarf::DW_LNS_copy);
475 return;
476 }
477
478 // Bias the opcode by the special opcode base.
479 Temp += DWARF2_LINE_OPCODE_BASE;
480
481 // Avoid overflow when addr_delta is large.
482 if (AddrDelta < 256 + MAX_SPECIAL_ADDR_DELTA) {
483 // Try using a special opcode.
484 Opcode = Temp + AddrDelta * DWARF2_LINE_RANGE;
485 if (Opcode <= 255) {
486 OS << char(Opcode);
487 return;
488 }
489
490 // Try using DW_LNS_const_add_pc followed by special op.
491 Opcode = Temp + (AddrDelta - MAX_SPECIAL_ADDR_DELTA) * DWARF2_LINE_RANGE;
492 if (Opcode <= 255) {
493 OS << char(dwarf::DW_LNS_const_add_pc);
494 OS << char(Opcode);
495 return;
496 }
497 }
498
499 // Otherwise use DW_LNS_advance_pc.
500 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000501 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000502
503 if (NeedCopy)
504 OS << char(dwarf::DW_LNS_copy);
505 else
506 OS << char(Temp);
507}
508
Kevin Enderbye7739d42011-12-09 18:09:40 +0000509// Utility function to write a tuple for .debug_abbrev.
510static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
511 MCOS->EmitULEB128IntValue(Name);
512 MCOS->EmitULEB128IntValue(Form);
513}
514
515// When generating dwarf for assembly source files this emits
516// the data for .debug_abbrev section which contains three DIEs.
517static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
518 MCContext &context = MCOS->getContext();
519 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
520
521 // DW_TAG_compile_unit DIE abbrev (1).
522 MCOS->EmitULEB128IntValue(1);
523 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
524 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
525 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
526 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
527 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
528 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000529 if (!context.getCompilationDir().empty())
530 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000531 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
532 if (!DwarfDebugFlags.empty())
533 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
534 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
535 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
536 EmitAbbrev(MCOS, 0, 0);
537
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000538 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000539 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000540 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000541 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
542 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
543 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
544 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
545 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000546 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
547 EmitAbbrev(MCOS, 0, 0);
548
549 // DW_TAG_unspecified_parameters DIE abbrev (3).
550 MCOS->EmitULEB128IntValue(3);
551 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
552 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
553 EmitAbbrev(MCOS, 0, 0);
554
555 // Terminate the abbreviations for this compilation unit.
556 MCOS->EmitIntValue(0, 1);
557}
558
559// When generating dwarf for assembly source files this emits the data for
560// .debug_aranges section. Which contains a header and a table of pairs of
561// PointerSize'ed values for the address and size of section(s) with line table
562// entries (just the default .text in our case) and a terminating pair of zeros.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000563static void EmitGenDwarfAranges(MCStreamer *MCOS,
564 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000565 MCContext &context = MCOS->getContext();
566
567 // Create a symbol at the end of the section that we are creating the dwarf
568 // debugging info to use later in here as part of the expression to calculate
569 // the size of the section for the table.
570 MCOS->SwitchSection(context.getGenDwarfSection());
571 MCSymbol *SectionEndSym = context.CreateTempSymbol();
572 MCOS->EmitLabel(SectionEndSym);
573 context.setGenDwarfSectionEndSym(SectionEndSym);
574
575 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
576
577 // This will be the length of the .debug_aranges section, first account for
578 // the size of each item in the header (see below where we emit these items).
579 int Length = 4 + 2 + 4 + 1 + 1;
580
581 // Figure the padding after the header before the table of address and size
582 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000583 const MCAsmInfo *asmInfo = context.getAsmInfo();
584 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000585 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
586 if (Pad == 2 * AddrSize)
587 Pad = 0;
588 Length += Pad;
589
590 // Add the size of the pair of PointerSize'ed values for the address and size
591 // of the one default .text section we have in the table.
592 Length += 2 * AddrSize;
593 // And the pair of terminating zeros.
594 Length += 2 * AddrSize;
595
596
597 // Emit the header for this section.
598 // The 4 byte length not including the 4 byte value for the length.
599 MCOS->EmitIntValue(Length - 4, 4);
600 // The 2 byte version, which is 2.
601 MCOS->EmitIntValue(2, 2);
602 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000603 // of the .debug_info.
604 if (InfoSectionSymbol)
605 MCOS->EmitSymbolValue(InfoSectionSymbol, 4);
606 else
607 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000608 // The 1 byte size of an address.
609 MCOS->EmitIntValue(AddrSize, 1);
610 // The 1 byte size of a segment descriptor, we use a value of zero.
611 MCOS->EmitIntValue(0, 1);
612 // Align the header with the padding if needed, before we put out the table.
613 for(int i = 0; i < Pad; i++)
614 MCOS->EmitIntValue(0, 1);
615
616 // Now emit the table of pairs of PointerSize'ed values for the section(s)
617 // address and size, in our case just the one default .text section.
618 const MCExpr *Addr = MCSymbolRefExpr::Create(
619 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
620 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
621 *context.getGenDwarfSectionStartSym(), *SectionEndSym, 0);
Rafael Espindola98629c42014-03-20 21:26:38 +0000622 MCOS->EmitValue(Addr, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000623 MCOS->EmitAbsValue(Size, AddrSize);
624
625 // And finally the pair of terminating zeros.
626 MCOS->EmitIntValue(0, AddrSize);
627 MCOS->EmitIntValue(0, AddrSize);
628}
629
630// When generating dwarf for assembly source files this emits the data for
631// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000632// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000633static void EmitGenDwarfInfo(MCStreamer *MCOS,
634 const MCSymbol *AbbrevSectionSymbol,
635 const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000636 MCContext &context = MCOS->getContext();
637
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000638 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000639
640 // Create a symbol at the start and end of this section used in here for the
641 // expression to calculate the length in the header.
642 MCSymbol *InfoStart = context.CreateTempSymbol();
643 MCOS->EmitLabel(InfoStart);
644 MCSymbol *InfoEnd = context.CreateTempSymbol();
645
646 // First part: the header.
647
648 // The 4 byte total length of the information for this compilation unit, not
649 // including these 4 bytes.
650 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
651 MCOS->EmitAbsValue(Length, 4);
652
653 // The 2 byte DWARF version, which is 2.
654 MCOS->EmitIntValue(2, 2);
655
656 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
657 // it is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000658 if (AbbrevSectionSymbol) {
659 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4);
660 } else {
661 MCOS->EmitIntValue(0, 4);
662 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000663
Bill Wendlingbc07a892013-06-18 07:20:20 +0000664 const MCAsmInfo *asmInfo = context.getAsmInfo();
665 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000666 // The 1 byte size of an address.
667 MCOS->EmitIntValue(AddrSize, 1);
668
669 // Second part: the compile_unit DIE.
670
671 // The DW_TAG_compile_unit DIE abbrev (1).
672 MCOS->EmitULEB128IntValue(1);
673
674 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
675 // which is at the start of that section so this is zero.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000676 if (LineSectionSymbol) {
677 MCOS->EmitSymbolValue(LineSectionSymbol, 4);
678 } else {
679 MCOS->EmitIntValue(0, 4);
680 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000681
682 // AT_low_pc, the first address of the default .text section.
683 const MCExpr *Start = MCSymbolRefExpr::Create(
684 context.getGenDwarfSectionStartSym(), MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000685 MCOS->EmitValue(Start, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686
687 // AT_high_pc, the last address of the default .text section.
688 const MCExpr *End = MCSymbolRefExpr::Create(
689 context.getGenDwarfSectionEndSym(), MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000690 MCOS->EmitValue(End, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000691
692 // AT_name, the name of the source file. Reconstruct from the first directory
693 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000694 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000695 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000696 MCOS->EmitBytes(MCDwarfDirs[0]);
697 MCOS->EmitBytes("/");
Kevin Enderbye7739d42011-12-09 18:09:40 +0000698 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000699 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000700 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000701 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000702 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
703
704 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000705 if (!context.getCompilationDir().empty()) {
706 MCOS->EmitBytes(context.getCompilationDir());
707 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
708 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000709
710 // AT_APPLE_flags, the command line arguments of the assembler tool.
711 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
712 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000713 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000714 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
715 }
716
717 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000718 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
719 if (!DwarfDebugProducer.empty()){
720 MCOS->EmitBytes(DwarfDebugProducer);
721 }
722 else {
723 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM "));
724 MCOS->EmitBytes(StringRef(PACKAGE_VERSION));
725 MCOS->EmitBytes(StringRef(")"));
726 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000727 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
728
729 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
730 // draft has no standard code for assembler.
731 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
732
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000733 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000734
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000735 // Loop on saved info for dwarf labels and create the DIEs for them.
736 const std::vector<const MCGenDwarfLabelEntry *> &Entries =
737 MCOS->getContext().getMCGenDwarfLabelEntries();
738 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000739 Entries.begin(), ie = Entries.end(); it != ie;
740 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000741 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000742
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000743 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000744 MCOS->EmitULEB128IntValue(2);
745
746 // AT_name, of the label without any leading underbar.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000747 MCOS->EmitBytes(Entry->getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000748 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
749
750 // AT_decl_file, index into the file table.
751 MCOS->EmitIntValue(Entry->getFileNumber(), 4);
752
753 // AT_decl_line, source line number.
754 MCOS->EmitIntValue(Entry->getLineNumber(), 4);
755
756 // AT_low_pc, start address of the label.
757 const MCExpr *AT_low_pc = MCSymbolRefExpr::Create(Entry->getLabel(),
758 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000759 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000760
Kevin Enderbye7739d42011-12-09 18:09:40 +0000761 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
762 MCOS->EmitIntValue(0, 1);
763
764 // The DW_TAG_unspecified_parameters DIE abbrev (3).
765 MCOS->EmitULEB128IntValue(3);
766
767 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
768 MCOS->EmitIntValue(0, 1);
769 }
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000770 // Deallocate the MCGenDwarfLabelEntry classes that saved away the info
771 // for the dwarf labels.
772 for (std::vector<const MCGenDwarfLabelEntry *>::const_iterator it =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773 Entries.begin(), ie = Entries.end(); it != ie;
774 ++it) {
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000775 const MCGenDwarfLabelEntry *Entry = *it;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000776 delete Entry;
777 }
778
779 // Add the NULL DIE terminating the Compile Unit DIE's.
780 MCOS->EmitIntValue(0, 1);
781
782 // Now set the value of the symbol at the end of the info section.
783 MCOS->EmitLabel(InfoEnd);
784}
785
786//
787// When generating dwarf for assembly source files this emits the Dwarf
788// sections.
789//
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000790void MCGenDwarfInfo::Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000791 // Create the dwarf sections in this order (.debug_line already created).
792 MCContext &context = MCOS->getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000793 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000794 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000795 AsmInfo->doesDwarfUseRelocationsAcrossSections();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000796 if (!CreateDwarfSectionSymbols)
797 LineSectionSymbol = NULL;
798 MCSymbol *AbbrevSectionSymbol = NULL;
799 MCSymbol *InfoSectionSymbol = NULL;
Kevin Enderbye7739d42011-12-09 18:09:40 +0000800 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000801 if (CreateDwarfSectionSymbols) {
802 InfoSectionSymbol = context.CreateTempSymbol();
803 MCOS->EmitLabel(InfoSectionSymbol);
804 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000805 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000806 if (CreateDwarfSectionSymbols) {
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000807 AbbrevSectionSymbol = context.CreateTempSymbol();
808 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000809 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000810 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
811
812 // If there are no line table entries then do not emit any section contents.
David Blaikie11765bc2014-03-13 17:55:28 +0000813 if (!context.hasMCLineSections())
Kevin Enderbye7739d42011-12-09 18:09:40 +0000814 return;
815
816 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000817 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000818
819 // Output the data for .debug_abbrev section.
820 EmitGenDwarfAbbrev(MCOS);
821
822 // Output the data for .debug_info section.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000823 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000824}
825
826//
827// When generating dwarf for assembly source files this is called when symbol
828// for a label is created. If this symbol is not a temporary and is in the
829// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000830// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000831//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000832void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000833 SourceMgr &SrcMgr, SMLoc &Loc) {
Eric Christopher73dc95c2012-02-25 01:02:44 +0000834 // We won't create dwarf labels for temporary symbols or symbols not in
Kevin Enderbye7739d42011-12-09 18:09:40 +0000835 // the default text.
836 if (Symbol->isTemporary())
837 return;
838 MCContext &context = MCOS->getContext();
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000839 if (context.getGenDwarfSection() != MCOS->getCurrentSection().first)
Kevin Enderbye7739d42011-12-09 18:09:40 +0000840 return;
841
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000842 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000843 // underbar if any.
844 StringRef Name = Symbol->getName();
845 if (Name.startswith("_"))
846 Name = Name.substr(1, Name.size()-1);
847
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000848 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000849 unsigned FileNumber = context.getGenDwarfFileNumber();
850
851 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000852 // pass it in as for some symbols we won't create a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000853 int CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
854 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
855
856 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
857 // values so that they don't have things like an ARM thumb bit from the
858 // original symbol. So when used they won't get a low bit set after
859 // relocation.
860 MCSymbol *Label = context.CreateTempSymbol();
861 MCOS->EmitLabel(Label);
862
863 // Create and entry for the info and add it to the other entries.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000864 MCGenDwarfLabelEntry *Entry =
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000865 new MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label);
866 MCOS->getContext().addMCGenDwarfLabelEntry(Entry);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000867}
868
Rafael Espindola0a017a62010-12-10 07:39:47 +0000869static int getDataAlignmentFactor(MCStreamer &streamer) {
870 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000871 const MCAsmInfo *asmInfo = context.getAsmInfo();
872 int size = asmInfo->getCalleeSaveStackSlotSize();
873 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000874 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000875 else
876 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000877}
878
Rafael Espindola5395f442011-04-22 00:08:43 +0000879static unsigned getSizeForEncoding(MCStreamer &streamer,
880 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000881 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000882 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000883 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000884 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000885 case dwarf::DW_EH_PE_absptr:
886 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000887 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000888 case dwarf::DW_EH_PE_udata2:
889 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000890 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000891 case dwarf::DW_EH_PE_udata4:
892 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000893 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000894 case dwarf::DW_EH_PE_udata8:
895 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000896 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000897 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000898}
899
Iain Sandoe618def62014-01-08 10:22:54 +0000900static void EmitFDESymbol(MCStreamer &streamer, const MCSymbol &symbol,
901 unsigned symbolEncoding, bool isEH,
902 const char *comment = 0) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000903 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000904 const MCAsmInfo *asmInfo = context.getAsmInfo();
905 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
906 symbolEncoding,
907 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000908 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Bill Wendlingf166ab42011-06-30 22:02:20 +0000909 if (streamer.isVerboseAsm() && comment) streamer.AddComment(comment);
Iain Sandoe618def62014-01-08 10:22:54 +0000910 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
911 streamer.EmitAbsValue(v, size);
912 else
913 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000914}
915
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000916static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
917 unsigned symbolEncoding) {
918 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000919 const MCAsmInfo *asmInfo = context.getAsmInfo();
920 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
921 symbolEncoding,
922 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000923 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000924 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000925}
926
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000927namespace {
928 class FrameEmitterImpl {
929 int CFAOffset;
Rafael Espindolabf60fb02011-04-28 03:26:11 +0000930 int CIENum;
Rafael Espindola750cb612011-05-01 04:49:54 +0000931 bool UsingCFI;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +0000932 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +0000933 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000934 public:
Bill Wendling4d9aa512011-07-22 21:18:59 +0000935 FrameEmitterImpl(bool usingCFI, bool isEH)
936 : CFAOffset(0), CIENum(0), UsingCFI(usingCFI), IsEH(isEH),
937 SectionStart(0) {}
938
939 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000940
Bill Wendling3af441f2013-09-05 00:54:52 +0000941 /// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +0000942 void EmitCompactUnwind(MCStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +0000943 const MCDwarfFrameInfo &frame);
944
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000945 const MCSymbol &EmitCIE(MCStreamer &streamer,
946 const MCSymbol *personality,
947 unsigned personalityEncoding,
948 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +0000949 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +0000950 unsigned lsdaEncoding,
951 bool IsSimple);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000952 MCSymbol *EmitFDE(MCStreamer &streamer,
953 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +0000954 const MCDwarfFrameInfo &frame);
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000955 void EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +0000956 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000957 MCSymbol *BaseLabel);
958 void EmitCFIInstruction(MCStreamer &Streamer,
959 const MCCFIInstruction &Instr);
960 };
Bill Wendling567a1ae2011-06-30 21:25:51 +0000961
962} // end anonymous namespace
963
964static void EmitEncodingByte(MCStreamer &Streamer, unsigned Encoding,
965 StringRef Prefix) {
966 if (Streamer.isVerboseAsm()) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000967 const char *EncStr;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000968 switch (Encoding) {
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000969 default: EncStr = "<unknown encoding>"; break;
970 case dwarf::DW_EH_PE_absptr: EncStr = "absptr"; break;
971 case dwarf::DW_EH_PE_omit: EncStr = "omit"; break;
972 case dwarf::DW_EH_PE_pcrel: EncStr = "pcrel"; break;
973 case dwarf::DW_EH_PE_udata4: EncStr = "udata4"; break;
974 case dwarf::DW_EH_PE_udata8: EncStr = "udata8"; break;
975 case dwarf::DW_EH_PE_sdata4: EncStr = "sdata4"; break;
976 case dwarf::DW_EH_PE_sdata8: EncStr = "sdata8"; break;
977 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
978 EncStr = "pcrel udata4";
979 break;
980 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
981 EncStr = "pcrel sdata4";
982 break;
983 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
984 EncStr = "pcrel udata8";
985 break;
986 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
987 EncStr = "screl sdata8";
988 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000989 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata4:
990 EncStr = "indirect pcrel udata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000991 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000992 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata4:
993 EncStr = "indirect pcrel sdata4";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000994 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000995 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_udata8:
996 EncStr = "indirect pcrel udata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +0000997 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +0000998 case dwarf::DW_EH_PE_indirect |dwarf::DW_EH_PE_pcrel|dwarf::DW_EH_PE_sdata8:
999 EncStr = "indirect pcrel sdata8";
Benjamin Kramerd3309a32012-01-20 14:42:37 +00001000 break;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001001 }
1002
1003 Streamer.AddComment(Twine(Prefix) + " = " + EncStr);
1004 }
1005
1006 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001007}
1008
1009void FrameEmitterImpl::EmitCFIInstruction(MCStreamer &Streamer,
1010 const MCCFIInstruction &Instr) {
1011 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001012 bool VerboseAsm = Streamer.isVerboseAsm();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001013
1014 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001015 case MCCFIInstruction::OpRegister: {
1016 unsigned Reg1 = Instr.getRegister();
1017 unsigned Reg2 = Instr.getRegister2();
1018 if (VerboseAsm) {
1019 Streamer.AddComment("DW_CFA_register");
1020 Streamer.AddComment(Twine("Reg1 ") + Twine(Reg1));
1021 Streamer.AddComment(Twine("Reg2 ") + Twine(Reg2));
1022 }
1023 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1024 Streamer.EmitULEB128IntValue(Reg1);
1025 Streamer.EmitULEB128IntValue(Reg2);
1026 return;
1027 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001028 case MCCFIInstruction::OpWindowSave: {
1029 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1030 return;
1031 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001032 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001033 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001034 if (VerboseAsm) {
1035 Streamer.AddComment("DW_CFA_undefined");
1036 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1037 }
1038 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1039 Streamer.EmitULEB128IntValue(Reg);
1040 return;
1041 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001042 case MCCFIInstruction::OpAdjustCfaOffset:
1043 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001044 const bool IsRelative =
1045 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1046
1047 if (VerboseAsm)
1048 Streamer.AddComment("DW_CFA_def_cfa_offset");
1049 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1050
1051 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001052 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001053 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001054 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001055
1056 if (VerboseAsm)
1057 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1058 Streamer.EmitULEB128IntValue(CFAOffset);
1059
1060 return;
1061 }
1062 case MCCFIInstruction::OpDefCfa: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001063 if (VerboseAsm)
1064 Streamer.AddComment("DW_CFA_def_cfa");
1065 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
1066
1067 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001068 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1069 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001070
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001071 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001072
1073 if (VerboseAsm)
1074 Streamer.AddComment(Twine("Offset " + Twine(CFAOffset)));
1075 Streamer.EmitULEB128IntValue(CFAOffset);
1076
1077 return;
1078 }
1079
1080 case MCCFIInstruction::OpDefCfaRegister: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001081 if (VerboseAsm)
1082 Streamer.AddComment("DW_CFA_def_cfa_register");
1083 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
1084
1085 if (VerboseAsm)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001086 Streamer.AddComment(Twine("Reg ") + Twine(Instr.getRegister()));
1087 Streamer.EmitULEB128IntValue(Instr.getRegister());
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001088
1089 return;
1090 }
1091
1092 case MCCFIInstruction::OpOffset:
1093 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001094 const bool IsRelative =
1095 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001096
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001097 unsigned Reg = Instr.getRegister();
1098 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001099 if (IsRelative)
1100 Offset -= CFAOffset;
1101 Offset = Offset / dataAlignmentFactor;
1102
1103 if (Offset < 0) {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001104 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended_sf");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001105 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001106 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001107 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001108 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001109 Streamer.EmitSLEB128IntValue(Offset);
1110 } else if (Reg < 64) {
Bill Wendling40cc7492011-06-30 23:47:40 +00001111 if (VerboseAsm) Streamer.AddComment(Twine("DW_CFA_offset + Reg(") +
1112 Twine(Reg) + ")");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001113 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001114 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001115 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001116 } else {
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001117 if (VerboseAsm) Streamer.AddComment("DW_CFA_offset_extended");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001118 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001119 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001120 Streamer.EmitULEB128IntValue(Reg);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001121 if (VerboseAsm) Streamer.AddComment(Twine("Offset ") + Twine(Offset));
Rafael Espindolaea61f222011-04-21 23:26:40 +00001122 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001123 }
1124 return;
1125 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001126 case MCCFIInstruction::OpRememberState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001127 if (VerboseAsm) Streamer.AddComment("DW_CFA_remember_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001128 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1129 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001130 case MCCFIInstruction::OpRestoreState:
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001131 if (VerboseAsm) Streamer.AddComment("DW_CFA_restore_state");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001132 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1133 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001134 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001135 unsigned Reg = Instr.getRegister();
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001136 if (VerboseAsm) Streamer.AddComment("DW_CFA_same_value");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001137 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Bill Wendlinge7fe47e2011-06-30 21:45:12 +00001138 if (VerboseAsm) Streamer.AddComment(Twine("Reg ") + Twine(Reg));
Rafael Espindola6aea5922011-04-21 23:39:26 +00001139 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001140 return;
1141 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001142 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001143 unsigned Reg = Instr.getRegister();
Rafael Espindola4ea99812011-12-29 21:43:03 +00001144 if (VerboseAsm) {
1145 Streamer.AddComment("DW_CFA_restore");
1146 Streamer.AddComment(Twine("Reg ") + Twine(Reg));
1147 }
1148 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1149 return;
1150 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001151 case MCCFIInstruction::OpEscape:
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001152 if (VerboseAsm) Streamer.AddComment("Escape bytes");
Eric Christophere3ab3d02013-01-09 01:57:54 +00001153 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001154 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001155 }
1156 llvm_unreachable("Unhandled case in switch");
1157}
1158
1159/// EmitFrameMoves - Emit frame instructions to describe the layout of the
1160/// frame.
1161void FrameEmitterImpl::EmitCFIInstructions(MCStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001162 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001163 MCSymbol *BaseLabel) {
1164 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1165 const MCCFIInstruction &Instr = Instrs[i];
1166 MCSymbol *Label = Instr.getLabel();
1167 // Throw out move if the label is invalid.
1168 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1169
1170 // Advance row if new location.
1171 if (BaseLabel && Label) {
1172 MCSymbol *ThisSym = Label;
1173 if (ThisSym != BaseLabel) {
Bill Wendling2fd8d772011-06-30 22:35:49 +00001174 if (streamer.isVerboseAsm()) streamer.AddComment("DW_CFA_advance_loc4");
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001175 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1176 BaseLabel = ThisSym;
1177 }
1178 }
1179
1180 EmitCFIInstruction(streamer, Instr);
1181 }
1182}
1183
Bill Wendling3af441f2013-09-05 00:54:52 +00001184/// EmitCompactUnwind - Emit the unwind information in a compact way.
Bill Wendling5be12a12013-04-10 22:03:59 +00001185void FrameEmitterImpl::EmitCompactUnwind(MCStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001186 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001187 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001188 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001189 bool VerboseAsm = Streamer.isVerboseAsm();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001190
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001191 // range-start range-length compact-unwind-enc personality-func lsda
1192 // _foo LfooEnd-_foo 0x00000023 0 0
1193 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1194 //
1195 // .section __LD,__compact_unwind,regular,debug
1196 //
1197 // # compact unwind for _foo
1198 // .quad _foo
1199 // .set L1,LfooEnd-_foo
1200 // .long L1
1201 // .long 0x01010001
1202 // .quad 0
1203 // .quad 0
1204 //
1205 // # compact unwind for _bar
1206 // .quad _bar
1207 // .set L2,LbarEnd-_bar
1208 // .long L2
1209 // .long 0x01020011
1210 // .quad __gxx_personality
1211 // .quad except_tab1
1212
Bill Wendling49bc6802011-07-19 00:06:12 +00001213 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001214 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001215 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001216
Bill Wendlingdafd5982011-07-14 23:34:45 +00001217 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001218 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001219 Encoding |= 0x40000000;
1220
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001221 // Range Start
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001222 unsigned FDEEncoding = MOFI->getFDEEncoding(UsingCFI);
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001223 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001224 if (VerboseAsm) Streamer.AddComment("Range Start");
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001225 Streamer.EmitSymbolValue(Frame.Function, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001226
1227 // Range Length
1228 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1229 *Frame.End, 0);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001230 if (VerboseAsm) Streamer.AddComment("Range Length");
Bill Wendling75174662011-06-30 00:30:52 +00001231 Streamer.EmitAbsValue(Range, 4);
1232
Bill Wendling75174662011-06-30 00:30:52 +00001233 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001234 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
Benjamin Kramer69d57cf2011-11-07 21:00:59 +00001235 if (VerboseAsm) Streamer.AddComment("Compact Unwind Encoding: 0x" +
1236 Twine::utohexstr(Encoding));
Bill Wendling75174662011-06-30 00:30:52 +00001237 Streamer.EmitIntValue(Encoding, Size);
1238
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001239 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001240 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001241 if (VerboseAsm) Streamer.AddComment("Personality Function");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001242 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001243 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001244 else
1245 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001246
1247 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001248 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001249 if (VerboseAsm) Streamer.AddComment("LSDA");
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001250 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001251 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001252 else
1253 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001254}
1255
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001256const MCSymbol &FrameEmitterImpl::EmitCIE(MCStreamer &streamer,
1257 const MCSymbol *personality,
1258 unsigned personalityEncoding,
1259 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001260 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001261 unsigned lsdaEncoding,
1262 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001263 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001264 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001265 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001266 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001267
1268 MCSymbol *sectionStart;
Evan Cheng76792992011-07-20 05:58:47 +00001269 if (MOFI->isFunctionEHFrameSymbolPrivate() || !IsEH)
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001270 sectionStart = context.CreateTempSymbol();
1271 else
1272 sectionStart = context.GetOrCreateSymbol(Twine("EH_frame") + Twine(CIENum));
1273
Bill Wendling567a1ae2011-06-30 21:25:51 +00001274 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001275 CIENum++;
1276
Bill Wendlingb6adf462011-07-07 00:54:13 +00001277 MCSymbol *sectionEnd = context.CreateTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001278
1279 // Length
1280 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1281 *sectionEnd, 4);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001282 if (verboseAsm) streamer.AddComment("CIE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001283 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001284
1285 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001286 unsigned CIE_ID = IsEH ? 0 : -1;
Bill Wendling567a1ae2011-06-30 21:25:51 +00001287 if (verboseAsm) streamer.AddComment("CIE ID Tag");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001288 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001289
1290 // Version
Bill Wendling567a1ae2011-06-30 21:25:51 +00001291 if (verboseAsm) streamer.AddComment("DW_CIE_VERSION");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001292 streamer.EmitIntValue(dwarf::DW_CIE_VERSION, 1);
1293
1294 // Augmentation String
1295 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001296 if (IsEH) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001297 if (verboseAsm) streamer.AddComment("CIE Augmentation");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001298 Augmentation += "z";
1299 if (personality)
1300 Augmentation += "P";
1301 if (lsda)
1302 Augmentation += "L";
1303 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001304 if (IsSignalFrame)
1305 Augmentation += "S";
Eric Christophere3ab3d02013-01-09 01:57:54 +00001306 streamer.EmitBytes(Augmentation.str());
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001307 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001308 streamer.EmitIntValue(0, 1);
1309
1310 // Code Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001311 if (verboseAsm) streamer.AddComment("CIE Code Alignment Factor");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001312 streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001313
1314 // Data Alignment Factor
Bill Wendling567a1ae2011-06-30 21:25:51 +00001315 if (verboseAsm) streamer.AddComment("CIE Data Alignment Factor");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001316 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1317
1318 // Return Address Register
Bill Wendling567a1ae2011-06-30 21:25:51 +00001319 if (verboseAsm) streamer.AddComment("CIE Return Address Column");
Bill Wendlingbc07a892013-06-18 07:20:20 +00001320 streamer.EmitULEB128IntValue(MRI->getDwarfRegNum(MRI->getRARegister(), true));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001321
1322 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001323
1324 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001325 if (IsEH) {
1326 if (personality) {
1327 // Personality Encoding
1328 augmentationLength += 1;
1329 // Personality
1330 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1331 }
1332 if (lsda)
1333 augmentationLength += 1;
1334 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001335 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001336
Bill Wendling567a1ae2011-06-30 21:25:51 +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 (optional)
1341 if (personality) {
1342 // Personality Encoding
Bill Wendling567a1ae2011-06-30 21:25:51 +00001343 EmitEncodingByte(streamer, personalityEncoding,
1344 "Personality Encoding");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001345 // Personality
Bill Wendling567a1ae2011-06-30 21:25:51 +00001346 if (verboseAsm) streamer.AddComment("Personality");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001347 EmitPersonality(streamer, *personality, personalityEncoding);
1348 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001349
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001350 if (lsda)
Bill Wendling567a1ae2011-06-30 21:25:51 +00001351 EmitEncodingByte(streamer, lsdaEncoding, "LSDA Encoding");
1352
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001353 // Encoding of the FDE pointers
Evan Chengbbf3b0d2011-07-20 19:50:42 +00001354 EmitEncodingByte(streamer, MOFI->getFDEEncoding(UsingCFI),
Bill Wendling567a1ae2011-06-30 21:25:51 +00001355 "FDE Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001356 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001357
1358 // Initial Instructions
1359
Bill Wendlingbc07a892013-06-18 07:20:20 +00001360 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001361 if (!IsSimple) {
1362 const std::vector<MCCFIInstruction> &Instructions =
1363 MAI->getInitialFrameState();
1364 EmitCFIInstructions(streamer, Instructions, NULL);
1365 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001366
1367 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001368 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001369
1370 streamer.EmitLabel(sectionEnd);
1371 return *sectionStart;
1372}
1373
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001374MCSymbol *FrameEmitterImpl::EmitFDE(MCStreamer &streamer,
1375 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001376 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001377 MCContext &context = streamer.getContext();
1378 MCSymbol *fdeStart = context.CreateTempSymbol();
1379 MCSymbol *fdeEnd = context.CreateTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001380 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Bill Wendlingf166ab42011-06-30 22:02:20 +00001381 bool verboseAsm = streamer.isVerboseAsm();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001382
Evan Chengeee86452011-08-24 22:31:37 +00001383 if (IsEH && frame.Function && !MOFI->isFunctionEHFrameSymbolPrivate()) {
Bill Wendlingf166ab42011-06-30 22:02:20 +00001384 MCSymbol *EHSym =
1385 context.GetOrCreateSymbol(frame.Function->getName() + Twine(".eh"));
Rafael Espindola349c3292011-04-28 12:50:37 +00001386 streamer.EmitEHSymAttributes(frame.Function, EHSym);
Rafael Espindola96b07932011-04-28 02:46:42 +00001387 streamer.EmitLabel(EHSym);
1388 }
1389
Rafael Espindola0a017a62010-12-10 07:39:47 +00001390 // Length
1391 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001392 if (verboseAsm) streamer.AddComment("FDE Length");
Rafael Espindolaae124da2011-04-27 01:43:49 +00001393 streamer.EmitAbsValue(Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001394
1395 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001396
Rafael Espindola0a017a62010-12-10 07:39:47 +00001397 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001398 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001399 if (IsEH) {
1400 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1401 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001402 if (verboseAsm) streamer.AddComment("FDE CIE Offset");
Rafael Espindola27390b42011-05-10 15:20:23 +00001403 streamer.EmitAbsValue(offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001404 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001405 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1406 cieStart, 0);
1407 streamer.EmitAbsValue(offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001408 } else {
1409 streamer.EmitSymbolValue(&cieStart, 4);
1410 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001411
Rafael Espindola0a017a62010-12-10 07:39:47 +00001412 // PC Begin
Nick Lewycky333449c2012-08-12 08:09:45 +00001413 unsigned PCEncoding = IsEH ? MOFI->getFDEEncoding(UsingCFI)
1414 : (unsigned)dwarf::DW_EH_PE_absptr;
1415 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +00001416 EmitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH, "FDE initial location");
Rafael Espindola0a017a62010-12-10 07:39:47 +00001417
1418 // PC Range
1419 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1420 *frame.End, 0);
Bill Wendlingf166ab42011-06-30 22:02:20 +00001421 if (verboseAsm) streamer.AddComment("FDE address range");
Nick Lewycky333449c2012-08-12 08:09:45 +00001422 streamer.EmitAbsValue(Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001423
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001424 if (IsEH) {
1425 // Augmentation Data Length
1426 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001427
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001428 if (frame.Lsda)
1429 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001430
Bill Wendlingf166ab42011-06-30 22:02:20 +00001431 if (verboseAsm) streamer.AddComment("Augmentation size");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001432 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001433
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001434 // Augmentation Data
1435 if (frame.Lsda)
Iain Sandoe618def62014-01-08 10:22:54 +00001436 EmitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true,
1437 "Language Specific Data Area");
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001438 }
Rafael Espindola68067662011-04-29 03:06:29 +00001439
Rafael Espindola0a017a62010-12-10 07:39:47 +00001440 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001441 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001442
Rafael Espindola0a017a62010-12-10 07:39:47 +00001443 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001444 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001445
1446 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001447}
1448
Benjamin Kramer570dd782010-12-30 22:34:44 +00001449namespace {
1450 struct CIEKey {
David Majnemere035cf92014-01-27 17:20:25 +00001451 static const CIEKey getEmptyKey() { return CIEKey(0, 0, -1, false, false); }
1452 static const CIEKey getTombstoneKey() { return CIEKey(0, -1, 0, false, false); }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001453
Benjamin Kramer570dd782010-12-30 22:34:44 +00001454 CIEKey(const MCSymbol* Personality_, unsigned PersonalityEncoding_,
David Majnemere035cf92014-01-27 17:20:25 +00001455 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_) :
Rafael Espindola3c47e372012-01-23 21:51:52 +00001456 Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
David Majnemere035cf92014-01-27 17:20:25 +00001457 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1458 IsSimple(IsSimple_) {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001459 }
1460 const MCSymbol* Personality;
1461 unsigned PersonalityEncoding;
1462 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001463 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001464 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001465 };
1466}
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001467
1468namespace llvm {
1469 template <>
1470 struct DenseMapInfo<CIEKey> {
1471 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001472 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001473 }
1474 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001475 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001476 }
1477 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001478 return static_cast<unsigned>(hash_combine(Key.Personality,
1479 Key.PersonalityEncoding,
1480 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001481 Key.IsSignalFrame,
1482 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001483 }
1484 static bool isEqual(const CIEKey &LHS,
1485 const CIEKey &RHS) {
1486 return LHS.Personality == RHS.Personality &&
1487 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001488 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001489 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1490 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001491 }
1492 };
1493}
1494
Bill Wendling550c76d2013-09-09 19:48:37 +00001495void MCDwarfFrameEmitter::Emit(MCStreamer &Streamer, MCAsmBackend *MAB,
1496 bool UsingCFI, bool IsEH) {
1497 Streamer.generateCompactUnwindEncodings(MAB);
1498
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001499 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001500 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001501 FrameEmitterImpl Emitter(UsingCFI, IsEH);
Bill Wendling9803abb2011-09-06 18:37:11 +00001502 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001503
Bill Wendling9803abb2011-09-06 18:37:11 +00001504 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001505 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001506 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001507 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001508 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1509 const MCDwarfFrameInfo &Frame = FrameArray[i];
1510 if (Frame.CompactUnwindEncoding == 0) continue;
1511 if (!SectionEmitted) {
1512 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001513 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001514 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001515 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001516 NeedsEHFrameSection |=
1517 Frame.CompactUnwindEncoding ==
1518 MOFI->getCompactUnwindDwarfEHFrameOnly();
Bob Wilson0e180f22013-05-07 20:56:33 +00001519 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001520 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001521 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001522
Tim Northoverd1c6f512014-03-29 09:03:13 +00001523 if (!NeedsEHFrameSection) return;
1524
Bill Wendlinga800f952013-05-30 18:52:57 +00001525 const MCSection &Section =
1526 IsEH ? *const_cast<MCObjectFileInfo*>(MOFI)->getEHFrameSection() :
1527 *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001528
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001529 Streamer.SwitchSection(&Section);
1530 MCSymbol *SectionStart = Context.CreateTempSymbol();
1531 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001532 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001533
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001534 MCSymbol *FDEEnd = NULL;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001535 DenseMap<CIEKey, const MCSymbol*> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001536
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001537 const MCSymbol *DummyDebugKey = NULL;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001538 NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001539 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1540 const MCDwarfFrameInfo &Frame = FrameArray[i];
Tim Northoverd1c6f512014-03-29 09:03:13 +00001541
1542 // Emit the label from the previous iteration
1543 if (FDEEnd) {
1544 Streamer.EmitLabel(FDEEnd);
1545 FDEEnd = NULL;
1546 }
1547
1548 if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding !=
1549 MOFI->getCompactUnwindDwarfEHFrameOnly())
1550 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1551 // of by the compact unwind encoding.
1552 continue;
1553
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001554 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001555 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001556 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1557 if (!CIEStart)
1558 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1559 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001560 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001561 Frame.LsdaEncoding,
1562 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001563
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001564 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001565 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001566
Bill Wendlingbc07a892013-06-18 07:20:20 +00001567 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001568 if (FDEEnd)
1569 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001570}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001571
1572void MCDwarfFrameEmitter::EmitAdvanceLoc(MCStreamer &Streamer,
1573 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001574 MCContext &Context = Streamer.getContext();
Rafael Espindola736a35d2010-12-28 05:39:27 +00001575 SmallString<256> Tmp;
1576 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001577 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001578 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001579}
1580
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001581void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1582 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001583 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001584 // Scale the address delta by the minimum instruction length.
1585 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1586
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001587 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001588 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001589 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1590 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001591 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001592 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1593 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001594 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola563301d2010-12-29 02:30:49 +00001595 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001596 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Rafael Espindola563301d2010-12-29 02:30:49 +00001597 OS << uint8_t( AddrDelta & 0xff);
1598 OS << uint8_t((AddrDelta >> 8) & 0xff);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001599 } else {
Rafael Espindola563301d2010-12-29 02:30:49 +00001600 // FIXME: check what is the correct behavior on a big endian machine.
Rafael Espindola736a35d2010-12-28 05:39:27 +00001601 assert(isUInt<32>(AddrDelta));
1602 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Rafael Espindola563301d2010-12-29 02:30:49 +00001603 OS << uint8_t( AddrDelta & 0xff);
1604 OS << uint8_t((AddrDelta >> 8) & 0xff);
1605 OS << uint8_t((AddrDelta >> 16) & 0xff);
1606 OS << uint8_t((AddrDelta >> 24) & 0xff);
1607
Rafael Espindola736a35d2010-12-28 05:39:27 +00001608 }
1609}