blob: c97d9bb6f92e11867a5a0d33d055dbfdffed8447 [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"
Benjamin Kramer502b9e12014-04-12 16:15:53 +000012#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000013#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/Config/config.h"
Evan Cheng76792992011-07-20 05:58:47 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCExpr.h"
Evan Cheng76792992011-07-20 05:58:47 +000019#include "llvm/MC/MCObjectFileInfo.h"
Rafael Espindola4066e8d2014-05-12 14:28:48 +000020#include "llvm/MC/MCObjectStreamer.h"
Evan Cheng76792992011-07-20 05:58:47 +000021#include "llvm/MC/MCRegisterInfo.h"
Oliver Stannard8b273082014-06-19 15:52:37 +000022#include "llvm/MC/MCSection.h"
Kevin Enderbye46564a2010-09-30 16:52:03 +000023#include "llvm/MC/MCSymbol.h"
Kevin Enderbye5930f12010-07-28 20:55:35 +000024#include "llvm/Support/Debug.h"
Rafael Espindola85d91982010-12-28 18:36:23 +000025#include "llvm/Support/ErrorHandling.h"
Jim Grosbachbf387df2012-08-08 23:56:06 +000026#include "llvm/Support/LEB128.h"
Kevin Enderbye7739d42011-12-09 18:09:40 +000027#include "llvm/Support/Path.h"
28#include "llvm/Support/SourceMgr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/raw_ostream.h"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000030
Kevin Enderbye5930f12010-07-28 20:55:35 +000031using namespace llvm;
32
Ulrich Weigand32d725b2013-06-12 14:46:54 +000033static inline uint64_t ScaleAddrDelta(MCContext &Context, uint64_t AddrDelta) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000034 unsigned MinInsnLength = Context.getAsmInfo()->getMinInstAlignment();
Ulrich Weigand32d725b2013-06-12 14:46:54 +000035 if (MinInsnLength == 1)
Kevin Enderbye46564a2010-09-30 16:52:03 +000036 return AddrDelta;
Ulrich Weigand32d725b2013-06-12 14:46:54 +000037 if (AddrDelta % MinInsnLength != 0) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000038 // TODO: report this error, but really only once.
39 ;
40 }
Ulrich Weigand32d725b2013-06-12 14:46:54 +000041 return AddrDelta / MinInsnLength;
Kevin Enderbye46564a2010-09-30 16:52:03 +000042}
43
44//
45// This is called when an instruction is assembled into the specified section
46// and if there is information from the last .loc directive that has yet to have
47// a line entry made for it is made.
48//
Rafael Espindola0709a7b2015-05-21 19:20:38 +000049void MCLineEntry::Make(MCObjectStreamer *MCOS, MCSection *Section) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000050 if (!MCOS->getContext().getDwarfLocSeen())
51 return;
52
53 // Create a symbol at in the current section for use in the line entry.
Jim Grosbach6f482002015-05-18 18:43:14 +000054 MCSymbol *LineSym = MCOS->getContext().createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +000055 // Set the value of the symbol to use for the MCLineEntry.
56 MCOS->EmitLabel(LineSym);
57
58 // Get the current .loc info saved in the context.
59 const MCDwarfLoc &DwarfLoc = MCOS->getContext().getCurrentDwarfLoc();
60
61 // Create a (local) line entry with the symbol and the current .loc info.
62 MCLineEntry LineEntry(LineSym, DwarfLoc);
63
64 // clear DwarfLocSeen saying the current .loc info is now used.
Jim Grosbach6f482002015-05-18 18:43:14 +000065 MCOS->getContext().clearDwarfLocSeen();
Kevin Enderbye46564a2010-09-30 16:52:03 +000066
Kevin Enderbye46564a2010-09-30 16:52:03 +000067 // Add the line entry to this section's entries.
David Blaikiea55e64f2014-03-12 22:28:56 +000068 MCOS->getContext()
David Blaikied9012ba2014-03-13 21:59:51 +000069 .getMCDwarfLineTable(MCOS->getContext().getDwarfCompileUnitID())
David Blaikie11765bc2014-03-13 17:55:28 +000070 .getMCLineSections()
David Blaikiea55e64f2014-03-12 22:28:56 +000071 .addLineEntry(LineEntry, Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +000072}
73
74//
75// This helper routine returns an expression of End - Start + IntVal .
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000076//
Rafael Espindolad66e65d2010-12-09 23:08:35 +000077static inline const MCExpr *MakeStartMinusEndExpr(const MCStreamer &MCOS,
78 const MCSymbol &Start,
79 const MCSymbol &End,
80 int IntVal) {
Kevin Enderbye46564a2010-09-30 16:52:03 +000081 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
82 const MCExpr *Res =
Jim Grosbach13760bd2015-05-30 01:25:56 +000083 MCSymbolRefExpr::create(&End, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000084 const MCExpr *RHS =
Jim Grosbach13760bd2015-05-30 01:25:56 +000085 MCSymbolRefExpr::create(&Start, Variant, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000086 const MCExpr *Res1 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000087 MCBinaryExpr::create(MCBinaryExpr::Sub, Res, RHS, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000088 const MCExpr *Res2 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000089 MCConstantExpr::create(IntVal, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000090 const MCExpr *Res3 =
Jim Grosbach13760bd2015-05-30 01:25:56 +000091 MCBinaryExpr::create(MCBinaryExpr::Sub, Res1, Res2, MCOS.getContext());
Kevin Enderbye46564a2010-09-30 16:52:03 +000092 return Res3;
93}
94
Kevin Enderbye46564a2010-09-30 16:52:03 +000095//
96// This emits the Dwarf line table for the specified section from the entries
97// in the LineSection.
98//
David Blaikief4ad6982014-03-12 22:35:23 +000099static inline void
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000100EmitDwarfLineTable(MCObjectStreamer *MCOS, MCSection *Section,
David Blaikief4ad6982014-03-12 22:35:23 +0000101 const MCLineSection::MCLineEntryCollection &LineEntries) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000102 unsigned FileNum = 1;
103 unsigned LastLine = 1;
104 unsigned Column = 0;
105 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0;
106 unsigned Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000107 unsigned Discriminator = 0;
Craig Topperbb694de2014-04-13 04:57:38 +0000108 MCSymbol *LastLabel = nullptr;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000109
110 // Loop through each MCLineEntry and encode the dwarf line number table.
David Blaikiea55e64f2014-03-12 22:28:56 +0000111 for (auto it = LineEntries.begin(),
112 ie = LineEntries.end();
113 it != ie; ++it) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000114
115 if (FileNum != it->getFileNum()) {
116 FileNum = it->getFileNum();
117 MCOS->EmitIntValue(dwarf::DW_LNS_set_file, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000118 MCOS->EmitULEB128IntValue(FileNum);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000119 }
120 if (Column != it->getColumn()) {
121 Column = it->getColumn();
122 MCOS->EmitIntValue(dwarf::DW_LNS_set_column, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000123 MCOS->EmitULEB128IntValue(Column);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000124 }
Diego Novillo5b5cf502014-02-14 19:27:53 +0000125 if (Discriminator != it->getDiscriminator()) {
126 Discriminator = it->getDiscriminator();
Logan Chien5b776b72014-02-22 14:00:39 +0000127 unsigned Size = getULEB128Size(Discriminator);
Diego Novillo5b5cf502014-02-14 19:27:53 +0000128 MCOS->EmitIntValue(dwarf::DW_LNS_extended_op, 1);
129 MCOS->EmitULEB128IntValue(Size + 1);
130 MCOS->EmitIntValue(dwarf::DW_LNE_set_discriminator, 1);
131 MCOS->EmitULEB128IntValue(Discriminator);
132 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000133 if (Isa != it->getIsa()) {
134 Isa = it->getIsa();
135 MCOS->EmitIntValue(dwarf::DW_LNS_set_isa, 1);
Rafael Espindola5e874982010-11-02 17:22:24 +0000136 MCOS->EmitULEB128IntValue(Isa);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000137 }
138 if ((it->getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) {
139 Flags = it->getFlags();
140 MCOS->EmitIntValue(dwarf::DW_LNS_negate_stmt, 1);
141 }
142 if (it->getFlags() & DWARF2_FLAG_BASIC_BLOCK)
143 MCOS->EmitIntValue(dwarf::DW_LNS_set_basic_block, 1);
144 if (it->getFlags() & DWARF2_FLAG_PROLOGUE_END)
145 MCOS->EmitIntValue(dwarf::DW_LNS_set_prologue_end, 1);
146 if (it->getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN)
147 MCOS->EmitIntValue(dwarf::DW_LNS_set_epilogue_begin, 1);
148
Rafael Espindola1d37f352010-11-13 01:06:27 +0000149 int64_t LineDelta = static_cast<int64_t>(it->getLine()) - LastLine;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000150 MCSymbol *Label = it->getLabel();
151
152 // At this point we want to emit/create the sequence to encode the delta in
153 // line numbers and the increment of the address from the previous Label
154 // and the current Label.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000155 const MCAsmInfo *asmInfo = MCOS->getContext().getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000156 MCOS->EmitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label,
Bill Wendlingbc07a892013-06-18 07:20:20 +0000157 asmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000158
159 LastLine = it->getLine();
160 LastLabel = Label;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000161 }
162
163 // Emit a DW_LNE_end_sequence for the end of the section.
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000164 // Use the section end label to compute the address delta and use INT64_MAX
165 // as the line delta which is the signal that this is actually a
166 // DW_LNE_end_sequence.
167 MCSymbol *SectionEnd = MCOS->endSection(Section);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000168
Rafael Espindolaf2b408c2015-03-23 21:22:04 +0000169 // Switch back the dwarf line section, in case endSection had to switch the
170 // section.
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000171 MCContext &Ctx = MCOS->getContext();
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000172 MCOS->SwitchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
Rafael Espindolab58867c2010-11-19 02:26:16 +0000173
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000174 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
Evan Chengc7ac6902011-07-14 05:43:07 +0000175 MCOS->EmitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
Rafael Espindolaae3d78a2015-03-23 20:25:31 +0000176 AsmInfo->getPointerSize());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000177}
178
179//
180// This emits the Dwarf file and the line tables.
181//
Frederic Rissa5ab8442015-08-07 15:14:08 +0000182void MCDwarfLineTable::Emit(MCObjectStreamer *MCOS,
183 MCDwarfLineTableParams Params) {
Rafael Espindola0a017a62010-12-10 07:39:47 +0000184 MCContext &context = MCOS->getContext();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000185
David Blaikie8bf66c42014-04-01 07:35:52 +0000186 auto &LineTables = context.getMCDwarfLineTables();
187
188 // Bail out early so we don't switch to the debug_line section needlessly and
189 // in doing so create an unnecessary (if empty) section.
190 if (LineTables.empty())
191 return;
David Blaikie11765bc2014-03-13 17:55:28 +0000192
193 // Switch to the section where the table will be emitted into.
194 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfLineSection());
195
Manman Ren4e042a62013-02-05 21:52:47 +0000196 // Handle the rest of the Compile Units.
David Blaikie8bf66c42014-04-01 07:35:52 +0000197 for (const auto &CUIDTablePair : LineTables)
Frederic Rissa5ab8442015-08-07 15:14:08 +0000198 CUIDTablePair.second.EmitCU(MCOS, Params);
Manman Ren4e042a62013-02-05 21:52:47 +0000199}
200
Frederic Rissa5ab8442015-08-07 15:14:08 +0000201void MCDwarfDwoLineTable::Emit(MCStreamer &MCOS,
202 MCDwarfLineTableParams Params) const {
203 MCOS.EmitLabel(Header.Emit(&MCOS, Params, None).second);
David Blaikie8287aff2014-03-18 02:13:23 +0000204}
205
Frederic Rissa5ab8442015-08-07 15:14:08 +0000206std::pair<MCSymbol *, MCSymbol *>
207MCDwarfLineTableHeader::Emit(MCStreamer *MCOS,
208 MCDwarfLineTableParams Params) const {
David Blaikie8287aff2014-03-18 02:13:23 +0000209 static const char StandardOpcodeLengths[] = {
210 0, // length of DW_LNS_copy
211 1, // length of DW_LNS_advance_pc
212 1, // length of DW_LNS_advance_line
213 1, // length of DW_LNS_set_file
214 1, // length of DW_LNS_set_column
215 0, // length of DW_LNS_negate_stmt
216 0, // length of DW_LNS_set_basic_block
217 0, // length of DW_LNS_const_add_pc
218 1, // length of DW_LNS_fixed_advance_pc
219 0, // length of DW_LNS_set_prologue_end
220 0, // length of DW_LNS_set_epilogue_begin
221 1 // DW_LNS_set_isa
222 };
Frederic Rissa5ab8442015-08-07 15:14:08 +0000223 assert(array_lengthof(StandardOpcodeLengths) >=
Aaron Ballmand8ac7de2015-08-10 15:22:39 +0000224 (Params.DWARF2LineOpcodeBase - 1U));
Craig Topper0013be12015-09-21 05:32:41 +0000225 return Emit(MCOS, Params, makeArrayRef(StandardOpcodeLengths,
226 Params.DWARF2LineOpcodeBase - 1));
David Blaikie8287aff2014-03-18 02:13:23 +0000227}
228
Rafael Espindolac23174b2014-08-15 15:12:13 +0000229static const MCExpr *forceExpAbs(MCStreamer &OS, const MCExpr* Expr) {
230 MCContext &Context = OS.getContext();
231 assert(!isa<MCSymbolRefExpr>(Expr));
232 if (Context.getAsmInfo()->hasAggressiveSymbolFolding())
233 return Expr;
234
Jim Grosbach6f482002015-05-18 18:43:14 +0000235 MCSymbol *ABS = Context.createTempSymbol();
Rafael Espindolac23174b2014-08-15 15:12:13 +0000236 OS.EmitAssignment(ABS, Expr);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000237 return MCSymbolRefExpr::create(ABS, Context);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000238}
239
240static void emitAbsValue(MCStreamer &OS, const MCExpr *Value, unsigned Size) {
241 const MCExpr *ABS = forceExpAbs(OS, Value);
242 OS.EmitValue(ABS, Size);
243}
244
David Blaikie8287aff2014-03-18 02:13:23 +0000245std::pair<MCSymbol *, MCSymbol *>
Frederic Rissa5ab8442015-08-07 15:14:08 +0000246MCDwarfLineTableHeader::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
David Blaikie8287aff2014-03-18 02:13:23 +0000247 ArrayRef<char> StandardOpcodeLengths) const {
Manman Ren4e042a62013-02-05 21:52:47 +0000248 MCContext &context = MCOS->getContext();
249
250 // Create a symbol at the beginning of the line table.
David Blaikie11765bc2014-03-13 17:55:28 +0000251 MCSymbol *LineStartSym = Label;
Manman Ren4e042a62013-02-05 21:52:47 +0000252 if (!LineStartSym)
Jim Grosbach6f482002015-05-18 18:43:14 +0000253 LineStartSym = context.createTempSymbol();
Manman Ren4e042a62013-02-05 21:52:47 +0000254 // Set the value of the symbol, as we are at the start of the line table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000255 MCOS->EmitLabel(LineStartSym);
256
257 // Create a symbol for the end of the section (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000258 MCSymbol *LineEndSym = context.createTempSymbol();
Kevin Enderbye46564a2010-09-30 16:52:03 +0000259
260 // The first 4 bytes is the total length of the information for this
261 // compilation unit (not including these 4 bytes for the length).
Rafael Espindolac23174b2014-08-15 15:12:13 +0000262 emitAbsValue(*MCOS,
263 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *LineEndSym, 4), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000264
265 // Next 2 bytes is the Version, which is Dwarf 2.
266 MCOS->EmitIntValue(2, 2);
267
268 // Create a symbol for the end of the prologue (to be set when we get there).
Jim Grosbach6f482002015-05-18 18:43:14 +0000269 MCSymbol *ProEndSym = context.createTempSymbol(); // Lprologue_end
Kevin Enderbye46564a2010-09-30 16:52:03 +0000270
271 // Length of the prologue, is the next 4 bytes. Which is the start of the
272 // section to the end of the prologue. Not including the 4 bytes for the
273 // total length, the 2 bytes for the version, and these 4 bytes for the
274 // length of the prologue.
Rafael Espindolac23174b2014-08-15 15:12:13 +0000275 emitAbsValue(
276 *MCOS,
277 MakeStartMinusEndExpr(*MCOS, *LineStartSym, *ProEndSym, (4 + 2 + 4)), 4);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000278
279 // Parameters of the state machine, are next.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000280 MCOS->EmitIntValue(context.getAsmInfo()->getMinInstAlignment(), 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000281 MCOS->EmitIntValue(DWARF2_LINE_DEFAULT_IS_STMT, 1);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000282 MCOS->EmitIntValue(Params.DWARF2LineBase, 1);
283 MCOS->EmitIntValue(Params.DWARF2LineRange, 1);
David Blaikie8287aff2014-03-18 02:13:23 +0000284 MCOS->EmitIntValue(StandardOpcodeLengths.size() + 1, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000285
286 // Standard opcode lengths
David Blaikie8287aff2014-03-18 02:13:23 +0000287 for (char Length : StandardOpcodeLengths)
288 MCOS->EmitIntValue(Length, 1);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000289
290 // Put out the directory and file tables.
291
292 // First the directory table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000293 for (unsigned i = 0; i < MCDwarfDirs.size(); i++) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000294 MCOS->EmitBytes(MCDwarfDirs[i]); // the DirectoryName
295 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Kevin Enderbye46564a2010-09-30 16:52:03 +0000296 }
297 MCOS->EmitIntValue(0, 1); // Terminate the directory list
298
299 // Second the file table.
Kevin Enderbye46564a2010-09-30 16:52:03 +0000300 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) {
David Blaikiec2df16b2014-03-17 18:13:58 +0000301 assert(!MCDwarfFiles[i].Name.empty());
David Blaikiea55ddad2014-03-13 18:55:04 +0000302 MCOS->EmitBytes(MCDwarfFiles[i].Name); // FileName
Eric Christophere3ab3d02013-01-09 01:57:54 +0000303 MCOS->EmitBytes(StringRef("\0", 1)); // the null term. of the string
Rafael Espindola5e874982010-11-02 17:22:24 +0000304 // the Directory num
David Blaikiea55ddad2014-03-13 18:55:04 +0000305 MCOS->EmitULEB128IntValue(MCDwarfFiles[i].DirIndex);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000306 MCOS->EmitIntValue(0, 1); // last modification timestamp (always 0)
307 MCOS->EmitIntValue(0, 1); // filesize (always 0)
308 }
309 MCOS->EmitIntValue(0, 1); // Terminate the file list
310
311 // This is the end of the prologue, so set the value of the symbol at the
312 // end of the prologue (that was used in a previous expression).
313 MCOS->EmitLabel(ProEndSym);
314
David Blaikie5cff0e62014-03-13 21:47:12 +0000315 return std::make_pair(LineStartSym, LineEndSym);
316}
317
Frederic Rissa5ab8442015-08-07 15:14:08 +0000318void MCDwarfLineTable::EmitCU(MCObjectStreamer *MCOS,
319 MCDwarfLineTableParams Params) const {
320 MCSymbol *LineEndSym = Header.Emit(MCOS, Params).second;
David Blaikie5cff0e62014-03-13 21:47:12 +0000321
Kevin Enderbye46564a2010-09-30 16:52:03 +0000322 // Put out the line tables.
David Blaikie11765bc2014-03-13 17:55:28 +0000323 for (const auto &LineSec : MCLineSections.getMCLineEntries())
324 EmitDwarfLineTable(MCOS, LineSec.first, LineSec.second);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000325
Kevin Enderbye46564a2010-09-30 16:52:03 +0000326 // This is the end of the section, so set the value of the symbol at the end
327 // of this section (that was used in a previous expression).
328 MCOS->EmitLabel(LineEndSym);
329}
330
David Blaikiec2df16b2014-03-17 18:13:58 +0000331unsigned MCDwarfLineTable::getFile(StringRef &Directory, StringRef &FileName,
David Blaikied9012ba2014-03-13 21:59:51 +0000332 unsigned FileNumber) {
David Blaikie5cff0e62014-03-13 21:47:12 +0000333 return Header.getFile(Directory, FileName, FileNumber);
334}
335
David Blaikiec2df16b2014-03-17 18:13:58 +0000336unsigned MCDwarfLineTableHeader::getFile(StringRef &Directory,
337 StringRef &FileName,
David Blaikiec714ef42014-03-17 01:52:11 +0000338 unsigned FileNumber) {
David Blaikiec7f29dc2014-03-17 23:29:40 +0000339 if (Directory == CompilationDir)
340 Directory = "";
David Blaikiec2df16b2014-03-17 18:13:58 +0000341 if (FileName.empty()) {
342 FileName = "<stdin>";
343 Directory = "";
344 }
345 assert(!FileName.empty());
David Blaikiec714ef42014-03-17 01:52:11 +0000346 if (FileNumber == 0) {
347 FileNumber = SourceIdMap.size() + 1;
348 assert((MCDwarfFiles.empty() || FileNumber == MCDwarfFiles.size()) &&
349 "Don't mix autonumbered and explicit numbered line table usage");
Pete Cooperb7800812015-05-20 19:12:14 +0000350 SmallString<256> Buffer;
David Blaikie5106ce72014-11-19 05:49:42 +0000351 auto IterBool = SourceIdMap.insert(
Pete Cooperb7800812015-05-20 19:12:14 +0000352 std::make_pair((Directory + Twine('\0') + FileName).toStringRef(Buffer),
353 FileNumber));
David Blaikie5106ce72014-11-19 05:49:42 +0000354 if (!IterBool.second)
355 return IterBool.first->second;
David Blaikiec714ef42014-03-17 01:52:11 +0000356 }
David Blaikie498589c2014-03-13 19:15:04 +0000357 // Make space for this FileNumber in the MCDwarfFiles vector if needed.
358 MCDwarfFiles.resize(FileNumber + 1);
359
360 // Get the new MCDwarfFile slot for this FileNumber.
361 MCDwarfFile &File = MCDwarfFiles[FileNumber];
362
363 // It is an error to use see the same number more than once.
364 if (!File.Name.empty())
365 return 0;
366
367 if (Directory.empty()) {
368 // Separate the directory part from the basename of the FileName.
369 StringRef tFileName = sys::path::filename(FileName);
370 if (!tFileName.empty()) {
371 Directory = sys::path::parent_path(FileName);
372 if (!Directory.empty())
373 FileName = tFileName;
374 }
375 }
376
377 // Find or make an entry in the MCDwarfDirs vector for this Directory.
378 // Capture directory name.
379 unsigned DirIndex;
380 if (Directory.empty()) {
381 // For FileNames with no directories a DirIndex of 0 is used.
382 DirIndex = 0;
383 } else {
384 DirIndex = 0;
385 for (unsigned End = MCDwarfDirs.size(); DirIndex < End; DirIndex++) {
386 if (Directory == MCDwarfDirs[DirIndex])
387 break;
388 }
389 if (DirIndex >= MCDwarfDirs.size())
390 MCDwarfDirs.push_back(Directory);
391 // The DirIndex is one based, as DirIndex of 0 is used for FileNames with
392 // no directories. MCDwarfDirs[] is unlike MCDwarfFiles[] in that the
393 // directory names are stored at MCDwarfDirs[DirIndex-1] where FileNames
394 // are stored at MCDwarfFiles[FileNumber].Name .
395 DirIndex++;
396 }
397
398 File.Name = FileName;
399 File.DirIndex = DirIndex;
400
401 // return the allocated FileNumber.
402 return FileNumber;
403}
404
Kevin Enderbye46564a2010-09-30 16:52:03 +0000405/// Utility function to emit the encoding to a streamer.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000406void MCDwarfLineAddr::Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params,
407 int64_t LineDelta, uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000408 MCContext &Context = MCOS->getContext();
Alp Tokere69170a2014-06-26 22:52:05 +0000409 SmallString<256> Tmp;
410 raw_svector_ostream OS(Tmp);
Frederic Rissa5ab8442015-08-07 15:14:08 +0000411 MCDwarfLineAddr::Encode(Context, Params, LineDelta, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +0000412 MCOS->EmitBytes(OS.str());
Kevin Enderbye46564a2010-09-30 16:52:03 +0000413}
414
Frederic Rissa5ab8442015-08-07 15:14:08 +0000415/// Given a special op, return the address skip amount (in units of
416/// DWARF2_LINE_MIN_INSN_LENGTH).
417static uint64_t SpecialAddr(MCDwarfLineTableParams Params, uint64_t op) {
418 return (op - Params.DWARF2LineOpcodeBase) / Params.DWARF2LineRange;
419}
420
Kevin Enderbye46564a2010-09-30 16:52:03 +0000421/// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000422void MCDwarfLineAddr::Encode(MCContext &Context, MCDwarfLineTableParams Params,
423 int64_t LineDelta, uint64_t AddrDelta,
424 raw_ostream &OS) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000425 uint64_t Temp, Opcode;
426 bool NeedCopy = false;
427
Frederic Rissa5ab8442015-08-07 15:14:08 +0000428 // The maximum address skip amount that can be encoded with a special op.
429 uint64_t MaxSpecialAddrDelta = SpecialAddr(Params, 255);
430
Kevin Enderbye46564a2010-09-30 16:52:03 +0000431 // Scale the address delta by the minimum instruction length.
Ulrich Weigand32d725b2013-06-12 14:46:54 +0000432 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000433
434 // A LineDelta of INT64_MAX is a signal that this is actually a
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000435 // DW_LNE_end_sequence. We cannot use special opcodes here, since we want the
Kevin Enderbye46564a2010-09-30 16:52:03 +0000436 // end_sequence to emit the matrix entry.
437 if (LineDelta == INT64_MAX) {
Frederic Rissa5ab8442015-08-07 15:14:08 +0000438 if (AddrDelta == MaxSpecialAddrDelta)
Kevin Enderbye46564a2010-09-30 16:52:03 +0000439 OS << char(dwarf::DW_LNS_const_add_pc);
Frederic Rissceb18362015-03-15 20:45:39 +0000440 else if (AddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000441 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000442 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000443 }
444 OS << char(dwarf::DW_LNS_extended_op);
445 OS << char(1);
446 OS << char(dwarf::DW_LNE_end_sequence);
447 return;
448 }
449
450 // Bias the line delta by the base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000451 Temp = LineDelta - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000452
453 // If the line increment is out of range of a special opcode, we must encode
454 // it with DW_LNS_advance_line.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000455 if (Temp >= Params.DWARF2LineRange) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000456 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000457 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000458
459 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000460 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000461 NeedCopy = true;
462 }
463
464 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
465 if (LineDelta == 0 && AddrDelta == 0) {
466 OS << char(dwarf::DW_LNS_copy);
467 return;
468 }
469
470 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000471 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000472
473 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000474 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000475 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000476 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000477 if (Opcode <= 255) {
478 OS << char(Opcode);
479 return;
480 }
481
482 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000483 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000484 if (Opcode <= 255) {
485 OS << char(dwarf::DW_LNS_const_add_pc);
486 OS << char(Opcode);
487 return;
488 }
489 }
490
491 // Otherwise use DW_LNS_advance_pc.
492 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000493 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000494
495 if (NeedCopy)
496 OS << char(dwarf::DW_LNS_copy);
497 else
498 OS << char(Temp);
499}
500
Kevin Enderbye7739d42011-12-09 18:09:40 +0000501// Utility function to write a tuple for .debug_abbrev.
502static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
503 MCOS->EmitULEB128IntValue(Name);
504 MCOS->EmitULEB128IntValue(Form);
505}
506
507// When generating dwarf for assembly source files this emits
508// the data for .debug_abbrev section which contains three DIEs.
509static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
510 MCContext &context = MCOS->getContext();
511 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
512
513 // DW_TAG_compile_unit DIE abbrev (1).
514 MCOS->EmitULEB128IntValue(1);
515 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
516 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinson22d0d312015-12-23 01:57:31 +0000517 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list,
518 context.getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
519 : dwarf::DW_FORM_data4);
520 if (context.getGenDwarfSectionSyms().size() > 1 &&
521 context.getDwarfVersion() >= 3) {
522 EmitAbbrev(MCOS, dwarf::DW_AT_ranges,
523 context.getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
524 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000525 } else {
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 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000529 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000530 if (!context.getCompilationDir().empty())
531 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000532 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
533 if (!DwarfDebugFlags.empty())
534 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
535 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
536 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
537 EmitAbbrev(MCOS, 0, 0);
538
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000539 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000540 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000541 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000542 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
543 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
544 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
545 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
546 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000547 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
548 EmitAbbrev(MCOS, 0, 0);
549
550 // DW_TAG_unspecified_parameters DIE abbrev (3).
551 MCOS->EmitULEB128IntValue(3);
552 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
553 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
554 EmitAbbrev(MCOS, 0, 0);
555
556 // Terminate the abbreviations for this compilation unit.
557 MCOS->EmitIntValue(0, 1);
558}
559
560// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000561// .debug_aranges section. This section contains a header and a table of pairs
562// of PointerSize'ed values for the address and size of section(s) with line
563// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000564static void EmitGenDwarfAranges(MCStreamer *MCOS,
565 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000566 MCContext &context = MCOS->getContext();
567
Oliver Stannard8b273082014-06-19 15:52:37 +0000568 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000569
570 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
571
572 // This will be the length of the .debug_aranges section, first account for
573 // the size of each item in the header (see below where we emit these items).
574 int Length = 4 + 2 + 4 + 1 + 1;
575
576 // Figure the padding after the header before the table of address and size
577 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000578 const MCAsmInfo *asmInfo = context.getAsmInfo();
579 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000580 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
581 if (Pad == 2 * AddrSize)
582 Pad = 0;
583 Length += Pad;
584
585 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000586 // of each section we have in the table.
587 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000588 // And the pair of terminating zeros.
589 Length += 2 * AddrSize;
590
591
592 // Emit the header for this section.
593 // The 4 byte length not including the 4 byte value for the length.
594 MCOS->EmitIntValue(Length - 4, 4);
595 // The 2 byte version, which is 2.
596 MCOS->EmitIntValue(2, 2);
597 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000598 // of the .debug_info.
599 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000600 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
601 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000602 else
603 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000604 // The 1 byte size of an address.
605 MCOS->EmitIntValue(AddrSize, 1);
606 // The 1 byte size of a segment descriptor, we use a value of zero.
607 MCOS->EmitIntValue(0, 1);
608 // Align the header with the padding if needed, before we put out the table.
609 for(int i = 0; i < Pad; i++)
610 MCOS->EmitIntValue(0, 1);
611
Oliver Stannard8b273082014-06-19 15:52:37 +0000612 // Now emit the table of pairs of PointerSize'ed values for the section
613 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000614 for (MCSection *Sec : Sections) {
615 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000616 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000617 assert(StartSymbol && "StartSymbol must not be NULL");
618 assert(EndSymbol && "EndSymbol must not be NULL");
619
Jim Grosbach13760bd2015-05-30 01:25:56 +0000620 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000621 StartSymbol, MCSymbolRefExpr::VK_None, context);
622 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
623 *StartSymbol, *EndSymbol, 0);
624 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000625 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000626 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000627
628 // And finally the pair of terminating zeros.
629 MCOS->EmitIntValue(0, AddrSize);
630 MCOS->EmitIntValue(0, AddrSize);
631}
632
633// When generating dwarf for assembly source files this emits the data for
634// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000635// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000636static void EmitGenDwarfInfo(MCStreamer *MCOS,
637 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000638 const MCSymbol *LineSectionSymbol,
639 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000640 MCContext &context = MCOS->getContext();
641
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000642 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000643
644 // Create a symbol at the start and end of this section used in here for the
645 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000646 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000647 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000648 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000649
650 // First part: the header.
651
652 // The 4 byte total length of the information for this compilation unit, not
653 // including these 4 bytes.
654 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000655 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000656
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000657 // The 2 byte DWARF version.
658 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000659
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000660 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000661 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
662 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000663 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000664 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000665 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000666 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
667 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000668
Bill Wendlingbc07a892013-06-18 07:20:20 +0000669 const MCAsmInfo *asmInfo = context.getAsmInfo();
670 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000671 // The 1 byte size of an address.
672 MCOS->EmitIntValue(AddrSize, 1);
673
674 // Second part: the compile_unit DIE.
675
676 // The DW_TAG_compile_unit DIE abbrev (1).
677 MCOS->EmitULEB128IntValue(1);
678
679 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
680 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000681 if (LineSectionSymbol)
682 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
683 AsmInfo.needsDwarfSectionOffsetDirective());
684 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000685 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686
Oliver Stannard8b273082014-06-19 15:52:37 +0000687 if (RangesSectionSymbol) {
688 // There are multiple sections containing code, so we must use the
689 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000690
Oliver Stannard8b273082014-06-19 15:52:37 +0000691 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
692 // to the address range list for this compilation unit.
693 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
694 } else {
695 // If we only have one non-empty code section, we can use the simpler
696 // AT_low_pc and AT_high_pc attributes.
697
698 // Find the first (and only) non-empty text section
699 auto &Sections = context.getGenDwarfSectionSyms();
700 const auto TextSection = Sections.begin();
701 assert(TextSection != Sections.end() && "No text section found");
702
Rafael Espindolae0746792015-05-21 16:52:32 +0000703 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
704 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000705 assert(StartSymbol && "StartSymbol must not be NULL");
706 assert(EndSymbol && "EndSymbol must not be NULL");
707
708 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000709 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000710 StartSymbol, MCSymbolRefExpr::VK_None, context);
711 MCOS->EmitValue(Start, AddrSize);
712
713 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000714 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000715 EndSymbol, MCSymbolRefExpr::VK_None, context);
716 MCOS->EmitValue(End, AddrSize);
717 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000718
719 // AT_name, the name of the source file. Reconstruct from the first directory
720 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000721 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000722 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000723 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000724 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000725 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000726 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000727 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000728 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000729 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
730
731 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000732 if (!context.getCompilationDir().empty()) {
733 MCOS->EmitBytes(context.getCompilationDir());
734 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
735 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000736
737 // AT_APPLE_flags, the command line arguments of the assembler tool.
738 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
739 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000740 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000741 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
742 }
743
744 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000745 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000746 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000747 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000748 else
749 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000750 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
751
752 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
753 // draft has no standard code for assembler.
754 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
755
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000756 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000757
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000758 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000759 const std::vector<MCGenDwarfLabelEntry> &Entries =
760 MCOS->getContext().getMCGenDwarfLabelEntries();
761 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000762 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000763 MCOS->EmitULEB128IntValue(2);
764
765 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000766 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000767 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
768
769 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000770 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000771
772 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000773 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000774
775 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000776 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000778 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000779
Kevin Enderbye7739d42011-12-09 18:09:40 +0000780 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
781 MCOS->EmitIntValue(0, 1);
782
783 // The DW_TAG_unspecified_parameters DIE abbrev (3).
784 MCOS->EmitULEB128IntValue(3);
785
786 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
787 MCOS->EmitIntValue(0, 1);
788 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000789
790 // Add the NULL DIE terminating the Compile Unit DIE's.
791 MCOS->EmitIntValue(0, 1);
792
793 // Now set the value of the symbol at the end of the info section.
794 MCOS->EmitLabel(InfoEnd);
795}
796
Oliver Stannard8b273082014-06-19 15:52:37 +0000797// When generating dwarf for assembly source files this emits the data for
798// .debug_ranges section. We only emit one range list, which spans all of the
799// executable sections of this file.
800static void EmitGenDwarfRanges(MCStreamer *MCOS) {
801 MCContext &context = MCOS->getContext();
802 auto &Sections = context.getGenDwarfSectionSyms();
803
804 const MCAsmInfo *AsmInfo = context.getAsmInfo();
805 int AddrSize = AsmInfo->getPointerSize();
806
807 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
808
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000809 for (MCSection *Sec : Sections) {
810 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000811 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000812 assert(StartSymbol && "StartSymbol must not be NULL");
813 assert(EndSymbol && "EndSymbol must not be NULL");
814
815 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000816 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000817 StartSymbol, MCSymbolRefExpr::VK_None, context);
818 MCOS->EmitFill(AddrSize, 0xFF);
819 MCOS->EmitValue(SectionStartAddr, AddrSize);
820
821 // Emit a range list entry spanning this section
822 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
823 *StartSymbol, *EndSymbol, 0);
824 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000825 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000826 }
827
828 // Emit end of list entry
829 MCOS->EmitIntValue(0, AddrSize);
830 MCOS->EmitIntValue(0, AddrSize);
831}
832
Kevin Enderbye7739d42011-12-09 18:09:40 +0000833//
834// When generating dwarf for assembly source files this emits the Dwarf
835// sections.
836//
David Blaikie8bf66c42014-04-01 07:35:52 +0000837void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000838 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000839
840 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000841 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000842 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000843 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000844 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000845 if (CreateDwarfSectionSymbols)
846 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000847 MCSymbol *AbbrevSectionSymbol = nullptr;
848 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000849 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000850
851 // Create end symbols for each section, and remove empty sections
852 MCOS->getContext().finalizeDwarfSections(*MCOS);
853
854 // If there are no sections to generate debug info for, we don't need
855 // to do anything
856 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
857 return;
858
Oliver Stannard14f97d02014-09-22 10:45:16 +0000859 // We only use the .debug_ranges section if we have multiple code sections,
860 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000861 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000862 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
863 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000864 CreateDwarfSectionSymbols |= UseRangesSection;
865
Kevin Enderbye7739d42011-12-09 18:09:40 +0000866 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000867 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000868 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000869 MCOS->EmitLabel(InfoSectionSymbol);
870 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000871 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000872 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000873 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000874 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000875 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000876 if (UseRangesSection) {
877 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
878 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000879 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000880 MCOS->EmitLabel(RangesSectionSymbol);
881 }
882 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000883
Oliver Stannard8b273082014-06-19 15:52:37 +0000884 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
885
886 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000887
888 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000889 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000890
Oliver Stannard8b273082014-06-19 15:52:37 +0000891 if (UseRangesSection)
892 EmitGenDwarfRanges(MCOS);
893
Kevin Enderbye7739d42011-12-09 18:09:40 +0000894 // Output the data for .debug_abbrev section.
895 EmitGenDwarfAbbrev(MCOS);
896
897 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000898 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
899 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000900}
901
902//
903// When generating dwarf for assembly source files this is called when symbol
904// for a label is created. If this symbol is not a temporary and is in the
905// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000906// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000907//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000908void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000909 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000910 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000911 if (Symbol->isTemporary())
912 return;
913 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000914 // We won't create dwarf labels for symbols in sections that we are not
915 // generating debug info for.
916 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000917 return;
918
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000919 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000920 // underbar if any.
921 StringRef Name = Symbol->getName();
922 if (Name.startswith("_"))
923 Name = Name.substr(1, Name.size()-1);
924
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000925 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000926 unsigned FileNumber = context.getGenDwarfFileNumber();
927
928 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000929 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000930 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000931 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
932
933 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
934 // values so that they don't have things like an ARM thumb bit from the
935 // original symbol. So when used they won't get a low bit set after
936 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000937 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000938 MCOS->EmitLabel(Label);
939
940 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000941 MCOS->getContext().addMCGenDwarfLabelEntry(
942 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000943}
944
Rafael Espindola0a017a62010-12-10 07:39:47 +0000945static int getDataAlignmentFactor(MCStreamer &streamer) {
946 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000947 const MCAsmInfo *asmInfo = context.getAsmInfo();
948 int size = asmInfo->getCalleeSaveStackSlotSize();
949 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000950 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000951 else
952 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000953}
954
Rafael Espindola5395f442011-04-22 00:08:43 +0000955static unsigned getSizeForEncoding(MCStreamer &streamer,
956 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000957 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000958 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000959 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000960 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000961 case dwarf::DW_EH_PE_absptr:
962 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000963 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000964 case dwarf::DW_EH_PE_udata2:
965 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000966 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000967 case dwarf::DW_EH_PE_udata4:
968 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000969 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000970 case dwarf::DW_EH_PE_udata8:
971 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000972 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000973 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000974}
975
Rafael Espindolafe963b12014-08-15 03:07:13 +0000976static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
977 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000978 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000979 const MCAsmInfo *asmInfo = context.getAsmInfo();
980 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
981 symbolEncoding,
982 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000983 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000984 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000985 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000986 else
987 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000988}
989
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000990static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
991 unsigned symbolEncoding) {
992 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000993 const MCAsmInfo *asmInfo = context.getAsmInfo();
994 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
995 symbolEncoding,
996 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000997 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000998 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000999}
1000
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001001namespace {
Rafael Espindola68c21652015-11-05 23:54:18 +00001002class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001003 int CFAOffset = 0;
1004 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001005 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001006 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001007
Rafael Espindola68c21652015-11-05 23:54:18 +00001008public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001009 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1010 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001011
Rafael Espindola68c21652015-11-05 23:54:18 +00001012 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001013 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001014
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001015 const MCSymbol &EmitCIE(const MCSymbol *personality,
Rafael Espindola68c21652015-11-05 23:54:18 +00001016 unsigned personalityEncoding, const MCSymbol *lsda,
1017 bool IsSignalFrame, unsigned lsdaEncoding,
1018 bool IsSimple);
Rafael Espindola46be4352015-11-06 03:02:51 +00001019 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001020 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001021 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001022 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001023 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001024};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001025
1026} // end anonymous namespace
1027
Rafael Espindolafe963b12014-08-15 03:07:13 +00001028static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001029 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001030}
1031
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001032void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001033 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001034 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001035
1036 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001037 case MCCFIInstruction::OpRegister: {
1038 unsigned Reg1 = Instr.getRegister();
1039 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001040 if (!IsEH) {
1041 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1042 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1043 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001044 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1045 Streamer.EmitULEB128IntValue(Reg1);
1046 Streamer.EmitULEB128IntValue(Reg2);
1047 return;
1048 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001049 case MCCFIInstruction::OpWindowSave: {
1050 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1051 return;
1052 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001053 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001054 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001055 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1056 Streamer.EmitULEB128IntValue(Reg);
1057 return;
1058 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001059 case MCCFIInstruction::OpAdjustCfaOffset:
1060 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001061 const bool IsRelative =
1062 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1063
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001064 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1065
1066 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001067 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001068 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001069 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001070
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001071 Streamer.EmitULEB128IntValue(CFAOffset);
1072
1073 return;
1074 }
1075 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001076 unsigned Reg = Instr.getRegister();
1077 if (!IsEH)
1078 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001079 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001080 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001081 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001082 Streamer.EmitULEB128IntValue(CFAOffset);
1083
1084 return;
1085 }
1086
1087 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001088 unsigned Reg = Instr.getRegister();
1089 if (!IsEH)
1090 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001091 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001092 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001093
1094 return;
1095 }
1096
1097 case MCCFIInstruction::OpOffset:
1098 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001099 const bool IsRelative =
1100 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001101
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001102 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001103 if (!IsEH)
1104 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1105
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001106 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001107 if (IsRelative)
1108 Offset -= CFAOffset;
1109 Offset = Offset / dataAlignmentFactor;
1110
1111 if (Offset < 0) {
1112 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1113 Streamer.EmitULEB128IntValue(Reg);
1114 Streamer.EmitSLEB128IntValue(Offset);
1115 } else if (Reg < 64) {
1116 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001117 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001118 } else {
1119 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001120 Streamer.EmitULEB128IntValue(Reg);
1121 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001122 }
1123 return;
1124 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001125 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001126 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1127 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001128 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001129 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1130 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001131 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001132 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001133 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001134 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001135 return;
1136 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001137 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001138 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001139 if (!IsEH)
1140 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001141 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1142 return;
1143 }
Michael Kuperstein259f1502015-10-07 07:01:31 +00001144 case MCCFIInstruction::OpGnuArgsSize: {
1145 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1146 Streamer.EmitULEB128IntValue(Instr.getOffset());
1147 return;
1148 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001149 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001150 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001151 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001152 }
1153 llvm_unreachable("Unhandled case in switch");
1154}
1155
Rafael Espindolafe963b12014-08-15 03:07:13 +00001156/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001157void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001158 MCSymbol *BaseLabel) {
1159 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1160 const MCCFIInstruction &Instr = Instrs[i];
1161 MCSymbol *Label = Instr.getLabel();
1162 // Throw out move if the label is invalid.
1163 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1164
1165 // Advance row if new location.
1166 if (BaseLabel && Label) {
1167 MCSymbol *ThisSym = Label;
1168 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001169 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001170 BaseLabel = ThisSym;
1171 }
1172 }
1173
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001174 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001175 }
1176}
1177
Rafael Espindolafe963b12014-08-15 03:07:13 +00001178/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001179void FrameEmitterImpl::EmitCompactUnwind(const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001180 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001181 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001182
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001183 // range-start range-length compact-unwind-enc personality-func lsda
1184 // _foo LfooEnd-_foo 0x00000023 0 0
1185 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1186 //
1187 // .section __LD,__compact_unwind,regular,debug
1188 //
1189 // # compact unwind for _foo
1190 // .quad _foo
1191 // .set L1,LfooEnd-_foo
1192 // .long L1
1193 // .long 0x01010001
1194 // .quad 0
1195 // .quad 0
1196 //
1197 // # compact unwind for _bar
1198 // .quad _bar
1199 // .set L2,LbarEnd-_bar
1200 // .long L2
1201 // .long 0x01020011
1202 // .quad __gxx_personality
1203 // .quad except_tab1
1204
Bill Wendling49bc6802011-07-19 00:06:12 +00001205 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001206 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001207 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001208
Bill Wendlingdafd5982011-07-14 23:34:45 +00001209 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001210 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001211 Encoding |= 0x40000000;
1212
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001213 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001214 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001215 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001216 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001217
1218 // Range Length
1219 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1220 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001221 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001222
Bill Wendling75174662011-06-30 00:30:52 +00001223 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001224 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1225 Streamer.EmitIntValue(Encoding, Size);
1226
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001227 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001228 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001229 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001230 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001231 else
1232 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001233
1234 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001235 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001236 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001237 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001238 else
1239 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001240}
1241
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001242static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1243 if (IsEH)
1244 return 1;
1245 switch (DwarfVersion) {
1246 case 2:
1247 return 1;
1248 case 3:
1249 return 3;
1250 case 4:
1251 return 4;
1252 }
1253 llvm_unreachable("Unknown version");
1254}
1255
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001256const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001257 unsigned personalityEncoding,
1258 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001259 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001260 unsigned lsdaEncoding,
1261 bool IsSimple) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001262 MCContext &context = Streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001263 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001264 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001265
Jim Grosbach6f482002015-05-18 18:43:14 +00001266 MCSymbol *sectionStart = context.createTempSymbol();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001267 Streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001268
Jim Grosbach6f482002015-05-18 18:43:14 +00001269 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001270
1271 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001272 const MCExpr *Length =
1273 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1274 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001275
1276 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001277 unsigned CIE_ID = IsEH ? 0 : -1;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001278 Streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001279
1280 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001281 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001282 Streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001283
1284 // Augmentation String
1285 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001286 if (IsEH) {
1287 Augmentation += "z";
1288 if (personality)
1289 Augmentation += "P";
1290 if (lsda)
1291 Augmentation += "L";
1292 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001293 if (IsSignalFrame)
1294 Augmentation += "S";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001295 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001296 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001297 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001298
Keith Walkerea9483f2015-05-12 15:25:08 +00001299 if (CIEVersion >= 4) {
1300 // Address Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001301 Streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001302
1303 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001304 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001305 }
1306
Rafael Espindola0a017a62010-12-10 07:39:47 +00001307 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001308 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001309
1310 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001311 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001312
1313 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001314 if (CIEVersion == 1) {
1315 assert(MRI->getRARegister() <= 255 &&
1316 "DWARF 2 encodes return_address_register in one byte");
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001317 Streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001318 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001319 Streamer.EmitULEB128IntValue(
Frederic Rissadbb3f22015-02-26 19:48:07 +00001320 MRI->getDwarfRegNum(MRI->getRARegister(), IsEH));
Oliver Stannardf7693f42014-06-19 15:39:33 +00001321 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001322
1323 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001324
1325 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001326 if (IsEH) {
1327 if (personality) {
1328 // Personality Encoding
1329 augmentationLength += 1;
1330 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001331 augmentationLength += getSizeForEncoding(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001332 }
1333 if (lsda)
1334 augmentationLength += 1;
1335 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001336 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001337
Rafael Espindola6efa6fb2015-11-06 00:05:57 +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
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001343 emitEncodingByte(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001344 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001345 EmitPersonality(Streamer, *personality, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001346 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001347
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001348 if (lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001349 emitEncodingByte(Streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001350
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001351 // Encoding of the FDE pointers
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001352 emitEncodingByte(Streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001353 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001354
1355 // Initial Instructions
1356
Bill Wendlingbc07a892013-06-18 07:20:20 +00001357 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001358 if (!IsSimple) {
1359 const std::vector<MCCFIInstruction> &Instructions =
1360 MAI->getInitialFrameState();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001361 EmitCFIInstructions(Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001362 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001363
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001364 InitialCFAOffset = CFAOffset;
1365
Rafael Espindola0a017a62010-12-10 07:39:47 +00001366 // Padding
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001367 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001368
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001369 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001370 return *sectionStart;
1371}
1372
Rafael Espindola46be4352015-11-06 03:02:51 +00001373void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1374 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001375 bool LastInSection,
1376 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001377 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001378 MCSymbol *fdeStart = context.createTempSymbol();
1379 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001380 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001381
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001382 CFAOffset = InitialCFAOffset;
1383
Rafael Espindola0a017a62010-12-10 07:39:47 +00001384 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001385 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1386 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001387
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001388 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001389
Rafael Espindola0a017a62010-12-10 07:39:47 +00001390 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001391 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001392 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001393 const MCExpr *offset =
1394 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1395 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001396 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001397 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001398 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001399 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001400 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001401 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001402 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001403
Rafael Espindola0a017a62010-12-10 07:39:47 +00001404 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001405 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001406 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001407 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1408 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001409
1410 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001411 const MCExpr *Range =
1412 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1413 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001414
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001415 if (IsEH) {
1416 // Augmentation Data Length
1417 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001418
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001419 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001420 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001421
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001422 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001423
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001424 // Augmentation Data
1425 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001426 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001427 }
Rafael Espindola68067662011-04-29 03:06:29 +00001428
Rafael Espindola0a017a62010-12-10 07:39:47 +00001429 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001430 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001431
Rafael Espindola0a017a62010-12-10 07:39:47 +00001432 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001433 // The size of a .eh_frame section has to be a multiple of the alignment
1434 // since a null CIE is interpreted as the end. Old systems overaligned
1435 // .eh_frame, so we do too and account for it in the last FDE.
1436 unsigned Align = LastInSection ? asmInfo->getPointerSize() : PCSize;
1437 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001438
Rafael Espindola46be4352015-11-06 03:02:51 +00001439 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001440}
1441
Benjamin Kramer570dd782010-12-30 22:34:44 +00001442namespace {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001443struct CIEKey {
1444 static const CIEKey getEmptyKey() {
1445 return CIEKey(nullptr, 0, -1, false, false);
1446 }
1447 static const CIEKey getTombstoneKey() {
1448 return CIEKey(nullptr, -1, 0, false, false);
1449 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001450
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001451 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1452 unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple)
1453 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1454 LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame),
1455 IsSimple(IsSimple) {}
1456 const MCSymbol *Personality;
1457 unsigned PersonalityEncoding;
1458 unsigned LsdaEncoding;
1459 bool IsSignalFrame;
1460 bool IsSimple;
1461};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001462} // anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001463
1464namespace llvm {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001465template <> struct DenseMapInfo<CIEKey> {
1466 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1467 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1468 static unsigned getHashValue(const CIEKey &Key) {
1469 return static_cast<unsigned>(
1470 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1471 Key.IsSignalFrame, Key.IsSimple));
1472 }
1473 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1474 return LHS.Personality == RHS.Personality &&
1475 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1476 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1477 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1478 LHS.IsSimple == RHS.IsSimple;
1479 }
1480};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001481} // namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001482
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001483void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001484 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001485 Streamer.generateCompactUnwindEncodings(MAB);
1486
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001487 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001488 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001489 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001490 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001491
Bill Wendling9803abb2011-09-06 18:37:11 +00001492 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001493 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001494 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001495 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001496 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1497 const MCDwarfFrameInfo &Frame = FrameArray[i];
1498 if (Frame.CompactUnwindEncoding == 0) continue;
1499 if (!SectionEmitted) {
1500 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001501 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001502 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001503 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001504 NeedsEHFrameSection |=
1505 Frame.CompactUnwindEncoding ==
1506 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001507 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001508 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001509 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001510
Tim Northoverd1c6f512014-03-29 09:03:13 +00001511 if (!NeedsEHFrameSection) return;
1512
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001513 MCSection &Section =
1514 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1515 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001516
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001517 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001518 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001519 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001520
Eric Christopherd29430d2014-06-19 20:00:09 +00001521 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001522
Craig Topperbb694de2014-04-13 04:57:38 +00001523 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001524 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001525 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1526 const MCDwarfFrameInfo &Frame = *I;
1527 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001528 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001529 MOFI->getCompactUnwindDwarfEHFrameOnly())
1530 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1531 // of by the compact unwind encoding.
1532 continue;
1533
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001534 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001535 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001536 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1537 if (!CIEStart)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001538 CIEStart = &Emitter.EmitCIE(Frame.Personality, Frame.PersonalityEncoding,
1539 Frame.Lsda, Frame.IsSignalFrame,
1540 Frame.LsdaEncoding, Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001541
Rafael Espindola97588e12015-11-06 13:14:59 +00001542 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001543 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001544}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001545
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001546void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001547 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001548 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001549 SmallString<256> Tmp;
1550 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001551 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001552 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001553}
1554
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001555void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1556 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001557 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001558 // Scale the address delta by the minimum instruction length.
1559 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1560
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001561 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001562 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001563 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1564 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001565 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001566 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1567 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001568 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001569 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001570 if (Context.getAsmInfo()->isLittleEndian())
1571 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1572 else
1573 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001574 } else {
1575 assert(isUInt<32>(AddrDelta));
1576 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001577 if (Context.getAsmInfo()->isLittleEndian())
1578 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1579 else
1580 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001581 }
1582}