blob: 759f90e41b614a83e87ca8c66fa687dba16c9705 [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//
David Majnemer8cc30782016-01-21 01:59:03 +000049void MCDwarfLineEntry::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();
David Majnemer8cc30782016-01-21 01:59:03 +000055 // Set the value of the symbol to use for the MCDwarfLineEntry.
Kevin Enderbye46564a2010-09-30 16:52:03 +000056 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.
David Majnemer8cc30782016-01-21 01:59:03 +000062 MCDwarfLineEntry LineEntry(LineSym, DwarfLoc);
Kevin Enderbye46564a2010-09-30 16:52:03 +000063
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 Majnemer8cc30782016-01-21 01:59:03 +0000101 const MCLineSection::MCDwarfLineEntryCollection &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
David Majnemer8cc30782016-01-21 01:59:03 +0000110 // Loop through each MCDwarfLineEntry 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) {
Adrian Prantld6cc53f2016-03-09 17:32:56 +0000347 // File numbers start with 1 and/or after any file numbers
348 // allocated by inline-assembler .file directives.
349 FileNumber = MCDwarfFiles.empty() ? 1 : MCDwarfFiles.size();
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 Risse5b78d82016-01-31 22:06:35 +0000455 if (Temp >= Params.DWARF2LineRange ||
456 Temp + Params.DWARF2LineOpcodeBase > 255) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000457 OS << char(dwarf::DW_LNS_advance_line);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000458 encodeSLEB128(LineDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000459
460 LineDelta = 0;
Frederic Rissa5ab8442015-08-07 15:14:08 +0000461 Temp = 0 - Params.DWARF2LineBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000462 NeedCopy = true;
463 }
464
465 // Use DW_LNS_copy instead of a "line +0, addr +0" special opcode.
466 if (LineDelta == 0 && AddrDelta == 0) {
467 OS << char(dwarf::DW_LNS_copy);
468 return;
469 }
470
471 // Bias the opcode by the special opcode base.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000472 Temp += Params.DWARF2LineOpcodeBase;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000473
474 // Avoid overflow when addr_delta is large.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000475 if (AddrDelta < 256 + MaxSpecialAddrDelta) {
Kevin Enderbye46564a2010-09-30 16:52:03 +0000476 // Try using a special opcode.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000477 Opcode = Temp + AddrDelta * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000478 if (Opcode <= 255) {
479 OS << char(Opcode);
480 return;
481 }
482
483 // Try using DW_LNS_const_add_pc followed by special op.
Frederic Rissa5ab8442015-08-07 15:14:08 +0000484 Opcode = Temp + (AddrDelta - MaxSpecialAddrDelta) * Params.DWARF2LineRange;
Kevin Enderbye46564a2010-09-30 16:52:03 +0000485 if (Opcode <= 255) {
486 OS << char(dwarf::DW_LNS_const_add_pc);
487 OS << char(Opcode);
488 return;
489 }
490 }
491
492 // Otherwise use DW_LNS_advance_pc.
493 OS << char(dwarf::DW_LNS_advance_pc);
Jim Grosbachbf387df2012-08-08 23:56:06 +0000494 encodeULEB128(AddrDelta, OS);
Kevin Enderbye46564a2010-09-30 16:52:03 +0000495
496 if (NeedCopy)
497 OS << char(dwarf::DW_LNS_copy);
Frederic Risse5b78d82016-01-31 22:06:35 +0000498 else {
499 assert(Temp <= 255 && "Buggy special opcode encoding.");
Kevin Enderbye46564a2010-09-30 16:52:03 +0000500 OS << char(Temp);
Frederic Risse5b78d82016-01-31 22:06:35 +0000501 }
Kevin Enderbye46564a2010-09-30 16:52:03 +0000502}
503
Kevin Enderbye7739d42011-12-09 18:09:40 +0000504// Utility function to write a tuple for .debug_abbrev.
505static void EmitAbbrev(MCStreamer *MCOS, uint64_t Name, uint64_t Form) {
506 MCOS->EmitULEB128IntValue(Name);
507 MCOS->EmitULEB128IntValue(Form);
508}
509
510// When generating dwarf for assembly source files this emits
511// the data for .debug_abbrev section which contains three DIEs.
512static void EmitGenDwarfAbbrev(MCStreamer *MCOS) {
513 MCContext &context = MCOS->getContext();
514 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
515
516 // DW_TAG_compile_unit DIE abbrev (1).
517 MCOS->EmitULEB128IntValue(1);
518 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_compile_unit);
519 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
Paul Robinsone95fa422016-01-04 18:49:15 +0000520 EmitAbbrev(MCOS, dwarf::DW_AT_stmt_list, context.getDwarfVersion() >= 4
521 ? dwarf::DW_FORM_sec_offset
522 : dwarf::DW_FORM_data4);
Paul Robinson22d0d312015-12-23 01:57:31 +0000523 if (context.getGenDwarfSectionSyms().size() > 1 &&
524 context.getDwarfVersion() >= 3) {
Paul Robinsone95fa422016-01-04 18:49:15 +0000525 EmitAbbrev(MCOS, dwarf::DW_AT_ranges, context.getDwarfVersion() >= 4
526 ? dwarf::DW_FORM_sec_offset
Paul Robinson22d0d312015-12-23 01:57:31 +0000527 : dwarf::DW_FORM_data4);
Oliver Stannard8b273082014-06-19 15:52:37 +0000528 } else {
529 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
530 EmitAbbrev(MCOS, dwarf::DW_AT_high_pc, dwarf::DW_FORM_addr);
531 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000532 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
Andrew Trick32591d32013-12-10 04:39:09 +0000533 if (!context.getCompilationDir().empty())
534 EmitAbbrev(MCOS, dwarf::DW_AT_comp_dir, dwarf::DW_FORM_string);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000535 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
536 if (!DwarfDebugFlags.empty())
537 EmitAbbrev(MCOS, dwarf::DW_AT_APPLE_flags, dwarf::DW_FORM_string);
538 EmitAbbrev(MCOS, dwarf::DW_AT_producer, dwarf::DW_FORM_string);
539 EmitAbbrev(MCOS, dwarf::DW_AT_language, dwarf::DW_FORM_data2);
540 EmitAbbrev(MCOS, 0, 0);
541
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000542 // DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000543 MCOS->EmitULEB128IntValue(2);
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000544 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_label);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000545 MCOS->EmitIntValue(dwarf::DW_CHILDREN_yes, 1);
546 EmitAbbrev(MCOS, dwarf::DW_AT_name, dwarf::DW_FORM_string);
547 EmitAbbrev(MCOS, dwarf::DW_AT_decl_file, dwarf::DW_FORM_data4);
548 EmitAbbrev(MCOS, dwarf::DW_AT_decl_line, dwarf::DW_FORM_data4);
549 EmitAbbrev(MCOS, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000550 EmitAbbrev(MCOS, dwarf::DW_AT_prototyped, dwarf::DW_FORM_flag);
551 EmitAbbrev(MCOS, 0, 0);
552
553 // DW_TAG_unspecified_parameters DIE abbrev (3).
554 MCOS->EmitULEB128IntValue(3);
555 MCOS->EmitULEB128IntValue(dwarf::DW_TAG_unspecified_parameters);
556 MCOS->EmitIntValue(dwarf::DW_CHILDREN_no, 1);
557 EmitAbbrev(MCOS, 0, 0);
558
559 // Terminate the abbreviations for this compilation unit.
560 MCOS->EmitIntValue(0, 1);
561}
562
563// When generating dwarf for assembly source files this emits the data for
Oliver Stannard8b273082014-06-19 15:52:37 +0000564// .debug_aranges section. This section contains a header and a table of pairs
565// of PointerSize'ed values for the address and size of section(s) with line
566// table entries.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000567static void EmitGenDwarfAranges(MCStreamer *MCOS,
568 const MCSymbol *InfoSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000569 MCContext &context = MCOS->getContext();
570
Oliver Stannard8b273082014-06-19 15:52:37 +0000571 auto &Sections = context.getGenDwarfSectionSyms();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000572
573 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
574
575 // This will be the length of the .debug_aranges section, first account for
576 // the size of each item in the header (see below where we emit these items).
577 int Length = 4 + 2 + 4 + 1 + 1;
578
579 // Figure the padding after the header before the table of address and size
580 // pairs who's values are PointerSize'ed.
Bill Wendlingbc07a892013-06-18 07:20:20 +0000581 const MCAsmInfo *asmInfo = context.getAsmInfo();
582 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000583 int Pad = 2 * AddrSize - (Length & (2 * AddrSize - 1));
584 if (Pad == 2 * AddrSize)
585 Pad = 0;
586 Length += Pad;
587
588 // Add the size of the pair of PointerSize'ed values for the address and size
Oliver Stannard8b273082014-06-19 15:52:37 +0000589 // of each section we have in the table.
590 Length += 2 * AddrSize * Sections.size();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000591 // And the pair of terminating zeros.
592 Length += 2 * AddrSize;
593
594
595 // Emit the header for this section.
596 // The 4 byte length not including the 4 byte value for the length.
597 MCOS->EmitIntValue(Length - 4, 4);
598 // The 2 byte version, which is 2.
599 MCOS->EmitIntValue(2, 2);
600 // The 4 byte offset to the compile unit in the .debug_info from the start
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000601 // of the .debug_info.
602 if (InfoSectionSymbol)
Saleem Abdulrasoolfcefa212014-09-06 19:57:48 +0000603 MCOS->EmitSymbolValue(InfoSectionSymbol, 4,
604 asmInfo->needsDwarfSectionOffsetDirective());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000605 else
606 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000607 // The 1 byte size of an address.
608 MCOS->EmitIntValue(AddrSize, 1);
609 // The 1 byte size of a segment descriptor, we use a value of zero.
610 MCOS->EmitIntValue(0, 1);
611 // Align the header with the padding if needed, before we put out the table.
612 for(int i = 0; i < Pad; i++)
613 MCOS->EmitIntValue(0, 1);
614
Oliver Stannard8b273082014-06-19 15:52:37 +0000615 // Now emit the table of pairs of PointerSize'ed values for the section
616 // addresses and sizes.
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000617 for (MCSection *Sec : Sections) {
618 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000619 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000620 assert(StartSymbol && "StartSymbol must not be NULL");
621 assert(EndSymbol && "EndSymbol must not be NULL");
622
Jim Grosbach13760bd2015-05-30 01:25:56 +0000623 const MCExpr *Addr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000624 StartSymbol, MCSymbolRefExpr::VK_None, context);
625 const MCExpr *Size = MakeStartMinusEndExpr(*MCOS,
626 *StartSymbol, *EndSymbol, 0);
627 MCOS->EmitValue(Addr, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000628 emitAbsValue(*MCOS, Size, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000629 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000630
631 // And finally the pair of terminating zeros.
632 MCOS->EmitIntValue(0, AddrSize);
633 MCOS->EmitIntValue(0, AddrSize);
634}
635
636// When generating dwarf for assembly source files this emits the data for
637// .debug_info section which contains three parts. The header, the compile_unit
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000638// DIE and a list of label DIEs.
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000639static void EmitGenDwarfInfo(MCStreamer *MCOS,
640 const MCSymbol *AbbrevSectionSymbol,
Oliver Stannard8b273082014-06-19 15:52:37 +0000641 const MCSymbol *LineSectionSymbol,
642 const MCSymbol *RangesSectionSymbol) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000643 MCContext &context = MCOS->getContext();
644
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000645 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000646
647 // Create a symbol at the start and end of this section used in here for the
648 // expression to calculate the length in the header.
Jim Grosbach6f482002015-05-18 18:43:14 +0000649 MCSymbol *InfoStart = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000650 MCOS->EmitLabel(InfoStart);
Jim Grosbach6f482002015-05-18 18:43:14 +0000651 MCSymbol *InfoEnd = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000652
653 // First part: the header.
654
655 // The 4 byte total length of the information for this compilation unit, not
656 // including these 4 bytes.
657 const MCExpr *Length = MakeStartMinusEndExpr(*MCOS, *InfoStart, *InfoEnd, 4);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000658 emitAbsValue(*MCOS, Length, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000659
Oliver Stannard7eacbd52014-05-01 08:46:02 +0000660 // The 2 byte DWARF version.
661 MCOS->EmitIntValue(context.getDwarfVersion(), 2);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000662
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000663 const MCAsmInfo &AsmInfo = *context.getAsmInfo();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000664 // The 4 byte offset to the debug abbrevs from the start of the .debug_abbrev,
665 // it is at the start of that section so this is zero.
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000666 if (AbbrevSectionSymbol == nullptr)
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000667 MCOS->EmitIntValue(0, 4);
Saleem Abdulrasool7d095302014-07-17 16:27:44 +0000668 else
Saleem Abdulrasool00426d92014-07-19 21:01:58 +0000669 MCOS->EmitSymbolValue(AbbrevSectionSymbol, 4,
670 AsmInfo.needsDwarfSectionOffsetDirective());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000671
Bill Wendlingbc07a892013-06-18 07:20:20 +0000672 const MCAsmInfo *asmInfo = context.getAsmInfo();
673 int AddrSize = asmInfo->getPointerSize();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000674 // The 1 byte size of an address.
675 MCOS->EmitIntValue(AddrSize, 1);
676
677 // Second part: the compile_unit DIE.
678
679 // The DW_TAG_compile_unit DIE abbrev (1).
680 MCOS->EmitULEB128IntValue(1);
681
682 // DW_AT_stmt_list, a 4 byte offset from the start of the .debug_line section,
683 // which is at the start of that section so this is zero.
Saleem Abdulrasool5c70de12014-09-05 04:15:00 +0000684 if (LineSectionSymbol)
685 MCOS->EmitSymbolValue(LineSectionSymbol, 4,
686 AsmInfo.needsDwarfSectionOffsetDirective());
687 else
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000688 MCOS->EmitIntValue(0, 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000689
Oliver Stannard8b273082014-06-19 15:52:37 +0000690 if (RangesSectionSymbol) {
691 // There are multiple sections containing code, so we must use the
692 // .debug_ranges sections.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000693
Oliver Stannard8b273082014-06-19 15:52:37 +0000694 // AT_ranges, the 4 byte offset from the start of the .debug_ranges section
695 // to the address range list for this compilation unit.
696 MCOS->EmitSymbolValue(RangesSectionSymbol, 4);
697 } else {
698 // If we only have one non-empty code section, we can use the simpler
699 // AT_low_pc and AT_high_pc attributes.
700
701 // Find the first (and only) non-empty text section
702 auto &Sections = context.getGenDwarfSectionSyms();
703 const auto TextSection = Sections.begin();
704 assert(TextSection != Sections.end() && "No text section found");
705
Rafael Espindolae0746792015-05-21 16:52:32 +0000706 MCSymbol *StartSymbol = (*TextSection)->getBeginSymbol();
707 MCSymbol *EndSymbol = (*TextSection)->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000708 assert(StartSymbol && "StartSymbol must not be NULL");
709 assert(EndSymbol && "EndSymbol must not be NULL");
710
711 // AT_low_pc, the first address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000712 const MCExpr *Start = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000713 StartSymbol, MCSymbolRefExpr::VK_None, context);
714 MCOS->EmitValue(Start, AddrSize);
715
716 // AT_high_pc, the last address of the default .text section.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000717 const MCExpr *End = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000718 EndSymbol, MCSymbolRefExpr::VK_None, context);
719 MCOS->EmitValue(End, AddrSize);
720 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000721
722 // AT_name, the name of the source file. Reconstruct from the first directory
723 // and file table entries.
David Blaikie533490de2014-03-13 19:05:33 +0000724 const SmallVectorImpl<std::string> &MCDwarfDirs = context.getMCDwarfDirs();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000725 if (MCDwarfDirs.size() > 0) {
Eric Christophere3ab3d02013-01-09 01:57:54 +0000726 MCOS->EmitBytes(MCDwarfDirs[0]);
Yaron Keren15217202014-05-16 13:16:30 +0000727 MCOS->EmitBytes(sys::path::get_separator());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000728 }
David Blaikiea55ddad2014-03-13 18:55:04 +0000729 const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles =
Kevin Enderbye7739d42011-12-09 18:09:40 +0000730 MCOS->getContext().getMCDwarfFiles();
David Blaikiea55ddad2014-03-13 18:55:04 +0000731 MCOS->EmitBytes(MCDwarfFiles[1].Name);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000732 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
733
734 // AT_comp_dir, the working directory the assembly was done in.
Andrew Trick32591d32013-12-10 04:39:09 +0000735 if (!context.getCompilationDir().empty()) {
736 MCOS->EmitBytes(context.getCompilationDir());
737 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
738 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000739
740 // AT_APPLE_flags, the command line arguments of the assembler tool.
741 StringRef DwarfDebugFlags = context.getDwarfDebugFlags();
742 if (!DwarfDebugFlags.empty()){
Eric Christophere3ab3d02013-01-09 01:57:54 +0000743 MCOS->EmitBytes(DwarfDebugFlags);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000744 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
745 }
746
747 // AT_producer, the version of the assembler tool.
Kevin Enderbye82ada62013-01-16 17:46:23 +0000748 StringRef DwarfDebugProducer = context.getDwarfDebugProducer();
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000749 if (!DwarfDebugProducer.empty())
Kevin Enderbye82ada62013-01-16 17:46:23 +0000750 MCOS->EmitBytes(DwarfDebugProducer);
Saleem Abdulrasool19f8bc62014-07-17 16:27:35 +0000751 else
752 MCOS->EmitBytes(StringRef("llvm-mc (based on LLVM " PACKAGE_VERSION ")"));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000753 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
754
755 // AT_language, a 4 byte value. We use DW_LANG_Mips_Assembler as the dwarf2
756 // draft has no standard code for assembler.
757 MCOS->EmitIntValue(dwarf::DW_LANG_Mips_Assembler, 2);
758
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000759 // Third part: the list of label DIEs.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000760
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000761 // Loop on saved info for dwarf labels and create the DIEs for them.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000762 const std::vector<MCGenDwarfLabelEntry> &Entries =
763 MCOS->getContext().getMCGenDwarfLabelEntries();
764 for (const auto &Entry : Entries) {
Kevin Enderby8d4a2202012-01-10 17:52:29 +0000765 // The DW_TAG_label DIE abbrev (2).
Kevin Enderbye7739d42011-12-09 18:09:40 +0000766 MCOS->EmitULEB128IntValue(2);
767
768 // AT_name, of the label without any leading underbar.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000769 MCOS->EmitBytes(Entry.getName());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000770 MCOS->EmitIntValue(0, 1); // NULL byte to terminate the string.
771
772 // AT_decl_file, index into the file table.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000773 MCOS->EmitIntValue(Entry.getFileNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000774
775 // AT_decl_line, source line number.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000776 MCOS->EmitIntValue(Entry.getLineNumber(), 4);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000777
778 // AT_low_pc, start address of the label.
Jim Grosbach13760bd2015-05-30 01:25:56 +0000779 const MCExpr *AT_low_pc = MCSymbolRefExpr::create(Entry.getLabel(),
Kevin Enderbye7739d42011-12-09 18:09:40 +0000780 MCSymbolRefExpr::VK_None, context);
Rafael Espindola98629c42014-03-20 21:26:38 +0000781 MCOS->EmitValue(AT_low_pc, AddrSize);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000782
Kevin Enderbye7739d42011-12-09 18:09:40 +0000783 // DW_AT_prototyped, a one byte flag value of 0 saying we have no prototype.
784 MCOS->EmitIntValue(0, 1);
785
786 // The DW_TAG_unspecified_parameters DIE abbrev (3).
787 MCOS->EmitULEB128IntValue(3);
788
789 // Add the NULL DIE terminating the DW_TAG_unspecified_parameters DIE's.
790 MCOS->EmitIntValue(0, 1);
791 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000792
793 // Add the NULL DIE terminating the Compile Unit DIE's.
794 MCOS->EmitIntValue(0, 1);
795
796 // Now set the value of the symbol at the end of the info section.
797 MCOS->EmitLabel(InfoEnd);
798}
799
Oliver Stannard8b273082014-06-19 15:52:37 +0000800// When generating dwarf for assembly source files this emits the data for
801// .debug_ranges section. We only emit one range list, which spans all of the
802// executable sections of this file.
803static void EmitGenDwarfRanges(MCStreamer *MCOS) {
804 MCContext &context = MCOS->getContext();
805 auto &Sections = context.getGenDwarfSectionSyms();
806
807 const MCAsmInfo *AsmInfo = context.getAsmInfo();
808 int AddrSize = AsmInfo->getPointerSize();
809
810 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
811
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000812 for (MCSection *Sec : Sections) {
813 const MCSymbol *StartSymbol = Sec->getBeginSymbol();
Rafael Espindolae0746792015-05-21 16:52:32 +0000814 MCSymbol *EndSymbol = Sec->getEndSymbol(context);
Oliver Stannard8b273082014-06-19 15:52:37 +0000815 assert(StartSymbol && "StartSymbol must not be NULL");
816 assert(EndSymbol && "EndSymbol must not be NULL");
817
818 // Emit a base address selection entry for the start of this section
Jim Grosbach13760bd2015-05-30 01:25:56 +0000819 const MCExpr *SectionStartAddr = MCSymbolRefExpr::create(
Oliver Stannard8b273082014-06-19 15:52:37 +0000820 StartSymbol, MCSymbolRefExpr::VK_None, context);
821 MCOS->EmitFill(AddrSize, 0xFF);
822 MCOS->EmitValue(SectionStartAddr, AddrSize);
823
824 // Emit a range list entry spanning this section
825 const MCExpr *SectionSize = MakeStartMinusEndExpr(*MCOS,
826 *StartSymbol, *EndSymbol, 0);
827 MCOS->EmitIntValue(0, AddrSize);
Rafael Espindolac23174b2014-08-15 15:12:13 +0000828 emitAbsValue(*MCOS, SectionSize, AddrSize);
Oliver Stannard8b273082014-06-19 15:52:37 +0000829 }
830
831 // Emit end of list entry
832 MCOS->EmitIntValue(0, AddrSize);
833 MCOS->EmitIntValue(0, AddrSize);
834}
835
Kevin Enderbye7739d42011-12-09 18:09:40 +0000836//
837// When generating dwarf for assembly source files this emits the Dwarf
838// sections.
839//
David Blaikie8bf66c42014-04-01 07:35:52 +0000840void MCGenDwarfInfo::Emit(MCStreamer *MCOS) {
Kevin Enderbye7739d42011-12-09 18:09:40 +0000841 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000842
843 // Create the dwarf sections in this order (.debug_line already created).
Bill Wendlingbc07a892013-06-18 07:20:20 +0000844 const MCAsmInfo *AsmInfo = context.getAsmInfo();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000845 bool CreateDwarfSectionSymbols =
Bill Wendlingbc07a892013-06-18 07:20:20 +0000846 AsmInfo->doesDwarfUseRelocationsAcrossSections();
David Blaikie8bf66c42014-04-01 07:35:52 +0000847 MCSymbol *LineSectionSymbol = nullptr;
David Blaikie34641612014-04-01 08:07:52 +0000848 if (CreateDwarfSectionSymbols)
849 LineSectionSymbol = MCOS->getDwarfLineTableSymbol(0);
Craig Topperbb694de2014-04-13 04:57:38 +0000850 MCSymbol *AbbrevSectionSymbol = nullptr;
851 MCSymbol *InfoSectionSymbol = nullptr;
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000852 MCSymbol *RangesSectionSymbol = nullptr;
Oliver Stannard8b273082014-06-19 15:52:37 +0000853
854 // Create end symbols for each section, and remove empty sections
855 MCOS->getContext().finalizeDwarfSections(*MCOS);
856
857 // If there are no sections to generate debug info for, we don't need
858 // to do anything
859 if (MCOS->getContext().getGenDwarfSectionSyms().empty())
860 return;
861
Oliver Stannard14f97d02014-09-22 10:45:16 +0000862 // We only use the .debug_ranges section if we have multiple code sections,
863 // and we are emitting a DWARF version which supports it.
Oliver Stannard8b273082014-06-19 15:52:37 +0000864 const bool UseRangesSection =
Oliver Stannard14f97d02014-09-22 10:45:16 +0000865 MCOS->getContext().getGenDwarfSectionSyms().size() > 1 &&
866 MCOS->getContext().getDwarfVersion() >= 3;
Oliver Stannard8b273082014-06-19 15:52:37 +0000867 CreateDwarfSectionSymbols |= UseRangesSection;
868
Kevin Enderbye7739d42011-12-09 18:09:40 +0000869 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfInfoSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000870 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000871 InfoSectionSymbol = context.createTempSymbol();
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000872 MCOS->EmitLabel(InfoSectionSymbol);
873 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000874 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfAbbrevSection());
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000875 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000876 AbbrevSectionSymbol = context.createTempSymbol();
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000877 MCOS->EmitLabel(AbbrevSectionSymbol);
Rafael Espindolac22c85c2012-02-28 21:13:05 +0000878 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000879 if (UseRangesSection) {
880 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfRangesSection());
881 if (CreateDwarfSectionSymbols) {
Jim Grosbach6f482002015-05-18 18:43:14 +0000882 RangesSectionSymbol = context.createTempSymbol();
Oliver Stannard8b273082014-06-19 15:52:37 +0000883 MCOS->EmitLabel(RangesSectionSymbol);
884 }
885 }
Kevin Enderbye7739d42011-12-09 18:09:40 +0000886
Oliver Stannard8b273082014-06-19 15:52:37 +0000887 assert((RangesSectionSymbol != NULL) || !UseRangesSection);
888
889 MCOS->SwitchSection(context.getObjectFileInfo()->getDwarfARangesSection());
Kevin Enderbye7739d42011-12-09 18:09:40 +0000890
891 // Output the data for .debug_aranges section.
Alexey Samsonov00fd5252012-11-14 09:55:38 +0000892 EmitGenDwarfAranges(MCOS, InfoSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000893
Oliver Stannard8b273082014-06-19 15:52:37 +0000894 if (UseRangesSection)
895 EmitGenDwarfRanges(MCOS);
896
Kevin Enderbye7739d42011-12-09 18:09:40 +0000897 // Output the data for .debug_abbrev section.
898 EmitGenDwarfAbbrev(MCOS);
899
900 // Output the data for .debug_info section.
Oliver Stannard8b273082014-06-19 15:52:37 +0000901 EmitGenDwarfInfo(MCOS, AbbrevSectionSymbol, LineSectionSymbol,
902 RangesSectionSymbol);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000903}
904
905//
906// When generating dwarf for assembly source files this is called when symbol
907// for a label is created. If this symbol is not a temporary and is in the
908// section that dwarf is being generated for, save the needed info to create
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000909// a dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000910//
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000911void MCGenDwarfLabelEntry::Make(MCSymbol *Symbol, MCStreamer *MCOS,
Kevin Enderbye7739d42011-12-09 18:09:40 +0000912 SourceMgr &SrcMgr, SMLoc &Loc) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000913 // We won't create dwarf labels for temporary symbols.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000914 if (Symbol->isTemporary())
915 return;
916 MCContext &context = MCOS->getContext();
Oliver Stannard8b273082014-06-19 15:52:37 +0000917 // We won't create dwarf labels for symbols in sections that we are not
918 // generating debug info for.
919 if (!context.getGenDwarfSectionSyms().count(MCOS->getCurrentSection().first))
Kevin Enderbye7739d42011-12-09 18:09:40 +0000920 return;
921
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000922 // The dwarf label's name does not have the symbol name's leading
Kevin Enderbye7739d42011-12-09 18:09:40 +0000923 // underbar if any.
924 StringRef Name = Symbol->getName();
925 if (Name.startswith("_"))
926 Name = Name.substr(1, Name.size()-1);
927
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000928 // Get the dwarf file number to be used for the dwarf label.
Kevin Enderbye7739d42011-12-09 18:09:40 +0000929 unsigned FileNumber = context.getGenDwarfFileNumber();
930
931 // Finding the line number is the expensive part which is why we just don't
Kevin Enderbyf7d77062012-01-10 21:12:34 +0000932 // pass it in as for some symbols we won't create a dwarf label.
Alp Tokera55b95b2014-07-06 10:33:31 +0000933 unsigned CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
Kevin Enderbye7739d42011-12-09 18:09:40 +0000934 unsigned LineNumber = SrcMgr.FindLineNumber(Loc, CurBuffer);
935
936 // We create a temporary symbol for use for the AT_high_pc and AT_low_pc
937 // values so that they don't have things like an ARM thumb bit from the
938 // original symbol. So when used they won't get a low bit set after
939 // relocation.
Jim Grosbach6f482002015-05-18 18:43:14 +0000940 MCSymbol *Label = context.createTempSymbol();
Kevin Enderbye7739d42011-12-09 18:09:40 +0000941 MCOS->EmitLabel(Label);
942
943 // Create and entry for the info and add it to the other entries.
David Blaikie1e0fc8f2014-04-11 00:43:52 +0000944 MCOS->getContext().addMCGenDwarfLabelEntry(
945 MCGenDwarfLabelEntry(Name, FileNumber, LineNumber, Label));
Kevin Enderbye7739d42011-12-09 18:09:40 +0000946}
947
Rafael Espindola0a017a62010-12-10 07:39:47 +0000948static int getDataAlignmentFactor(MCStreamer &streamer) {
949 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000950 const MCAsmInfo *asmInfo = context.getAsmInfo();
951 int size = asmInfo->getCalleeSaveStackSlotSize();
952 if (asmInfo->isStackGrowthDirectionUp())
Rafael Espindola0a017a62010-12-10 07:39:47 +0000953 return size;
Evan Chenga83b37a2011-07-15 02:09:41 +0000954 else
955 return -size;
Rafael Espindola0a017a62010-12-10 07:39:47 +0000956}
957
Rafael Espindola5395f442011-04-22 00:08:43 +0000958static unsigned getSizeForEncoding(MCStreamer &streamer,
959 unsigned symbolEncoding) {
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000960 MCContext &context = streamer.getContext();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000961 unsigned format = symbolEncoding & 0x0f;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000962 switch (format) {
Craig Toppera2886c22012-02-07 05:05:23 +0000963 default: llvm_unreachable("Unknown Encoding");
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000964 case dwarf::DW_EH_PE_absptr:
965 case dwarf::DW_EH_PE_signed:
Bill Wendlingbc07a892013-06-18 07:20:20 +0000966 return context.getAsmInfo()->getPointerSize();
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000967 case dwarf::DW_EH_PE_udata2:
968 case dwarf::DW_EH_PE_sdata2:
Rafael Espindola5395f442011-04-22 00:08:43 +0000969 return 2;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000970 case dwarf::DW_EH_PE_udata4:
971 case dwarf::DW_EH_PE_sdata4:
Rafael Espindola5395f442011-04-22 00:08:43 +0000972 return 4;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000973 case dwarf::DW_EH_PE_udata8:
974 case dwarf::DW_EH_PE_sdata8:
Rafael Espindola5395f442011-04-22 00:08:43 +0000975 return 8;
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000976 }
Rafael Espindola5395f442011-04-22 00:08:43 +0000977}
978
Rafael Espindolafe963b12014-08-15 03:07:13 +0000979static void emitFDESymbol(MCObjectStreamer &streamer, const MCSymbol &symbol,
980 unsigned symbolEncoding, bool isEH) {
Rafael Espindola6bd6a702011-04-28 21:04:39 +0000981 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000982 const MCAsmInfo *asmInfo = context.getAsmInfo();
983 const MCExpr *v = asmInfo->getExprForFDESymbol(&symbol,
984 symbolEncoding,
985 streamer);
Rafael Espindola5395f442011-04-22 00:08:43 +0000986 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Iain Sandoe618def62014-01-08 10:22:54 +0000987 if (asmInfo->doDwarfFDESymbolsUseAbsDiff() && isEH)
Rafael Espindolac23174b2014-08-15 15:12:13 +0000988 emitAbsValue(streamer, v, size);
Iain Sandoe618def62014-01-08 10:22:54 +0000989 else
990 streamer.EmitValue(v, size);
Rafael Espindola1de2dd02010-12-27 15:56:22 +0000991}
992
Rafael Espindolac5dac4d2011-04-28 16:09:09 +0000993static void EmitPersonality(MCStreamer &streamer, const MCSymbol &symbol,
994 unsigned symbolEncoding) {
995 MCContext &context = streamer.getContext();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000996 const MCAsmInfo *asmInfo = context.getAsmInfo();
997 const MCExpr *v = asmInfo->getExprForPersonalitySymbol(&symbol,
998 symbolEncoding,
999 streamer);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001000 unsigned size = getSizeForEncoding(streamer, symbolEncoding);
Rafael Espindolafd057852011-05-01 03:50:49 +00001001 streamer.EmitValue(v, size);
Rafael Espindolac5dac4d2011-04-28 16:09:09 +00001002}
1003
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001004namespace {
Rafael Espindola68c21652015-11-05 23:54:18 +00001005class FrameEmitterImpl {
Rafael Espindolaa1d960e2015-11-05 23:55:51 +00001006 int CFAOffset = 0;
1007 int InitialCFAOffset = 0;
Rafael Espindola68c21652015-11-05 23:54:18 +00001008 bool IsEH;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001009 MCObjectStreamer &Streamer;
Bill Wendling4d9aa512011-07-22 21:18:59 +00001010
Rafael Espindola68c21652015-11-05 23:54:18 +00001011public:
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001012 FrameEmitterImpl(bool IsEH, MCObjectStreamer &Streamer)
1013 : IsEH(IsEH), Streamer(Streamer) {}
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001014
Rafael Espindola68c21652015-11-05 23:54:18 +00001015 /// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001016 void EmitCompactUnwind(const MCDwarfFrameInfo &frame);
Rafael Espindola68c21652015-11-05 23:54:18 +00001017
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001018 const MCSymbol &EmitCIE(const MCSymbol *personality,
Rafael Espindola68c21652015-11-05 23:54:18 +00001019 unsigned personalityEncoding, const MCSymbol *lsda,
1020 bool IsSignalFrame, unsigned lsdaEncoding,
1021 bool IsSimple);
Rafael Espindola46be4352015-11-06 03:02:51 +00001022 void EmitFDE(const MCSymbol &cieStart, const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001023 bool LastInSection, const MCSymbol &SectionStart);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001024 void EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola68c21652015-11-05 23:54:18 +00001025 MCSymbol *BaseLabel);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001026 void EmitCFIInstruction(const MCCFIInstruction &Instr);
Rafael Espindola68c21652015-11-05 23:54:18 +00001027};
Bill Wendling567a1ae2011-06-30 21:25:51 +00001028
1029} // end anonymous namespace
1030
Rafael Espindolafe963b12014-08-15 03:07:13 +00001031static void emitEncodingByte(MCObjectStreamer &Streamer, unsigned Encoding) {
Bill Wendling567a1ae2011-06-30 21:25:51 +00001032 Streamer.EmitIntValue(Encoding, 1);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001033}
1034
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001035void FrameEmitterImpl::EmitCFIInstruction(const MCCFIInstruction &Instr) {
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001036 int dataAlignmentFactor = getDataAlignmentFactor(Streamer);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001037 auto *MRI = Streamer.getContext().getRegisterInfo();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001038
1039 switch (Instr.getOperation()) {
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001040 case MCCFIInstruction::OpRegister: {
1041 unsigned Reg1 = Instr.getRegister();
1042 unsigned Reg2 = Instr.getRegister2();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001043 if (!IsEH) {
1044 Reg1 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg1, true), false);
1045 Reg2 = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg2, true), false);
1046 }
Rafael Espindolacdb9a532012-11-25 15:14:49 +00001047 Streamer.EmitIntValue(dwarf::DW_CFA_register, 1);
1048 Streamer.EmitULEB128IntValue(Reg1);
1049 Streamer.EmitULEB128IntValue(Reg2);
1050 return;
1051 }
Venkatraman Govindaraju3816d432013-09-26 14:49:40 +00001052 case MCCFIInstruction::OpWindowSave: {
1053 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_window_save, 1);
1054 return;
1055 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001056 case MCCFIInstruction::OpUndefined: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001057 unsigned Reg = Instr.getRegister();
Rafael Espindola9bb24782012-11-23 16:59:41 +00001058 Streamer.EmitIntValue(dwarf::DW_CFA_undefined, 1);
1059 Streamer.EmitULEB128IntValue(Reg);
1060 return;
1061 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001062 case MCCFIInstruction::OpAdjustCfaOffset:
1063 case MCCFIInstruction::OpDefCfaOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001064 const bool IsRelative =
1065 Instr.getOperation() == MCCFIInstruction::OpAdjustCfaOffset;
1066
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001067 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_offset, 1);
1068
1069 if (IsRelative)
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001070 CFAOffset += Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001071 else
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001072 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001073
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001074 Streamer.EmitULEB128IntValue(CFAOffset);
1075
1076 return;
1077 }
1078 case MCCFIInstruction::OpDefCfa: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001079 unsigned Reg = Instr.getRegister();
1080 if (!IsEH)
1081 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001082 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001083 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001084 CFAOffset = -Instr.getOffset();
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001085 Streamer.EmitULEB128IntValue(CFAOffset);
1086
1087 return;
1088 }
1089
1090 case MCCFIInstruction::OpDefCfaRegister: {
Frederic Rissadbb3f22015-02-26 19:48:07 +00001091 unsigned Reg = Instr.getRegister();
1092 if (!IsEH)
1093 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001094 Streamer.EmitIntValue(dwarf::DW_CFA_def_cfa_register, 1);
Frederic Rissadbb3f22015-02-26 19:48:07 +00001095 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001096
1097 return;
1098 }
1099
1100 case MCCFIInstruction::OpOffset:
1101 case MCCFIInstruction::OpRelOffset: {
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001102 const bool IsRelative =
1103 Instr.getOperation() == MCCFIInstruction::OpRelOffset;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001104
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001105 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001106 if (!IsEH)
1107 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
1108
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001109 int Offset = Instr.getOffset();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001110 if (IsRelative)
1111 Offset -= CFAOffset;
1112 Offset = Offset / dataAlignmentFactor;
1113
1114 if (Offset < 0) {
1115 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended_sf, 1);
1116 Streamer.EmitULEB128IntValue(Reg);
1117 Streamer.EmitSLEB128IntValue(Offset);
1118 } else if (Reg < 64) {
1119 Streamer.EmitIntValue(dwarf::DW_CFA_offset + Reg, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001120 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001121 } else {
1122 Streamer.EmitIntValue(dwarf::DW_CFA_offset_extended, 1);
Rafael Espindolaea61f222011-04-21 23:26:40 +00001123 Streamer.EmitULEB128IntValue(Reg);
1124 Streamer.EmitULEB128IntValue(Offset);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001125 }
1126 return;
1127 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001128 case MCCFIInstruction::OpRememberState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001129 Streamer.EmitIntValue(dwarf::DW_CFA_remember_state, 1);
1130 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001131 case MCCFIInstruction::OpRestoreState:
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001132 Streamer.EmitIntValue(dwarf::DW_CFA_restore_state, 1);
1133 return;
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001134 case MCCFIInstruction::OpSameValue: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001135 unsigned Reg = Instr.getRegister();
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001136 Streamer.EmitIntValue(dwarf::DW_CFA_same_value, 1);
Rafael Espindola6aea5922011-04-21 23:39:26 +00001137 Streamer.EmitULEB128IntValue(Reg);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001138 return;
1139 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001140 case MCCFIInstruction::OpRestore: {
Rafael Espindolacc0c74a2012-11-24 04:33:48 +00001141 unsigned Reg = Instr.getRegister();
Frederic Rissadbb3f22015-02-26 19:48:07 +00001142 if (!IsEH)
1143 Reg = MRI->getDwarfRegNum(MRI->getLLVMRegNum(Reg, true), false);
Rafael Espindola4ea99812011-12-29 21:43:03 +00001144 Streamer.EmitIntValue(dwarf::DW_CFA_restore | Reg, 1);
1145 return;
1146 }
Michael Kuperstein259f1502015-10-07 07:01:31 +00001147 case MCCFIInstruction::OpGnuArgsSize: {
1148 Streamer.EmitIntValue(dwarf::DW_CFA_GNU_args_size, 1);
1149 Streamer.EmitULEB128IntValue(Instr.getOffset());
1150 return;
1151 }
Rafael Espindola5dce65b2012-11-24 03:10:54 +00001152 case MCCFIInstruction::OpEscape:
Eric Christophere3ab3d02013-01-09 01:57:54 +00001153 Streamer.EmitBytes(Instr.getValues());
Rafael Espindolaef4aa352011-12-29 20:24:47 +00001154 return;
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001155 }
1156 llvm_unreachable("Unhandled case in switch");
1157}
1158
Rafael Espindolafe963b12014-08-15 03:07:13 +00001159/// Emit frame instructions to describe the layout of the frame.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001160void FrameEmitterImpl::EmitCFIInstructions(ArrayRef<MCCFIInstruction> Instrs,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001161 MCSymbol *BaseLabel) {
1162 for (unsigned i = 0, N = Instrs.size(); i < N; ++i) {
1163 const MCCFIInstruction &Instr = Instrs[i];
1164 MCSymbol *Label = Instr.getLabel();
1165 // Throw out move if the label is invalid.
1166 if (Label && !Label->isDefined()) continue; // Not emitted, in dead code.
1167
1168 // Advance row if new location.
1169 if (BaseLabel && Label) {
1170 MCSymbol *ThisSym = Label;
1171 if (ThisSym != BaseLabel) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001172 Streamer.EmitDwarfAdvanceFrameAddr(BaseLabel, ThisSym);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001173 BaseLabel = ThisSym;
1174 }
1175 }
1176
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001177 EmitCFIInstruction(Instr);
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001178 }
1179}
1180
Rafael Espindolafe963b12014-08-15 03:07:13 +00001181/// Emit the unwind information in a compact way.
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001182void FrameEmitterImpl::EmitCompactUnwind(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:
Eric Christopher2ae71802015-12-28 23:02:42 +00001254 case 5:
Rafael Espindolaeffdc7e2015-04-28 13:55:31 +00001255 return 4;
1256 }
1257 llvm_unreachable("Unknown version");
1258}
1259
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001260const MCSymbol &FrameEmitterImpl::EmitCIE(const MCSymbol *personality,
Rafael Espindola1ec0f462011-04-12 16:12:03 +00001261 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 Espindola6efa6fb2015-11-06 00:05:57 +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();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +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
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001276 const MCExpr *Length =
1277 MakeStartMinusEndExpr(Streamer, *sectionStart, *sectionEnd, 4);
1278 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;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001282 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());
Rafael Espindola6efa6fb2015-11-06 00:05:57 +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";
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001299 Streamer.EmitBytes(Augmentation);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001300 }
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001301 Streamer.EmitIntValue(0, 1);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001302
Keith Walkerea9483f2015-05-12 15:25:08 +00001303 if (CIEVersion >= 4) {
1304 // Address Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001305 Streamer.EmitIntValue(context.getAsmInfo()->getPointerSize(), 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001306
1307 // Segment Descriptor Size
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001308 Streamer.EmitIntValue(0, 1);
Keith Walkerea9483f2015-05-12 15:25:08 +00001309 }
1310
Rafael Espindola0a017a62010-12-10 07:39:47 +00001311 // Code Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001312 Streamer.EmitULEB128IntValue(context.getAsmInfo()->getMinInstAlignment());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001313
1314 // Data Alignment Factor
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001315 Streamer.EmitSLEB128IntValue(getDataAlignmentFactor(Streamer));
Rafael Espindola0a017a62010-12-10 07:39:47 +00001316
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");
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001321 Streamer.EmitIntValue(MRI->getDwarfRegNum(MRI->getRARegister(), IsEH), 1);
Oliver Stannardf7693f42014-06-19 15:39:33 +00001322 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001323 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
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001335 augmentationLength += getSizeForEncoding(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001336 }
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 Espindola6efa6fb2015-11-06 00:05:57 +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 Espindola6efa6fb2015-11-06 00:05:57 +00001347 emitEncodingByte(Streamer, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001348 // Personality
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001349 EmitPersonality(Streamer, *personality, personalityEncoding);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001350 }
Bill Wendling567a1ae2011-06-30 21:25:51 +00001351
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001352 if (lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +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 Espindola6efa6fb2015-11-06 00:05:57 +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();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001365 EmitCFIInstructions(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
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001371 Streamer.EmitValueToAlignment(IsEH ? 4 : MAI->getPointerSize());
Rafael Espindola0a017a62010-12-10 07:39:47 +00001372
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001373 Streamer.EmitLabel(sectionEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001374 return *sectionStart;
1375}
1376
Rafael Espindola46be4352015-11-06 03:02:51 +00001377void FrameEmitterImpl::EmitFDE(const MCSymbol &cieStart,
1378 const MCDwarfFrameInfo &frame,
Rafael Espindola97588e12015-11-06 13:14:59 +00001379 bool LastInSection,
1380 const MCSymbol &SectionStart) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001381 MCContext &context = Streamer.getContext();
Jim Grosbach6f482002015-05-18 18:43:14 +00001382 MCSymbol *fdeStart = context.createTempSymbol();
1383 MCSymbol *fdeEnd = context.createTempSymbol();
Evan Cheng76792992011-07-20 05:58:47 +00001384 const MCObjectFileInfo *MOFI = context.getObjectFileInfo();
Rafael Espindola0a017a62010-12-10 07:39:47 +00001385
Rafael Espindola8b4817b2015-03-24 21:47:31 +00001386 CFAOffset = InitialCFAOffset;
1387
Rafael Espindola0a017a62010-12-10 07:39:47 +00001388 // Length
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001389 const MCExpr *Length = MakeStartMinusEndExpr(Streamer, *fdeStart, *fdeEnd, 0);
1390 emitAbsValue(Streamer, Length, 4);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001391
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001392 Streamer.EmitLabel(fdeStart);
Rafael Espindola99f67352011-05-10 20:59:42 +00001393
Rafael Espindola0a017a62010-12-10 07:39:47 +00001394 // CIE Pointer
Bill Wendlingbc07a892013-06-18 07:20:20 +00001395 const MCAsmInfo *asmInfo = context.getAsmInfo();
Rafael Espindola27390b42011-05-10 15:20:23 +00001396 if (IsEH) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001397 const MCExpr *offset =
1398 MakeStartMinusEndExpr(Streamer, cieStart, *fdeStart, 0);
1399 emitAbsValue(Streamer, offset, 4);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001400 } else if (!asmInfo->doesDwarfUseRelocationsAcrossSections()) {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001401 const MCExpr *offset =
Rafael Espindola97588e12015-11-06 13:14:59 +00001402 MakeStartMinusEndExpr(Streamer, SectionStart, cieStart, 0);
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001403 emitAbsValue(Streamer, offset, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001404 } else {
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001405 Streamer.EmitSymbolValue(&cieStart, 4);
Rafael Espindola27390b42011-05-10 15:20:23 +00001406 }
Bill Wendlingf166ab42011-06-30 22:02:20 +00001407
Rafael Espindola0a017a62010-12-10 07:39:47 +00001408 // PC Begin
Rafael Espindola01ee31b2014-05-12 13:40:49 +00001409 unsigned PCEncoding =
Rafael Espindolaaa7851d2014-05-12 13:47:05 +00001410 IsEH ? MOFI->getFDEEncoding() : (unsigned)dwarf::DW_EH_PE_absptr;
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001411 unsigned PCSize = getSizeForEncoding(Streamer, PCEncoding);
1412 emitFDESymbol(Streamer, *frame.Begin, PCEncoding, IsEH);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001413
1414 // PC Range
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001415 const MCExpr *Range =
1416 MakeStartMinusEndExpr(Streamer, *frame.Begin, *frame.End, 0);
1417 emitAbsValue(Streamer, Range, PCSize);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001418
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001419 if (IsEH) {
1420 // Augmentation Data Length
1421 unsigned augmentationLength = 0;
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001422
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001423 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001424 augmentationLength += getSizeForEncoding(Streamer, frame.LsdaEncoding);
Rafael Espindola6a85f9b2011-04-29 21:50:57 +00001425
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001426 Streamer.EmitULEB128IntValue(augmentationLength);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001427
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001428 // Augmentation Data
1429 if (frame.Lsda)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001430 emitFDESymbol(Streamer, *frame.Lsda, frame.LsdaEncoding, true);
Rafael Espindola1ecb12f2011-05-10 03:54:12 +00001431 }
Rafael Espindola68067662011-04-29 03:06:29 +00001432
Rafael Espindola0a017a62010-12-10 07:39:47 +00001433 // Call Frame Instructions
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001434 EmitCFIInstructions(frame.Instructions, frame.Begin);
Rafael Espindolaa75b87b2010-12-28 04:15:37 +00001435
Rafael Espindola0a017a62010-12-10 07:39:47 +00001436 // Padding
Rafael Espindola46be4352015-11-06 03:02:51 +00001437 // The size of a .eh_frame section has to be a multiple of the alignment
1438 // since a null CIE is interpreted as the end. Old systems overaligned
1439 // .eh_frame, so we do too and account for it in the last FDE.
1440 unsigned Align = LastInSection ? asmInfo->getPointerSize() : PCSize;
1441 Streamer.EmitValueToAlignment(Align);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001442
Rafael Espindola46be4352015-11-06 03:02:51 +00001443 Streamer.EmitLabel(fdeEnd);
Rafael Espindola0a017a62010-12-10 07:39:47 +00001444}
1445
Benjamin Kramer570dd782010-12-30 22:34:44 +00001446namespace {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001447struct CIEKey {
1448 static const CIEKey getEmptyKey() {
1449 return CIEKey(nullptr, 0, -1, false, false);
1450 }
1451 static const CIEKey getTombstoneKey() {
1452 return CIEKey(nullptr, -1, 0, false, false);
1453 }
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001454
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001455 CIEKey(const MCSymbol *Personality, unsigned PersonalityEncoding,
1456 unsigned LsdaEncoding, bool IsSignalFrame, bool IsSimple)
1457 : Personality(Personality), PersonalityEncoding(PersonalityEncoding),
1458 LsdaEncoding(LsdaEncoding), IsSignalFrame(IsSignalFrame),
1459 IsSimple(IsSimple) {}
1460 const MCSymbol *Personality;
1461 unsigned PersonalityEncoding;
1462 unsigned LsdaEncoding;
1463 bool IsSignalFrame;
1464 bool IsSimple;
1465};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001466} // anonymous namespace
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001467
1468namespace llvm {
Rafael Espindola5b2131c2015-11-06 14:12:17 +00001469template <> struct DenseMapInfo<CIEKey> {
1470 static CIEKey getEmptyKey() { return CIEKey::getEmptyKey(); }
1471 static CIEKey getTombstoneKey() { return CIEKey::getTombstoneKey(); }
1472 static unsigned getHashValue(const CIEKey &Key) {
1473 return static_cast<unsigned>(
1474 hash_combine(Key.Personality, Key.PersonalityEncoding, Key.LsdaEncoding,
1475 Key.IsSignalFrame, Key.IsSimple));
1476 }
1477 static bool isEqual(const CIEKey &LHS, const CIEKey &RHS) {
1478 return LHS.Personality == RHS.Personality &&
1479 LHS.PersonalityEncoding == RHS.PersonalityEncoding &&
1480 LHS.LsdaEncoding == RHS.LsdaEncoding &&
1481 LHS.IsSignalFrame == RHS.IsSignalFrame &&
1482 LHS.IsSimple == RHS.IsSimple;
1483 }
1484};
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001485} // namespace llvm
Rafael Espindola1de2dd02010-12-27 15:56:22 +00001486
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001487void MCDwarfFrameEmitter::Emit(MCObjectStreamer &Streamer, MCAsmBackend *MAB,
Rafael Espindola8285b772014-05-12 13:34:25 +00001488 bool IsEH) {
Bill Wendling550c76d2013-09-09 19:48:37 +00001489 Streamer.generateCompactUnwindEncodings(MAB);
1490
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001491 MCContext &Context = Streamer.getContext();
Bill Wendlinga800f952013-05-30 18:52:57 +00001492 const MCObjectFileInfo *MOFI = Context.getObjectFileInfo();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001493 FrameEmitterImpl Emitter(IsEH, Streamer);
Saleem Abdulrasool3f3cefd2014-07-13 19:03:36 +00001494 ArrayRef<MCDwarfFrameInfo> FrameArray = Streamer.getDwarfFrameInfos();
Bill Wendling4d9aa512011-07-22 21:18:59 +00001495
Bill Wendling9803abb2011-09-06 18:37:11 +00001496 // Emit the compact unwind info if available.
Tim Northoverd1c6f512014-03-29 09:03:13 +00001497 bool NeedsEHFrameSection = !MOFI->getSupportsCompactUnwindWithoutEHFrame();
Bill Wendling4e9fc022013-04-24 03:11:14 +00001498 if (IsEH && MOFI->getCompactUnwindSection()) {
Bill Wendling4e9fc022013-04-24 03:11:14 +00001499 bool SectionEmitted = false;
Bob Wilson0e180f22013-05-07 20:56:33 +00001500 for (unsigned i = 0, n = FrameArray.size(); i < n; ++i) {
1501 const MCDwarfFrameInfo &Frame = FrameArray[i];
1502 if (Frame.CompactUnwindEncoding == 0) continue;
1503 if (!SectionEmitted) {
1504 Streamer.SwitchSection(MOFI->getCompactUnwindSection());
Bill Wendlingbc07a892013-06-18 07:20:20 +00001505 Streamer.EmitValueToAlignment(Context.getAsmInfo()->getPointerSize());
Bob Wilson0e180f22013-05-07 20:56:33 +00001506 SectionEmitted = true;
Bill Wendling4e9fc022013-04-24 03:11:14 +00001507 }
Tim Northoverd1c6f512014-03-29 09:03:13 +00001508 NeedsEHFrameSection |=
1509 Frame.CompactUnwindEncoding ==
1510 MOFI->getCompactUnwindDwarfEHFrameOnly();
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001511 Emitter.EmitCompactUnwind(Frame);
Bill Wendling4d9aa512011-07-22 21:18:59 +00001512 }
Bill Wendling4e9fc022013-04-24 03:11:14 +00001513 }
Bill Wendling4d9aa512011-07-22 21:18:59 +00001514
Tim Northoverd1c6f512014-03-29 09:03:13 +00001515 if (!NeedsEHFrameSection) return;
1516
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001517 MCSection &Section =
1518 IsEH ? *const_cast<MCObjectFileInfo *>(MOFI)->getEHFrameSection()
1519 : *MOFI->getDwarfFrameSection();
Tim Northoverd1c6f512014-03-29 09:03:13 +00001520
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001521 Streamer.SwitchSection(&Section);
Jim Grosbach6f482002015-05-18 18:43:14 +00001522 MCSymbol *SectionStart = Context.createTempSymbol();
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001523 Streamer.EmitLabel(SectionStart);
Rafael Espindola7c715152011-04-29 02:42:28 +00001524
Eric Christopherd29430d2014-06-19 20:00:09 +00001525 DenseMap<CIEKey, const MCSymbol *> CIEStarts;
Rafael Espindola9141b612010-12-26 20:20:31 +00001526
Craig Topperbb694de2014-04-13 04:57:38 +00001527 const MCSymbol *DummyDebugKey = nullptr;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001528 bool CanOmitDwarf = MOFI->getOmitDwarfIfHaveCompactUnwind();
Rafael Espindola46be4352015-11-06 03:02:51 +00001529 for (auto I = FrameArray.begin(), E = FrameArray.end(); I != E;) {
1530 const MCDwarfFrameInfo &Frame = *I;
1531 ++I;
Tim Northoverf8e47e42015-10-28 22:56:36 +00001532 if (CanOmitDwarf && Frame.CompactUnwindEncoding !=
Tim Northoverd1c6f512014-03-29 09:03:13 +00001533 MOFI->getCompactUnwindDwarfEHFrameOnly())
1534 // Don't generate an EH frame if we don't need one. I.e., it's taken care
1535 // of by the compact unwind encoding.
1536 continue;
1537
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001538 CIEKey Key(Frame.Personality, Frame.PersonalityEncoding,
David Majnemere035cf92014-01-27 17:20:25 +00001539 Frame.LsdaEncoding, Frame.IsSignalFrame, Frame.IsSimple);
Bill Wendlinga4b9d1f2011-06-23 07:44:54 +00001540 const MCSymbol *&CIEStart = IsEH ? CIEStarts[Key] : DummyDebugKey;
1541 if (!CIEStart)
Rafael Espindola6efa6fb2015-11-06 00:05:57 +00001542 CIEStart = &Emitter.EmitCIE(Frame.Personality, Frame.PersonalityEncoding,
1543 Frame.Lsda, Frame.IsSignalFrame,
1544 Frame.LsdaEncoding, Frame.IsSimple);
Bill Wendlinge8fc92a2011-06-23 01:06:23 +00001545
Rafael Espindola97588e12015-11-06 13:14:59 +00001546 Emitter.EmitFDE(*CIEStart, Frame, I == E, *SectionStart);
Rafael Espindola32c74ea2010-12-17 00:28:02 +00001547 }
Rafael Espindola0a017a62010-12-10 07:39:47 +00001548}
Rafael Espindola736a35d2010-12-28 05:39:27 +00001549
Rafael Espindola4066e8d2014-05-12 14:28:48 +00001550void MCDwarfFrameEmitter::EmitAdvanceLoc(MCObjectStreamer &Streamer,
Rafael Espindola736a35d2010-12-28 05:39:27 +00001551 uint64_t AddrDelta) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001552 MCContext &Context = Streamer.getContext();
Alp Tokere69170a2014-06-26 22:52:05 +00001553 SmallString<256> Tmp;
1554 raw_svector_ostream OS(Tmp);
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001555 MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OS);
Eric Christophere3ab3d02013-01-09 01:57:54 +00001556 Streamer.EmitBytes(OS.str());
Rafael Espindola736a35d2010-12-28 05:39:27 +00001557}
1558
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001559void MCDwarfFrameEmitter::EncodeAdvanceLoc(MCContext &Context,
1560 uint64_t AddrDelta,
Rafael Espindolaab39c632011-05-08 14:35:21 +00001561 raw_ostream &OS) {
Ulrich Weigand32d725b2013-06-12 14:46:54 +00001562 // Scale the address delta by the minimum instruction length.
1563 AddrDelta = ScaleAddrDelta(Context, AddrDelta);
1564
Rafael Espindola6bbfb6c2010-12-28 23:38:03 +00001565 if (AddrDelta == 0) {
Rafael Espindolaab39c632011-05-08 14:35:21 +00001566 } else if (isUIntN(6, AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001567 uint8_t Opcode = dwarf::DW_CFA_advance_loc | AddrDelta;
1568 OS << Opcode;
Rafael Espindolaab39c632011-05-08 14:35:21 +00001569 } else if (isUInt<8>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001570 OS << uint8_t(dwarf::DW_CFA_advance_loc1);
1571 OS << uint8_t(AddrDelta);
Rafael Espindolaab39c632011-05-08 14:35:21 +00001572 } else if (isUInt<16>(AddrDelta)) {
Rafael Espindola736a35d2010-12-28 05:39:27 +00001573 OS << uint8_t(dwarf::DW_CFA_advance_loc2);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001574 if (Context.getAsmInfo()->isLittleEndian())
1575 support::endian::Writer<support::little>(OS).write<uint16_t>(AddrDelta);
1576 else
1577 support::endian::Writer<support::big>(OS).write<uint16_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001578 } else {
1579 assert(isUInt<32>(AddrDelta));
1580 OS << uint8_t(dwarf::DW_CFA_advance_loc4);
Benjamin Kramer58675d42015-06-17 15:14:35 +00001581 if (Context.getAsmInfo()->isLittleEndian())
1582 support::endian::Writer<support::little>(OS).write<uint32_t>(AddrDelta);
1583 else
1584 support::endian::Writer<support::big>(OS).write<uint32_t>(AddrDelta);
Rafael Espindola736a35d2010-12-28 05:39:27 +00001585 }
1586}