blob: 58cf652b68f249b24f32a40c410ae1deade152a8 [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);
517 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4);
Oliver Stannard14f97d02014-09-22 10:45:16 +0000518 if (MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
519 MCOS->getContext().getDwarfVersion() >= 3) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000520 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4);
521 } else {
522 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
523 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
524 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000525 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000526 if (!context.getCompilationDir().empty())
527 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000528 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
529 if (!DwarfDebugFlags.empty())
530 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
531 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
532 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
533 EmitAbbrev(MCOS, 0, 0);
534
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000535 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000536 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000537 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000538 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
539 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
540 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
541 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
542 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000543 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
544 EmitAbbrev(MCOS, 0, 0);
545
546 // DW_TAG_unspecified_parameters DIE abbrev (3).
547 MCOS->EmitULEB128IntValue(3);
548 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
549 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
550 EmitAbbrev(MCOS, 0, 0);
551
552 // Terminate the abbreviations for this compilation unit.
553 MCOS->EmitIntValue(0, 1);
554}
555
556// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000557// .debug_aranges section. This section contains a header and a table of pairs
558// of PointerSize'ed values for the address and size of section(s) with line
559// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000560static void EmitGenDwarfAranges(MCStreamer *MCOS,
561 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000562 MCContext &context = MCOS->getContext();
563
Oliver Stannard8b273082014-06-19 15:52:37 +0000564 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000565
566 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
567
568 // This will be the length of the .debug_aranges section, first account for
569 // the size of each item in the header (see below where we emit these items).
570 int Length = 4 + 2 + 4 + 1 + 1;
571
572 // Figure the padding after the header before the table of address and size
573 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000574 const MCAsmInfo *asmInfo = context.getAsmInfo();
575 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000576 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
577 if (Pad == 2 * AddrSize)
578 Pad = 0;
579 Length += Pad;
580
581 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000582 // of each section we have in the table.
583 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000584 // And the pair of terminating zeros.
585 Length += 2 * AddrSize;
586
587
588 // Emit the header for this section.
589 // The 4 byte length not including the 4 byte value for the length.
590 MCOS->EmitIntValue(Length - 4, 4);
591 // The 2 byte version, which is 2.
592 MCOS->EmitIntValue(2, 2);
593 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000594 // of the .debug_info.
595 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000596 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
597 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000598 else
599 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000600 // The 1 byte size of an address.
601 MCOS->EmitIntValue(AddrSize, 1);
602 // The 1 byte size of a segment descriptor, we use a value of zero.
603 MCOS->EmitIntValue(0, 1);
604 // Align the header with the padding if needed, before we put out the table.
605 for(int i = 0; i < Pad; i++)
606 MCOS->EmitIntValue(0, 1);
607
Oliver Stannard8b273082014-06-19 15:52:37 +0000608 // Now emit the table of pairs of PointerSize'ed values for the section
609 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000610 for (MCSection *Sec : Sections) {
611 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000612 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000613 assert(StartSymbol && "StartSymbol must not be NULL");
614 assert(EndSymbol && "EndSymbol must not be NULL");
615
Jim Grosbach13760bd2015-05-30 01:25:56 +0000616 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000617 StartSymbol, MCSymbolRefExpr::VK_None, context);
618 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
619 *StartSymbol, *EndSymbol, 0);
620 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000621 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000622 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000623
624 // And finally the pair of terminating zeros.
625 MCOS->EmitIntValue(0, AddrSize);
626 MCOS->EmitIntValue(0, AddrSize);
627}
628
629// When generating dwarf for assembly source files this emits the data for
630// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000631// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000632static void EmitGenDwarfInfo(MCStreamer *MCOS,
633 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000634 const MCSymbol *LineSectionSymbol,
635 const MCSymbol *RangesSectionSymbol) {
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.
Jim Grosbach6f482002015-05-18 18:43:14 +0000642 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000643 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000644 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000645
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);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000651 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000652
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000653 // The 2 byte DWARF version.
654 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000655
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000656 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000657 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
658 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000659 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000660 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000661 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000662 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
663 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000664
Bill Wendlingbc07a892013-06-18 07:20:20 +0000665 const MCAsmInfo *asmInfo = context.getAsmInfo();
666 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000667 // The 1 byte size of an address.
668 MCOS->EmitIntValue(AddrSize, 1);
669
670 // Second part: the compile_unit DIE.
671
672 // The DW_TAG_compile_unit DIE abbrev (1).
673 MCOS->EmitULEB128IntValue(1);
674
675 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
676 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000677 if (LineSectionSymbol)
678 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
679 AsmInfo.needsDwarfSectionOffsetDirective());
680 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000681 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000682
Oliver Stannard8b273082014-06-19 15:52:37 +0000683 if (RangesSectionSymbol) {
684 // There are multiple sections containing code, so we must use the
685 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000686
Oliver Stannard8b273082014-06-19 15:52:37 +0000687 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
688 // to the address range list for this compilation unit.
689 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
690 } else {
691 // If we only have one non-empty code section, we can use the simpler
692 // AT_low_pc and AT_high_pc attributes.
693
694 // Find the first (and only) non-empty text section
695 auto &Sections = context.getGenDwarfSectionSyms();
696 const auto TextSection = Sections.begin();
697 assert(TextSection != Sections.end() && "No text section found");
698
Rafael Espindolae0746792015-05-21 16:52:32 +0000699 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
700 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000701 assert(StartSymbol && "StartSymbol must not be NULL");
702 assert(EndSymbol && "EndSymbol must not be NULL");
703
704 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000705 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000706 StartSymbol, MCSymbolRefExpr::VK_None, context);
707 MCOS->EmitValue(Start, AddrSize);
708
709 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000710 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000711 EndSymbol, MCSymbolRefExpr::VK_None, context);
712 MCOS->EmitValue(End, AddrSize);
713 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000714
715 // AT_name, the name of the source file. Reconstruct from the first directory
716 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000717 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000718 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000719 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000720 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000721 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000722 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000723 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000724 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000725 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
726
727 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000728 if (!context.getCompilationDir().empty()) {
729 MCOS->EmitBytes(context.getCompilationDir());
730 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
731 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000732
733 // AT_APPLE_flags, the command line arguments of the assembler tool.
734 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
735 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000736 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000737 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
738 }
739
740 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000741 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000742 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000743 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000744 else
745 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000746 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
747
748 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
749 // draft has no standard code for assembler.
750 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
751
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000752 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000753
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000754 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000755 const std::vector<MCGenDwarfLabelEntry> &Entries =
756 MCOS->getContext().getMCGenDwarfLabelEntries();
757 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000758 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000759 MCOS->EmitULEB128IntValue(2);
760
761 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000762 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000763 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
764
765 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000766 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000767
768 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000769 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000770
771 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000772 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000773 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000774 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000775
Kevin Enderbye7739d42011-12-09 18:09:40 +0000776 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
777 MCOS->EmitIntValue(0, 1);
778
779 // The DW_TAG_unspecified_parameters DIE abbrev (3).
780 MCOS->EmitULEB128IntValue(3);
781
782 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
783 MCOS->EmitIntValue(0, 1);
784 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000785
786 // Add the NULL DIE terminating the Compile Unit DIE's.
787 MCOS->EmitIntValue(0, 1);
788
789 // Now set the value of the symbol at the end of the info section.
790 MCOS->EmitLabel(InfoEnd);
791}
792
Oliver Stannard8b273082014-06-19 15:52:37 +0000793// When generating dwarf for assembly source files this emits the data for
794// .debug_ranges section. We only emit one range list, which spans all of the
795// executable sections of this file.
796static void EmitGenDwarfRanges(MCStreamer *MCOS) {
797 MCContext &context = MCOS->getContext();
798 auto &Sections = context.getGenDwarfSectionSyms();
799
800 const MCAsmInfo *AsmInfo = context.getAsmInfo();
801 int AddrSize = AsmInfo->getPointerSize();
802
803 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
804
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000805 for (MCSection *Sec : Sections) {
806 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000807 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000808 assert(StartSymbol && "StartSymbol must not be NULL");
809 assert(EndSymbol && "EndSymbol must not be NULL");
810
811 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000812 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000813 StartSymbol, MCSymbolRefExpr::VK_None, context);
814 MCOS->EmitFill(AddrSize, 0xFF);
815 MCOS->EmitValue(SectionStartAddr, AddrSize);
816
817 // Emit a range list entry spanning this section
818 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
819 *StartSymbol, *EndSymbol, 0);
820 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000821 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000822 }
823
824 // Emit end of list entry
825 MCOS->EmitIntValue(0, AddrSize);
826 MCOS->EmitIntValue(0, AddrSize);
827}
828
Kevin Enderbye7739d42011-12-09 18:09:40 +0000829//
830// When generating dwarf for assembly source files this emits the Dwarf
831// sections.
832//
David Blaikie8bf66c42014-04-01 07:35:52 +0000833void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000834 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000835
836 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000837 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000838 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000839 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000840 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000841 if (CreateDwarfSectionSymbols)
842 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000843 MCSymbol *AbbrevSectionSymbol = nullptr;
844 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000845 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000846
847 // Create end symbols for each section, and remove empty sections
848 MCOS->getContext().finalizeDwarfSections(*MCOS);
849
850 // If there are no sections to generate debug info for, we don't need
851 // to do anything
852 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
853 return;
854
Oliver Stannard14f97d02014-09-22 10:45:16 +0000855 // We only use the .debug_ranges section if we have multiple code sections,
856 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000857 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000858 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
859 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000860 CreateDwarfSectionSymbols |= UseRangesSection;
861
Kevin Enderbye7739d42011-12-09 18:09:40 +0000862 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000863 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000864 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000865 MCOS->EmitLabel(InfoSectionSymbol);
866 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000867 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000868 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000869 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000870 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000871 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000872 if (UseRangesSection) {
873 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
874 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000875 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000876 MCOS->EmitLabel(RangesSectionSymbol);
877 }
878 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000879
Oliver Stannard8b273082014-06-19 15:52:37 +0000880 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
881
882 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000883
884 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000885 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000886
Oliver Stannard8b273082014-06-19 15:52:37 +0000887 if (UseRangesSection)
888 EmitGenDwarfRanges(MCOS);
889
Kevin Enderbye7739d42011-12-09 18:09:40 +0000890 // Output the data for .debug_abbrev section.
891 EmitGenDwarfAbbrev(MCOS);
892
893 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000894 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
895 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000896}
897
898//
899// When generating dwarf for assembly source files this is called when symbol
900// for a label is created. If this symbol is not a temporary and is in the
901// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000902// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000903//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000904void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000905 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000906 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000907 if (Symbol->isTemporary())
908 return;
909 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000910 // We won't create dwarf labels for symbols in sections that we are not
911 // generating debug info for.
912 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000913 return;
914
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000915 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000916 // underbar if any.
917 StringRef Name = Symbol->getName();
918 if (Name.startswith("_"))
919 Name = Name.substr(1, Name.size()-1);
920
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000921 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000922 unsigned FileNumber = context.getGenDwarfFileNumber();
923
924 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000925 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000926 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000927 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
928
929 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
930 // values so that they don't have things like an ARM thumb bit from the
931 // original symbol. So when used they won't get a low bit set after
932 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000933 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000934 MCOS->EmitLabel(Label);
935
936 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000937 MCOS->getContext().addMCGenDwarfLabelEntry(
938 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000939}
940
Rafael Espindola0a017a62010-12-10 07:39:47 +0000941static int getDataAlignmentFactor(MCStreamer &streamer) {
942 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000943 const MCAsmInfo *asmInfo = context.getAsmInfo();
944 int size = asmInfo->getCalleeSaveStackSlotSize();
945 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000946 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000947 else
948 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000949}
950
Rafael Espindola5395f442011-04-22 00:08:43 +0000951static unsigned getSizeForEncoding(MCStreamer &streamer,
952 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000953 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000954 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000955 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000956 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000957 case dwarf::DW_EH_PE_absptr:
958 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000959 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000960 case dwarf::DW_EH_PE_udata2:
961 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000962 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000963 case dwarf::DW_EH_PE_udata4:
964 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000965 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000966 case dwarf::DW_EH_PE_udata8:
967 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000968 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000969 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000970}
971
Rafael Espindolafe963b12014-08-15 03:07:13 +0000972static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
973 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000974 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000975 const MCAsmInfo *asmInfo = context.getAsmInfo();
976 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
977 symbolEncoding,
978 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000979 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000980 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000981 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000982 else
983 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000984}
985
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000986static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
987 unsigned symbolEncoding) {
988 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000989 const MCAsmInfo *asmInfo = context.getAsmInfo();
990 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
991 symbolEncoding,
992 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000993 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +0000994 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000995}
996
Rafael Espindola1ec0f462011-04-12 16:12:03 +0000997namespace {
998 class FrameEmitterImpl {
999 int CFAOffset;
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001000 int InitialCFAOffset;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001001 bool IsEH;
Rafael Espindola99f67352011-05-10 20:59:42 +00001002 const MCSymbol *SectionStart;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001003 public:
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001004 FrameEmitterImpl(bool isEH)
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001005 : CFAOffset(0), InitialCFAOffset(0), IsEH(isEH), SectionStart(nullptr) {
1006 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001007
1008 void setSectionStart(const MCSymbol *Label) { SectionStart = Label; }
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001009
Rafael Espindolafe963b12014-08-15 03:07:13 +00001010 /// Emit the unwind information in a compact way.
1011 void EmitCompactUnwind(MCObjectStreamer &streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001012 const MCDwarfFrameInfo &frame);
1013
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001014 const MCSymbol &EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001015 const MCSymbol *personality,
1016 unsigned personalityEncoding,
1017 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001018 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001019 unsigned lsdaEncoding,
1020 bool IsSimple);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001021 MCSymbol *EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001022 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001023 const MCDwarfFrameInfo &frame);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001024 void EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001025 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001026 MCSymbol *BaseLabel);
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001027 void EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001028 const MCCFIInstruction &Instr);
1029 };
Bill Wendling567a1ae2011-06-30 21:25:51 +00001030
1031} // end anonymous namespace
1032
Rafael Espindolafe963b12014-08-15 03:07:13 +00001033static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001034 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001035}
1036
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001037void FrameEmitterImpl::EmitCFIInstruction(MCObjectStreamer &Streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001038 const MCCFIInstruction &Instr) {
1039 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001040 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001041
1042 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001043 case MCCFIInstruction::OpRegister: {
1044 unsigned Reg1 = Instr.getRegister();
1045 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001046 if (!IsEH) {
1047 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1048 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1049 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001050 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1051 Streamer.EmitULEB128IntValue(Reg1);
1052 Streamer.EmitULEB128IntValue(Reg2);
1053 return;
1054 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001055 case MCCFIInstruction::OpWindowSave: {
1056 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1057 return;
1058 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001059 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001060 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001061 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1062 Streamer.EmitULEB128IntValue(Reg);
1063 return;
1064 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001065 case MCCFIInstruction::OpAdjustCfaOffset:
1066 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001067 const bool IsRelative =
1068 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1069
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001070 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1071
1072 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001073 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001074 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001075 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001076
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001077 Streamer.EmitULEB128IntValue(CFAOffset);
1078
1079 return;
1080 }
1081 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001082 unsigned Reg = Instr.getRegister();
1083 if (!IsEH)
1084 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001085 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001086 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001087 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001088 Streamer.EmitULEB128IntValue(CFAOffset);
1089
1090 return;
1091 }
1092
1093 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001094 unsigned Reg = Instr.getRegister();
1095 if (!IsEH)
1096 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001097 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001098 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001099
1100 return;
1101 }
1102
1103 case MCCFIInstruction::OpOffset:
1104 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001105 const bool IsRelative =
1106 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001107
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001108 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001109 if (!IsEH)
1110 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1111
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001112 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001113 if (IsRelative)
1114 Offset -= CFAOffset;
1115 Offset = Offset / dataAlignmentFactor;
1116
1117 if (Offset < 0) {
1118 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1119 Streamer.EmitULEB128IntValue(Reg);
1120 Streamer.EmitSLEB128IntValue(Offset);
1121 } else if (Reg < 64) {
1122 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001123 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001124 } else {
1125 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001126 Streamer.EmitULEB128IntValue(Reg);
1127 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001128 }
1129 return;
1130 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001131 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001132 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1133 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001134 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001135 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1136 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001137 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001138 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001139 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001140 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001141 return;
1142 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001143 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001144 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001145 if (!IsEH)
1146 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001147 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1148 return;
1149 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001150 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001151 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001152 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001153 }
1154 llvm_unreachable("Unhandled case in switch");
1155}
1156
Rafael Espindolafe963b12014-08-15 03:07:13 +00001157/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001158void FrameEmitterImpl::EmitCFIInstructions(MCObjectStreamer &streamer,
Bill Wendling10543922013-09-04 22:35:29 +00001159 ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001160 MCSymbol *BaseLabel) {
1161 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1162 const MCCFIInstruction &Instr = Instrs[i];
1163 MCSymbol *Label = Instr.getLabel();
1164 // Throw out move if the label is invalid.
1165 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1166
1167 // Advance row if new location.
1168 if (BaseLabel && Label) {
1169 MCSymbol *ThisSym = Label;
1170 if (ThisSym != BaseLabel) {
1171 streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
1172 BaseLabel = ThisSym;
1173 }
1174 }
1175
1176 EmitCFIInstruction(streamer, Instr);
1177 }
1178}
1179
Rafael Espindolafe963b12014-08-15 03:07:13 +00001180/// Emit the unwind information in a compact way.
1181void FrameEmitterImpl::EmitCompactUnwind(MCObjectStreamer &Streamer,
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001182 const MCDwarfFrameInfo &Frame) {
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001183 MCContext &Context = Streamer.getContext();
Evan Cheng76792992011-07-20 05:58:47 +00001184 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001185
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001186 // range-start range-length compact-unwind-enc personality-func lsda
1187 // _foo LfooEnd-_foo 0x00000023 0 0
1188 // _bar LbarEnd-_bar 0x00000025 __gxx_personality except_tab1
1189 //
1190 // .section __LD,__compact_unwind,regular,debug
1191 //
1192 // # compact unwind for _foo
1193 // .quad _foo
1194 // .set L1,LfooEnd-_foo
1195 // .long L1
1196 // .long 0x01010001
1197 // .quad 0
1198 // .quad 0
1199 //
1200 // # compact unwind for _bar
1201 // .quad _bar
1202 // .set L2,LbarEnd-_bar
1203 // .long L2
1204 // .long 0x01020011
1205 // .quad __gxx_personality
1206 // .quad except_tab1
1207
Bill Wendling49bc6802011-07-19 00:06:12 +00001208 uint32_t Encoding = Frame.CompactUnwindEncoding;
Bill Wendling99bce5f2013-04-18 23:16:46 +00001209 if (!Encoding) return;
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001210 bool DwarfEHFrameOnly = (Encoding == MOFI->getCompactUnwindDwarfEHFrameOnly());
Bill Wendlingb6adf462011-07-07 00:54:13 +00001211
Bill Wendlingdafd5982011-07-14 23:34:45 +00001212 // The encoding needs to know we have an LSDA.
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001213 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendlingdafd5982011-07-14 23:34:45 +00001214 Encoding |= 0x40000000;
1215
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001216 // Range Start
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001217 unsigned FDEEncoding = MOFI->getFDEEncoding();
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001218 unsigned Size = getSizeForEncoding(Streamer, FDEEncoding);
Rafael Espindola88604822014-06-23 15:34:32 +00001219 Streamer.EmitSymbolValue(Frame.Begin, Size);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001220
1221 // Range Length
1222 const MCExpr *Range = MakeStartMinusEndExpr(Streamer, *Frame.Begin,
1223 *Frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001224 emitAbsValue(Streamer, Range, 4);
Bill Wendling75174662011-06-30 00:30:52 +00001225
Bill Wendling75174662011-06-30 00:30:52 +00001226 // Compact Encoding
Bill Wendling75174662011-06-30 00:30:52 +00001227 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_udata4);
1228 Streamer.EmitIntValue(Encoding, Size);
1229
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001230 // Personality Function
Bill Wendlingdafd5982011-07-14 23:34:45 +00001231 Size = getSizeForEncoding(Streamer, dwarf::DW_EH_PE_absptr);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001232 if (!DwarfEHFrameOnly && Frame.Personality)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001233 Streamer.EmitSymbolValue(Frame.Personality, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001234 else
1235 Streamer.EmitIntValue(0, Size); // No personality fn
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001236
1237 // LSDA
Bill Wendling4cdb2062011-06-29 23:53:16 +00001238 Size = getSizeForEncoding(Streamer, Frame.LsdaEncoding);
Bill Wendling2d1df6b2013-04-10 21:42:06 +00001239 if (!DwarfEHFrameOnly && Frame.Lsda)
Bill Wendling8fa4ada2011-06-29 23:49:12 +00001240 Streamer.EmitSymbolValue(Frame.Lsda, Size);
Bill Wendling4cdb2062011-06-29 23:53:16 +00001241 else
1242 Streamer.EmitIntValue(0, Size); // No LSDA
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001243}
1244
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001245static unsigned getCIEVersion(bool IsEH, unsigned DwarfVersion) {
1246 if (IsEH)
1247 return 1;
1248 switch (DwarfVersion) {
1249 case 2:
1250 return 1;
1251 case 3:
1252 return 3;
1253 case 4:
1254 return 4;
1255 }
1256 llvm_unreachable("Unknown version");
1257}
1258
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001259const MCSymbol &FrameEmitterImpl::EmitCIE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001260 const MCSymbol *personality,
1261 unsigned personalityEncoding,
1262 const MCSymbol *lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001263 bool IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001264 unsigned lsdaEncoding,
1265 bool IsSimple) {
Rafael Espindola0a017a62010-12-10 07:39:47 +00001266 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +00001267 const MCRegisterInfo *MRI = context.getRegisterInfo();
Evan Cheng76792992011-07-20 05:58:47 +00001268 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001269
Jim Grosbach6f482002015-05-18 18:43:14 +00001270 MCSymbol *sectionStart = context.createTempSymbol();
Bill Wendling567a1ae2011-06-30 21:25:51 +00001271 streamer.EmitLabel(sectionStart);
Rafael Espindolabf60fb02011-04-28 03:26:11 +00001272
Jim Grosbach6f482002015-05-18 18:43:14 +00001273 MCSymbol *sectionEnd = context.createTempSymbol();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001274
1275 // Length
1276 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *sectionStart,
1277 *sectionEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001278 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001279
1280 // CIE ID
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001281 unsigned CIE_ID = IsEH ? 0 : -1;
1282 streamer.EmitIntValue(CIE_ID, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001283
1284 // Version
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001285 uint8_t CIEVersion = getCIEVersion(IsEH, context.getDwarfVersion());
Oliver Stannardf7693f42014-06-19 15:39:33 +00001286 streamer.EmitIntValue(CIEVersion, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001287
1288 // Augmentation String
1289 SmallString<8> Augmentation;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001290 if (IsEH) {
1291 Augmentation += "z";
1292 if (personality)
1293 Augmentation += "P";
1294 if (lsda)
1295 Augmentation += "L";
1296 Augmentation += "R";
Rafael Espindola3c47e372012-01-23 21:51:52 +00001297 if (IsSignalFrame)
1298 Augmentation += "S";
Yaron Keren92e1b622015-03-18 10:17:07 +00001299 streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001300 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001301 streamer.EmitIntValue(0, 1);
1302
Keith Walkerea9483f2015-05-12 15:25:08 +00001303 if (CIEVersion >= 4) {
1304 // Address Size
1305 streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1);
1306
1307 // Segment Descriptor Size
1308 streamer.EmitIntValue(0, 1);
1309 }
1310
Rafael Espindola0a017a62010-12-10 07:39:47 +00001311 // 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
1315 streamer.EmitSLEB128IntValue(getDataAlignmentFactor(streamer));
1316
1317 // Return Address Register
Oliver Stannardf7693f42014-06-19 15:39:33 +00001318 if (CIEVersion == 1) {
1319 assert(MRI->getRARegister() <= 255 &&
1320 "DWARF 2 encodes return_address_register in one byte");
Frederic Rissadbb3f22015-02-26 19:48:07 +00001321 streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001322 } else {
1323 streamer.EmitULEB128IntValue(
Frederic Rissadbb3f22015-02-26 19:48:07 +00001324 MRI->getDwarfRegNum(MRI->getRARegister(), IsEH));
Oliver Stannardf7693f42014-06-19 15:39:33 +00001325 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001326
1327 // Augmentation Data Length (optional)
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001328
1329 unsigned augmentationLength = 0;
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001330 if (IsEH) {
1331 if (personality) {
1332 // Personality Encoding
1333 augmentationLength += 1;
1334 // Personality
1335 augmentationLength += getSizeForEncoding(streamer, personalityEncoding);
1336 }
1337 if (lsda)
1338 augmentationLength += 1;
1339 // Encoding of the FDE pointers
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001340 augmentationLength += 1;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001341
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001342 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001343
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001344 // Augmentation Data (optional)
1345 if (personality) {
1346 // Personality Encoding
Rafael Espindolafe963b12014-08-15 03:07:13 +00001347 emitEncodingByte(streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001348 // Personality
1349 EmitPersonality(streamer, *personality, personalityEncoding);
1350 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001351
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001352 if (lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001353 emitEncodingByte(streamer, lsdaEncoding);
Bill Wendling567a1ae2011-06-30 21:25:51 +00001354
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001355 // Encoding of the FDE pointers
Rafael Espindolafe963b12014-08-15 03:07:13 +00001356 emitEncodingByte(streamer, MOFI->getFDEEncoding());
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001357 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001358
1359 // Initial Instructions
1360
Bill Wendlingbc07a892013-06-18 07:20:20 +00001361 const MCAsmInfo *MAI = context.getAsmInfo();
David Majnemere035cf92014-01-27 17:20:25 +00001362 if (!IsSimple) {
1363 const std::vector<MCCFIInstruction> &Instructions =
1364 MAI->getInitialFrameState();
Craig Topperbb694de2014-04-13 04:57:38 +00001365 EmitCFIInstructions(streamer, Instructions, nullptr);
David Majnemere035cf92014-01-27 17:20:25 +00001366 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001367
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001368 InitialCFAOffset = CFAOffset;
1369
Rafael Espindola0a017a62010-12-10 07:39:47 +00001370 // Padding
Bill Wendlingbc07a892013-06-18 07:20:20 +00001371 streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001372
1373 streamer.EmitLabel(sectionEnd);
1374 return *sectionStart;
1375}
1376
Rafael Espindola1bb4a3f2014-05-12 14:40:12 +00001377MCSymbol *FrameEmitterImpl::EmitFDE(MCObjectStreamer &streamer,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001378 const MCSymbol &cieStart,
Rafael Espindola0e130d12011-05-10 03:01:39 +00001379 const MCDwarfFrameInfo &frame) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001380 MCContext &context = streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001381 MCSymbol *fdeStart = context.createTempSymbol();
1382 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001383 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001384
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001385 CFAOffset = InitialCFAOffset;
1386
Rafael Espindola0a017a62010-12-10 07:39:47 +00001387 // Length
1388 const MCExpr *Length = MakeStartMinusEndExpr(streamer, *fdeStart, *fdeEnd, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001389 emitAbsValue(streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001390
1391 streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001392
Rafael Espindola0a017a62010-12-10 07:39:47 +00001393 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001394 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001395 if (IsEH) {
1396 const MCExpr *offset = MakeStartMinusEndExpr(streamer, cieStart, *fdeStart,
1397 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001398 emitAbsValue(streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001399 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola99f67352011-05-10 20:59:42 +00001400 const MCExpr *offset = MakeStartMinusEndExpr(streamer, *SectionStart,
1401 cieStart, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001402 emitAbsValue(streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001403 } else {
1404 streamer.EmitSymbolValue(&cieStart, 4);
1405 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001406
Rafael Espindola0a017a62010-12-10 07:39:47 +00001407 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001408 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001409 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Nick Lewycky333449c2012-08-12 08:09:45 +00001410 unsigned PCSize = getSizeForEncoding(streamer, PCEncoding);
Rafael Espindolafe963b12014-08-15 03:07:13 +00001411 emitFDESymbol(streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001412
1413 // PC Range
1414 const MCExpr *Range = MakeStartMinusEndExpr(streamer, *frame.Begin,
1415 *frame.End, 0);
Rafael Espindolac23174b2014-08-15 15:12:13 +00001416 emitAbsValue(streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001417
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001418 if (IsEH) {
1419 // Augmentation Data Length
1420 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001421
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001422 if (frame.Lsda)
1423 augmentationLength += getSizeForEncoding(streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001424
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001425 streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001426
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001427 // Augmentation Data
1428 if (frame.Lsda)
Rafael Espindolafe963b12014-08-15 03:07:13 +00001429 emitFDESymbol(streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001430 }
Rafael Espindola68067662011-04-29 03:06:29 +00001431
Rafael Espindola0a017a62010-12-10 07:39:47 +00001432 // Call Frame Instructions
Rafael Espindola290d7162010-12-29 01:42:56 +00001433 EmitCFIInstructions(streamer, frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001434
Rafael Espindola0a017a62010-12-10 07:39:47 +00001435 // Padding
Nick Lewycky333449c2012-08-12 08:09:45 +00001436 streamer.EmitValueToAlignment(PCSize);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001437
1438 return fdeEnd;
Rafael Espindola0a017a62010-12-10 07:39:47 +00001439}
1440
Benjamin Kramer570dd782010-12-30 22:34:44 +00001441namespace {
1442 struct CIEKey {
Craig Topperbb694de2014-04-13 04:57:38 +00001443 static const CIEKey getEmptyKey() {
1444 return CIEKey(nullptr, 0, -1, false, false);
1445 }
1446 static const CIEKey getTombstoneKey() {
1447 return CIEKey(nullptr, -1, 0, false, false);
1448 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001449
Eric Christopherd29430d2014-06-19 20:00:09 +00001450 CIEKey(const MCSymbol *Personality_, unsigned PersonalityEncoding_,
1451 unsigned LsdaEncoding_, bool IsSignalFrame_, bool IsSimple_)
1452 : Personality(Personality_), PersonalityEncoding(PersonalityEncoding_),
1453 LsdaEncoding(LsdaEncoding_), IsSignalFrame(IsSignalFrame_),
1454 IsSimple(IsSimple_) {}
1455 const MCSymbol *Personality;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001456 unsigned PersonalityEncoding;
1457 unsigned LsdaEncoding;
Rafael Espindola3c47e372012-01-23 21:51:52 +00001458 bool IsSignalFrame;
David Majnemere035cf92014-01-27 17:20:25 +00001459 bool IsSimple;
Benjamin Kramer570dd782010-12-30 22:34:44 +00001460 };
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001461} // anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001462
1463namespace llvm {
1464 template <>
1465 struct DenseMapInfo<CIEKey> {
1466 static CIEKey getEmptyKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001467 return CIEKey::getEmptyKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001468 }
1469 static CIEKey getTombstoneKey() {
Benjamin Kramer570dd782010-12-30 22:34:44 +00001470 return CIEKey::getTombstoneKey();
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001471 }
1472 static unsigned getHashValue(const CIEKey &Key) {
Benjamin Kramer7a426b52012-04-11 14:06:39 +00001473 return static_cast<unsigned>(hash_combine(Key.Personality,
1474 Key.PersonalityEncoding,
1475 Key.LsdaEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001476 Key.IsSignalFrame,
1477 Key.IsSimple));
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001478 }
1479 static bool isEqual(const CIEKey &LHS,
1480 const CIEKey &RHS) {
1481 return LHS.Personality == RHS.Personality &&
1482 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
Rafael Espindola3c47e372012-01-23 21:51:52 +00001483 LHS.LsdaEncoding == RHS.LsdaEncoding &&
David Majnemere035cf92014-01-27 17:20:25 +00001484 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1485 LHS.IsSimple == RHS.IsSimple;
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001486 }
1487 };
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001488} // namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001489
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001490void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001491 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001492 Streamer.generateCompactUnwindEncodings(MAB);
1493
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001494 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001495 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001496 FrameEmitterImpl Emitter(IsEH);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001497 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001498
Bill Wendling9803abb2011-09-06 18:37:11 +00001499 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001500 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001501 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001502 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001503 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1504 const MCDwarfFrameInfo &Frame = FrameArray[i];
1505 if (Frame.CompactUnwindEncoding == 0) continue;
1506 if (!SectionEmitted) {
1507 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001508 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001509 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001510 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001511 NeedsEHFrameSection |=
1512 Frame.CompactUnwindEncoding ==
1513 MOFI->getCompactUnwindDwarfEHFrameOnly();
Bob Wilson0e180f22013-05-07 20:56:33 +00001514 Emitter.EmitCompactUnwind(Streamer, Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001515 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001516 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001517
Tim Northoverd1c6f512014-03-29 09:03:13 +00001518 if (!NeedsEHFrameSection) return;
1519
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001520 MCSection &Section =
1521 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1522 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001523
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001524 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001525 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001526 Streamer.EmitLabel(SectionStart);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001527 Emitter.setSectionStart(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001528
Craig Topperbb694de2014-04-13 04:57:38 +00001529 MCSymbol *FDEEnd = nullptr;
Eric Christopherd29430d2014-06-19 20:00:09 +00001530 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001531
Craig Topperbb694de2014-04-13 04:57:38 +00001532 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001533 NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001534 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1535 const MCDwarfFrameInfo &Frame = FrameArray[i];
Tim Northoverd1c6f512014-03-29 09:03:13 +00001536
1537 // Emit the label from the previous iteration
1538 if (FDEEnd) {
1539 Streamer.EmitLabel(FDEEnd);
Craig Topperbb694de2014-04-13 04:57:38 +00001540 FDEEnd = nullptr;
Tim Northoverd1c6f512014-03-29 09:03:13 +00001541 }
1542
1543 if (!NeedsEHFrameSection && Frame.CompactUnwindEncoding !=
1544 MOFI->getCompactUnwindDwarfEHFrameOnly())
1545 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1546 // of by the compact unwind encoding.
1547 continue;
1548
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001549 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001550 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001551 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1552 if (!CIEStart)
1553 CIEStart = &Emitter.EmitCIE(Streamer, Frame.Personality,
1554 Frame.PersonalityEncoding, Frame.Lsda,
Rafael Espindola3c47e372012-01-23 21:51:52 +00001555 Frame.IsSignalFrame,
David Majnemere035cf92014-01-27 17:20:25 +00001556 Frame.LsdaEncoding,
1557 Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001558
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001559 FDEEnd = Emitter.EmitFDE(Streamer, *CIEStart, Frame);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001560 }
Rafael Espindola9141b612010-12-26 20:20:31 +00001561
Bill Wendlingbc07a892013-06-18 07:20:20 +00001562 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001563 if (FDEEnd)
1564 Streamer.EmitLabel(FDEEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001565}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001566
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001567void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001568 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001569 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001570 SmallString<256> Tmp;
1571 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001572 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001573 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001574}
1575
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001576void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1577 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001578 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001579 // Scale the address delta by the minimum instruction length.
1580 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1581
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001582 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001583 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001584 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1585 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001586 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001587 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1588 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001589 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001590 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001591 if (Context.getAsmInfo()->isLittleEndian())
1592 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1593 else
1594 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001595 } else {
1596 assert(isUInt<32>(AddrDelta));
1597 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001598 if (Context.getAsmInfo()->isLittleEndian())
1599 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1600 else
1601 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001602 }
1603}